gpt4 book ai didi

python - 不相交路径算法

转载 作者:塔克拉玛干 更新时间:2023-11-03 05:17:43 25 4
gpt4 key购买 nike

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

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

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 循环来芬兰语吗?如何使用?

最佳答案

我试过了,成功了!

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

关于python - 不相交路径算法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41751958/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com