Skip to content

Commit f490d33

Browse files
committed
fixed some errors in the scc logic
1 parent 5c2571e commit f490d33

File tree

4 files changed

+59
-9
lines changed

4 files changed

+59
-9
lines changed

Headers/0003_Graph/0004_StronglyConnectedComponents.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,14 @@ class StronglyConnectedComponentsGraph
2626
map<SCCNode*, list<SCCNode*>> _adjlistT;
2727
map<int, SCCNode*> _nodeMap;
2828
list<SCCNode*> _nodesFinishingTimeOrder;
29-
vector<vector<SCCNode*>> _allConnectedComponents;
29+
vector<vector<int>> _allConnectedComponents;
3030
SCCNode* MakeOrFindNode(int value);
3131
void DepthFirstSearchOnGraphG(SCCNode* DFSNode);
32-
vector<SCCNode*> DepthFirstSearchOnGraphT(SCCNode* DFSNode, vector<SCCNode*> connectedComponents);
32+
vector<int> DepthFirstSearchOnGraphT(SCCNode* DFSNode, vector<int> connectedComponents);
3333
public:
3434
void PushDirectedEdge(int valueU, int valueV);
3535
void PushSingleNode(int valueU);
3636
void DFSOnGraphG();
3737
void DFSOnGraphT();
38-
void FindAllStronglyConnectedComponents();
38+
vector<vector<int>> FindAllStronglyConnectedComponents();
3939
};

SourceCodes/0003_Graph/0004_StronglyConnectedComponents.cc

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,11 @@ void StronglyConnectedComponentsGraph::DepthFirstSearchOnGraphG(SCCNode* nodeU)
4747
this->_nodesFinishingTimeOrder.push_front(nodeU);
4848
}
4949

50-
vector<SCCNode*> StronglyConnectedComponentsGraph::DepthFirstSearchOnGraphT(SCCNode* nodeU, vector<SCCNode*> connectedComponents)
50+
vector<int> StronglyConnectedComponentsGraph::DepthFirstSearchOnGraphT(SCCNode* nodeU, vector<int> connectedComponents)
5151
{
5252
nodeU->color = GRAY;
53-
for (auto nodeV : this->_adjlistG[nodeU])
53+
connectedComponents.push_back(nodeU->data);
54+
for (auto nodeV : this->_adjlistT[nodeU])
5455
{
5556
if (nodeV->color == WHITE)
5657
{
@@ -71,7 +72,7 @@ void StronglyConnectedComponentsGraph::PushDirectedEdge(int valueU, int valueV)
7172
this->_adjlistG[nodeU].push_back(nodeV);
7273

7374
// Creating the transpose of the actual graph.
74-
this->_adjlistG[nodeV].push_back(nodeU);
75+
this->_adjlistT[nodeV].push_back(nodeU);
7576
}
7677

7778
void StronglyConnectedComponentsGraph::PushSingleNode(int valueU)
@@ -103,15 +104,16 @@ void StronglyConnectedComponentsGraph::DFSOnGraphT()
103104
{
104105
if (iterator->color == WHITE)
105106
{
106-
vector<SCCNode*> connectedComponents;
107+
vector<int> connectedComponents;
107108
auto result = this->DepthFirstSearchOnGraphT(iterator, connectedComponents);
108109
this->_allConnectedComponents.push_back(result);
109110
}
110111
}
111112
}
112113

113-
114-
void StronglyConnectedComponentsGraph::FindAllStronglyConnectedComponents()
114+
vector<vector<int>> StronglyConnectedComponentsGraph::FindAllStronglyConnectedComponents()
115115
{
116116
this->DFSOnGraphG();
117+
this->DFSOnGraphT();
118+
return this->_allConnectedComponents;
117119
}

Tests/0000_CommonUtilities/UnitTestHelper.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,4 +51,20 @@ class UnitTestHelper
5151
}
5252
return result;
5353
}
54+
55+
template<typename T>
56+
string VerifyVectorResult(vector<vector<T>> vector)
57+
{
58+
string result = "";
59+
for (auto& iterator : vector)
60+
{
61+
result += "[";
62+
for (auto& it : iterator)
63+
{
64+
result += to_string(it) + " ";
65+
}
66+
result += "]";
67+
}
68+
return result;
69+
}
5470
};
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#include<gtest/gtest.h>
2+
#include "../Headers/0003_Graph/0004_StronglyConnectedComponents.h"
3+
#include "../0000_CommonUtilities/UnitTestHelper.h"
4+
5+
namespace StronglyConnectedComponentsTest
6+
{
7+
UnitTestHelper unitTestHelper;
8+
9+
TEST(StronglyConnectedComponentsTesting, SimpleGraphTest)
10+
{
11+
StronglyConnectedComponentsGraph graph;
12+
13+
graph.PushDirectedEdge(1, 2);
14+
graph.PushDirectedEdge(2, 3);
15+
graph.PushDirectedEdge(2, 5);
16+
graph.PushDirectedEdge(2, 6);
17+
graph.PushDirectedEdge(3, 4);
18+
graph.PushDirectedEdge(3, 7);
19+
graph.PushDirectedEdge(4, 3);
20+
graph.PushDirectedEdge(4, 8);
21+
graph.PushDirectedEdge(5, 1);
22+
graph.PushDirectedEdge(5, 6);
23+
graph.PushDirectedEdge(6, 7);
24+
graph.PushDirectedEdge(7, 6);
25+
graph.PushDirectedEdge(7, 8);
26+
graph.PushDirectedEdge(8, 8);
27+
28+
string actualResult = unitTestHelper.VerifyVectorResult(graph.FindAllStronglyConnectedComponents());
29+
string expectedResult = "";
30+
EXPECT_EQ(actualResult, expectedResult);
31+
}
32+
}

0 commit comments

Comments
 (0)