forked from keon/algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmaximum_flow.py
More file actions
236 lines (204 loc) · 6.27 KB
/
maximum_flow.py
File metadata and controls
236 lines (204 loc) · 6.27 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
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
"""
Maximum Flow Algorithms
Implements Ford-Fulkerson (DFS), Edmonds-Karp (BFS) and Dinic's algorithm
for computing maximum flow in a flow network.
Reference: https://en.wikipedia.org/wiki/Maximum_flow_problem
Complexity:
Ford-Fulkerson: O(E * f) where f is the max flow value
Edmonds-Karp: O(V * E^2)
Dinic: O(V^2 * E)
"""
from __future__ import annotations
from queue import Queue
def _dfs(
capacity: list[list[int]],
flow: list[list[int]],
visit: list[bool],
vertices: int,
idx: int,
sink: int,
current_flow: int = 1 << 63,
) -> int:
"""DFS helper for Ford-Fulkerson.
Args:
capacity: Capacity matrix.
flow: Current flow matrix.
visit: Visited flags.
vertices: Total number of vertices.
idx: Current vertex index.
sink: Sink vertex index.
current_flow: Flow available along this path.
Returns:
Flow pushed along the augmenting path found.
"""
if idx == sink:
return current_flow
visit[idx] = True
for nxt in range(vertices):
if not visit[nxt] and flow[idx][nxt] < capacity[idx][nxt]:
available_flow = min(current_flow, capacity[idx][nxt] - flow[idx][nxt])
tmp = _dfs(capacity, flow, visit, vertices, nxt, sink, available_flow)
if tmp:
flow[idx][nxt] += tmp
flow[nxt][idx] -= tmp
return tmp
return 0
def ford_fulkerson(capacity: list[list[int]], source: int, sink: int) -> int:
"""Compute maximum flow using Ford-Fulkerson (DFS).
Args:
capacity: Capacity matrix.
source: Source vertex.
sink: Sink vertex.
Returns:
The maximum flow value.
Examples:
>>> ford_fulkerson([[0, 10, 0], [0, 0, 10], [0, 0, 0]], 0, 2)
10
"""
vertices = len(capacity)
ret = 0
flow = [[0] * vertices for _ in range(vertices)]
while True:
visit = [False for _ in range(vertices)]
tmp = _dfs(capacity, flow, visit, vertices, source, sink)
if tmp:
ret += tmp
else:
break
return ret
def edmonds_karp(capacity: list[list[int]], source: int, sink: int) -> int:
"""Compute maximum flow using Edmonds-Karp (BFS).
Args:
capacity: Capacity matrix.
source: Source vertex.
sink: Sink vertex.
Returns:
The maximum flow value.
Examples:
>>> edmonds_karp([[0, 10, 0], [0, 0, 10], [0, 0, 0]], 0, 2)
10
"""
vertices = len(capacity)
ret = 0
flow = [[0] * vertices for _ in range(vertices)]
while True:
tmp = 0
queue: Queue[tuple[int, int]] = Queue()
visit = [False for _ in range(vertices)]
par = [-1 for _ in range(vertices)]
visit[source] = True
queue.put((source, 1 << 63))
while queue.qsize():
front = queue.get()
idx, current_flow = front
if idx == sink:
tmp = current_flow
break
for nxt in range(vertices):
if not visit[nxt] and flow[idx][nxt] < capacity[idx][nxt]:
visit[nxt] = True
par[nxt] = idx
queue.put(
(nxt, min(current_flow, capacity[idx][nxt] - flow[idx][nxt]))
)
if par[sink] == -1:
break
ret += tmp
parent = par[sink]
idx = sink
while parent != -1:
flow[parent][idx] += tmp
flow[idx][parent] -= tmp
idx = parent
parent = par[parent]
return ret
def _dinic_bfs(
capacity: list[list[int]],
flow: list[list[int]],
level: list[int],
source: int,
sink: int,
) -> bool:
"""BFS level graph construction for Dinic's algorithm.
Args:
capacity: Capacity matrix.
flow: Current flow matrix.
level: Level array (modified in place).
source: Source vertex.
sink: Sink vertex.
Returns:
True if sink is reachable from source.
"""
vertices = len(capacity)
queue: Queue[int] = Queue()
queue.put(source)
level[source] = 0
while queue.qsize():
front = queue.get()
for nxt in range(vertices):
if level[nxt] == -1 and flow[front][nxt] < capacity[front][nxt]:
level[nxt] = level[front] + 1
queue.put(nxt)
return level[sink] != -1
def _dinic_dfs(
capacity: list[list[int]],
flow: list[list[int]],
level: list[int],
idx: int,
sink: int,
work: list[int],
current_flow: int = 1 << 63,
) -> int:
"""DFS blocking flow for Dinic's algorithm.
Args:
capacity: Capacity matrix.
flow: Current flow matrix.
level: Level array.
idx: Current vertex.
sink: Sink vertex.
work: Work pointer array.
current_flow: Available flow.
Returns:
Flow pushed.
"""
if idx == sink:
return current_flow
vertices = len(capacity)
while work[idx] < vertices:
nxt = work[idx]
if level[nxt] == level[idx] + 1 and flow[idx][nxt] < capacity[idx][nxt]:
available_flow = min(current_flow, capacity[idx][nxt] - flow[idx][nxt])
tmp = _dinic_dfs(capacity, flow, level, nxt, sink, work, available_flow)
if tmp > 0:
flow[idx][nxt] += tmp
flow[nxt][idx] -= tmp
return tmp
work[idx] += 1
return 0
def dinic(capacity: list[list[int]], source: int, sink: int) -> int:
"""Compute maximum flow using Dinic's algorithm.
Args:
capacity: Capacity matrix.
source: Source vertex.
sink: Sink vertex.
Returns:
The maximum flow value.
Examples:
>>> dinic([[0, 10, 0], [0, 0, 10], [0, 0, 0]], 0, 2)
10
"""
vertices = len(capacity)
flow = [[0] * vertices for _ in range(vertices)]
ret = 0
while True:
level = [-1 for _ in range(vertices)]
work = [0 for _ in range(vertices)]
if not _dinic_bfs(capacity, flow, level, source, sink):
break
while True:
tmp = _dinic_dfs(capacity, flow, level, source, sink, work)
if tmp > 0:
ret += tmp
else:
break
return ret