How can I access the 'epath' output of get.shortest.paths to get the edge names?

一世执手 提交于 2019-12-12 02:44:59

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!