-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMinCostPathInAWeightedFlowNetwork.cs
More file actions
286 lines (241 loc) · 9.27 KB
/
MinCostPathInAWeightedFlowNetwork.cs
File metadata and controls
286 lines (241 loc) · 9.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
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
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
// Given a flow network with costs associated with each edge find the minimum cost path.
namespace MinCostPathInAWeightedFlowNetwork
{
public class MinCostAugmentingPathSearcher
{
private MinPriorityQueue _pq = new MinPriorityQueue();
public Tuple<bool, IList<Step>> FindPath(FlowNetwork network)
{
var costToReachSource = new List<int>(Enumerable.Repeat(int.MaxValue, network.NVertices));
costToReachSource[network.Source] = 0;
IList<Step> path = new List<Step>(Enumerable.Repeat(new Step(int.MinValue, Step.Directions.FORWARD), network.NVertices));
var visited = new List<bool>(Enumerable.Repeat(false, network.NVertices));
visited[network.Source] = true;
_pq.Enqueue(network.Source, 0);
while (!_pq.Empty)
{
var u = _pq.DequeueMin();
// If we have reached the sink vertex we are done.
if (u == network.Sink) { break; }
for (var v = 0; v < network.NVertices; v++)
{
if (v == u || v == network.Source) { continue; }
// forward edge with remaining capacity if cost is better.
var forwardEdge = network.Edges[u][v];
if (forwardEdge.Capacity > 0 && forwardEdge.Flow <= forwardEdge.Capacity)
{
var newCost = costToReachSource[u] + forwardEdge.Cost;
if (newCost >= 0 && newCost < costToReachSource[v])
{
// This route to reach v is cheaper than we have seen before.
path[v] = new Step(u, Step.Directions.FORWARD);
costToReachSource[v] = newCost;
if (visited[v])
{
_pq.updateKey(v, newCost);
}
else
{
_pq.Enqueue(v, newCost);
visited[v] = true;
}
}
}
// backward edge with flow if cost is better.
var backwardEdge = network.Edges[v][u];
if (backwardEdge.Flow > 0)
{
var newCost = costToReachSource[u] - backwardEdge.Cost;
if (newCost >= 0 && newCost < costToReachSource[v])
{
// This route to reach v is cheaper than we have seen before.
path[v] = new Step(u, Step.Directions.BACKWARD);
costToReachSource[v] = newCost;
if(visited[v])
{
_pq.updateKey(v, newCost);
}
else
{
_pq.Enqueue(v, newCost);
visited[v] = true;
}
}
}
}
}
// return the path if we reached the sink vertex
return (costToReachSource[network.Sink] != int.MaxValue) ? Tuple.Create(true, path) : Tuple.Create(false, path);
}
}
[TestClass]
public class MinCostAugmentingPathSearcherTests
{
[TestMethod]
public void WhenNoPath_ExpectFalseReturned()
{
var edges = new[]
{
Tuple.Create(0, 1, 10, 20),
Tuple.Create(2, 3, 20, 30),
};
var network = new FlowNetwork(edges, 1, 3);
var path = new MinCostAugmentingPathSearcher().FindPath(network);
Assert.IsFalse(path.Item1);
}
[TestMethod]
public void WhenOneValidPath_ExpectPathReturned()
{
var edges = new[]
{
Tuple.Create(1, 2, 10, 20),
Tuple.Create(2, 3, 20, 30),
};
var network = new FlowNetwork(edges, 1, 3);
var path = new MinCostAugmentingPathSearcher().FindPath(network);
Assert.IsTrue(path.Item1);
Assert.AreEqual(2, path.Item2[3].Previous);
Assert.AreEqual(1, path.Item2[2].Previous);
Assert.AreEqual(int.MinValue, path.Item2[1].Previous); // Source
}
[TestMethod]
public void WhenVertexesNotIncludedInPath_ExpectPreviousIntMin()
{
var edges = new[]
{
Tuple.Create(2, 3, 20, 30),
};
var network = new FlowNetwork(edges, 2, 3);
var path = new MinCostAugmentingPathSearcher().FindPath(network);
Assert.AreEqual(int.MinValue, path.Item2[1].Previous);
Assert.AreEqual(int.MinValue, path.Item2[0].Previous);
}
}
#region VALUE OBJECTS
public struct Edge
{
public readonly int Capacity;
public readonly int Cost;
public int Flow;
public Edge(int cap, int cost)
{
Capacity = cap;
Cost = cost;
Flow = 0;
}
}
/// <summary>
/// Flow network is implemented as adjacency matrix which works best for dense graphs.
/// </summary>
public class FlowNetwork
{
public readonly int Source;
public readonly int Sink;
public readonly int NVertices;
public readonly List<List<Edge>> Edges;
/// <summary>
/// Create a new cost weighted flow network.
/// </summary>
/// <param name="edges">u, v, capacity, cost</param>
/// <param name="source">source vertex</param>
/// <param name="sink">sink vertex</param>
public FlowNetwork(ICollection<Tuple<int, int, int, int>> edges, int source, int sink)
{
Source = source;
Sink = sink;
NVertices = FindMaxVertexId(edges) + 1;
Edges = CreateEmptyAdjacencyMatrix(NVertices);
// Assign edges to adjacency matrix
foreach (var edge in edges)
{
Edges[edge.Item1][edge.Item2] = new Edge(edge.Item3, edge.Item4);
}
}
private static List<List<Edge>> CreateEmptyAdjacencyMatrix(int nVertices)
{
var matrix = new List<List<Edge>>(nVertices);
for (var u = 0; u <= nVertices; u++)
{
var row = new List<Edge>(nVertices);
for (var v = 0; v <= nVertices; v++)
{
row.Add(new Edge(0, 0));
}
matrix.Add(row);
}
Debug.Assert(matrix.Count == nVertices + 1, "Expected " + nVertices + "x" + nVertices + " matrix.");
Debug.Assert(matrix.All(x => x.Count == nVertices + 1), "Expected " + nVertices + "x" + nVertices + " matrix.");
return matrix;
}
private static int FindMaxVertexId(ICollection<Tuple<int, int, int, int>> edges)
{
var maxVertex = 0;
foreach (var edge in edges)
{
if (edge.Item1 > maxVertex)
maxVertex = edge.Item1;
if (edge.Item2 > maxVertex)
maxVertex = edge.Item2;
}
return maxVertex;
}
}
public struct Step
{
public readonly int Previous;
public readonly Directions Direction;
public enum Directions : int
{
FORWARD,
BACKWARD
}
public Step(int previous, Directions direction)
{
Previous = previous;
Direction = direction;
}
}
#endregion
#region IGNORE
/// <summary>
/// Throwaway code for demo purposes. A real min heap has O(nlog(n)) insert/remove.
/// </summary>
public class MinPriorityQueue
{
private List<Tuple<int, int>> _elements;
Comparer<Tuple<int, int>> _keyComparer;
public MinPriorityQueue()
{
_keyComparer = Comparer<Tuple<int, int>>.Create((a, b) => { return (a.Item1 < b.Item1) ? -1 : (a.Item1 > b.Item1) ? 1 : 0; });
Clear();
}
public void Clear() { _elements = new List<Tuple<int,int>>(); }
public bool Empty { get { return _elements.Count == 0; } }
public void Enqueue(int value, int key)
{
_elements.Add(Tuple.Create(key, value));
_elements.Sort(_keyComparer);
}
public void updateKey(int value, int newKey)
{
var ele = _elements.Find(x => x.Item2 == value);
_elements.Remove(ele);
_elements.Add(Tuple.Create(newKey, ele.Item2));
_elements.Sort(_keyComparer);
}
public int DequeueMin()
{
if (_elements.Count == 0) { return 0; }
var ele = _elements[0];
_elements.RemoveAt(0);
_elements.Sort(_keyComparer);
return ele.Item2;
}
}
#endregion
}