【问题标题】:Disjoint Paths Algorithm不相交路径算法
【发布时间】:2017-01-19 21:24:43
【问题描述】:

计算查找增广路径的最简单方法是什么?

使用标记遍历计算边缘不相交路径以查找增强路径

def paths(G, s, t):                           # Edge-disjoint path coun
    H, M, count = tr(G), set(), 0             # Transpose, matching, result
    while True:                               # Until the function returns
        Q, P = {s}, {}                              # Traversal queue + tree
        while Q:                                    # Discovered, unvisited
            u = Q.pop()                             # Get one
            if u == t:                              # Augmenting path!
                count += 1                     # That means one more path
                break                               # End the traversal
            forw = (v for v in G[u] if (u,v) not in M)  # Possible new edges
            back = (v for v in H[u] if (v,u) in M)  # Cancellations
            for v in chain(forw, back):       # Along out- and in-edges
                if v in P: continue           # Already visited? Ignore
                P[v] = u                            # Traversal predecessor
                Q.add(v)                            # New node discovered
        else:                                       # Didn't reach t?
            return count                            # We're donefinnish

我可以使用 I while 循环来完成吗?

【问题讨论】:

    标签: python algorithm path disjoint-sets


    【解决方案1】:

    我试过了,它成功了!

    while u != s:    
        u, v = P[u], u
        if v in G[u]:
            M.add((u,v))
    else:
        M.remove((v,u))
    

    【讨论】:

      猜你喜欢
      • 2017-08-10
      • 2013-10-24
      • 1970-01-01
      • 2011-06-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-06-13
      相关资源
      最近更新 更多