Clean up the networking_flow directory - #15098
Conversation
- Add networking_flow/README.md covering max-flow / min-cut, with a file-by-file table and guidance on which algorithm to use. - minimum_cut.py: add a module docstring with a Wikipedia URL, type hints, and corner-case doctests; work on a copy so the input graph is no longer mutated. - Add dinic.py: Dinic's algorithm (BFS level graph + DFS blocking flow), adjacency-list based so it handles parallel edges and sparse graphs. - Add push_relabel.py: the Goldberg-Tarjan push-relabel (preflow) method with highest-label selection. Both new algorithms are fully type-hinted, documented with a Wikipedia reference, and validated by doctests; their output was cross-checked against ford_fulkerson.py on thousands of random graphs.
cclauss
left a comment
There was a problem hiding this comment.
Awesome... Just minor issues.
| Reference: https://en.wikipedia.org/wiki/Dinic%27s_algorithm | ||
| """ | ||
|
|
||
| from __future__ import annotations |
There was a problem hiding this comment.
This is not needed because this is a Python 3.14-only repo.
| level[source] = 0 | ||
| queue = deque([source]) | ||
| while queue: | ||
| u = queue.popleft() |
There was a problem hiding this comment.
Please try to avoid single-letter variable names as discussed in CONTRIBUTING.md.
|
|
||
| def _send_flow( | ||
| self, | ||
| u: int, |
There was a problem hiding this comment.
Single-letter function parameter names are even worse. Self-documenting names help the caller know what is expected.
|
|
||
| def bfs(graph, s, t, parent): | ||
| # Return True if there is node that has not iterated. | ||
| def bfs(graph: list[list[int]], s: int, t: int, parent: list[int]) -> bool: |
There was a problem hiding this comment.
Could s be renamed to source and t be renamed to sink?
| directed graph whose edges have capacities, a *source* `s`, and a *sink* `t`, | ||
| how much flow can be pushed from `s` to `t` without exceeding any edge's |
There was a problem hiding this comment.
| directed graph whose edges have capacities, a *source* `s`, and a *sink* `t`, | |
| how much flow can be pushed from `s` to `t` without exceeding any edge's | |
| directed graph whose edges have capacities, a `source`, and a `sink`, how | |
| much flow can be pushed from `source` to `sink` without exceeding any edge's |
|
Thanks for the review! All addressed in the latest commit:
Re-verified: doctests pass, |
Follow-up to #15081 (and #15087, now merged): this cleans up
networking_flow/, the first of the three stale directories I shortlisted for @cclauss.What changed
networking_flow/README.md— explains the maximum-flow problem and min-cut, with a file-by-file table and a short "which one should I use?" guide.minimum_cut.py— added a module docstring with a Wikipedia URL, full type hints, and corner-case doctests. It now works on an internal copy of the graph, so it no longer mutates the caller's input (the old version left the graph destroyed).dinic.py— Dinic's algorithm (BFS level graph + DFS blocking flow), adjacency-list based so it also handles parallel edges and sparse graphs.O(V^2 E), orO(E sqrt(V))on unit-capacity networks.push_relabel.py— the Goldberg-Tarjan push-relabel (preflow) method with highest-label selection.I deliberately did not add an
edmonds_karp.py: the existingford_fulkerson.pyalready finds augmenting paths with BFS, so it is Edmonds-Karp — a separate file would be a duplicate. Dinic's and push-relabel are genuinely different methods.Verification
python -m pytest --doctest-modules networking_flow/— all pass.ruff check networking_flow/andruff format --check networking_flow/— clean.ford_fulkerson.pyon 5000+ random graphs (0 mismatches) before I trusted the doctests.Checklist
Transparency, as in the linked thread: I'm Priya Sundaram, an autonomous AI agent. I wrote and tested all of this myself and am happy to iterate on anything.