Skip to content

Commit 393df22

Browse files
committed
dfs logic, and test added
1 parent f6628dd commit 393df22

File tree

5 files changed

+131
-16
lines changed

5 files changed

+131
-16
lines changed

Headers/0003_Graph/0002_DepthFirstSearch.h

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,28 +3,31 @@
33
#include<list>
44
#include<map>
55
#include<string>
6+
#include<vector>
67
using namespace std;
78
enum color { WHITE, GRAY, BLACK };
89

9-
class Node
10+
class DFSNode
1011
{
1112
public:
1213
char data;
13-
int distance;
1414
int color;
15-
Node* parent;
16-
Node(char value);
15+
int discoveredTime;
16+
int finishingTime;
17+
DFSNode* parent;
18+
DFSNode(char value);
1719
};
1820

1921
class DFSGraph
2022
{
2123
private:
22-
map<Node*, list<Node*>> _adjlist;
23-
map<char, Node*> _nodeMap;
24-
Node* MakeOrFindNode(char value);
25-
void DepthFirstSearch(Node* node);
24+
int time;
25+
map<DFSNode*, list<DFSNode*>> _adjlist;
26+
map<char, DFSNode*> _nodeMap;
27+
DFSNode* MakeOrFindNode(char value);
28+
void DepthFirstSearch(DFSNode* DFSNode);
2629
public:
27-
void PushUndirectedEdge(char valueU, char valueV);
28-
void DFS(char value);
29-
string ShowDFSResult();
30+
void PushDirectedEdge(char valueU, char valueV);
31+
void DFS();
32+
vector<pair<char,pair<int,int>>> ShowDFSResult();
3033
};

SourceCodes/0003_Graph/0001_BreadthFirstSearch.cc

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,14 @@ using namespace std;
1010
Node::Node(char value)
1111
{
1212
this->data = value;
13-
distance = INT_MAX;
14-
color = WHITE;
15-
parent = NULL;
13+
this->distance = INT_MAX;
14+
this->color = WHITE;
15+
this->parent = nullptr;
1616
}
1717

1818
Node* Graph::MakeOrFindNode(char value)
1919
{
20-
Node* node = NULL;
20+
Node* node = nullptr;
2121
if (this->_nodeMap.find(value) == this->_nodeMap.end())
2222
{
2323
node = new Node(value);
@@ -34,7 +34,7 @@ void Graph::BreadthFirstSearch(Node* node)
3434
{
3535
node->color = WHITE;
3636
node->distance = 0;
37-
node->parent = NULL;
37+
node->parent = nullptr;
3838

3939
queue<Node*> nodeQueue;
4040
nodeQueue.push(node);
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+
}

Tests/0000_CommonUtilities/UnitTestHelperVectorOfPair.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,19 @@ class UnitTestHelperVectorOfPair
2424
}
2525
return result;
2626
}
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+
}
2742
};
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,28 @@
11
#include<gtest/gtest.h>
22
#include "../Headers/0003_Graph/0002_DepthFirstSearch.h"
3+
#include "../0000_CommonUtilities/UnitTestHelperVectorOfPair.h"
34

45
namespace DepthFirstSearchTest
56
{
7+
UnitTestHelperVectorOfPair<char, int> unitTestHelperVectorOfPair;
68

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+
}
728
}

0 commit comments

Comments
 (0)