问题
I would like to access the 'epath' output of the get.shortest.paths and get the names of the edges, rather than the ID's. At the moment, I have the edge ID's, but I need the names of the edges. For example:
# create graph
solid = graph_from_literal(A-D,A-C,D-F,D-C,C-B,B-E)
# get edges of shortest paths
solid.epaths = get.shortest.paths(solid, from = 1, to = V(solid),output = 'epath')
# get verticies of shortest paths
solid.vpaths = get.shortest.paths(solid, from = 1, to = V(solid),output = 'vpath')
I can access the names of the verticies:
names(unlist(solid.vpaths$vpath[4]))
But I cannot access the names of the edges, it just comes up null:
names(unlist(solid.epaths$epath[4]))
If you have another method I'm open to suggestions. In the end, I basically need a character vector of the edge names.
Thanks!
回答1:
Give them names to get names:
library(igraph)
solid = graph_from_literal(A-D,A-C,D-F,D-C,C-B,B-E)
E(solid)$name <- letters[seq_len(ecount(solid))]
plot(solid, edge.label = E(solid)$name)
solid.epaths = get.shortest.paths(solid, from = 1, to = 4, output = 'epath')
solid.vpaths = get.shortest.paths(solid, from = 1, to = 4, output = 'vpath')
names(unlist(solid.vpaths$vpath))
# [1] "A" "D" "F"
names(unlist(solid.epaths$epath))
# [1] "a" "d"
来源:https://stackoverflow.com/questions/39858377/how-can-i-access-the-epath-output-of-get-shortest-paths-to-get-the-edge-names