【发布时间】:2019-03-02 09:34:44
【问题描述】:
关于 IGraph 最短路径计算,我缺少一些东西。
假设我生成一个网络(在 stackoverflow 中找到某处)并执行简单的计算:
library(igraph);
relations <- data.frame(from=c("Bob", "Cecil", "Cecil", "David", "David", "Esmeralda"), to=c("Alice", "Bob", "Alice", "Alice", "Bob", "Alice"));
g = simplify(graph_from_data_frame(d=relations, directed=T), remove.multiple = F, remove.loops = T);
#plotting the network in order to appreciate the directions of the edges
plot(g,edge.arrow.size=0.5);
#V(g)[5] is "Alice" which apparently should not be able to reach any node
print(all_shortest_paths(g,from=V(g)[5],to=V(g),mode="all")$res);
如您所见,找到的最短路径是:
> print(all_shortest_paths(g,from=V(g)[5],to=V(g),mode="all")$res);
[[1]]
+ 2/5 vertices, named, from 823c15d:
[1] Alice Bob
[[2]]
+ 2/5 vertices, named, from 823c15d:
[1] Alice Cecil
[[3]]
+ 2/5 vertices, named, from 823c15d:
[1] Alice David
[[4]]
+ 2/5 vertices, named, from 823c15d:
[1] Alice Esmeralda
[[5]]
+ 1/5 vertex, named, from 823c15d:
[1] Alice
我的期望是不应该返回最短路径,因为在有向图中,Alice 没有从自身出去的边。 这是因为当我计算最短路径时,我使用的是选项:
mode="all"
不知何故,这甚至适用于有向图?
当然,如果我改变图形构造并设置:
directed=F
即
library(igraph);
relations <- data.frame(from=c("Bob", "Cecil", "Cecil", "David", "David", "Esmeralda"), to=c("Alice", "Bob", "Alice", "Alice", "Bob", "Alice"));
g = simplify(graph_from_data_frame(d=relations, directed=F), remove.multiple = F, remove.loops = T);
#plottin the network in order to appreciate the directions of the edges
plot(g,edge.arrow.size=0.5);
#V(g)[5] is "Alice" which apparently should not be able to reach any node
print(all_shortest_paths(g,from=V(g)[5],to=V(g),mode="all")$res);
返回相同的结果。
发生了什么事?我是不是太累了,无法直截了当?
【问题讨论】: