【问题标题】:Determine source vertices and find all paths to a target vertex确定源顶点并找到目标顶点的所有路径
【发布时间】:2019-07-09 19:21:15
【问题描述】:

给定以下有向图:

g <- make_graph(c("k","z", "x","z", "z","d", "z","a", "a","b",
                  "b","c", "d","e", "e","c", "c","f", "f","g"), directed = TRUE)

plot(g)

我想获取从两个源顶点“x”和“k”到目标顶点“c”的所有路径,而不在路径的开头指定源顶点。

预期结果:

路径1:k -&gt; z -&gt; a -&gt; b -&gt; c

路径 2:x -&gt; z -&gt; d -&gt; e -&gt; c

目前我已经弄清楚如何通过使用子组件将所有顶点获取到“c”顶点:

subcomponent(g, "c", mode = "in")

这不是我想要的。

【问题讨论】:

  • 两者都是路径的开始。
  • 我试过这个:all_simple_paths(g, from = "c", to = V(g), mode = "in")
  • 这还不错,但仍需要一些工作
  • 您是否正在寻找从图表的“根”到“c”的路径?这样的事情可能是相关的:stackoverflow.com/q/47519204/1222578
  • All paths in directed tree graph from root to leaves in igraph R。因此,要检测 x 和 k:from = V(g)[degree(g, v = V(g), mode = "in") == 0]。然后lapply(from, function(v) all_simple_paths(g, from = v, to = "c"))。区分k z d e ck z a b cx z d e cx z a b c 的规则是什么?

标签: r igraph


【解决方案1】:

这个问题与All paths in directed tree graph from root to leaves in igraph R 高度相关。基本方法相同,但这里的路径是从几个源顶点确定的。

计算传入边的顶点degree (mode == "in")。要识别源顶点,请检查度数是否为零。使用生成的逻辑向量来索引顶点 (V(g)[...])。

from <- V(g)[degree(g, v = V(g), mode = "in") == 0]
from
# + 2/10 vertices, named, from 6cff414:
# [1] k x

遍历源顶点以查找从每个源到“c”的所有简单路径:

lapply(from, function(v) all_simple_paths(g, from = v, to = "c")) 

# $k
# $k[[1]]
# + 5/10 vertices, named, from 6cff414:
# [1] k z d e c
# 
# $k[[2]]
# + 5/10 vertices, named, from 6cff414:
# [1] k z a b c
# 
# 
# $x
# $x[[1]]
# + 5/10 vertices, named, from 6cff414:
# [1] x z d e c
# 
# $x[[2]]
# + 5/10 vertices, named, from 6cff414:
# [1] x z a b c

OP 没有提供任何规则来区分每个源顶点的两条替代路径。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-08-09
    • 2021-03-11
    • 2023-04-11
    • 2018-05-14
    • 2013-10-25
    • 2021-06-30
    • 2019-02-14
    相关资源
    最近更新 更多