Skip to content

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
wants to merge 6 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions networking_flow/vertex_limited.py
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):

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 function vertex_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

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