robertspierre
robertspierre

Reputation: 4441

igraph: convert list to vertex sequence

I am using the R implementation of igraph. I did not understand why get_diamenter() returns a igraph.vs object, while shortest_paths()$vpath returns a list object. I am trying to plot the shortest path between two nodes. I can plot the network diameter, since I can do

diam <- get_diameter(net_vc, directed=F)
vcol <- rep("gray40", vcount(net_vc))
vcol[diam] <- "gold"

But I cannot plot the shortest path in a similar way:

sp = shortest_paths(net_vc, from=V(net_vc)$Name=="I0", to=V(net_vc)$Name=="I11")
sp <- sp$vpath
vcol <- rep("gray40", vcount(net_vc))
vcol[sp] <- "gold"

It returns: Error in vcol[sp] <- "gold" : invalid subscript type 'list'

How can I convert this list to a vertex sequence, so that as.vector(sp) would return the position of the vertices, so as to index vcol?

Upvotes: 0

Views: 305

Answers (1)

G5W
G5W

Reputation: 37661

The documentation for get_diameter says:

get_diameter returns a path with the actual diameter. If there are many shortest paths of the length of the diameter, then it returns the first one found.

So it returns a single path. On the other hand, shortest_paths allows a list of to vertices. It will return a list of shortest paths, a list of igraph.vs's, one for each vertex in the to list. You will get a list even if you call shortest_paths with a single vertex in to. So, to set the colors the way you want you need to reference the first (and only) element of the list. You need:

sp = shortest_paths(net_vc, from=V(net_vc)$Name=="I0", to=V(net_vc)$Name=="I11")
sp <- sp$vpath[[1]]
vcol <- rep("gray40", vcount(net_vc))
vcol[sp] <- "gold"

Upvotes: 1

Related Questions