Skip to content

Commit b3d64c1

Browse files
authored
Merge pull request #19 from Debashis08/feature-graph-implementation
Feature graph implementation
2 parents 8b314bc + b414507 commit b3d64c1

14 files changed

+271
-164
lines changed

Headers/0003_Graph/0001_BreadthFirstSearch.h

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,22 +11,22 @@ enum color { WHITE, GRAY, BLACK };
1111
class BFSNode
1212
{
1313
public:
14-
char data;
14+
int data;
1515
int distance;
1616
int color;
1717
BFSNode* parent;
18-
BFSNode(char value);
18+
BFSNode(int value);
1919
};
2020

2121
class BFSGraph
2222
{
2323
private:
2424
map<BFSNode*, list<BFSNode*>> _adjlist;
25-
map<char, BFSNode*> _nodeMap;
26-
BFSNode* MakeOrFindNode(char value);
25+
map<int, BFSNode*> _nodeMap;
26+
BFSNode* MakeOrFindNode(int value);
2727
void BreadthFirstSearch(BFSNode* node);
2828
public:
29-
void PushUndirectedEdge(char valueU, char valueV);
30-
void BFS(char value);
31-
vector<pair<char, int>> ShowBFSResult();
29+
void PushUndirectedEdge(int valueU, int valueV);
30+
void BFS(int value);
31+
vector<pair<int, int>> ShowBFSResult();
3232
};

Headers/0003_Graph/0002_DepthFirstSearch.h

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,24 +10,24 @@ enum color { WHITE, GRAY, BLACK };
1010
class DFSNode
1111
{
1212
public:
13-
char data;
13+
int data;
1414
int color;
1515
int discoveredTime;
1616
int finishingTime;
1717
DFSNode* parent;
18-
DFSNode(char value);
18+
DFSNode(int value);
1919
};
2020

2121
class DFSGraph
2222
{
2323
private:
2424
int time;
2525
map<DFSNode*, list<DFSNode*>> _adjlist;
26-
map<char, DFSNode*> _nodeMap;
27-
DFSNode* MakeOrFindNode(char value);
26+
map<int, DFSNode*> _nodeMap;
27+
DFSNode* MakeOrFindNode(int value);
2828
void DepthFirstSearch(DFSNode* DFSNode);
2929
public:
30-
void PushDirectedEdge(char valueU, char valueV);
30+
void PushDirectedEdge(int valueU, int valueV);
3131
void DFS();
32-
vector<pair<char,pair<int,int>>> ShowDFSResult();
32+
vector<pair<int,pair<int,int>>> ShowDFSResult();
3333
};
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#pragma once
2+
3+
#include<list>
4+
#include<map>
5+
#include<string>
6+
#include<vector>
7+
using namespace std;
8+
enum color { WHITE, GRAY, BLACK };
9+
10+
class TopologicalSortNode
11+
{
12+
public:
13+
int data;
14+
int color;
15+
int discoveryTime;
16+
int finishingTime;
17+
TopologicalSortNode* parent;
18+
TopologicalSortNode(int value);
19+
};
20+
21+
class TopologicalSortGraph
22+
{
23+
private:
24+
int time;
25+
map<TopologicalSortNode*, list<TopologicalSortNode*>> _adjlist;
26+
map<int, TopologicalSortNode*> _nodeMap;
27+
TopologicalSortNode* MakeOrFindNode(int value);
28+
list<TopologicalSortNode*> _topologicalSortedNodeList;
29+
void DepthFirstSearch(TopologicalSortNode* DFSNode);
30+
public:
31+
void PushDirectedEdge(int valueU, int valueV);
32+
void TopologicalSort();
33+
vector<pair<int, pair<int, int>>> ShowTopologicalSortResult();
34+
};

SourceCodes/0003_Graph/0001_BreadthFirstSearch.cc

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,15 @@
77
#include<climits>
88
using namespace std;
99

10-
BFSNode::BFSNode(char value)
10+
BFSNode::BFSNode(int value)
1111
{
1212
this->data = value;
1313
this->distance = INT_MAX;
1414
this->color = WHITE;
1515
this->parent = nullptr;
1616
}
1717

18-
BFSNode* BFSGraph::MakeOrFindNode(char value)
18+
BFSNode* BFSGraph::MakeOrFindNode(int value)
1919
{
2020
BFSNode* node = nullptr;
2121
if (this->_nodeMap.find(value) == this->_nodeMap.end())
@@ -58,7 +58,7 @@ void BFSGraph::BreadthFirstSearch(BFSNode* node)
5858
}
5959
}
6060

61-
void BFSGraph::PushUndirectedEdge(char valueU, char valueV)
61+
void BFSGraph::PushUndirectedEdge(int valueU, int valueV)
6262
{
6363
BFSNode* nodeU = this->MakeOrFindNode(valueU);
6464
BFSNode* nodeV = this->MakeOrFindNode(valueV);
@@ -67,14 +67,14 @@ void BFSGraph::PushUndirectedEdge(char valueU, char valueV)
6767
this->_adjlist[nodeV].push_back(nodeU);
6868
}
6969

70-
void BFSGraph::BFS(char value)
70+
void BFSGraph::BFS(int value)
7171
{
7272
this->BreadthFirstSearch(this->_nodeMap[value]);
7373
}
7474

75-
vector<pair<char, int>> BFSGraph::ShowBFSResult()
75+
vector<pair<int, int>> BFSGraph::ShowBFSResult()
7676
{
77-
vector<pair<char, int>> result;
77+
vector<pair<int, int>> result;
7878
for (auto& node : this->_nodeMap)
7979
{
8080
result.push_back(make_pair(node.first, node.second->distance));

SourceCodes/0003_Graph/0002_DepthFirstSearch.cc

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
#include<climits>
55
using namespace std;
66

7-
DFSNode::DFSNode(char value)
7+
DFSNode::DFSNode(int value)
88
{
99
this->data = value;
1010
this->discoveredTime = INT_MAX;
@@ -13,7 +13,7 @@ DFSNode::DFSNode(char value)
1313
this->parent = nullptr;
1414
}
1515

16-
DFSNode* DFSGraph::MakeOrFindNode(char value)
16+
DFSNode* DFSGraph::MakeOrFindNode(int value)
1717
{
1818
DFSNode* node = nullptr;
1919
if (this->_nodeMap.find(value) == this->_nodeMap.end())
@@ -27,6 +27,7 @@ DFSNode* DFSGraph::MakeOrFindNode(char value)
2727
}
2828
return node;
2929
}
30+
3031
void DFSGraph::DepthFirstSearch(DFSNode* nodeU)
3132
{
3233
this->time++;
@@ -45,7 +46,7 @@ void DFSGraph::DepthFirstSearch(DFSNode* nodeU)
4546
nodeU->finishingTime = time;
4647
}
4748

48-
void DFSGraph::PushDirectedEdge(char valueU, char valueV)
49+
void DFSGraph::PushDirectedEdge(int valueU, int valueV)
4950
{
5051
DFSNode* nodeU = this->MakeOrFindNode(valueU);
5152
DFSNode* nodeV = this->MakeOrFindNode(valueV);
@@ -65,9 +66,9 @@ void DFSGraph::DFS()
6566
}
6667
}
6768

68-
vector<pair<char, pair<int, int>>> DFSGraph::ShowDFSResult()
69+
vector<pair<int, pair<int, int>>> DFSGraph::ShowDFSResult()
6970
{
70-
vector<pair<char, pair<int, int>>> result;
71+
vector<pair<int, pair<int, int>>> result;
7172
for (auto& node : this->_nodeMap)
7273
{
7374
result.push_back(make_pair(node.first, make_pair(node.second->discoveredTime, node.second->finishingTime)));
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
#include "../Headers/0003_Graph/0003_TopologicalSort.h"
2+
#include<vector>
3+
#include<utility>
4+
#include<climits>
5+
using namespace std;
6+
7+
TopologicalSortNode::TopologicalSortNode(int value)
8+
{
9+
this->data = value;
10+
this->discoveryTime = INT_MAX;
11+
this->finishingTime = INT_MAX;
12+
this->color = WHITE;
13+
this->parent = nullptr;
14+
}
15+
16+
TopologicalSortNode* TopologicalSortGraph::MakeOrFindNode(int value)
17+
{
18+
TopologicalSortNode* node = nullptr;
19+
if (this->_nodeMap.find(value) == this->_nodeMap.end())
20+
{
21+
node = new TopologicalSortNode(value);
22+
this->_nodeMap[value] = node;
23+
}
24+
else
25+
{
26+
node = this->_nodeMap[value];
27+
}
28+
return node;
29+
}
30+
31+
void TopologicalSortGraph::DepthFirstSearch(TopologicalSortNode* nodeU)
32+
{
33+
this->time++;
34+
nodeU->discoveryTime = this->time;
35+
nodeU->color = GRAY;
36+
for (auto nodeV : this->_adjlist[nodeU])
37+
{
38+
if (nodeV->color == WHITE)
39+
{
40+
nodeV->parent = nodeU;
41+
this->DepthFirstSearch(nodeV);
42+
}
43+
}
44+
nodeU->color = BLACK;
45+
this->time++;
46+
nodeU->finishingTime = time;
47+
this->_topologicalSortedNodeList.push_front(nodeU);
48+
}
49+
50+
void TopologicalSortGraph::PushDirectedEdge(int valueU, int valueV)
51+
{
52+
TopologicalSortNode* nodeU = this->MakeOrFindNode(valueU);
53+
TopologicalSortNode* nodeV = this->MakeOrFindNode(valueV);
54+
55+
this->_adjlist[nodeU].push_back(nodeV);
56+
}
57+
58+
void TopologicalSortGraph::TopologicalSort()
59+
{
60+
this->time = 0;
61+
for (auto& iterator : this->_nodeMap)
62+
{
63+
if (iterator.second->color == WHITE)
64+
{
65+
this->DepthFirstSearch(iterator.second);
66+
}
67+
}
68+
}
69+
70+
vector<pair<int, pair<int, int>>> TopologicalSortGraph::ShowTopologicalSortResult()
71+
{
72+
vector<pair<int, pair<int, int>>> result;
73+
for (auto& node : this->_topologicalSortedNodeList)
74+
{
75+
result.push_back(make_pair(node->data, make_pair(node->discoveryTime, node->finishingTime)));
76+
}
77+
return result;
78+
}

SourceCodes/0003_Graph/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
set(0003GRAPH_SOURCES
33
0001_BreadthFirstSearch.cc
44
0002_DepthFirstSearch.cc
5+
0003_TopologicalSort.cc
56
)
67

78
# Create a library target

Tests/0000_CommonUtilities/UnitTestHelper.h

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@
55
#include<string>
66
using namespace std;
77

8-
template<typename T>
98
class UnitTestHelper
109
{
1110
public:
11+
template<typename T>
1212
string VerifyVectorResult(vector<T> vector)
1313
{
1414
string result = "";
@@ -19,4 +19,36 @@ class UnitTestHelper
1919
result.pop_back();
2020
return result;
2121
}
22+
23+
template<typename T>
24+
string VerifyVectorResult(vector<pair<T,T>> vector)
25+
{
26+
string result = "";
27+
for (auto& iterator : vector)
28+
{
29+
result += to_string(iterator.first) + "(" + to_string(iterator.second) + ")" + " ";
30+
}
31+
32+
if (!result.empty())
33+
{
34+
result.pop_back();
35+
}
36+
return result;
37+
}
38+
39+
template<typename T>
40+
string VerifyVectorResult(vector<pair<T, pair<T, T>>> vector)
41+
{
42+
string result = "";
43+
for (auto& iterator : vector)
44+
{
45+
result += to_string(iterator.first) + "(" + to_string(iterator.second.first) + "," + to_string(iterator.second.second) + ")" + " ";
46+
}
47+
48+
if (!result.empty())
49+
{
50+
result.pop_back();
51+
}
52+
return result;
53+
}
2254
};

Tests/0000_CommonUtilities/UnitTestHelperVectorOfPair.h

Lines changed: 0 additions & 42 deletions
This file was deleted.

Tests/0002_Tree/0001_BinarySearchTreeTest.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
namespace BinarySearchTreeTest
77
{
8-
UnitTestHelper<int> unitTestHelper;
8+
UnitTestHelper unitTestHelper;
99

1010
TEST(BSTInsertData, RecursiveInorderTest)
1111
{

0 commit comments

Comments
 (0)