Skip to content

Commit c1be0e9

Browse files
authored
Merge pull request #22 from Debashis08/release
Release
2 parents 4bd32c5 + d3e4d1b commit c1be0e9

File tree

3 files changed

+128
-0
lines changed

3 files changed

+128
-0
lines changed

Headers/0003_Graph/0003_TopologicalSort.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,15 @@ class TopologicalSortGraph
2222
{
2323
private:
2424
int time;
25+
bool hasCycle;
2526
map<TopologicalSortNode*, list<TopologicalSortNode*>> _adjlist;
2627
map<int, TopologicalSortNode*> _nodeMap;
2728
TopologicalSortNode* MakeOrFindNode(int value);
2829
list<TopologicalSortNode*> _topologicalSortedNodeList;
2930
void DepthFirstSearch(TopologicalSortNode* DFSNode);
3031
public:
3132
void PushDirectedEdge(int valueU, int valueV);
33+
void PushSingleNode(int valueU);
3234
void TopologicalSort();
3335
vector<pair<int, pair<int, int>>> ShowTopologicalSortResult();
3436
};

SourceCodes/0003_Graph/0003_TopologicalSort.cc

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#include<vector>
33
#include<utility>
44
#include<climits>
5+
#include<stdexcept>
56
using namespace std;
67

78
TopologicalSortNode::TopologicalSortNode(int value)
@@ -40,6 +41,11 @@ void TopologicalSortGraph::DepthFirstSearch(TopologicalSortNode* nodeU)
4041
nodeV->parent = nodeU;
4142
this->DepthFirstSearch(nodeV);
4243
}
44+
else if (nodeV->color == GRAY)
45+
{
46+
this->hasCycle = true;
47+
return;
48+
}
4349
}
4450
nodeU->color = BLACK;
4551
this->time++;
@@ -55,6 +61,11 @@ void TopologicalSortGraph::PushDirectedEdge(int valueU, int valueV)
5561
this->_adjlist[nodeU].push_back(nodeV);
5662
}
5763

64+
void TopologicalSortGraph::PushSingleNode(int valueU)
65+
{
66+
TopologicalSortNode* nodeU = this->MakeOrFindNode(valueU);
67+
}
68+
5869
void TopologicalSortGraph::TopologicalSort()
5970
{
6071
this->time = 0;
@@ -63,12 +74,20 @@ void TopologicalSortGraph::TopologicalSort()
6374
if (iterator.second->color == WHITE)
6475
{
6576
this->DepthFirstSearch(iterator.second);
77+
if (this->hasCycle == true)
78+
{
79+
break;
80+
}
6681
}
6782
}
6883
}
6984

7085
vector<pair<int, pair<int, int>>> TopologicalSortGraph::ShowTopologicalSortResult()
7186
{
87+
if (this->hasCycle == true)
88+
{
89+
throw runtime_error("Cycle Detected");
90+
}
7291
vector<pair<int, pair<int, int>>> result;
7392
for (auto& node : this->_topologicalSortedNodeList)
7493
{
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
#include<gtest/gtest.h>
2+
#include "../Headers/0003_Graph/0003_TopologicalSort.h"
3+
#include "../0000_CommonUtilities/UnitTestHelper.h"
4+
5+
namespace TopologicalSortTest
6+
{
7+
UnitTestHelper unitTestHelper;
8+
9+
TEST(TopoSortTesting, ShowTopoSortResult)
10+
{
11+
TopologicalSortGraph graph;
12+
13+
graph.PushDirectedEdge(1, 2);
14+
graph.PushDirectedEdge(1, 4);
15+
graph.PushDirectedEdge(2, 3);
16+
graph.PushDirectedEdge(4, 3);
17+
graph.PushSingleNode(5);
18+
graph.PushDirectedEdge(6, 7);
19+
graph.PushDirectedEdge(6, 8);
20+
graph.PushDirectedEdge(7, 4);
21+
graph.PushDirectedEdge(7, 8);
22+
graph.PushDirectedEdge(9, 8);
23+
24+
graph.TopologicalSort();
25+
26+
string actualResult = unitTestHelper.VerifyVectorResult(graph.ShowTopologicalSortResult());
27+
string expectedResult = "9(17,18) 6(11,16) 7(12,15) 8(13,14) 5(9,10) 1(1,8) 4(6,7) 2(2,5) 3(3,4)";
28+
29+
EXPECT_EQ(actualResult, expectedResult);
30+
}
31+
32+
// Test with a larger graph and multiple paths between nodes
33+
TEST(TopoSortTesting, LargeGraphMultiplePaths)
34+
{
35+
TopologicalSortGraph graph;
36+
graph.PushDirectedEdge(1, 2);
37+
graph.PushDirectedEdge(1, 3);
38+
graph.PushDirectedEdge(2, 4);
39+
graph.PushDirectedEdge(3, 4);
40+
graph.PushDirectedEdge(4, 5);
41+
graph.PushDirectedEdge(5, 6);
42+
graph.PushDirectedEdge(6, 7);
43+
44+
graph.TopologicalSort();
45+
string actualResult = unitTestHelper.VerifyVectorResult(graph.ShowTopologicalSortResult());
46+
string expectedResult = "1(1,14) 3(12,13) 2(2,11) 4(3,10) 5(4,9) 6(5,8) 7(6,7)";
47+
48+
EXPECT_EQ(actualResult, expectedResult);
49+
}
50+
51+
// Test with a graph containing disconnected components
52+
TEST(TopoSortTesting, DisconnectedGraph)
53+
{
54+
TopologicalSortGraph graph;
55+
graph.PushDirectedEdge(1, 2);
56+
graph.PushDirectedEdge(3, 4);
57+
graph.PushDirectedEdge(5, 6);
58+
59+
graph.TopologicalSort();
60+
string actualResult = unitTestHelper.VerifyVectorResult(graph.ShowTopologicalSortResult());
61+
string expectedResult = "5(9,12) 6(10,11) 3(5,8) 4(6,7) 1(1,4) 2(2,3)";
62+
63+
EXPECT_EQ(actualResult, expectedResult);
64+
}
65+
66+
// Test with a graph that has multiple nodes pointing to the same node
67+
TEST(TopoSortTesting, MultipleIncomingEdges)
68+
{
69+
TopologicalSortGraph graph;
70+
graph.PushDirectedEdge(1, 3);
71+
graph.PushDirectedEdge(2, 3);
72+
graph.PushDirectedEdge(3, 4);
73+
74+
graph.TopologicalSort();
75+
string actualResult = unitTestHelper.VerifyVectorResult(graph.ShowTopologicalSortResult());
76+
string expectedResult = "2(7,8) 1(1,6) 3(2,5) 4(3,4)";
77+
78+
EXPECT_EQ(actualResult, expectedResult);
79+
}
80+
81+
// Test for a single-node graph to check the base case
82+
TEST(TopoSortTesting, SingleNodeGraph)
83+
{
84+
TopologicalSortGraph graph;
85+
graph.PushSingleNode(1);
86+
87+
graph.TopologicalSort();
88+
string actualResult = unitTestHelper.VerifyVectorResult(graph.ShowTopologicalSortResult());
89+
string expectedResult = "1(1,2)";
90+
91+
EXPECT_EQ(actualResult, expectedResult);
92+
}
93+
94+
// Test with a cyclic graph to verify it can detect cycles (assuming cycle detection is implemented)
95+
TEST(TopoSortTesting, CyclicGraph)
96+
{
97+
TopologicalSortGraph graph;
98+
graph.PushDirectedEdge(1, 2);
99+
graph.PushDirectedEdge(2, 3);
100+
graph.PushDirectedEdge(3, 1); // Cycle: 1 -> 2 -> 3 -> 1
101+
102+
graph.TopologicalSort();
103+
104+
// Expected output if cycle detection is implemented
105+
EXPECT_THROW(graph.ShowTopologicalSortResult(), runtime_error);
106+
}
107+
}

0 commit comments

Comments
 (0)