igraph 中的 BFS father 属性错误
BFS father attribute in igraph is wrong
我正在使用 igraph 包,我不确定它是否是一个错误,但 $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 但它不是。
有人可以解释一下发生了什么吗?如果是这样,我该如何解决这个问题,使我的父亲元素反映出类似于第一种情况的东西。
有点奇怪,但出于某种原因,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 上提出这个问题,因为这似乎(至少对我来说)是不受欢迎的行为。
我正在使用 igraph 包,我不确定它是否是一个错误,但 $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 但它不是。
有人可以解释一下发生了什么吗?如果是这样,我该如何解决这个问题,使我的父亲元素反映出类似于第一种情况的东西。
有点奇怪,但出于某种原因,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 上提出这个问题,因为这似乎(至少对我来说)是不受欢迎的行为。