-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathMaximumBiPartiteMatchingFordFulkersonBased.cs
More file actions
127 lines (99 loc) · 3.99 KB
/
MaximumBiPartiteMatchingFordFulkersonBased.cs
File metadata and controls
127 lines (99 loc) · 3.99 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
using System;
using System.Collections.Generic;
namespace AlgorithmsAndDataStructures.Algorithms.Graph.MaximumFlow;
public class MaximumBiPartiteMatchingFordFulkersonBased
{
#pragma warning disable CA1822 // Mark members as static
public int MaxMatching(int[][] graph)
#pragma warning restore CA1822 // Mark members as static
{
if (graph is null) return default;
var colors = new int[graph.Length];
const int startColor = 0;
for (var i = 0; i < colors.Length; i++) colors[i] = -1;
for (var i = 0; i < graph.Length; i++)
if (colors[i] == -1)
Bfs(graph, colors, i, startColor);
var leftSetVertices = new HashSet<int>();
for (var i = 0; i < colors.Length; i++)
if (colors[i] == 0)
leftSetVertices.Add(i);
var flowNetwork = BuildFlowNetwork(graph, leftSetVertices);
var residualGraph = new int[flowNetwork.Length][];
for (var i = 0; i < residualGraph.Length; i++)
{
residualGraph[i] = new int[flowNetwork[i].Length];
for (var j = 0; j < residualGraph[i].Length; j++) residualGraph[i][j] = flowNetwork[i][j];
}
bool hasPath;
var flow = 0;
const int source = 0;
var sink = flowNetwork.Length - 1;
do
{
var visited = new bool[residualGraph.Length];
var delta = GetPath(residualGraph, source, sink, visited, int.MaxValue);
hasPath = delta > 0;
flow += delta;
} while (hasPath);
return flow;
}
private static int GetPath(IReadOnlyList<int[]> residualGraph, int currentVertex, int targetVertex,
IList<bool> visited, int flow)
{
if (currentVertex == targetVertex) return flow;
visited[currentVertex] = true;
for (var i = 0; i < residualGraph.Count; i++)
if (residualGraph[currentVertex][i] > 0 && !visited[i])
{
var delta = GetPath(residualGraph, i, targetVertex, visited,
Math.Min(flow, residualGraph[currentVertex][i]));
if (delta > 0)
{
residualGraph[currentVertex][i] = residualGraph[currentVertex][i] - delta;
residualGraph[i][currentVertex] = residualGraph[i][currentVertex] + delta;
return delta;
}
}
return 0;
}
private static int[][] BuildFlowNetwork(IReadOnlyList<int[]> graph, ICollection<int> leftSetVertices)
{
var flowNetwork = new int[graph.Count + 2][];
flowNetwork[0] = new int[flowNetwork.Length];
flowNetwork[^1] = new int[flowNetwork.Length];
for (var i = 0; i < graph.Count; i++)
{
var currentVertex = i + 1;
var isLeftSideVertex = leftSetVertices.Contains(i);
flowNetwork[currentVertex] = new int[flowNetwork.Length];
if (isLeftSideVertex) flowNetwork[0][currentVertex] = 1;
for (var j = 0; j < graph.Count; j++) flowNetwork[currentVertex][j + 1] = graph[i][j];
if (!isLeftSideVertex) flowNetwork[currentVertex][flowNetwork.Length - 1] = 1;
}
return flowNetwork;
}
private static void Bfs(IReadOnlyList<int[]> graph, IList<int> colors, int startVertex, int startColor)
{
var queue = new Queue<int>();
colors[startVertex] = startColor;
queue.Enqueue(startVertex);
while (queue.Count > 0)
{
var currentVertex = queue.Dequeue();
for (var i = 0; i < graph.Count; i++)
{
if (graph[currentVertex][i] < 1) continue;
if (colors[i] != -1)
{
if (colors[i] == colors[currentVertex]) throw new Exception("Graph is not bipartite.");
}
else
{
colors[i] = 1 ^ colors[currentVertex];
queue.Enqueue(i);
}
}
}
}
}