Skip to content

Commit 4f2650a

Browse files
authored
fix ford_fulkerson.py
1 parent fa3f95d commit 4f2650a

File tree

1 file changed

+6
-9
lines changed

1 file changed

+6
-9
lines changed

networking_flow/ford_fulkerson.py

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -28,24 +28,21 @@ def breadth_first_search(graph: list, source: int, sink: int, parents: list) ->
2828
parents: Parent list
2929
3030
Returns:
31-
True if there is a node that has not iterated.
31+
True if there is a path from source to sink
3232
3333
>>> breadth_first_search(graph, 0, 5, [-1, -1, -1, -1, -1, -1])
3434
True
35-
>>> breadth_first_search(graph, 0, 6, [-1, -1, -1, -1, -1, -1])
36-
Traceback (most recent call last):
37-
...
38-
IndexError: list index out of range
3935
"""
4036
num_nodes = len(graph)
4137
visited = [False] * num_nodes
42-
queue: deque[int] = deque()
38+
queue = [] # Using list instead of deque
4339

4440
queue.append(source)
4541
visited[source] = True
4642

4743
while queue:
48-
current_node = queue.popleft()
44+
# Use pop(0) to simulate deque's popleft()
45+
current_node = queue.pop(0)
4946

5047
# If we reached the sink, we can stop early
5148
if current_node == sink:
@@ -95,7 +92,7 @@ def ford_fulkerson(graph: list, source: int, sink: int) -> int:
9592
# Augment the flow while there is a path from source to sink
9693
while breadth_first_search(residual_graph, source, sink, parents):
9794
# Find the minimum residual capacity along the path
98-
path_flow = 10**9 # Large integer instead of float
95+
path_flow = float('inf')
9996
current_node = sink
10097

10198
# Find the minimum capacity in the path
@@ -122,4 +119,4 @@ def ford_fulkerson(graph: list, source: int, sink: int) -> int:
122119
from doctest import testmod
123120

124121
testmod()
125-
print(f"{ford_fulkerson(graph, source=0, sink=5) = }")
122+
print(f"{ford_fulkerson(graph, source=0, sink=5) = }")

0 commit comments

Comments
 (0)