Skip to content

Commit dca5e1c

Browse files
authored
Merge pull request #1 from Debashis08/feature-graph-implementation
Feature graph implementation
2 parents f0cb3cc + 416536c commit dca5e1c

File tree

3 files changed

+108
-20
lines changed

3 files changed

+108
-20
lines changed
Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,30 @@
11
#pragma once
22

3-
#include<map>
43
#include<list>
4+
#include<map>
5+
#include<string>
56
using namespace std;
67
enum color { WHITE, GRAY, BLACK };
8+
79
class Node
810
{
911
public:
1012
char data;
1113
int distance;
1214
int color;
1315
Node* parent;
14-
Node(char val);
15-
};
16-
17-
class CompareNodes
18-
{
19-
public:
20-
bool operator()(Node* temp_u, Node* temp_v);
16+
Node(char value);
2117
};
2218

2319
class BFSGraph
2420
{
2521
private:
26-
map<Node*, list<Node*>, CompareNodes> adjlist;
27-
list<Node*> node_list;
28-
Node* find_node_in_list(char val);
29-
void graph_bfs(Node* node);
22+
map<Node*, list<Node*>> _adjlist;
23+
map<char, Node*> _nodeMap;
24+
Node* MakeOrFindNode(char value);
25+
void BreadthFirstSearch(Node* node);
3026
public:
31-
void push_edge(char val_u, char val_v);
32-
void show_graph_data();
33-
void bfs(char val);
34-
void show_bfs_result();
27+
void PushUndirectedEdge(char valueU, char valueV);
28+
void BFS(char value);
29+
string ShowBFSResult();
3530
};
Lines changed: 69 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,80 @@
11
#include "../Headers/0003_Graph/0001_BreadthFirstSearch.h"
2+
#include<iostream>
3+
#include<queue>
4+
#include<string>
25
using namespace std;
36

4-
Node::Node(char val)
7+
Node::Node(char value)
58
{
6-
this->data = val;
9+
this->data = value;
710
distance = INT_MAX;
811
color = WHITE;
912
parent = NULL;
1013
}
1114

12-
bool CompareNodes::operator()(Node* temp_u, Node* temp_v)
15+
Node* BFSGraph::MakeOrFindNode(char value)
1316
{
14-
return (temp_u->data < temp_v->data);
17+
Node* node = NULL;
18+
if (this->_nodeMap.find(value) == this->_nodeMap.end())
19+
{
20+
node = new Node(value);
21+
this->_nodeMap[value] = node;
22+
}
23+
else
24+
{
25+
node = this->_nodeMap[value];
26+
}
27+
return node;
28+
}
29+
30+
void BFSGraph::BreadthFirstSearch(Node* node)
31+
{
32+
node->color = WHITE;
33+
node->distance = 0;
34+
node->parent = NULL;
35+
36+
queue<Node*> nodeQueue;
37+
nodeQueue.push(node);
38+
39+
while (nodeQueue.empty()!=true)
40+
{
41+
Node* currentNode = nodeQueue.front();
42+
nodeQueue.pop();
43+
44+
for (auto adjacentNode : this->_adjlist[currentNode])
45+
{
46+
if (adjacentNode->color == WHITE)
47+
{
48+
adjacentNode->color = GRAY;
49+
adjacentNode->parent = currentNode;
50+
adjacentNode->distance = currentNode->distance + 1;
51+
nodeQueue.push(adjacentNode);
52+
}
53+
}
54+
currentNode->color = BLACK;
55+
}
56+
}
57+
58+
void BFSGraph::PushUndirectedEdge(char valueU, char valueV)
59+
{
60+
Node* nodeU = this->MakeOrFindNode(valueU);
61+
Node* nodeV = this->MakeOrFindNode(valueV);
62+
63+
this->_adjlist[nodeU].push_back(nodeV);
64+
this->_adjlist[nodeV].push_back(nodeU);
65+
}
66+
67+
void BFSGraph::BFS(char value)
68+
{
69+
this->BreadthFirstSearch(this->_nodeMap[value]);
70+
}
71+
72+
string BFSGraph::ShowBFSResult()
73+
{
74+
string result = "";
75+
for (auto value : this->_nodeMap)
76+
{
77+
result = result + " " + value.first + "(" + to_string(value.second->distance) + ")";
78+
}
79+
return result;
1580
}
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<string>
3+
#include "../Headers/0003_Graph/0001_BreadthFirstSearch.h"
4+
5+
// Demonstrate some basic assertions.
6+
namespace BreadthFirstSearchTest
7+
{
8+
TEST(BFSTesting, ShowBFSResultTest) {
9+
BFSGraph graph;
10+
11+
graph.PushUndirectedEdge('s', 'r');
12+
graph.PushUndirectedEdge('s', 'w');
13+
graph.PushUndirectedEdge('r', 'v');
14+
graph.PushUndirectedEdge('w', 't');
15+
graph.PushUndirectedEdge('w', 'x');
16+
graph.PushUndirectedEdge('t', 'x');
17+
graph.PushUndirectedEdge('t', 'u');
18+
graph.PushUndirectedEdge('x', 'u');
19+
graph.PushUndirectedEdge('x', 'y');
20+
graph.PushUndirectedEdge('u', 'y');
21+
22+
graph.BFS('s');
23+
24+
string actualResult = graph.ShowBFSResult();
25+
string expectedResult = " r(1) s(0) t(2) u(3) v(2) w(1) x(2) y(3)";
26+
EXPECT_EQ(actualResult, expectedResult);
27+
}
28+
}

0 commit comments

Comments
 (0)