2017-09-19 1 views
4

igraph 패키지를 사용하고 있으며 버그인지 여부는 확실하지 않지만 $ father 출력은 때때로 의미가 없습니다. 특히, 내가 꼭지점 속성의 이름을 바꿀 때.igraph의 BFS father 속성이 잘못되었습니다.

h<-make_tree(10) 

#with normal vertex attributes 
graph.bfs(h, root="1", neimode='out', order=TRUE, father=TRUE,unreachable=FALSE) #father output seems correct 
plot(h,layout=layout_as_tree) 

#with renamed vertex attributes 
set.seed(1) 
h<-set.vertex.attribute(h, "name", value=sample(1:10,10)) 
plot(h,layout=layout_as_tree) 
graph.bfs(h, root="3", neimode='out', order=TRUE, father=TRUE,unreachable=FALSE) #father output seems wrong 

나는 이름을 바꾼 정점의 아버지는 경우 잘못된 속성을 왜 이해가 안

#with normal vertex attributes 
$order 
+ 10/10 vertices, from ff55a96: 
[1] 1 2 3 4 5 6 7 8 9 10 

$rank 
NULL 

$father 
+ 10/10 vertices, from ff55a96: 
[1] NA 1 1 2 2 3 3 4 4 5 

#with renamed vertex attributes 
$order 
+ 10/10 vertices, named, from 170f7a0: 
[1] 3 4 5 7 2 8 9 6 10 1 

$rank 
NULL 

$father 
+ 10/10 vertices, named, from 170f7a0: 
[1] 3 4 5 7 2 8 9 6 10 1 

아래와 같은 출력을 얻을 수 있습니다. 예를 들어, 첫 번째 요소는 NA이지만 그렇지 않아야합니다.

누군가 무슨 일이 일어 났는지 설명 할 수 있습니까? 그렇다면 어떻게하면 내 아버지 요소가 첫 번째 경우와 비슷한 것을 반영하도록이 문제를 해결할 수 있습니다.

답변

1

다소 이상하지만, bfs 함수는 father 벡터의 이름에 정점 이름을 직접 할당합니다. 소스 코드에 코드의 54-55 라인을 참조하십시오 :

if (father) 
     names(res$father) <- V(graph)$name 

분명히, 이것은 단순히 그래프의 이름의 벡터로 res$father의 이름을 덮어 씁니다. 이 조건문은 igraph_opt("add.vertex.names") 인수가 참이어야합니다.

따라서 정점 이름을 false로 추가하는 전역 옵션을 설정하면이 문제를 피할 수 있습니다. 이제

> igraph_options()$add.vertex.names 
[1] TRUE 
> igraph_options(add.vertex.names=F) 
> igraph_options()$add.vertex.names 
[1] FALSE 

그것을 작동합니다 :

h<-make_tree(10) 
set.seed(1) 
h<-set_vertex_attr(h, "name", value=sample(1:10,10)) 
bfs(h, root=1, neimode='out', order=TRUE, rank=TRUE, father=TRUE,unreachable=FALSE) 

출력 :

$root 
[1] 1 

$neimode 
[1] "out" 

$order 
+ 10/10 vertices, named: 
[1] 3 4 5 7 2 8 9 6 10 1 

$rank 
[1] 1 2 3 4 5 6 7 8 9 10 

$father 
+ 10/10 vertices, named: 
[1] <NA> 3 3 4 4 5 5 7 7 2 

$pred 
NULL 

$succ 
NULL 

$dist 
NULL 

이 바람직하지 않은 행동처럼 (적어도 나에게) 것 때문에, igraph GitHub의에이 문제를 제기 가치가있을 수도 있습니다 .

+0

감사합니다. 그러나이 예제를 시도하십시오. 코드에 버그가 있습니다. 'tree <-graph_from_literal (3 + 5 : 6,6- + 2,3-3-4); obj <-graph.bfs (tree, root = 3, neimode = "out", father = TRUE, order = TRUE, unreachable = FALSE); obj $ order; obj $ father'. 출력은'obj $ order = 6 2 '및'obj $ father = 3 5 6 2 4'입니다. 그러나 나는 그것이'obj $ order = 3 5 6 4 2'와'obj $ father = NA 3 3 3 2'라고 기대합니다. 결과는 변경되지 않는다 – dexter

+0

'graph_from_literal'을 사용하면 문자로 저장되는 정점 이름이 자동으로 할당되기 때문입니다. 이 예에서,'root = 3' 인자는 노드 "6"('V (tree) $ name [3]') 인 세 번째 노드를 호출합니다. 인수를'bfs (tree, root = '3', neimode = "out", father = TRUE, order = TRUE, unreachable = FALSE)'로 변경하십시오. – paqmo

+0

'order' 결과와'father' 결과가 정확히 일치하지 않기 때문에 여전히 이상한 것들이 있습니다. 그것은 6이 2가 아니라 4의 아버지임을 나타냅니다. 버그 일 수 있습니다. – paqmo