-
Notifications
You must be signed in to change notification settings - Fork 292
/
FordFulkerson.cs
225 lines (179 loc) · 7.13 KB
/
FordFulkerson.cs
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
using System;
using System.Collections.Generic;
using Advanced.Algorithms.DataStructures.Graph;
using Advanced.Algorithms.DataStructures.Graph.AdjacencyList;
namespace Advanced.Algorithms.Graph;
/// <summary>
/// A ford-fulkerson max flox implementation on weighted directed graph using
/// adjacency list representation of graph and residual graph.
/// </summary>
public class FordFulkersonMaxFlow<T, TW> where TW : IComparable
{
private readonly IFlowOperators<TW> @operator;
public FordFulkersonMaxFlow(IFlowOperators<TW> @operator)
{
this.@operator = @operator;
}
/// <summary>
/// Compute max flow by searching a path
/// and then augmenting the residual graph until
/// no more path exists in residual graph with possible flow.
/// </summary>
public TW ComputeMaxFlow(IDiGraph<T> graph,
T source, T sink)
{
ValidateOperator(graph);
var residualGraph = CreateResidualGraph(graph);
var path = Dfs(residualGraph, source, sink);
var result = @operator.DefaultWeight;
while (path != null)
{
result = @operator.AddWeights(result, AugmentResidualGraph(residualGraph, path));
path = Dfs(residualGraph, source, sink);
}
return result;
}
/// <summary>
/// Return all flow Paths.
/// </summary>
public List<List<T>> ComputeMaxFlowAndReturnFlowPath(IDiGraph<T> graph,
T source, T sink)
{
ValidateOperator(graph);
var residualGraph = CreateResidualGraph(graph);
var path = Dfs(residualGraph, source, sink);
var flow = @operator.DefaultWeight;
var result = new List<List<T>>();
while (path != null)
{
result.Add(path);
flow = @operator.AddWeights(flow, AugmentResidualGraph(residualGraph, path));
path = Dfs(residualGraph, source, sink);
}
return result;
}
private void ValidateOperator(IDiGraph<T> graph)
{
if (@operator == null)
throw new ArgumentException("Provide an operator implementation for generic type W during initialization.");
if (!graph.IsWeightedGraph)
if (@operator.DefaultWeight.GetType() != typeof(int))
throw new ArgumentException("Edges of unweighted graphs are assigned an imaginary weight of one (1)." +
"Provide an appropriate IFlowOperators<int> operator implementation during initialization.");
}
/// <summary>
/// Augment current Path to residual Graph.
/// </summary>
private TW AugmentResidualGraph(WeightedDiGraph<T, TW> residualGraph, List<T> path)
{
var min = @operator.MaxWeight;
for (var i = 0; i < path.Count - 1; i++)
{
var vertex1 = residualGraph.FindVertex(path[i]);
var vertex2 = residualGraph.FindVertex(path[i + 1]);
var edgeValue = vertex1.OutEdges[vertex2];
if (min.CompareTo(edgeValue) > 0) min = edgeValue;
}
//augment path
for (var i = 0; i < path.Count - 1; i++)
{
var vertex1 = residualGraph.FindVertex(path[i]);
var vertex2 = residualGraph.FindVertex(path[i + 1]);
//substract from forward paths
vertex1.OutEdges[vertex2] = @operator.SubstractWeights(vertex1.OutEdges[vertex2], min);
//add for backward paths
vertex2.OutEdges[vertex1] = @operator.AddWeights(vertex2.OutEdges[vertex1], min);
}
return min;
}
/// <summary>
/// Depth first search to find a path to sink in residual graph from source.
/// </summary>
private List<T> Dfs(WeightedDiGraph<T, TW> residualGraph, T source, T sink)
{
//init parent lookup table to trace path
var parentLookUp = new Dictionary<WeightedDiGraphVertex<T, TW>, WeightedDiGraphVertex<T, TW>>();
foreach (var vertex in residualGraph.Vertices) parentLookUp.Add(vertex.Value, null);
//regular DFS stuff
var stack = new Stack<WeightedDiGraphVertex<T, TW>>();
var visited = new HashSet<WeightedDiGraphVertex<T, TW>>();
stack.Push(residualGraph.Vertices[source]);
visited.Add(residualGraph.Vertices[source]);
WeightedDiGraphVertex<T, TW> currentVertex = null;
while (stack.Count > 0)
{
currentVertex = stack.Pop();
//reached sink? then break otherwise dig in
if (currentVertex.Key.Equals(sink))
break;
foreach (var edge in currentVertex.OutEdges)
//visit only if edge have available flow
if (!visited.Contains(edge.Key)
&& edge.Value.CompareTo(@operator.DefaultWeight) > 0)
{
//keep track of this to trace out path once sink is found
parentLookUp[edge.Key] = currentVertex;
stack.Push(edge.Key);
visited.Add(edge.Key);
}
}
//could'nt find a path
if (currentVertex == null || !currentVertex.Key.Equals(sink)) return null;
//traverse back from sink to find path to source
var path = new Stack<T>();
path.Push(sink);
while (currentVertex != null && !currentVertex.Key.Equals(source))
{
path.Push(parentLookUp[currentVertex].Key);
currentVertex = parentLookUp[currentVertex];
}
//now reverse the stack to get the path from source to sink
var result = new List<T>();
while (path.Count > 0) result.Add(path.Pop());
return result;
}
/// <summary>
/// Clones this graph and creates a residual graph.
/// </summary>
private WeightedDiGraph<T, TW> CreateResidualGraph(IDiGraph<T> graph)
{
var newGraph = new WeightedDiGraph<T, TW>();
//clone graph vertices
foreach (var vertex in graph.VerticesAsEnumberable) newGraph.AddVertex(vertex.Key);
//clone edges
foreach (var vertex in graph.VerticesAsEnumberable)
//Use either OutEdges or InEdges for cloning
//here we use OutEdges
foreach (var edge in vertex.OutEdges)
{
//original edge
newGraph.AddEdge(vertex.Key, edge.TargetVertexKey, edge.Weight<TW>());
//add a backward edge for residual graph with edge value as default(W)
newGraph.AddEdge(edge.TargetVertexKey, vertex.Key, default);
}
return newGraph;
}
}
/// <summary>
/// Operators to deal with generic Add, Substract etc on edge weights for flow algorithms such as ford-fulkerson
/// algorithm.
/// </summary>
public interface IFlowOperators<TW> where TW : IComparable
{
/// <summary>
/// default value for this type W.
/// </summary>
TW DefaultWeight { get; }
/// <summary>
/// returns the max for this type W.
/// </summary>
TW MaxWeight { get; }
/// <summary>
/// add two weights.
/// </summary>
TW AddWeights(TW a, TW b);
/// <summary>
/// substract b from a.
/// </summary>
TW SubstractWeights(TW a, TW b);
}