Skip to content

Commit 019820b

Browse files
committed
fix-test: 0017 max bipt. match, init test
1 parent 1caea77 commit 019820b

File tree

2 files changed

+46
-5
lines changed

2 files changed

+46
-5
lines changed

SourceCodes/0003_Graph/0017_MaximumBipartiteMatching.cc

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -83,12 +83,16 @@ namespace MaximumBipartiteMatching
8383

8484
for (int nodeV = 0; nodeV < this->_noOfVertices; nodeV++)
8585
{
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)
8791
{
8892
this->_color[nodeV] = 1 - this->_color[nodeU];
8993
nodeQueue.push(nodeV);
9094
}
91-
else if (this->_color[nodeV] == this->_color[nodeU])
95+
else if (this->_residualGraph[nodeU][nodeV] != 0 && this->_color[nodeV] == this->_color[nodeU])
9296
{
9397
this->_isBipartite = false;
9498
return;
@@ -208,11 +212,13 @@ namespace MaximumBipartiteMatching
208212

209213
vector<vector<int>> Graph::GetMatchings()
210214
{
211-
for (int nodeU = 0; nodeU < this->_noOfVertices; nodeU++)
215+
for (int nodeU = 0; nodeU < this->_adjMatrix.size(); nodeU++)
212216
{
213-
for (int nodeV = 0; nodeV < this->_noOfVertices; nodeV++)
217+
for (int nodeV = 0; nodeV < this->_adjMatrix.size(); nodeV++)
214218
{
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)
216222
{
217223
this->_matchings.push_back({ nodeU, nodeV });
218224
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
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+
}

0 commit comments

Comments
 (0)