Skip to content

Commit d068d17

Browse files
Address review: drop __future__ import, use descriptive names, apply README wording
1 parent 5ad6994 commit d068d17

3 files changed

Lines changed: 24 additions & 26 deletions

File tree

networking_flow/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
# Networking Flow
22

33
This directory collects algorithms for the **maximum-flow problem**: given a
4-
directed graph whose edges have capacities, a *source* `s`, and a *sink* `t`,
5-
how much flow can be pushed from `s` to `t` without exceeding any edge's
4+
directed graph whose edges have capacities, a `source`, and a `sink`, how
5+
much flow can be pushed from `source` to `sink` without exceeding any edge's
66
capacity?
77

88
Maximum flow turns up all over the place — routing traffic through a network,

networking_flow/dinic.py

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,6 @@
1818
Reference: https://en.wikipedia.org/wiki/Dinic%27s_algorithm
1919
"""
2020

21-
from __future__ import annotations
22-
2321
from collections import deque
2422

2523

@@ -60,7 +58,7 @@ def __init__(self, vertices: int) -> None:
6058
if vertices <= 0:
6159
raise ValueError("number of vertices must be positive")
6260
self.size = vertices
63-
# graph[u] holds indices into self.edges for edges leaving u.
61+
# graph[vertex] holds indices into self.edges for edges leaving that vertex.
6462
self.graph: list[list[int]] = [[] for _ in range(vertices)]
6563
# Each edge is stored as [destination, residual_capacity].
6664
# Edge i and its reverse edge i ^ 1 are always created together.
@@ -96,37 +94,37 @@ def _build_level_graph(self, source: int) -> list[int]:
9694
level[source] = 0
9795
queue = deque([source])
9896
while queue:
99-
u = queue.popleft()
100-
for edge_index in self.graph[u]:
97+
vertex = queue.popleft()
98+
for edge_index in self.graph[vertex]:
10199
destination, residual = self.edges[edge_index]
102100
if residual > 0 and level[destination] == -1:
103-
level[destination] = level[u] + 1
101+
level[destination] = level[vertex] + 1
104102
queue.append(destination)
105103
return level
106104

107105
def _send_flow(
108106
self,
109-
u: int,
107+
vertex: int,
110108
pushed: int,
111109
sink: int,
112110
level: list[int],
113111
progress: list[int],
114112
) -> int:
115113
"""Depth-first search that pushes a blocking flow along the level graph."""
116-
if u == sink:
114+
if vertex == sink:
117115
return pushed
118-
while progress[u] < len(self.graph[u]):
119-
edge_index = self.graph[u][progress[u]]
116+
while progress[vertex] < len(self.graph[vertex]):
117+
edge_index = self.graph[vertex][progress[vertex]]
120118
destination, residual = self.edges[edge_index]
121-
if residual > 0 and level[destination] == level[u] + 1:
119+
if residual > 0 and level[destination] == level[vertex] + 1:
122120
flow = self._send_flow(
123121
destination, min(pushed, residual), sink, level, progress
124122
)
125123
if flow > 0:
126124
self.edges[edge_index][1] -= flow
127125
self.edges[edge_index ^ 1][1] += flow
128126
return flow
129-
progress[u] += 1
127+
progress[vertex] += 1
130128
return 0
131129

132130
def max_flow(self, source: int, sink: int) -> int:

networking_flow/minimum_cut.py

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,9 @@
2222
]
2323

2424

25-
def bfs(graph: list[list[int]], s: int, t: int, parent: list[int]) -> bool:
25+
def bfs(graph: list[list[int]], source: int, sink: int, parent: list[int]) -> bool:
2626
"""
27-
Return True if the sink ``t`` is reachable from the source ``s`` in the
27+
Return True if the ``sink`` is reachable from the ``source`` in the
2828
residual ``graph``, recording the traversal tree in ``parent``.
2929
3030
>>> bfs(test_graph, 0, 5, [-1] * 6)
@@ -33,18 +33,18 @@ def bfs(graph: list[list[int]], s: int, t: int, parent: list[int]) -> bool:
3333
False
3434
"""
3535
visited = [False] * len(graph)
36-
queue = [s]
37-
visited[s] = True
36+
queue = [source]
37+
visited[source] = True
3838

3939
while queue:
40-
u = queue.pop(0)
41-
for ind in range(len(graph[u])):
42-
if visited[ind] is False and graph[u][ind] > 0:
43-
queue.append(ind)
44-
visited[ind] = True
45-
parent[ind] = u
46-
47-
return visited[t]
40+
node = queue.pop(0)
41+
for neighbor in range(len(graph[node])):
42+
if visited[neighbor] is False and graph[node][neighbor] > 0:
43+
queue.append(neighbor)
44+
visited[neighbor] = True
45+
parent[neighbor] = node
46+
47+
return visited[sink]
4848

4949

5050
def mincut(graph: list[list[int]], source: int, sink: int) -> list[tuple[int, int]]:

0 commit comments

Comments
 (0)