-
Notifications
You must be signed in to change notification settings - Fork 170
/
Copy pathDinic_max_flow.py
256 lines (197 loc) · 7.86 KB
/
Dinic_max_flow.py
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
"""
Code by BH4
This file includes:
- Flow graph data structure
- Dinic's algorithm for max flow
"""
class Vertex():
def __init__(self, node):
"""
adjacent has keys of other vertices with values being the weight of
the edge.
"""
self.id = node
self.adjacent = {}
def add_neighbor(self, neighbor, capacity=0, flow=0):
self.adjacent[neighbor] = (flow, capacity)
def get_residual(self, node_B):
edge = self.adjacent[node_B]
return edge[1] - edge[0]
def get_flow(self, node_B):
if node_B not in self.adjacent:
return 0
edge = self.adjacent[node_B]
return edge[0]
def set_flow(self, node_B, flow):
edge = self.adjacent[node_B]
self.adjacent[node_B] = (flow, edge[1])
def get_capacity(self, node_B):
edge = self.adjacent[node_B]
return edge[1]
def set_capacity(self, node_B, capacity):
edge = self.adjacent[node_B]
self.adjacent[node_B] = (edge[0], capacity)
def get_neighbors(self):
return self.adjacent.keys()
class Graph():
def __init__(self):
self.graph_dict = {}
self.source = None
self.sink = None
def add_vertex(self, node):
self.graph_dict[node] = Vertex(node)
def add_edge(self, start, end, capacity=0):
if start not in self.graph_dict:
self.add_vertex(start)
if end not in self.graph_dict:
self.add_vertex(end)
self.graph_dict[start].add_neighbor(end, capacity=capacity)
def create_directed_graph(self, sources, sinks, capacities):
"""
We will replace all the sources with a single source and all the sinks
with a single sink to simplify the problem.
The source will be labeled -1 and the sink will be labeled -2 to avoid
collisions with names of other nodes.
Capacities should be given as a matrix. Each element capacities[A][B]
is the capacity available for the edge from A to B
"""
for A in range(len(capacities)):
node_A = A
if A in sources:
node_A = -1
elif A in sinks:
node_A = -2
for B in range(len(capacities)):
node_B = B
if B in sources:
node_B = -1
elif B in sinks:
node_B = -2
# There is no reason to send bunnies to the same room.
if capacities[A][B] > 0 and node_A != node_B:
if node_A in self.graph_dict and node_B in self.graph_dict[node_A].get_neighbors():
tot_cap = self.graph_dict[node_A].get_capacity(node_B)+capacities[A][B]
self.graph_dict[node_A].set_capacity(node_B, tot_cap)
else:
self.add_edge(node_A, node_B, capacity=capacities[A][B])
self.source = -1
self.sink = -2
def residual_graph(self):
G_f = Graph()
for node_A in self.graph_dict:
for node_B in self.graph_dict[node_A].get_neighbors():
c_f_AB = self.graph_dict[node_A].get_residual(node_B)
c_f_BA = self.graph_dict[node_A].get_flow(node_B)
if c_f_AB > 0:
G_f.add_edge(node_A, node_B, capacity=c_f_AB)
if c_f_BA > 0:
G_f.add_edge(node_B, node_A, capacity=c_f_BA)
if self.source in G_f.graph_dict:
G_f.source = self.source
if self.sink in G_f.graph_dict:
G_f.sink = self.sink
return G_f
def level_graph(self):
"""
Use a bfs to define a level graph from the current graph.
"""
G_L = Graph()
# used_verts is a dictionary of the distances of each node from the source
used_verts = dict()
queue = [(self.source, 0)]
used_verts[self.source] = 0
sink_distance = None
while len(queue) > 0:
curr, dist = queue.pop(0)
if sink_distance is None or dist < sink_distance:
# Don't add any vertices which are as far or farther from
# the source as the sink. Since they won't reach the sink.
for neighbor in self.graph_dict[curr].get_neighbors():
c = self.graph_dict[curr].get_capacity(neighbor)
if neighbor not in used_verts:
queue.append((neighbor, dist+1))
used_verts[neighbor] = dist+1
G_L.add_edge(curr, neighbor, capacity=c)
if neighbor is self.sink:
sink_distance = dist+1
else:
if used_verts[neighbor] == dist+1:
G_L.add_edge(curr, neighbor, capacity=c)
if self.source in G_L.graph_dict:
G_L.source = self.source
if self.sink in G_L.graph_dict:
G_L.sink = self.sink
return G_L, sink_distance
def send_flow(self, node, n=None):
if node == self.source:
# Needs to be a number larger than the maximum possible flow
n = 200000000000
if node == self.sink:
return n
tot_new_flow = 0
for neighbor in self.graph_dict[node].get_neighbors():
if n > 0:
to_send = self.graph_dict[node].get_residual(neighbor)
if to_send > n:
to_send = n
actual_used = self.send_flow(neighbor, n=to_send)
already_sent = self.graph_dict[node].get_flow(neighbor)
self.graph_dict[node].set_flow(neighbor, actual_used+already_sent)
new_flow = actual_used
n -= new_flow
tot_new_flow += new_flow
return tot_new_flow
def blocking_flow(self):
self.send_flow(self.source)
def add_flow(self, other):
"""
Given a second graph, for any edge from A to B in self add the flow of
the same edge from other.
"""
for node_A in self.graph_dict:
for node_B in self.graph_dict[node_A].get_neighbors():
f_self = self.graph_dict[node_A].get_flow(node_B)
f_other = 0
f_other_reverse = 0
if node_A in other.graph_dict:
f_other = other.graph_dict[node_A].get_flow(node_B)
if node_B in other.graph_dict:
f_other_reverse = other.graph_dict[node_B].get_flow(node_A)
self.graph_dict[node_A].set_flow(node_B, f_self+f_other-f_other_reverse)
def sum_flow(self, node_A):
"""
Return total flow leaving node_A
"""
tot = 0
for node_B in self.graph_dict[node_A].get_neighbors():
tot += self.graph_dict[node_A].get_flow(node_B)
return tot
def max_flow(G):
"""
Implementation of Dinic's algorithm for calculating maximum flow.
"""
G_f = G.residual_graph()
G_L, sink_distance = G_f.level_graph()
# The algorithm repeats until the level
while sink_distance is not None:
G_L.blocking_flow()
G.add_flow(G_L)
G_f = G.residual_graph()
G_L, sink_distance = G_f.level_graph()
return G.sum_flow(G.source)
if __name__ == '__main__':
"""
Defined here is a graph whose maximum flow is 19.
Example from https://en.wikipedia.org/wiki/Dinic%27s_algorithm#Algorithm
"""
source = [0]
sink = [5]
capacities = [[0, 10, 10, 0, 0, 0],
[0, 0, 2, 4, 8, 0],
[0, 0, 0, 0, 9, 0],
[0, 0, 0, 0, 0, 10],
[0, 0, 0, 6, 0, 10],
[0, 0, 0, 0, 0, 0]]
G = Graph()
G.create_directed_graph(source, sink, capacities)
print('Maximum flow through graph is {}'.format(max_flow(G)))