Skip to content

Commit 216a14c

Browse files
committed
updation in ford_fulkerson_algorithm
1 parent 4cb6057 commit 216a14c

File tree

1 file changed

+16
-13
lines changed

1 file changed

+16
-13
lines changed

pydatastructs/graphs/algorithms.py

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1403,6 +1403,8 @@ def _find_path_dfs(graph, s, t, flow_pass):
14031403
>>> flow_passed = {}
14041404
>>> path_flow, parent = _find_path_dfs(graph, 's', 't', flow_passed)
14051405
"""
1406+
if s == t:
1407+
return 0, {}
14061408

14071409
visited = {}
14081410
stack = Stack()
@@ -1427,21 +1429,19 @@ def _find_path_dfs(graph, s, t, flow_pass):
14271429
parent[i_name] = curr
14281430
stack.append(i_name)
14291431

1430-
if t not in parent and t == s:
1431-
return 0, {}
1432+
if t not in parent:
1433+
return 0, {}
14321434

1433-
curr = t
1434-
path_flow = float('inf')
1435-
if t == s:
1436-
return 0, {}
1437-
while curr != s:
1438-
prev = parent[curr]
1439-
capacity = graph.get_edge(prev, curr).value
1440-
flow = flow_pass.get((prev, curr), 0)
1441-
path_flow = min(path_flow, capacity - flow)
1442-
curr = prev
1435+
curr = t
1436+
path_flow = float('inf')
1437+
while curr != s:
1438+
prev = parent[curr]
1439+
capacity = graph.get_edge(prev, curr).value
1440+
flow = flow_pass.get((prev, curr), 0)
1441+
path_flow = min(path_flow, capacity - flow)
1442+
curr = prev
14431443

1444-
return path_flow, parent
1444+
return path_flow, parent
14451445

14461446
def _max_flow_ford_fulkerson_(graph, s, t):
14471447
"""
@@ -1478,6 +1478,9 @@ def _max_flow_ford_fulkerson_(graph, s, t):
14781478
if s not in graph.vertices or t not in graph.vertices:
14791479
raise ValueError("Source or sink not in graph.")
14801480

1481+
if s == t:
1482+
return 0
1483+
14811484
ans = 0
14821485
flow_pass = {}
14831486

0 commit comments

Comments
 (0)