-
-
Notifications
You must be signed in to change notification settings - Fork 46.6k
Added vertex-limited network flow reduction #8720
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
Closed
Closed
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
b6d28fd
Added vertex-limited network flow reduction
51833ce
Added wikipedia link for explanation
2c2e53c
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] b80ad47
Pre-commit fix
f6eae02
Merge branch 'vertex-limited' of github.com:iiloni-umd/Python into ve…
99565cb
Module import fix
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
from networking_flow.ford_fulkerson import ford_fulkerson | ||
|
||
# Max-Flow reduction for vertex-limited networks (rather than edge-limited) | ||
""" | ||
Reduces a network that has vertex capacities into a network with edge capacities | ||
so that Ford-Fulkerson will work. | ||
Explanation: https://en.wikipedia.org/wiki/Maximum_flow_problem#Maximum_flow_with_vertex_capacities | ||
""" | ||
|
||
|
||
def vertex_limited(graph, limits): | ||
max_flow_graph = [[0 for i in range(2 * len(graph))] for j in range(2 * len(graph))] | ||
|
||
# Give all directed edges infinite capacity | ||
for src_vertex in range(len(graph)): | ||
for dest_vertex in range(len(graph[0])): | ||
if graph[src_vertex][dest_vertex] == 1: | ||
max_flow_graph[src_vertex][dest_vertex] = float("inf") | ||
|
||
# Expand all vertices into two vertices | ||
for src_vertex in range(len(graph)): | ||
# move all outbound edges of the vertex to the new vertex | ||
max_flow_graph[src_vertex + len(graph)] = max_flow_graph[src_vertex] | ||
max_flow_graph[src_vertex] = [0 for i in range(len(max_flow_graph))] | ||
|
||
# create edge with capacity equal to the vertex's limit | ||
# from the original vertex to the new vertex | ||
max_flow_graph[src_vertex][src_vertex + len(graph)] = limits[src_vertex] | ||
|
||
return max_flow_graph | ||
|
||
|
||
# Test Case | ||
|
||
|
||
limits = [29, 12, 14, 20, 7, 29] | ||
graph = [ # rows are source, columns are destination | ||
[0, 1, 1, 0, 0, 0], | ||
[0, 0, 1, 1, 0, 0], | ||
[0, 1, 0, 0, 1, 0], | ||
[0, 0, 1, 0, 0, 1], | ||
[0, 0, 0, 1, 0, 1], | ||
[0, 0, 0, 0, 0, 0], | ||
] | ||
|
||
mf_graph = vertex_limited(graph, limits) | ||
print(mf_graph) # prints graph reduced to a standard flow network | ||
print(ford_fulkerson(mf_graph, 0, len(mf_graph) - 1)) # max flow should be 19 |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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/vertex_limited.py
, please provide doctest for the functionvertex_limited
Please provide return type hint for the function:
vertex_limited
. If the function does not return a value, please provide the type hint as:def function() -> None:
Please provide type hint for the parameter:
graph
Please provide type hint for the parameter:
limits