-
Notifications
You must be signed in to change notification settings - Fork 1
/
PushRelabel.cs
120 lines (98 loc) · 3.62 KB
/
PushRelabel.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
using System;
using System.Collections.Generic;
namespace AlgorithmsAndDataStructures.Algorithms.Graph.MaximumFlow
{
public class PushRelabel
{
#pragma warning disable CA1822 // Mark members as static
public int GetMaxFlow(int[][] graph)
#pragma warning restore CA1822 // Mark members as static
{
if (graph is null)
{
return default;
}
var residualGraph = new int[graph.Length][];
var heights = new int[graph.Length];
heights[0] = graph.Length;
var excess = new int[graph.Length];
Initialize(graph, residualGraph, excess);
while (HasExcess(excess))
{
var currentVertex = GetHighestVertexWithAnExcess(excess, heights);
if (!Push(residualGraph, currentVertex, excess, heights))
{
Relabel(currentVertex, heights);
}
}
var flow = 0;
for (var i = 0; i < residualGraph.Length; i++)
{
flow += residualGraph[i][0];
}
return flow;
}
private static bool HasExcess(IReadOnlyList<int> excess)
{
for (var i = 1; i < excess.Count - 1; i++)
{
if (excess[i] > 0)
{
return true;
}
}
return false;
}
private static int GetHighestVertexWithAnExcess(IReadOnlyList<int> excess, IReadOnlyList<int> height)
{
var maxHeight = int.MinValue;
var maxHeightIndex = -1;
for (var i = 1; i < excess.Count - 1; i++)
{
if (excess[i] > 0 && (maxHeightIndex == -1 || height[maxHeightIndex] > maxHeight))
{
maxHeightIndex = i;
maxHeight = height[maxHeightIndex];
}
}
return maxHeightIndex;
}
private static void Initialize(IReadOnlyList<int[]> graph, IList<int[]> residualGraph, IList<int> excess)
{
for (var i = 0; i < graph.Count; i++)
{
residualGraph[i] = new int[graph.Count];
for (var j = 0; j < residualGraph[i].Length; j++)
{
residualGraph[i][j] = graph[i][j];
}
}
for (var i = 0; i < graph.Count; i++)
{
residualGraph[0][i] = 0;
residualGraph[i][0] = graph[0][i];
excess[i] = graph[0][i];
}
}
private static bool Push(IReadOnlyList<int[]> residualGraph, int currentVertex, IList<int> excess, IReadOnlyList<int> heights)
{
for (var i = 0; i < residualGraph.Count; i++)
{
if (residualGraph[currentVertex][i] > 0 && heights[currentVertex] == heights[i] + 1)
{
var delta = Math.Min(excess[currentVertex], residualGraph[currentVertex][i]);
residualGraph[currentVertex][i] = residualGraph[currentVertex][i] - delta;
residualGraph[i][currentVertex] = residualGraph[i][currentVertex] + delta;
excess[currentVertex] = excess[currentVertex] - delta;
excess[i] = excess[i] + delta;
return true;
}
}
return false;
}
private static void Relabel(int currentVertex, IList<int> heights)
{
heights[currentVertex] = heights[currentVertex] + 1;
}
}
}