-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMaxFlowInFlowNetworkFordFulkersonPathSearch.cs
More file actions
522 lines (441 loc) · 17.3 KB
/
MaxFlowInFlowNetworkFordFulkersonPathSearch.cs
File metadata and controls
522 lines (441 loc) · 17.3 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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
// Find the maximal flow for a given flow network using the Ford-Fulkerson augmenting path search method.
//
// As flows are integers this algorithm is guaranteed to complete with O(Ef) complexity. Where E is
// the number of edges and f is the maximum flow. In the worst case we would visit every edge (E) and
// increment its flow by one f times.
namespace MaxFlowInFlowNetworkFordFulkersonPathSearch
{
#region MAX FLOW CALCULATOR
public interface ISearchMethod
{
IEnumerable<IList<Step>> FindAugmentingPath(FlowNetwork network);
}
public class MaxFlowCalculator
{
private ISearchMethod _searchMethod;
/// <summary>
/// Updates the flow network's nodes with the maximal flow.
/// Returns false if no valid path could be found from source to sink.
/// </summary>
public bool UpdateMaxFlow(FlowNetwork networkToUpdate)
{
var atLeastOnePathFound = false;
foreach (var path in _searchMethod.FindAugmentingPath(networkToUpdate))
{
atLeastOnePathFound = true;
var minFlowOnPath = FindMinFlowOnPath(path, networkToUpdate);
AugmentFlowsOnPath(path, minFlowOnPath, networkToUpdate);
}
return atLeastOnePathFound;
}
/// <summary>
/// Search backwards along path to find the edge that can accept the
/// least amount (limiting case) of increased flow on the current path.
/// </summary>
protected static int FindMinFlowOnPath(IList<Step> path, FlowNetwork network)
{
int minFlowOnPath = int.MaxValue;
int v = network.SinkIndex;
while (v != network.SourceIndex)
{
int u = path[v].Previous;
int available;
if (path[v].Forward)
{
// Forward edges can be adjusted by the remaining capacity.
var edge = network.FindEdge(u, v);
available = edge.Capacity - edge.Flow;
}
else
{
// Backwards edges can only be reduced by their existing flow.
var edge = network.FindEdge(v, u);
available = edge.Flow;
}
// Is this edge's flow the minimum we have seen?
minFlowOnPath = (available < minFlowOnPath) ? available : minFlowOnPath;
// Follow reverse path to source.
v = u;
}
return minFlowOnPath;
}
/// <summary>
/// Augment the flows along the path with the lowest amount of flow found earlier.
/// </summary>
protected static void AugmentFlowsOnPath(IList<Step> path, int minFlowOnPath, FlowNetwork networkToUpdate)
{
int v = networkToUpdate.SinkIndex;
while (v != networkToUpdate.SourceIndex)
{
int u = path[v].Previous;
if (path[v].Forward)
{
networkToUpdate.AddFlow(u, v, minFlowOnPath);
}
else
{
networkToUpdate.AddFlow(v, u, -minFlowOnPath);
}
// Follow reverse path to source.
v = u;
}
}
}
[TestClass]
public class MaxFlowCalculatorTests
{
private class TestCalc : MaxFlowCalculator
{
public static int TestFindMinFlowOnPath(IList<Step> path, FlowNetwork network)
{
return FindMinFlowOnPath(path, network);
}
public static void TestAugmentFlowsOnPath(IList<Step> path, int minFlowOnPath, FlowNetwork networkToUpdate)
{
AugmentFlowsOnPath(path, minFlowOnPath, networkToUpdate);
}
}
[TestMethod]
public void WhenPathHasVertexWithFiveAndTwoAvailable_ExpectMinFlowIsTwo()
{
var edges = new[]
{
Tuple.Create(0, 1, 10),
Tuple.Create(1, 2, 10),
};
var network = new FlowNetwork(edges, 0, 2);
network.AddFlow(0, 1, 5); // Available: 5
network.AddFlow(1, 2, 8); // Available: 2
var path = new[]
{
new Step { Forward = true, Previous = -1 }, // [0]
new Step { Forward = true, Previous = 0 }, // [1]
new Step { Forward = true, Previous = 1 }, // [2]
};
var result = TestCalc.TestFindMinFlowOnPath(path, network);
Assert.AreEqual(2, result);
}
[TestMethod]
public void WhenBackwardsStepHasLowestFlow_ExpectBackwardsFlowIsMinFlow()
{
var edges = new[]
{
Tuple.Create(0, 1, 4),
Tuple.Create(1, 2, 4),
Tuple.Create(2, 4, 4),
Tuple.Create(3, 2, 4),
Tuple.Create(3, 4, 4),
};
var network = new FlowNetwork(edges, 0, 4);
network.AddFlow(1, 2, 1);
network.AddFlow(3, 2, 1);
network.AddFlow(3, 4, 2);
var path = new[]
{
new Step { Forward = true, Previous = -1 }, // 0->X
new Step { Forward = true, Previous = 0 }, // 1->0
new Step { Forward = true, Previous = 1 }, // 2->1 Available: 4 - 1 = 3
new Step { Forward = false, Previous = 2 }, // 3->2 Available: 1
new Step { Forward = true, Previous = 3 }, // 4->3 Available: 4 - 2 = 1
};
var result = TestCalc.TestFindMinFlowOnPath(path, network);
Assert.AreEqual(1, result);
}
[TestMethod]
public void WhenAugmentPathByTwo_ExpecForwardEdgesHaveFlowIncreased()
{
var edges = new[]
{
Tuple.Create(0, 1, 10),
Tuple.Create(1, 2, 10),
};
var network = new FlowNetwork(edges, 0, 2);
var path = new[]
{
new Step { Forward = true, Previous = -1 }, // 0->X
new Step { Forward = true, Previous = 0 }, // 1->0
new Step { Forward = true, Previous = 1 }, // 2->3
};
TestCalc.TestAugmentFlowsOnPath(path, 3, network);
Assert.AreEqual(3, network.FindEdge(0, 1).Flow);
Assert.AreEqual(3, network.FindEdge(1, 2).Flow);
}
[TestMethod]
public void WhenAugmentPathByTwo_ExpecBackwardsEdgesHaveFlowDecreased()
{
var edges = new[]
{
Tuple.Create(1, 0, 10),
Tuple.Create(2, 1, 10),
};
var network = new FlowNetwork(edges, 0, 2);
network.AddFlow(1, 0, 5);
network.AddFlow(2, 1, 5);
var path = new[]
{
new Step { Forward = true, Previous = -1 }, // 0->X
new Step { Forward = false, Previous = 0 }, // 1->0
new Step { Forward = false, Previous = 1 }, // 2->1
};
TestCalc.TestAugmentFlowsOnPath(path, 3, network);
Assert.AreEqual(2, network.FindEdge(1, 0).Flow);
Assert.AreEqual(2, network.FindEdge(2, 1).Flow);
}
}
/// <summary>
/// Uses a Ford-Fulkerson (depth-first search) approach to find an augmented path in the network.
/// </summary>
internal class FordFulkersonSearch : ISearchMethod
{
public IEnumerable<IList<Step>> FindAugmentingPath(FlowNetwork network)
{
var path = new Step[network.TotalVertices];
// Begin potential augmenting path at the source vertex.
path[network.SourceIndex] = new Step { Previous = -1 };
var verticesToVisit = new Stack<int>();
verticesToVisit.Push(network.SourceIndex);
while (verticesToVisit.Any())
{
// Expand augmented path by popping next vertex and exploring adjacent vertexes.
var u = verticesToVisit.Pop();
// Try to make forward progress by checking edges forwards edges (u,v)
// Forward edges must have unfilled capacity.
var forwardEdges = network.FindForwardEdges(u);
foreach (var forwardEdge in forwardEdges)
{
int v = forwardEdge.End;
// If not yet visited and has unused capacity then plan to increase.
if (path[v] == null && forwardEdge.Capacity > forwardEdge.Flow)
{
path[v] = new Step { Previous = u, Forward = true };
if (v == network.SinkIndex)
{
yield return path; // Found augmenting path
}
verticesToVisit.Push(v);
}
}
// Try to make backwards progress by checking edges backwards edges (v,u)
// Backwards edges must have flow that can be reduced.
var backwardEdges = network.FindBackwardEdges(u);
foreach (var backwardEdge in backwardEdges)
{
int v = backwardEdge.Start;
// Try to find an incoming edge into u who hasn't been visited and whose flow can be reduced.
if (path[v] == null && backwardEdge.Flow > 0)
{
path[v] = new Step { Previous = u, Forward = false };
verticesToVisit.Push(v);
}
}
}
yield break; // No augmenting path was found
}
}
[TestClass]
public class FordFulkersonSearchTests
{
[TestMethod]
public void WhenOnlySinglePath_ExpectPathReturned()
{
var edges = new[]
{
Tuple.Create(0, 1, 5),
Tuple.Create(1, 2, 5),
Tuple.Create(2, 3, 5),
};
var network = new FlowNetwork(edges, 0, 3);
var search = new FordFulkersonSearch();
var path = search.FindAugmentingPath(network).Single();
Assert.AreEqual(2, path[3].Previous);
Assert.AreEqual(1, path[2].Previous);
Assert.AreEqual(0, path[1].Previous);
Assert.AreEqual(-1, path[0].Previous);
}
[TestMethod]
public void WhenOnlySinglePathWithCapacity_ExpectPathReturned()
{
var edges = new[]
{
Tuple.Create(0, 1, 5),
Tuple.Create(1, 2, 5),
Tuple.Create(2, 4, 5),
Tuple.Create(2, 3, 5),
Tuple.Create(4, 3, 5),
};
var network = new FlowNetwork(edges, 0, 3);
network.AddFlow(2, 3, 5);
var search = new FordFulkersonSearch();
var path = search.FindAugmentingPath(network).Single();
Assert.AreEqual(4, path[3].Previous);
Assert.AreEqual(2, path[4].Previous);
Assert.AreEqual(1, path[2].Previous);
Assert.AreEqual(0, path[1].Previous);
Assert.AreEqual(-1, path[0].Previous);
}
}
#endregion
#region FLOW NETWORK
/// <summary>
/// Implemented in a hybrid dense/sparse fashion where the vertices are a dense array providing O(1) lookup
/// and the edges are implemented as a sparse collection with a worse case O(E) lookup. Works best
/// for networks where there are many vertices with few (localized traffic?) edges between them.
/// </summary>
public class FlowNetwork
{
private class Vertex
{
public IList<Edge> Forward { get; private set; }
public IList<Edge> Backwards { get; private set; }
public Vertex()
{
Forward = new List<Edge>();
Backwards = new List<Edge>();
}
public void AddForwardEdge(Edge edgeToAdd)
{
Forward.Add(edgeToAdd);
Debug.Assert(Forward.All(x => x.Start == edgeToAdd.Start), "All forward edge links should start on the current vertex.");
}
public void AddBackwardEdge(Edge edgeToAdd)
{
Backwards.Add(edgeToAdd);
Debug.Assert(Backwards.All(x => x.End == edgeToAdd.End), "All backwards edge links should end on the current vertex.");
}
}
public readonly int TotalVertices;
public readonly int SourceIndex;
public readonly int SinkIndex;
private IList<Vertex> Vertices;
public FlowNetwork(IEnumerable<Tuple<int, int, int>> edges, int source, int sink)
{
SourceIndex = source;
SinkIndex = sink;
TotalVertices = edges.Select(x => x.Item1)
.Concat(edges.Select(x => x.Item2))
.Max() + 1;
// Initialize vertex collection;
Vertices = Enumerable.Range(0, TotalVertices)
.Select(x => { return new Vertex(); })
.ToList();
// Populate edges
foreach (var edge in edges)
{
if (FindEdge(edge.Item1, edge.Item2) != null)
{
throw new Exception("Duplicate edge definition found while constructing flow network.");
}
var edgeToAdd = new Edge { Start = edge.Item1, End = edge.Item2, Capacity = edge.Item3 };
var startVertex = Vertices[edgeToAdd.Start];
startVertex.AddForwardEdge(edgeToAdd);
var endVertex = Vertices[edgeToAdd.End];
endVertex.AddBackwardEdge(edgeToAdd);
}
}
public IEnumerable<Edge> FindForwardEdges(int u)
{
var vertex = Vertices[u];
if (vertex.Forward == null)
{
return Enumerable.Empty<Edge>();
}
return vertex.Forward;
}
public IEnumerable<Edge> FindBackwardEdges(int u)
{
var vertex = Vertices[u];
if (vertex.Backwards == null)
{
return Enumerable.Empty<Edge>();
}
return vertex.Backwards;
}
/// <summary>
/// Worse case O(E) would require one vertex to have all edges.
/// Return null if edge does not exist.
/// </summary>
public Edge FindEdge(int u, int v)
{
var vertex = Vertices[u];
return vertex.Forward.FirstOrDefault(x => x.End == v);
Debug.Assert(vertex.Forward.All(x => x.Start == u), "Expected all forward edges to start at the current vertex.");
}
public void AddFlow(int u, int v, int flowToAdd)
{
var edge = FindEdge(u, v);
if (edge.Flow + flowToAdd > edge.Capacity)
{
throw new ArgumentOutOfRangeException("Additional flow would exceed vertex's capacity.");
}
edge.Flow += flowToAdd;
}
}
[TestClass]
public class FlowNetworkTests
{
[TestMethod]
public void WhenAddFlowToEdge_ExpectFlowRetainedBetweenEdgeQueries()
{
var edges = new[]
{
Tuple.Create(1, 2, 3),
Tuple.Create(2, 1, 4),
};
var network = new FlowNetwork(edges, 3, 1);
var before = network.FindEdge(1, 2);
Assert.AreEqual(3, before.Capacity);
Assert.AreEqual(0, before.Flow);
network.AddFlow(1, 2, 3);
var after = network.FindEdge(1, 2);
Assert.AreEqual(3, after.Capacity);
Assert.AreEqual(3, after.Flow);
}
[TestMethod]
public void WhenAddMaxFlowToEachEdge_ExpectEachVertexFlowEqualCapacity()
{
var edges = new[]
{
Tuple.Create(1, 2, 3),
Tuple.Create(2, 1, 4),
Tuple.Create(3, 2, 5),
Tuple.Create(3, 1, 6)
};
var network = new FlowNetwork(edges, 3, 1);
network.AddFlow(1, 2, 3);
network.AddFlow(2, 1, 4);
network.AddFlow(3, 2, 5);
network.AddFlow(3, 1, 6);
foreach (var u in Enumerable.Range(0, 3))
{
foreach (var v in Enumerable.Range(0, 3))
{
var edge = network.FindEdge(u, v);
if (edge == null)
{
continue;
}
Assert.AreEqual(edge.Capacity, edge.Flow);
}
}
}
}
[DebuggerDisplay("{Start}->{End}")]
public class Edge
{
public int Start { get; set; }
public int End { get; set; }
public int Capacity { get; set; }
public int Flow { get; set; }
}
public class Step
{
public int Previous { get; set; }
public bool Forward { get; set; }
}
#endregion
}