【问题标题】:Is R IGraph computing undirected shortest path in directed network?R IGraph 是在有向网络中计算无向最短路径吗?
【发布时间】: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);

返回相同的结果。

发生了什么事?我是不是太累了,无法直截了当?

【问题讨论】:

    标签: r igraph


    【解决方案1】:

    这正是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)
    

    现在用你的最短路径语句

    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
    

    我们得到了将 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。

    【讨论】:

    • 好的,所以如果使用mode="all",那么我的图是被声明为有向还是无向都没有关系。这就是我一直在寻找的答案。对我来说,定义一个函数来做一些从图形的角度来看不应该被允许的事情仍然很奇怪,但是......就是这样!
    • 是的。我认为这里的观点是,如果要使用定向路径,则必须使用mode="in"mode="out"。 “all”更多的是关于连接性。
    • 请问您说“all”更多的是关于连接性是什么意思?
    • 是的。当您使用“所有”边时,您将获得连接到节点的任何东西的路径(通过路径与方向无关)。这将报告同一连接组件中所有节点对之间的最短路径。
    猜你喜欢
    • 2018-10-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-06
    • 1970-01-01
    相关资源
    最近更新 更多