-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathMaxFlow_DFS.py
More file actions
81 lines (66 loc) · 2 KB
/
MaxFlow_DFS.py
File metadata and controls
81 lines (66 loc) · 2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
import numpy as np
from scipy.sparse import csr_matrix
from scipy.sparse.csgraph import depth_first_order
x = np.array([[0, 8, 0, 3],
[0, 0, 2, 5],
[0, 0, 0, 0],
[0, 0, 6, 0]])
def path(arr, s, t):
"""
:param arr: Array of predecessors
:param s: Source
:param t: Sink
:return: Shortest length path from source to sink
"""
source_vertex = s
temp = [t]
i = t
while arr[i] != -9999:
temp.append(arr[i])
i = arr[i]
return temp[::-1]
def find_bottleneck(G, arr):
"""
:param G: Input Graph
:param arr: Shortest path array
:return: capacity of bottleneck edge
"""
min_val = np.inf
for i in range(1, len(arr)):
start, end = arr[i - 1], arr[i]
if G[start][end] < min_val:
min_val = G[start][end]
return min_val
def augment(G1, short_path, bottleneck_edge):
"""
:param G1: Residual Graph Gf
:param short_path:
:param bottleneck_edge:
:return: Augmented Residual graph Gf'
"""
for i in range(1, len(short_path)):
start, end = short_path[i - 1], short_path[i]
G1[start][end] -= bottleneck_edge
G1[end][start] += bottleneck_edge
return G1
def edmonds_karp(G, s, t):
"""
:param G: Network Graph
:param s: Source
:param t: Sink
:return: Maximum flow through the network
"""
flow = 0
source, sink = s, t
nodes, predecessor = depth_first_order(csr_matrix(G), 0, directed=True, return_predecessors=True)
shortest_path = path(predecessor, source, sink)
# print shortest_path
while len(shortest_path) > 1:
bottleneck_edge = find_bottleneck(G, shortest_path)
flow += bottleneck_edge
G = augment(G, shortest_path, bottleneck_edge)
nodes, predecessor = depth_first_order(csr_matrix(G), 0, directed=True, return_predecessors=True)
shortest_path = path(predecessor, source, sink)
return flow
max_flow = edmonds_karp(x, 0, 2)
print max_flow