我正在使用 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
Run Code Online (Sandbox Code Playgroud)
我获得如下输出
#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
Run Code Online (Sandbox Code Playgroud)
我不明白为什么重命名顶点属性的父亲是错误的。例如,第一个元素应该是 NA 而不是。
有人可以解释发生了什么吗?如果是这样,我该如何解决这个问题,以便我的父亲元素反映类似于第一种情况的内容。
这有点奇怪,但由于某种原因,该bfs函数将顶点名称直接分配给向量名称father。参见源码中第54-55行代码:
if (father)
names(res$father) <- V(graph)$name
Run Code Online (Sandbox Code Playgroud)
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
Run Code Online (Sandbox Code Playgroud)
现在它应该可以工作:
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)
Run Code Online (Sandbox Code Playgroud)
输出:
$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
Run Code Online (Sandbox Code Playgroud)
可能值得在igraphgithub 上提出这个问题,因为这似乎(至少对我来说)是不良行为。
| 归档时间: |
|
| 查看次数: |
185 次 |
| 最近记录: |