From b6d28fdd78cfe6c6d4b79efd14c6c5bbffa01214 Mon Sep 17 00:00:00 2001 From: iiloni-umd Date: Tue, 9 May 2023 22:05:21 -0400 Subject: [PATCH 1/5] Added vertex-limited network flow reduction --- networking_flow/vertex_limited.py | 43 +++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 networking_flow/vertex_limited.py diff --git a/networking_flow/vertex_limited.py b/networking_flow/vertex_limited.py new file mode 100644 index 000000000000..6a7e281e0a46 --- /dev/null +++ b/networking_flow/vertex_limited.py @@ -0,0 +1,43 @@ +# 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. +""" + +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 original to the new vertex + max_flow_graph[src_vertex][src_vertex + len(graph)] = limits[src_vertex] + + return max_flow_graph + + +# Test Case + +from ford_fulkerson import ford_fulkerson + +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 \ No newline at end of file From 51833ced9744033667dc2a62b9bbf2da65a054cf Mon Sep 17 00:00:00 2001 From: iiloni-umd Date: Tue, 9 May 2023 22:13:08 -0400 Subject: [PATCH 2/5] Added wikipedia link for explanation --- networking_flow/vertex_limited.py | 1 + 1 file changed, 1 insertion(+) diff --git a/networking_flow/vertex_limited.py b/networking_flow/vertex_limited.py index 6a7e281e0a46..124a3c0cc0b4 100644 --- a/networking_flow/vertex_limited.py +++ b/networking_flow/vertex_limited.py @@ -1,6 +1,7 @@ # 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): From 2c2e53cd830e6d06d0ec1f1c162688819fabd172 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Wed, 10 May 2023 02:13:42 +0000 Subject: [PATCH 3/5] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- networking_flow/vertex_limited.py | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/networking_flow/vertex_limited.py b/networking_flow/vertex_limited.py index 124a3c0cc0b4..ca23e6e9fcdb 100644 --- a/networking_flow/vertex_limited.py +++ b/networking_flow/vertex_limited.py @@ -4,19 +4,20 @@ 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))] - + 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') + 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 + 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 original to the new vertex @@ -30,7 +31,7 @@ def vertex_limited(graph, limits): from ford_fulkerson import ford_fulkerson limits = [29, 12, 14, 20, 7, 29] -graph = [ # rows are source, columns are destination +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], @@ -40,5 +41,5 @@ def vertex_limited(graph, limits): ] 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 \ No newline at end of file +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 From b80ad476f52bacaf6f9fa9abcab5c663eb6d8135 Mon Sep 17 00:00:00 2001 From: iiloni-umd Date: Tue, 9 May 2023 22:56:43 -0400 Subject: [PATCH 4/5] Pre-commit fix --- networking_flow/vertex_limited.py | 26 +++++++++++++++----------- 1 file changed, 15 insertions(+), 11 deletions(-) diff --git a/networking_flow/vertex_limited.py b/networking_flow/vertex_limited.py index 124a3c0cc0b4..db6e034f6f22 100644 --- a/networking_flow/vertex_limited.py +++ b/networking_flow/vertex_limited.py @@ -1,25 +1,30 @@ +from 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. +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))] - + 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') + 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 + 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 original to the new vertex + # 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 @@ -27,10 +32,9 @@ def vertex_limited(graph, limits): # Test Case -from ford_fulkerson import ford_fulkerson limits = [29, 12, 14, 20, 7, 29] -graph = [ # rows are source, columns are destination +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], @@ -40,5 +44,5 @@ def vertex_limited(graph, limits): ] 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 \ No newline at end of file +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 From 99565cb2bfd5f9555de2afb81b39e8bdba0bf924 Mon Sep 17 00:00:00 2001 From: iiloni-umd Date: Tue, 9 May 2023 23:06:12 -0400 Subject: [PATCH 5/5] Module import fix --- networking_flow/vertex_limited.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/networking_flow/vertex_limited.py b/networking_flow/vertex_limited.py index db6e034f6f22..90c9559b3c14 100644 --- a/networking_flow/vertex_limited.py +++ b/networking_flow/vertex_limited.py @@ -1,4 +1,4 @@ -from ford_fulkerson import ford_fulkerson +from networking_flow.ford_fulkerson import ford_fulkerson # Max-Flow reduction for vertex-limited networks (rather than edge-limited) """