简体   繁体   English

R IGraph计算定向网络中的无向最短路径吗?

[英]Is R IGraph computing undirected shortest path in directed network?

there is something that i'm missing about IGraph shortest paths computation. 关于IGraph最短路径计算,我缺少一些东西。

suppose I generate a network (find somewhere in stackoverflow) and perform simple computations: 假设我生成一个网络(在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);

as you can see, the shortest paths found are: 如您所见,找到的最短路径是:

> 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

what I'm expecting is that no shortest paths should be returned since Alice, in a directed graph, has no edges that are going out from itself. 我期望的是,不应该返回最短的路径,因为在有向图中,爱丽丝没有从其自身伸出的边缘。 Is this due to the fact that, when I compute the shortest paths, I'm using the option: 这是由于以下事实:当我计算最短路径时,我正在使用以下选项:

mode="all"

and this, somehow, works even for directed graphs? 而且这以某种方式甚至适用于有向图?

Of course, if I change the graph construction and set: 当然,如果我更改图形构造并设置:

directed=F

ie

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);

the same results are returned. 返回相同的结果。

What is going on? 到底是怎么回事? Am I just too tired to get this straight? 我是否太累了,无法直截了当?

That is just what mode="all" means - use all edges regardless of direction. 这就是mode="all"含义-使用所有边缘而不管方向。

I will use a simpler graph to make it easy to see what is going on. 我将使用一个更简单的图形来轻松查看发生了什么。

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)

简单方向图

Now with your shortest path statement 现在,用最短路径声明

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

We get all paths connecting Alice to another node, even though the edges go in opposite directions. 即使边沿相反的方向,我们也可以获得将爱丽丝连接到另一个节点的所有路径。

I think that what you want is: 我认为您想要的是:

print(all_shortest_paths(g,from=V(g)[3],to=V(g),mode="out")$res)
[[1]]
+ 1/4 vertex, named:

Which gives only the zero-length path from Alice to itself. 这仅给出了从Alice到其自身的零长度路径。

Just for completeness, 为了完整性,

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

This follows paths using only incoming edges, so we get the path "from" Alice "to" Bob using the edge into Alice, but we get nothing else because there are no edges into Bob. 这仅使用进入的边缘来遵循路径,因此我们使用进入Alice的边缘将路径从“ Alice”到“ Bob”获得,但是由于没有进入Bob的边缘,因此我们一无所获。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM