我正在尝试从shortest_paths iGraph函数创建的对象中收集所有唯一边。
> data<-as.matrix(data)
> data
Q W E R T Y U I
Q 0 4 7 5 0 4 0 0
W 2 0 5 7 3 2 4 9
E 2 4 0 9 2 2 7 2
R 2 2 2 0 6 0 5 8
T 0 8 8 5 0 5 9 0
Y 4 7 2 6 0 0 5 7
U 0 0 0 0 2 0 0 9
I 2 0 6 7 0 2 0 0
network<-graph.adjacency(adjmatrix = data,weighted = TRUE, mode="directed" , diag = FALSE )
shortestPath<-shortest_paths(graph = network, from = 1, to = 1:8,mode = "out", output = "epath")
> shortestPaths$epath
[[1]]
+ 0/41 edges from b068eeb (vertex names):
[[2]]
+ 1/41 edge from b068eeb (vertex names):
[1] Q->W
[[3]]
+ 2/41 edges from b068eeb (vertex names):
[1] Q->Y Y->E
[[4]]
+ 1/41 edge from b068eeb (vertex names):
[1] Q->R
[[5]]
+ 2/41 edges from b068eeb (vertex names):
[1] Q->W W->T
[[6]]
+ 1/41 edge from b068eeb (vertex names):
[1] Q->Y
[[7]]
+ 2/41 edges from b068eeb (vertex names):
[1] Q->W W->U
[[8]]
+ 3/41 edges from b068eeb (vertex names):
[1] Q->Y Y->E E->I
我想计算最短路径中唯一边的数量,以便我可以计算定义为 -
的图的密度密度=(最短路径上的唯一边数)/(边总数)
需要对图中的所有对最短路径进行此操作。
如何计算全对设置中最短路径上的唯一边数。
由于
答案 0 :(得分:1)
您可以取消列表,找到唯一的边缘并获取结果向量的长度:
length(unique(unlist(shortestPath$epath)))