Skip to content

topological sort and test implemented #21

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Headers/0003_Graph/0003_TopologicalSort.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,15 @@ class TopologicalSortGraph
{
private:
int time;
bool hasCycle;
map<TopologicalSortNode*, list<TopologicalSortNode*>> _adjlist;
map<int, TopologicalSortNode*> _nodeMap;
TopologicalSortNode* MakeOrFindNode(int value);
list<TopologicalSortNode*> _topologicalSortedNodeList;
void DepthFirstSearch(TopologicalSortNode* DFSNode);
public:
void PushDirectedEdge(int valueU, int valueV);
void PushSingleNode(int valueU);
void TopologicalSort();
vector<pair<int, pair<int, int>>> ShowTopologicalSortResult();
};
19 changes: 19 additions & 0 deletions SourceCodes/0003_Graph/0003_TopologicalSort.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include<vector>
#include<utility>
#include<climits>
#include<stdexcept>
using namespace std;

TopologicalSortNode::TopologicalSortNode(int value)
Expand Down Expand Up @@ -40,6 +41,11 @@ void TopologicalSortGraph::DepthFirstSearch(TopologicalSortNode* nodeU)
nodeV->parent = nodeU;
this->DepthFirstSearch(nodeV);
}
else if (nodeV->color == GRAY)
{
this->hasCycle = true;
return;
}
}
nodeU->color = BLACK;
this->time++;
Expand All @@ -55,6 +61,11 @@ void TopologicalSortGraph::PushDirectedEdge(int valueU, int valueV)
this->_adjlist[nodeU].push_back(nodeV);
}

void TopologicalSortGraph::PushSingleNode(int valueU)
{
TopologicalSortNode* nodeU = this->MakeOrFindNode(valueU);
}

void TopologicalSortGraph::TopologicalSort()
{
this->time = 0;
Expand All @@ -63,12 +74,20 @@ void TopologicalSortGraph::TopologicalSort()
if (iterator.second->color == WHITE)
{
this->DepthFirstSearch(iterator.second);
if (this->hasCycle == true)
{
break;
}
}
}
}

vector<pair<int, pair<int, int>>> TopologicalSortGraph::ShowTopologicalSortResult()
{
if (this->hasCycle == true)
{
throw runtime_error("Cycle Detected");
}
vector<pair<int, pair<int, int>>> result;
for (auto& node : this->_topologicalSortedNodeList)
{
Expand Down
107 changes: 107 additions & 0 deletions Tests/0003_Graph/0003_TopologicalSortTest.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
#include<gtest/gtest.h>
#include "../Headers/0003_Graph/0003_TopologicalSort.h"
#include "../0000_CommonUtilities/UnitTestHelper.h"

namespace TopologicalSortTest
{
UnitTestHelper unitTestHelper;

TEST(TopoSortTesting, ShowTopoSortResult)
{
TopologicalSortGraph graph;

graph.PushDirectedEdge(1, 2);
graph.PushDirectedEdge(1, 4);
graph.PushDirectedEdge(2, 3);
graph.PushDirectedEdge(4, 3);
graph.PushSingleNode(5);
graph.PushDirectedEdge(6, 7);
graph.PushDirectedEdge(6, 8);
graph.PushDirectedEdge(7, 4);
graph.PushDirectedEdge(7, 8);
graph.PushDirectedEdge(9, 8);

graph.TopologicalSort();

string actualResult = unitTestHelper.VerifyVectorResult(graph.ShowTopologicalSortResult());
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)";

EXPECT_EQ(actualResult, expectedResult);
}

// Test with a larger graph and multiple paths between nodes
TEST(TopoSortTesting, LargeGraphMultiplePaths)
{
TopologicalSortGraph graph;
graph.PushDirectedEdge(1, 2);
graph.PushDirectedEdge(1, 3);
graph.PushDirectedEdge(2, 4);
graph.PushDirectedEdge(3, 4);
graph.PushDirectedEdge(4, 5);
graph.PushDirectedEdge(5, 6);
graph.PushDirectedEdge(6, 7);

graph.TopologicalSort();
string actualResult = unitTestHelper.VerifyVectorResult(graph.ShowTopologicalSortResult());
string expectedResult = "1(1,14) 3(12,13) 2(2,11) 4(3,10) 5(4,9) 6(5,8) 7(6,7)";

EXPECT_EQ(actualResult, expectedResult);
}

// Test with a graph containing disconnected components
TEST(TopoSortTesting, DisconnectedGraph)
{
TopologicalSortGraph graph;
graph.PushDirectedEdge(1, 2);
graph.PushDirectedEdge(3, 4);
graph.PushDirectedEdge(5, 6);

graph.TopologicalSort();
string actualResult = unitTestHelper.VerifyVectorResult(graph.ShowTopologicalSortResult());
string expectedResult = "5(9,12) 6(10,11) 3(5,8) 4(6,7) 1(1,4) 2(2,3)";

EXPECT_EQ(actualResult, expectedResult);
}

// Test with a graph that has multiple nodes pointing to the same node
TEST(TopoSortTesting, MultipleIncomingEdges)
{
TopologicalSortGraph graph;
graph.PushDirectedEdge(1, 3);
graph.PushDirectedEdge(2, 3);
graph.PushDirectedEdge(3, 4);

graph.TopologicalSort();
string actualResult = unitTestHelper.VerifyVectorResult(graph.ShowTopologicalSortResult());
string expectedResult = "2(7,8) 1(1,6) 3(2,5) 4(3,4)";

EXPECT_EQ(actualResult, expectedResult);
}

// Test for a single-node graph to check the base case
TEST(TopoSortTesting, SingleNodeGraph)
{
TopologicalSortGraph graph;
graph.PushSingleNode(1);

graph.TopologicalSort();
string actualResult = unitTestHelper.VerifyVectorResult(graph.ShowTopologicalSortResult());
string expectedResult = "1(1,2)";

EXPECT_EQ(actualResult, expectedResult);
}

// Test with a cyclic graph to verify it can detect cycles (assuming cycle detection is implemented)
TEST(TopoSortTesting, CyclicGraph)
{
TopologicalSortGraph graph;
graph.PushDirectedEdge(1, 2);
graph.PushDirectedEdge(2, 3);
graph.PushDirectedEdge(3, 1); // Cycle: 1 -> 2 -> 3 -> 1

graph.TopologicalSort();

// Expected output if cycle detection is implemented
EXPECT_THROW(graph.ShowTopologicalSortResult(), runtime_error);
}
}
Loading