Skip to content

Commit 64252c7

Browse files
authored
Merge pull request #15 from Debashis08/feature-graph-implementation
Feature graph implementation
2 parents 6440fb4 + 4828827 commit 64252c7

File tree

10 files changed

+238
-41
lines changed

10 files changed

+238
-41
lines changed

Headers/0003_Graph/0001_BreadthFirstSearch.h

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,29 +2,31 @@
22

33
#include<list>
44
#include<map>
5+
#include<utility>
56
#include<string>
7+
#include<vector>
68
using namespace std;
79
enum color { WHITE, GRAY, BLACK };
810

9-
class Node
11+
class BFSNode
1012
{
1113
public:
1214
char data;
1315
int distance;
1416
int color;
15-
Node* parent;
16-
Node(char value);
17+
BFSNode* parent;
18+
BFSNode(char value);
1719
};
1820

1921
class BFSGraph
2022
{
2123
private:
22-
map<Node*, list<Node*>> _adjlist;
23-
map<char, Node*> _nodeMap;
24-
Node* MakeOrFindNode(char value);
25-
void BreadthFirstSearch(Node* node);
24+
map<BFSNode*, list<BFSNode*>> _adjlist;
25+
map<char, BFSNode*> _nodeMap;
26+
BFSNode* MakeOrFindNode(char value);
27+
void BreadthFirstSearch(BFSNode* node);
2628
public:
2729
void PushUndirectedEdge(char valueU, char valueV);
2830
void BFS(char value);
29-
string ShowBFSResult();
31+
vector<pair<char, int>> ShowBFSResult();
3032
};
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
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 DFSNode
11+
{
12+
public:
13+
char data;
14+
int color;
15+
int discoveredTime;
16+
int finishingTime;
17+
DFSNode* parent;
18+
DFSNode(char value);
19+
};
20+
21+
class DFSGraph
22+
{
23+
private:
24+
int time;
25+
map<DFSNode*, list<DFSNode*>> _adjlist;
26+
map<char, DFSNode*> _nodeMap;
27+
DFSNode* MakeOrFindNode(char value);
28+
void DepthFirstSearch(DFSNode* DFSNode);
29+
public:
30+
void PushDirectedEdge(char valueU, char valueV);
31+
void DFS();
32+
vector<pair<char,pair<int,int>>> ShowDFSResult();
33+
};
Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,26 @@
11
#include "../Headers/0003_Graph/0001_BreadthFirstSearch.h"
22
#include<iostream>
33
#include<queue>
4+
#include<vector>
5+
#include<utility>
46
#include<string>
57
#include<climits>
68
using namespace std;
79

8-
Node::Node(char value)
10+
BFSNode::BFSNode(char value)
911
{
1012
this->data = value;
11-
distance = INT_MAX;
12-
color = WHITE;
13-
parent = NULL;
13+
this->distance = INT_MAX;
14+
this->color = WHITE;
15+
this->parent = nullptr;
1416
}
1517

16-
Node* BFSGraph::MakeOrFindNode(char value)
18+
BFSNode* BFSGraph::MakeOrFindNode(char value)
1719
{
18-
Node* node = NULL;
20+
BFSNode* node = nullptr;
1921
if (this->_nodeMap.find(value) == this->_nodeMap.end())
2022
{
21-
node = new Node(value);
23+
node = new BFSNode(value);
2224
this->_nodeMap[value] = node;
2325
}
2426
else
@@ -28,21 +30,21 @@ Node* BFSGraph::MakeOrFindNode(char value)
2830
return node;
2931
}
3032

31-
void BFSGraph::BreadthFirstSearch(Node* node)
33+
void BFSGraph::BreadthFirstSearch(BFSNode* node)
3234
{
3335
node->color = WHITE;
3436
node->distance = 0;
35-
node->parent = NULL;
37+
node->parent = nullptr;
3638

37-
queue<Node*> nodeQueue;
39+
queue<BFSNode*> nodeQueue;
3840
nodeQueue.push(node);
3941

4042
while (nodeQueue.empty()!=true)
4143
{
42-
Node* currentNode = nodeQueue.front();
44+
BFSNode* currentNode = nodeQueue.front();
4345
nodeQueue.pop();
4446

45-
for (auto adjacentNode : this->_adjlist[currentNode])
47+
for (auto &adjacentNode : this->_adjlist[currentNode])
4648
{
4749
if (adjacentNode->color == WHITE)
4850
{
@@ -58,8 +60,8 @@ void BFSGraph::BreadthFirstSearch(Node* node)
5860

5961
void BFSGraph::PushUndirectedEdge(char valueU, char valueV)
6062
{
61-
Node* nodeU = this->MakeOrFindNode(valueU);
62-
Node* nodeV = this->MakeOrFindNode(valueV);
63+
BFSNode* nodeU = this->MakeOrFindNode(valueU);
64+
BFSNode* nodeV = this->MakeOrFindNode(valueV);
6365

6466
this->_adjlist[nodeU].push_back(nodeV);
6567
this->_adjlist[nodeV].push_back(nodeU);
@@ -70,12 +72,12 @@ void BFSGraph::BFS(char value)
7072
this->BreadthFirstSearch(this->_nodeMap[value]);
7173
}
7274

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

SourceCodes/0003_Graph/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# Specify the source files
22
set(0003GRAPH_SOURCES
33
0001_BreadthFirstSearch.cc
4+
0002_DepthFirstSearch.cc
45
)
56

67
# Create a library target

Tests/0000_CommonUtilities/UnitTestHelper.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,11 @@ class UnitTestHelper
1212
string VerifyVectorResult(vector<T> vector)
1313
{
1414
string result = "";
15-
for (auto iterator : vector)
15+
for (auto& iterator : vector)
1616
{
1717
result += to_string(iterator) + " ";
1818
}
1919
result.pop_back();
2020
return result;
2121
}
22-
};
22+
};
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
#pragma once
2+
3+
#include<iostream>
4+
#include<vector>
5+
#include<utility>
6+
#include<string>
7+
using namespace std;
8+
9+
template<typename T1, typename T2>
10+
class UnitTestHelperVectorOfPair
11+
{
12+
public:
13+
string VerifyVectorOfPair(vector<pair<T1,T2>> vector)
14+
{
15+
string result = "";
16+
for (auto& iterator : vector)
17+
{
18+
result += string(1, iterator.first) + "(" + to_string(iterator.second) + ")" + " ";
19+
}
20+
21+
if (!result.empty())
22+
{
23+
result.pop_back();
24+
}
25+
return result;
26+
}
27+
28+
string VerifyVectorOfPairOfTwo(vector<pair<T1, pair<T2, T2>>> vector)
29+
{
30+
string result = "";
31+
for (auto& iterator : vector)
32+
{
33+
result += string(1, iterator.first) + "(" + to_string(iterator.second.first) + "," + to_string(iterator.second.second) + ")" + " ";
34+
}
35+
36+
if (!result.empty())
37+
{
38+
result.pop_back();
39+
}
40+
return result;
41+
}
42+
};

Tests/0003_Graph/0001_BreadthFirstSearchTest.cc

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
#include <gtest/gtest.h>
22
#include<string>
33
#include "../Headers/0003_Graph/0001_BreadthFirstSearch.h"
4+
#include "../0000_CommonUtilities/UnitTestHelperVectorOfPair.h"
45

5-
// Demonstrate some basic assertions.
66
namespace BreadthFirstSearchTest
77
{
8-
TEST(BFSTesting, ShowBFSResultTest) {
8+
UnitTestHelperVectorOfPair<char, int> unitTestHelperVectorOfPair;
9+
10+
TEST(BFSTesting, ShowBFSResultTest01)
11+
{
912
BFSGraph graph;
1013

1114
graph.PushUndirectedEdge('s', 'r');
@@ -21,8 +24,21 @@ namespace BreadthFirstSearchTest
2124

2225
graph.BFS('s');
2326

24-
string actualResult = graph.ShowBFSResult();
25-
string expectedResult = " r(1) s(0) t(2) u(3) v(2) w(1) x(2) y(3)";
27+
string actualResult = unitTestHelperVectorOfPair.VerifyVectorOfPair(graph.ShowBFSResult());
28+
string expectedResult = "r(1) s(0) t(2) u(3) v(2) w(1) x(2) y(3)";
2629
EXPECT_EQ(actualResult, expectedResult);
2730
}
31+
32+
TEST(BFSTesting, ShowBFSResultTest02)
33+
{
34+
BFSGraph graph;
35+
36+
graph.PushUndirectedEdge('s', 'r');
37+
38+
graph.BFS('s');
39+
40+
string actualResult = unitTestHelperVectorOfPair.VerifyVectorOfPair(graph.ShowBFSResult());
41+
string expectedResult = "r(1) s(0)";
42+
EXPECT_EQ(actualResult, expectedResult);
43+
}
2844
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#include<gtest/gtest.h>
2+
#include "../Headers/0003_Graph/0002_DepthFirstSearch.h"
3+
#include "../0000_CommonUtilities/UnitTestHelperVectorOfPair.h"
4+
5+
namespace DepthFirstSearchTest
6+
{
7+
UnitTestHelperVectorOfPair<char, int> unitTestHelperVectorOfPair;
8+
9+
TEST(DFSTesting, ShowDFSResultTest01)
10+
{
11+
DFSGraph graph;
12+
13+
graph.PushDirectedEdge('u', 'v');
14+
graph.PushDirectedEdge('u', 'x');
15+
graph.PushDirectedEdge('v', 'y');
16+
graph.PushDirectedEdge('w', 'y');
17+
graph.PushDirectedEdge('w', 'z');
18+
graph.PushDirectedEdge('x', 'v');
19+
graph.PushDirectedEdge('y', 'x');
20+
graph.PushDirectedEdge('z', 'z');
21+
22+
graph.DFS();
23+
24+
string actualResult = unitTestHelperVectorOfPair.VerifyVectorOfPairOfTwo(graph.ShowDFSResult());
25+
string expectedResult = "u(1,8) v(2,7) w(9,12) x(4,5) y(3,6) z(10,11)";
26+
EXPECT_EQ(actualResult, expectedResult);
27+
}
28+
}

0 commit comments

Comments
 (0)