File tree Expand file tree Collapse file tree 2 files changed +46
-5
lines changed Expand file tree Collapse file tree 2 files changed +46
-5
lines changed Original file line number Diff line number Diff line change @@ -83,12 +83,16 @@ namespace MaximumBipartiteMatching
83
83
84
84
for (int nodeV = 0 ; nodeV < this ->_noOfVertices ; nodeV++)
85
85
{
86
- if (this ->_adjMatrix [nodeU][nodeV] != 0 && this ->_color [nodeV] == WHITE)
86
+ if (nodeU == nodeV)
87
+ {
88
+ continue ;
89
+ }
90
+ else if (this ->_residualGraph [nodeU][nodeV] != 0 && this ->_color [nodeV] == WHITE)
87
91
{
88
92
this ->_color [nodeV] = 1 - this ->_color [nodeU];
89
93
nodeQueue.push (nodeV);
90
94
}
91
- else if (this ->_color [nodeV] == this ->_color [nodeU])
95
+ else if (this ->_residualGraph [nodeU][nodeV] != 0 && this -> _color [nodeV] == this ->_color [nodeU])
92
96
{
93
97
this ->_isBipartite = false ;
94
98
return ;
@@ -208,11 +212,13 @@ namespace MaximumBipartiteMatching
208
212
209
213
vector<vector<int >> Graph::GetMatchings ()
210
214
{
211
- for (int nodeU = 0 ; nodeU < this ->_noOfVertices ; nodeU++)
215
+ for (int nodeU = 0 ; nodeU < this ->_adjMatrix . size () ; nodeU++)
212
216
{
213
- for (int nodeV = 0 ; nodeV < this ->_noOfVertices ; nodeV++)
217
+ for (int nodeV = 0 ; nodeV < this ->_adjMatrix . size () ; nodeV++)
214
218
{
215
- if (this ->_residualGraph [nodeV][nodeU] == 1 )
219
+ if ((nodeU != this ->_source || nodeU != this ->_sink || nodeV != this ->_source || nodeV != this ->_sink )
220
+ &&
221
+ (this ->_adjMatrix [nodeU][nodeV] - this ->_residualGraph [nodeU][nodeV]) == 1 )
216
222
{
217
223
this ->_matchings .push_back ({ nodeU, nodeV });
218
224
}
Original file line number Diff line number Diff line change
1
+ #include < gtest/gtest.h>
2
+ #include " ../Headers/0003_Graph/0017_MaximumBipartiteMatching.h"
3
+ #include " ../0000_CommonUtilities/UnitTestHelper.h"
4
+
5
+ namespace MaximumBipartiteMatching
6
+ {
7
+ TEST (MaximumBipartiteMatching, SimpleGraph)
8
+ {
9
+ // Arrange
10
+ Graph graph;
11
+ UnitTestHelper unitTestHelper;
12
+ int noOfVertices = 9 ;
13
+ int expectedMaximumMatching = 3 ;
14
+ string expectedMatchings = " [0 1][2 6][3 5]" ;
15
+
16
+ // Act
17
+ graph.CreateGraph (noOfVertices);
18
+
19
+ graph.PushDirectedEdge (0 , 1 );
20
+ graph.PushDirectedEdge (2 , 1 );
21
+ graph.PushDirectedEdge (2 , 6 );
22
+ graph.PushDirectedEdge (3 , 5 );
23
+ graph.PushDirectedEdge (3 , 6 );
24
+ graph.PushDirectedEdge (3 , 7 );
25
+ graph.PushDirectedEdge (4 , 6 );
26
+ graph.PushDirectedEdge (8 , 6 );
27
+
28
+ int actualMaximumMatching = graph.FindMaximumBipartiteMatching ();
29
+ vector<vector<int >> actualMatchings = graph.GetMatchings ();
30
+
31
+ // Assert
32
+ ASSERT_EQ (expectedMaximumMatching, actualMaximumMatching);
33
+ ASSERT_EQ (expectedMatchings, unitTestHelper.SerializeVectorToString (actualMatchings));
34
+ }
35
+ }
You can’t perform that action at this time.
0 commit comments