Skip to content

Feature graph implementation #19

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 17, 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
14 changes: 7 additions & 7 deletions Headers/0003_Graph/0001_BreadthFirstSearch.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,22 @@ enum color { WHITE, GRAY, BLACK };
class BFSNode
{
public:
char data;
int data;
int distance;
int color;
BFSNode* parent;
BFSNode(char value);
BFSNode(int value);
};

class BFSGraph
{
private:
map<BFSNode*, list<BFSNode*>> _adjlist;
map<char, BFSNode*> _nodeMap;
BFSNode* MakeOrFindNode(char value);
map<int, BFSNode*> _nodeMap;
BFSNode* MakeOrFindNode(int value);
void BreadthFirstSearch(BFSNode* node);
public:
void PushUndirectedEdge(char valueU, char valueV);
void BFS(char value);
vector<pair<char, int>> ShowBFSResult();
void PushUndirectedEdge(int valueU, int valueV);
void BFS(int value);
vector<pair<int, int>> ShowBFSResult();
};
12 changes: 6 additions & 6 deletions Headers/0003_Graph/0002_DepthFirstSearch.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,24 +10,24 @@ enum color { WHITE, GRAY, BLACK };
class DFSNode
{
public:
char data;
int data;
int color;
int discoveredTime;
int finishingTime;
DFSNode* parent;
DFSNode(char value);
DFSNode(int value);
};

class DFSGraph
{
private:
int time;
map<DFSNode*, list<DFSNode*>> _adjlist;
map<char, DFSNode*> _nodeMap;
DFSNode* MakeOrFindNode(char value);
map<int, DFSNode*> _nodeMap;
DFSNode* MakeOrFindNode(int value);
void DepthFirstSearch(DFSNode* DFSNode);
public:
void PushDirectedEdge(char valueU, char valueV);
void PushDirectedEdge(int valueU, int valueV);
void DFS();
vector<pair<char,pair<int,int>>> ShowDFSResult();
vector<pair<int,pair<int,int>>> ShowDFSResult();
};
34 changes: 34 additions & 0 deletions Headers/0003_Graph/0003_TopologicalSort.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#pragma once

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

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

class TopologicalSortGraph
{
private:
int time;
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 TopologicalSort();
vector<pair<int, pair<int, int>>> ShowTopologicalSortResult();
};
12 changes: 6 additions & 6 deletions SourceCodes/0003_Graph/0001_BreadthFirstSearch.cc
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@
#include<climits>
using namespace std;

BFSNode::BFSNode(char value)
BFSNode::BFSNode(int value)
{
this->data = value;
this->distance = INT_MAX;
this->color = WHITE;
this->parent = nullptr;
}

BFSNode* BFSGraph::MakeOrFindNode(char value)
BFSNode* BFSGraph::MakeOrFindNode(int value)
{
BFSNode* node = nullptr;
if (this->_nodeMap.find(value) == this->_nodeMap.end())
Expand Down Expand Up @@ -58,7 +58,7 @@ void BFSGraph::BreadthFirstSearch(BFSNode* node)
}
}

void BFSGraph::PushUndirectedEdge(char valueU, char valueV)
void BFSGraph::PushUndirectedEdge(int valueU, int valueV)
{
BFSNode* nodeU = this->MakeOrFindNode(valueU);
BFSNode* nodeV = this->MakeOrFindNode(valueV);
Expand All @@ -67,14 +67,14 @@ void BFSGraph::PushUndirectedEdge(char valueU, char valueV)
this->_adjlist[nodeV].push_back(nodeU);
}

void BFSGraph::BFS(char value)
void BFSGraph::BFS(int value)
{
this->BreadthFirstSearch(this->_nodeMap[value]);
}

vector<pair<char, int>> BFSGraph::ShowBFSResult()
vector<pair<int, int>> BFSGraph::ShowBFSResult()
{
vector<pair<char, int>> result;
vector<pair<int, int>> result;
for (auto& node : this->_nodeMap)
{
result.push_back(make_pair(node.first, node.second->distance));
Expand Down
11 changes: 6 additions & 5 deletions SourceCodes/0003_Graph/0002_DepthFirstSearch.cc
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#include<climits>
using namespace std;

DFSNode::DFSNode(char value)
DFSNode::DFSNode(int value)
{
this->data = value;
this->discoveredTime = INT_MAX;
Expand All @@ -13,7 +13,7 @@ DFSNode::DFSNode(char value)
this->parent = nullptr;
}

DFSNode* DFSGraph::MakeOrFindNode(char value)
DFSNode* DFSGraph::MakeOrFindNode(int value)
{
DFSNode* node = nullptr;
if (this->_nodeMap.find(value) == this->_nodeMap.end())
Expand All @@ -27,6 +27,7 @@ DFSNode* DFSGraph::MakeOrFindNode(char value)
}
return node;
}

void DFSGraph::DepthFirstSearch(DFSNode* nodeU)
{
this->time++;
Expand All @@ -45,7 +46,7 @@ void DFSGraph::DepthFirstSearch(DFSNode* nodeU)
nodeU->finishingTime = time;
}

void DFSGraph::PushDirectedEdge(char valueU, char valueV)
void DFSGraph::PushDirectedEdge(int valueU, int valueV)
{
DFSNode* nodeU = this->MakeOrFindNode(valueU);
DFSNode* nodeV = this->MakeOrFindNode(valueV);
Expand All @@ -65,9 +66,9 @@ void DFSGraph::DFS()
}
}

vector<pair<char, pair<int, int>>> DFSGraph::ShowDFSResult()
vector<pair<int, pair<int, int>>> DFSGraph::ShowDFSResult()
{
vector<pair<char, pair<int, int>>> result;
vector<pair<int, pair<int, int>>> result;
for (auto& node : this->_nodeMap)
{
result.push_back(make_pair(node.first, make_pair(node.second->discoveredTime, node.second->finishingTime)));
Expand Down
78 changes: 78 additions & 0 deletions SourceCodes/0003_Graph/0003_TopologicalSort.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
#include "../Headers/0003_Graph/0003_TopologicalSort.h"
#include<vector>
#include<utility>
#include<climits>
using namespace std;

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

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

void TopologicalSortGraph::DepthFirstSearch(TopologicalSortNode* nodeU)
{
this->time++;
nodeU->discoveryTime = this->time;
nodeU->color = GRAY;
for (auto nodeV : this->_adjlist[nodeU])
{
if (nodeV->color == WHITE)
{
nodeV->parent = nodeU;
this->DepthFirstSearch(nodeV);
}
}
nodeU->color = BLACK;
this->time++;
nodeU->finishingTime = time;
this->_topologicalSortedNodeList.push_front(nodeU);
}

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

this->_adjlist[nodeU].push_back(nodeV);
}

void TopologicalSortGraph::TopologicalSort()
{
this->time = 0;
for (auto& iterator : this->_nodeMap)
{
if (iterator.second->color == WHITE)
{
this->DepthFirstSearch(iterator.second);
}
}
}

vector<pair<int, pair<int, int>>> TopologicalSortGraph::ShowTopologicalSortResult()
{
vector<pair<int, pair<int, int>>> result;
for (auto& node : this->_topologicalSortedNodeList)
{
result.push_back(make_pair(node->data, make_pair(node->discoveryTime, node->finishingTime)));
}
return result;
}
1 change: 1 addition & 0 deletions SourceCodes/0003_Graph/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
set(0003GRAPH_SOURCES
0001_BreadthFirstSearch.cc
0002_DepthFirstSearch.cc
0003_TopologicalSort.cc
)

# Create a library target
Expand Down
34 changes: 33 additions & 1 deletion Tests/0000_CommonUtilities/UnitTestHelper.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
#include<string>
using namespace std;

template<typename T>
class UnitTestHelper
{
public:
template<typename T>
string VerifyVectorResult(vector<T> vector)
{
string result = "";
Expand All @@ -19,4 +19,36 @@ class UnitTestHelper
result.pop_back();
return result;
}

template<typename T>
string VerifyVectorResult(vector<pair<T,T>> vector)
{
string result = "";
for (auto& iterator : vector)
{
result += to_string(iterator.first) + "(" + to_string(iterator.second) + ")" + " ";
}

if (!result.empty())
{
result.pop_back();
}
return result;
}

template<typename T>
string VerifyVectorResult(vector<pair<T, pair<T, T>>> vector)
{
string result = "";
for (auto& iterator : vector)
{
result += to_string(iterator.first) + "(" + to_string(iterator.second.first) + "," + to_string(iterator.second.second) + ")" + " ";
}

if (!result.empty())
{
result.pop_back();
}
return result;
}
};
42 changes: 0 additions & 42 deletions Tests/0000_CommonUtilities/UnitTestHelperVectorOfPair.h

This file was deleted.

2 changes: 1 addition & 1 deletion Tests/0002_Tree/0001_BinarySearchTreeTest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

namespace BinarySearchTreeTest
{
UnitTestHelper<int> unitTestHelper;
UnitTestHelper unitTestHelper;

TEST(BSTInsertData, RecursiveInorderTest)
{
Expand Down
Loading
Loading