Skip to content

Commit b414507

Browse files
committed
graph data datatype changes
1 parent 8201c77 commit b414507

File tree

8 files changed

+137
-161
lines changed

8 files changed

+137
-161
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
};

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)));

Tests/0000_CommonUtilities/UnitTestHelper.h

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,29 @@ class UnitTestHelper
2020
return result;
2121
}
2222

23-
template<typename T1, typename T2>
24-
string VerifyVectorResult(vector<pair<T1,T2>> vector)
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)
2541
{
2642
string result = "";
2743
for (auto& iterator : vector)
2844
{
29-
result += string(1, iterator.first) + "(" + to_string(iterator.second) + ")" + " ";
45+
result += to_string(iterator.first) + "(" + to_string(iterator.second.first) + "," + to_string(iterator.second.second) + ")" + " ";
3046
}
3147

3248
if (!result.empty())

Tests/0000_CommonUtilities/UnitTestHelperVectorOfPair.h

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

Tests/0003_Graph/0001_BreadthFirstSearchTest.cc

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -12,34 +12,34 @@ namespace BreadthFirstSearchTest
1212
{
1313
BFSGraph graph;
1414

15-
graph.PushUndirectedEdge('s', 'r');
16-
graph.PushUndirectedEdge('s', 'w');
17-
graph.PushUndirectedEdge('r', 'v');
18-
graph.PushUndirectedEdge('w', 't');
19-
graph.PushUndirectedEdge('w', 'x');
20-
graph.PushUndirectedEdge('t', 'x');
21-
graph.PushUndirectedEdge('t', 'u');
22-
graph.PushUndirectedEdge('x', 'u');
23-
graph.PushUndirectedEdge('x', 'y');
24-
graph.PushUndirectedEdge('u', 'y');
25-
26-
graph.BFS('s');
15+
graph.PushUndirectedEdge(1, 2);
16+
graph.PushUndirectedEdge(1, 3);
17+
graph.PushUndirectedEdge(2, 4);
18+
graph.PushUndirectedEdge(3, 5);
19+
graph.PushUndirectedEdge(3, 6);
20+
graph.PushUndirectedEdge(5, 6);
21+
graph.PushUndirectedEdge(5, 7);
22+
graph.PushUndirectedEdge(6, 7);
23+
graph.PushUndirectedEdge(6, 8);
24+
graph.PushUndirectedEdge(7, 8);
25+
26+
graph.BFS(1);
2727

2828
string actualResult = unitTestHelper.VerifyVectorResult(graph.ShowBFSResult());
29-
string expectedResult = "r(1) s(0) t(2) u(3) v(2) w(1) x(2) y(3)";
29+
string expectedResult = "1(0) 2(1) 3(1) 4(2) 5(2) 6(2) 7(3) 8(3)";
3030
EXPECT_EQ(actualResult, expectedResult);
3131
}
3232

3333
TEST(BFSTesting, ShowBFSResultTest02)
3434
{
3535
BFSGraph graph;
3636

37-
graph.PushUndirectedEdge('s', 'r');
37+
graph.PushUndirectedEdge(1, 2);
3838

39-
graph.BFS('s');
39+
graph.BFS(1);
4040

4141
string actualResult = unitTestHelper.VerifyVectorResult(graph.ShowBFSResult());
42-
string expectedResult = "r(1) s(0)";
42+
string expectedResult = "1(0) 2(1)";
4343
EXPECT_EQ(actualResult, expectedResult);
4444
}
4545
}

0 commit comments

Comments
 (0)