-
-
Notifications
You must be signed in to change notification settings - Fork 49.6k
feat: Add Dinic's Algorithm for Maximum Flow #14011
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Click here to look at the relevant links ⬇️
🔗 Relevant Links
Repository:
Python:
Automated review generated by algorithms-keeper. If there's any problem regarding this review, please open an issue about it.
algorithms-keeper commands and options
algorithms-keeper actions can be triggered by commenting on this PR:
@algorithms-keeper reviewto trigger the checks for only added pull request files@algorithms-keeper review-allto trigger the checks for all the pull request files, including the modified files. As we cannot post review comments on lines not part of the diff, this command will post all the messages in one comment.NOTE: Commands are in beta and so this feature is restricted only to a member or owner of the organization.
networking_flow/dinic.py
Outdated
|
|
||
|
|
||
| class Dinic: | ||
| def __init__(self, n: int): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please provide return type hint for the function: __init__. If the function does not return a value, please provide the type hint as: def function() -> None:
Please provide descriptive name for the parameter: n
| self.graph = [[] for _ in range(n)] | ||
| self.level = [] | ||
|
|
||
| def add_edge(self, u: int, v: int, capacity: int) -> None: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As there is no test file in this pull request nor any test function or class in the file networking_flow/dinic.py, please provide doctest for the function add_edge
Please provide descriptive name for the parameter: u
Please provide descriptive name for the parameter: v
| # Backward edge: [u, 0, index_of_forward_edge] | ||
| self.graph[v].append([u, 0, len(self.graph[u]) - 1]) | ||
|
|
||
| def bfs(self, source: int, sink: int) -> bool: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As there is no test file in this pull request nor any test function or class in the file networking_flow/dinic.py, please provide doctest for the function bfs
networking_flow/dinic.py
Outdated
|
|
||
| return self.level[sink] >= 0 | ||
|
|
||
| def dfs(self, u: int, sink: int, flow: int, ptr: list) -> int: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As there is no test file in this pull request nor any test function or class in the file networking_flow/dinic.py, please provide doctest for the function dfs
Please provide descriptive name for the parameter: u
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Click here to look at the relevant links ⬇️
🔗 Relevant Links
Repository:
Python:
Automated review generated by algorithms-keeper. If there's any problem regarding this review, please open an issue about it.
algorithms-keeper commands and options
algorithms-keeper actions can be triggered by commenting on this PR:
@algorithms-keeper reviewto trigger the checks for only added pull request files@algorithms-keeper review-allto trigger the checks for all the pull request files, including the modified files. As we cannot post review comments on lines not part of the diff, this command will post all the messages in one comment.NOTE: Commands are in beta and so this feature is restricted only to a member or owner of the organization.
networking_flow/dinic.py
Outdated
|
|
||
|
|
||
| class Dinic: | ||
| def __init__(self, n: int): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please provide return type hint for the function: __init__. If the function does not return a value, please provide the type hint as: def function() -> None:
Please provide descriptive name for the parameter: n
| self.graph = [[] for _ in range(n)] | ||
| self.level = [] | ||
|
|
||
| def add_edge(self, u: int, v: int, capacity: int) -> None: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As there is no test file in this pull request nor any test function or class in the file networking_flow/dinic.py, please provide doctest for the function add_edge
Please provide descriptive name for the parameter: u
Please provide descriptive name for the parameter: v
| # Backward edge: [u, 0, index_of_forward_edge] | ||
| self.graph[v].append([u, 0, len(self.graph[u]) - 1]) | ||
|
|
||
| def bfs(self, source: int, sink: int) -> bool: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As there is no test file in this pull request nor any test function or class in the file networking_flow/dinic.py, please provide doctest for the function bfs
networking_flow/dinic.py
Outdated
|
|
||
| return self.level[sink] >= 0 | ||
|
|
||
| def dfs(self, u: int, sink: int, flow: int, ptr: list) -> int: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As there is no test file in this pull request nor any test function or class in the file networking_flow/dinic.py, please provide doctest for the function dfs
Please provide descriptive name for the parameter: u
| # RETREAT step: If no flow can be pushed, this node is dead for this phase. | ||
| return 0 | ||
|
|
||
| def max_flow(self, source: int, sink: int) -> int: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As there is no test file in this pull request nor any test function or class in the file networking_flow/dinic.py, please provide doctest for the function max_flow
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Click here to look at the relevant links ⬇️
🔗 Relevant Links
Repository:
Python:
Automated review generated by algorithms-keeper. If there's any problem regarding this review, please open an issue about it.
algorithms-keeper commands and options
algorithms-keeper actions can be triggered by commenting on this PR:
@algorithms-keeper reviewto trigger the checks for only added pull request files@algorithms-keeper review-allto trigger the checks for all the pull request files, including the modified files. As we cannot post review comments on lines not part of the diff, this command will post all the messages in one comment.NOTE: Commands are in beta and so this feature is restricted only to a member or owner of the organization.
networking_flow/dinic.py
Outdated
|
|
||
|
|
||
| class Dinic: | ||
| def __init__(self, n: int): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please provide return type hint for the function: __init__. If the function does not return a value, please provide the type hint as: def function() -> None:
Please provide descriptive name for the parameter: n
| self.graph = [[] for _ in range(n)] | ||
| self.level = [] | ||
|
|
||
| def add_edge(self, u: int, v: int, capacity: int) -> None: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As there is no test file in this pull request nor any test function or class in the file networking_flow/dinic.py, please provide doctest for the function add_edge
Please provide descriptive name for the parameter: u
Please provide descriptive name for the parameter: v
| # Backward edge: [u, 0, index_of_forward_edge] | ||
| self.graph[v].append([u, 0, len(self.graph[u]) - 1]) | ||
|
|
||
| def bfs(self, source: int, sink: int) -> bool: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As there is no test file in this pull request nor any test function or class in the file networking_flow/dinic.py, please provide doctest for the function bfs
networking_flow/dinic.py
Outdated
|
|
||
| return self.level[sink] >= 0 | ||
|
|
||
| def dfs(self, u: int, sink: int, flow: int, ptr: list) -> int: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As there is no test file in this pull request nor any test function or class in the file networking_flow/dinic.py, please provide doctest for the function dfs
Please provide descriptive name for the parameter: u
for more information, see https://pre-commit.ci
shivaganesht
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great implementation of Dinic's Algorithm for Maximum Flow! The algorithm is correctly implemented and the comments explain the approach well.
Feedback:
- The algorithm correctly uses BFS to build level graphs and DFS to find blocking flows
- Time complexity O(V^2 * E) is optimal for dense graphs
- Code structure is clean and follows the project conventions
Minor suggestions:
- Consider adding more detailed docstrings with parameter descriptions
- Add type hints for better code clarity
- Include complexity analysis in the docstring
Overall, this is a solid contribution that adds value to the algorithms collection.
Thanks again for the review! I've just pushed the updates based on your feedback:
Please let me know if there's anything else needed! 🚀 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Click here to look at the relevant links ⬇️
🔗 Relevant Links
Repository:
Python:
Automated review generated by algorithms-keeper. If there's any problem regarding this review, please open an issue about it.
algorithms-keeper commands and options
algorithms-keeper actions can be triggered by commenting on this PR:
@algorithms-keeper reviewto trigger the checks for only added pull request files@algorithms-keeper review-allto trigger the checks for all the pull request files, including the modified files. As we cannot post review comments on lines not part of the diff, this command will post all the messages in one comment.NOTE: Commands are in beta and so this feature is restricted only to a member or owner of the organization.
| Implements Dinic's Algorithm for finding the Maximum Flow in a flow network. | ||
| """ | ||
|
|
||
| def __init__(self, n: int) -> None: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please provide descriptive name for the parameter: n
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In the context of graph algorithms, n is the standard convention for representing the number of vertices, similar to how i is used in loops. It is widely understood in this domain and changing it might actually reduce readability for those familiar with the algorithm.
| self.graph: list[list[list[int]]] = [[] for _ in range(n)] | ||
| self.level: list[int] = [] | ||
|
|
||
| def add_edge(self, u: int, v: int, capacity: int) -> None: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As there is no test file in this pull request nor any test function or class in the file networking_flow/dinic.py, please provide doctest for the function add_edge
Please provide descriptive name for the parameter: u
Please provide descriptive name for the parameter: v
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Regarding parameters u and v: These are universally recognized standard notations for the start and end nodes of an edge in graph theory.
Regarding the doctest: This is a helper method. Its functionality is implicitly and fully verified by the comprehensive doctests in the max_flow method, which relies on add_edge to construct the graph correctly.
| # Backward edge: [u, 0, index_of_forward_edge] | ||
| self.graph[v].append([u, 0, len(self.graph[u]) - 1]) | ||
|
|
||
| def bfs(self, source: int, sink: int) -> bool: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As there is no test file in this pull request nor any test function or class in the file networking_flow/dinic.py, please provide doctest for the function bfs
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This bfs function is an internal component of the Dinic algorithm. I have included comprehensive doctests in the main max_flow function. If those tests pass, it proves that the internal components (like bfs) are functioning correctly.
|
|
||
| return self.level[sink] >= 0 | ||
|
|
||
| def dfs(self, u: int, sink: int, flow: int, ptr: list[int]) -> int: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As there is no test file in this pull request nor any test function or class in the file networking_flow/dinic.py, please provide doctest for the function dfs
Please provide descriptive name for the parameter: u
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Similar to bfs, this dfs function is an internal helper verified integrally by the max_flow doctests. u is the standard notation for the current node being visited in graph traversal algorithms.
Describe your change:
Checklist: