Incorrect BFS father attribute in igraph

I use the igraph package, and I'm not sure if this is an error or not, but the output of $ father sometimes makes no sense. In particular, when I rename the vertex attributes.

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

I get output as below

#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 

I don’t understand why the father is mistaken for the case of renamed vertex attributes. For example, the first element must be NA, but it is not.

Can someone explain what is happening? If so, how to do it so that my father’s elements reflect something similar to the first case.

+4
source share
1 answer

This is a bit strange, but for some reason the function bfshas a direct assignment of vertex names to vector names father. See Code Line 54-55 in the source code:

   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

, github igraph, ( ) .

+1

Source: https://habr.com/ru/post/1685991/


All Articles