-
Notifications
You must be signed in to change notification settings - Fork 292
/
BiPartiteMatching_Tests.cs
240 lines (185 loc) · 7.03 KB
/
BiPartiteMatching_Tests.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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
using Advanced.Algorithms.DataStructures.Graph.AdjacencyList;
using Advanced.Algorithms.Graph;
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace Advanced.Algorithms.Tests.Graph
{
[TestClass]
public class BiPartititeMatchTests
{
/// <summary>
/// Test Max BiParitite Edges using Ford-Fukerson algorithm
/// </summary>
[TestMethod]
public void MaxBiPartiteMatch_AdjacencyListGraph_Smoke_Test()
{
var graph = new Graph<char>();
graph.AddVertex('A');
graph.AddVertex('B');
graph.AddVertex('C');
graph.AddVertex('D');
graph.AddVertex('E');
graph.AddVertex('F');
graph.AddVertex('G');
graph.AddVertex('H');
graph.AddVertex('I');
graph.AddEdge('A', 'F');
graph.AddEdge('B', 'F');
graph.AddEdge('B', 'G');
graph.AddEdge('C', 'H');
graph.AddEdge('C', 'I');
graph.AddEdge('D', 'G');
graph.AddEdge('D', 'H');
graph.AddEdge('E', 'F');
graph.AddEdge('E', 'I');
var algorithm = new BiPartiteMatching<char>(new BiPartiteMatchOperators());
var result = algorithm.GetMaxBiPartiteMatching(graph);
Assert.AreEqual(result.Count, 4);
}
/// <summary>
/// Test Max BiParitite Edges using Ford-Fukerson algorithm
/// </summary>
[TestMethod]
public void MaxBiPartiteMatch_AdjacencyListGraph_Accuracy_Test_1()
{
var graph = new Graph<char>();
graph.AddVertex('0');
graph.AddVertex('1');
graph.AddVertex('2');
graph.AddVertex('3');
graph.AddEdge('0', '2');
graph.AddEdge('1', '3');
var algorithm = new BiPartiteMatching<char>(new BiPartiteMatchOperators());
var result = algorithm.GetMaxBiPartiteMatching(graph);
Assert.AreEqual(result.Count, 2);
}
[TestMethod]
public void MaxBiPartiteMatch_AdjacencyMatrixGraph_Smoke_Test()
{
var graph = new Algorithms.DataStructures.Graph.AdjacencyMatrix.Graph<char>();
graph.AddVertex('A');
graph.AddVertex('B');
graph.AddVertex('C');
graph.AddVertex('D');
graph.AddVertex('E');
graph.AddVertex('F');
graph.AddVertex('G');
graph.AddVertex('H');
graph.AddVertex('I');
graph.AddEdge('A', 'F');
graph.AddEdge('B', 'F');
graph.AddEdge('B', 'G');
graph.AddEdge('C', 'H');
graph.AddEdge('C', 'I');
graph.AddEdge('D', 'G');
graph.AddEdge('D', 'H');
graph.AddEdge('E', 'F');
graph.AddEdge('E', 'I');
var algorithm = new BiPartiteMatching<char>(new BiPartiteMatchOperators());
var result = algorithm.GetMaxBiPartiteMatching(graph);
Assert.AreEqual(result.Count, 4);
}
/// <summary>
/// Test Max BiParitite Edges using Ford-Fukerson algorithm
/// </summary>
[TestMethod]
public void MaxBiPartiteMatch_AdjacencyMatrixGraph_Accuracy_Test_1()
{
var graph = new Algorithms.DataStructures.Graph.AdjacencyMatrix.Graph<char>();
graph.AddVertex('0');
graph.AddVertex('1');
graph.AddVertex('2');
graph.AddVertex('3');
graph.AddEdge('0', '2');
graph.AddEdge('1', '3');
var algorithm = new BiPartiteMatching<char>(new BiPartiteMatchOperators());
var result = algorithm.GetMaxBiPartiteMatching(graph);
Assert.AreEqual(result.Count, 2);
}
[TestMethod]
public void MaxBiPartiteMatch_AdjacencyListGraph_Accurancy_Test()
{
var graph = new Graph<char>();
graph.AddVertex('E');
graph.AddVertex('N');
graph.AddVertex('J');
graph.AddVertex('O');
graph.AddVertex('Y');
graph.AddVertex('Z');
graph.AddVertex('1');
graph.AddVertex('2');
graph.AddVertex('3');
graph.AddVertex('4');
graph.AddVertex('5');
graph.AddVertex('6');
graph.AddEdge('E', '1');
graph.AddEdge('N', '4');
graph.AddEdge('J', '1');
graph.AddEdge('J', '2');
graph.AddEdge('J', '4');
graph.AddEdge('O', '2');
graph.AddEdge('O', '3');
graph.AddEdge('Y', '3');
graph.AddEdge('Y', '5');
graph.AddEdge('Z', '4');
graph.AddEdge('Z', '5');
graph.AddEdge('Z', '6');
var algorithm = new BiPartiteMatching<char>(new BiPartiteMatchOperators());
var result = algorithm.GetMaxBiPartiteMatching(graph);
Assert.AreEqual(result.Count, 6);
}
[TestMethod]
public void MaxBiPartiteMatch_AdjacencyListGraph_Accurancy_Test_Fully_Connected_Bipartite_Graph()
{
var graph = new Graph<char>();
graph.AddVertex('A');
graph.AddVertex('B');
graph.AddVertex('C');
graph.AddVertex('D');
graph.AddVertex('E');
graph.AddVertex('F');
graph.AddEdge('A', 'D');
graph.AddEdge('A', 'E');
graph.AddEdge('A', 'F');
graph.AddEdge('B', 'D');
graph.AddEdge('B', 'E');
graph.AddEdge('B', 'F');
graph.AddEdge('C', 'D');
graph.AddEdge('C', 'E');
graph.AddEdge('C', 'F');
var algorithm = new BiPartiteMatching<char>(new BiPartiteMatchOperators());
var result = algorithm.GetMaxBiPartiteMatching(graph);
Assert.AreEqual(result.Count, 3);
}
/// <summary>
/// operators for generics
/// implemented for int type for edge weights
/// </summary>
public class BiPartiteMatchOperators : IBiPartiteMatchOperators<char>
{
private int currentIndex;
private readonly char[] randomVertices = { '#', '*' };
public int DefaultWeight => 0;
public int MaxWeight => int.MaxValue;
public int AddWeights(int a, int b)
{
return checked(a + b);
}
/// <summary>
/// we need only two random unique vertices not in given graph
/// for Source & Sink dummy nodes
/// </summary>
/// <returns></returns>
public char GetRandomUniqueVertex()
{
currentIndex = currentIndex == 2 ? 0 : currentIndex;
var random = randomVertices[currentIndex];
currentIndex++;
return random;
}
public int SubstractWeights(int a, int b)
{
return checked(a - b);
}
}
}
}