R图是否计算有向网络中的无向最短路径?

R图是否计算有向网络中的无向最短路径?,r,igraph,R,Igraph,关于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_f

关于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);
我所期望的是不应该返回最短路径,因为在有向图中,Alice没有自出的边。 这是因为当我计算最短路径时,我使用了以下选项:

mode="all"
这,不知怎的,甚至对有向图也是有效的

当然,如果我更改图形结构并设置:

directed=F
i、 e

库(igraph);

关系这正是
mode=“all”
的意思-使用所有边,而不考虑方向

我将使用一个更简单的图表,以便于查看发生了什么

rel2 <- data.frame(from=c("Bob", "Bob", "David"), 
        to=c("Alice", "Carol", "Carol"))
g = simplify(graph_from_data_frame(d=rel2, directed=T))
LO = layout_as_bipartite(g, types=c(F,F,T,T))
plot(g, layout=LO)
我们得到将Alice连接到另一个节点的所有路径,即使边的方向相反

我想你想要的是:

print(all_shortest_paths(g,from=V(g)[3],to=V(g),mode="out")$res)
[[1]]
+ 1/4 vertex, named:
它只给出从Alice到自身的零长度路径

为了完整起见

print(all_shortest_paths(g,from=V(g)[3],to=V(g),mode="in")$res)
[[1]]
+ 2/4 vertices, named:
[1] Alice Bob  

[[2]]
+ 1/4 vertex, named:
[1] Alice

这只使用传入的边跟随路径,因此我们使用Alice中的边得到“Alice”到“Bob”的路径,但是我们没有得到其他任何东西,因为Bob中没有边。

ok,因此如果使用
mode=“all”
则我的图被声明为定向图还是无向图并不重要。这就是我一直在寻找的答案。对我来说,定义一个函数来做一些从图形的角度来看不应该被允许的事情仍然很奇怪,但……就是这样!对我认为这里的观点是,如果要使用定向路径,必须使用
mode=“in”
mode=“out”
。“所有”更多的是关于连接性。请问你说“所有”更多的是关于连接性是什么意思?是的。使用“所有”边时,将获得连接到节点的任何对象的路径(通过路径而不考虑方向)。这将报告同一连接组件中所有节点对之间的最短路径。
print(all_shortest_paths(g,from=V(g)[3],to=V(g),mode="all")$res)
[[1]]
+ 2/4 vertices, named:
[1] Alice Bob  
[[2]]
+ 4/4 vertices, named:
[1] Alice Bob   Carol David
[[3]]
+ 1/4 vertex, named:
[1] Alice
[[4]]
+ 3/4 vertices, named:
[1] Alice Bob   Carol
print(all_shortest_paths(g,from=V(g)[3],to=V(g),mode="out")$res)
[[1]]
+ 1/4 vertex, named:
print(all_shortest_paths(g,from=V(g)[3],to=V(g),mode="in")$res)
[[1]]
+ 2/4 vertices, named:
[1] Alice Bob  

[[2]]
+ 1/4 vertex, named:
[1] Alice