Skip to content

Feature graph implementation #23

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 3 commits into from
Oct 29, 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: 1 addition & 1 deletion Headers/0003_Graph/0003_TopologicalSort.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ class TopologicalSortGraph
bool hasCycle;
map<TopologicalSortNode*, list<TopologicalSortNode*>> _adjlist;
map<int, TopologicalSortNode*> _nodeMap;
TopologicalSortNode* MakeOrFindNode(int value);
list<TopologicalSortNode*> _topologicalSortedNodeList;
TopologicalSortNode* MakeOrFindNode(int value);
void DepthFirstSearch(TopologicalSortNode* DFSNode);
public:
void PushDirectedEdge(int valueU, int valueV);
Expand Down
39 changes: 39 additions & 0 deletions Headers/0003_Graph/0004_StronglyConnectedComponents.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#pragma once

#include<list>
#include<map>
#include<string>
#include<vector>
using namespace std;
enum color { WHITE, GRAY, BLACK };

class SCCNode
{
public:
int data;
int color;
int discoveryTime;
int finishingTime;
SCCNode* parent;
SCCNode(int value);
};

class StronglyConnectedComponentsGraph
{
private:
int time;
map<SCCNode*, list<SCCNode*>> _adjlistG;
map<SCCNode*, list<SCCNode*>> _adjlistT;
map<int, SCCNode*> _nodeMap;
list<SCCNode*> _nodesFinishingTimeOrder;
vector<vector<int>> _allConnectedComponents;
SCCNode* MakeOrFindNode(int value);
void DepthFirstSearchOnGraphG(SCCNode* DFSNode);
void DepthFirstSearchOnGraphT(SCCNode* DFSNode, vector<int>& connectedComponents);
public:
void PushDirectedEdge(int valueU, int valueV);
void PushSingleNode(int valueU);
void DFSOnGraphG();
void DFSOnGraphT();
vector<vector<int>> FindAllStronglyConnectedComponents();
};
118 changes: 118 additions & 0 deletions SourceCodes/0003_Graph/0004_StronglyConnectedComponents.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
#include "../Headers/0003_Graph/0004_StronglyConnectedComponents.h"
#include<vector>
#include<utility>
#include<climits>
using namespace std;

SCCNode::SCCNode(int value)
{
this->data = value;
this->discoveryTime = INT_MAX;
this->finishingTime = INT_MAX;
this->color = WHITE;
this->parent = nullptr;
}

SCCNode* StronglyConnectedComponentsGraph::MakeOrFindNode(int value)
{
SCCNode* node = nullptr;
if (this->_nodeMap.find(value) == this->_nodeMap.end())
{
node = new SCCNode(value);
this->_nodeMap[value] = node;
}
else
{
node = this->_nodeMap[value];
}
return node;
}

void StronglyConnectedComponentsGraph::DepthFirstSearchOnGraphG(SCCNode* nodeU)
{
this->time++;
nodeU->discoveryTime = this->time;
nodeU->color = GRAY;
for (auto nodeV : this->_adjlistG[nodeU])
{
if (nodeV->color == WHITE)
{
nodeV->parent = nodeU;
this->DepthFirstSearchOnGraphG(nodeV);
}
}
nodeU->color = BLACK;
this->time++;
nodeU->finishingTime = time;
this->_nodesFinishingTimeOrder.push_front(nodeU);
}

void StronglyConnectedComponentsGraph::DepthFirstSearchOnGraphT(SCCNode* nodeU, vector<int>& connectedComponents)
{
nodeU->color = GRAY;
connectedComponents.push_back(nodeU->data);
for (auto nodeV : this->_adjlistT[nodeU])
{
if (nodeV->color == WHITE)
{
nodeV->parent = nodeU;
this->DepthFirstSearchOnGraphT(nodeV, connectedComponents);
}
}
nodeU->color = BLACK;
}

void StronglyConnectedComponentsGraph::PushDirectedEdge(int valueU, int valueV)
{
SCCNode* nodeU = this->MakeOrFindNode(valueU);
SCCNode* nodeV = this->MakeOrFindNode(valueV);

// Creating the actual graph.
this->_adjlistG[nodeU].push_back(nodeV);

// Creating the transpose of the actual graph.
this->_adjlistT[nodeV].push_back(nodeU);
}

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

void StronglyConnectedComponentsGraph::DFSOnGraphG()
{
this->time = 0;
for (auto& iterator : this->_nodeMap)
{
if (iterator.second->color == WHITE)
{
this->DepthFirstSearchOnGraphG(iterator.second);
}
}
}

void StronglyConnectedComponentsGraph::DFSOnGraphT()
{
for (auto& iterator : this->_nodeMap)
{
iterator.second->color = WHITE;
iterator.second->parent = nullptr;
}

for (auto& iterator : this->_nodesFinishingTimeOrder)
{
if (iterator->color == WHITE)
{
vector<int> connectedComponents;
this->DepthFirstSearchOnGraphT(iterator, connectedComponents);
this->_allConnectedComponents.push_back(connectedComponents);
}
}
}

vector<vector<int>> StronglyConnectedComponentsGraph::FindAllStronglyConnectedComponents()
{
this->DFSOnGraphG();
this->DFSOnGraphT();
return this->_allConnectedComponents;
}
1 change: 1 addition & 0 deletions SourceCodes/0003_Graph/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ set(0003GRAPH_SOURCES
0001_BreadthFirstSearch.cc
0002_DepthFirstSearch.cc
0003_TopologicalSort.cc
0004_StronglyConnectedComponents.cc
)

# Create a library target
Expand Down
20 changes: 20 additions & 0 deletions Tests/0000_CommonUtilities/UnitTestHelper.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,24 @@ class UnitTestHelper
}
return result;
}

template<typename T>
string VerifyVectorResult(vector<vector<T>> vector)
{
string result = "";
for (auto& iterator : vector)
{
result += "[";
for (auto& it : iterator)
{
result += to_string(it) + " ";
}
if (!result.empty())
{
result.pop_back();
}
result += "]";
}
return result;
}
};
32 changes: 32 additions & 0 deletions Tests/0003_Graph/0004_StronglyConnectedComponentsTest.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#include<gtest/gtest.h>
#include "../Headers/0003_Graph/0004_StronglyConnectedComponents.h"
#include "../0000_CommonUtilities/UnitTestHelper.h"

namespace StronglyConnectedComponentsTest
{
UnitTestHelper unitTestHelper;

TEST(StronglyConnectedComponentsTesting, SimpleGraphTest)
{
StronglyConnectedComponentsGraph graph;

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

string actualResult = unitTestHelper.VerifyVectorResult(graph.FindAllStronglyConnectedComponents());
string expectedResult = "[6 8 7][1 5][2 3][4]";
EXPECT_EQ(actualResult, expectedResult);
}
}
1 change: 1 addition & 0 deletions Tests/0003_Graph/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ add_executable(
0001_BreadthFirstSearchTest.cc
0002_DepthFirstSearchTest.cc
0003_TopologicalSortTest.cc
0004_StronglyConnectedComponentsTest.cc
)

target_link_libraries(
Expand Down
Loading