Skip to content

Feature graph implementation #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 11 additions & 16 deletions Headers/0003_Graph/0001_BreadthFirstSearch.h
Original file line number Diff line number Diff line change
@@ -1,35 +1,30 @@
#pragma once

#include<map>
#include<list>
#include<map>
#include<string>
using namespace std;
enum color { WHITE, GRAY, BLACK };

class Node
{
public:
char data;
int distance;
int color;
Node* parent;
Node(char val);
};

class CompareNodes
{
public:
bool operator()(Node* temp_u, Node* temp_v);
Node(char value);
};

class BFSGraph
{
private:
map<Node*, list<Node*>, CompareNodes> adjlist;
list<Node*> node_list;
Node* find_node_in_list(char val);
void graph_bfs(Node* node);
map<Node*, list<Node*>> _adjlist;
map<char, Node*> _nodeMap;
Node* MakeOrFindNode(char value);
void BreadthFirstSearch(Node* node);
public:
void push_edge(char val_u, char val_v);
void show_graph_data();
void bfs(char val);
void show_bfs_result();
void PushUndirectedEdge(char valueU, char valueV);
void BFS(char value);
string ShowBFSResult();
};
73 changes: 69 additions & 4 deletions SourceCodes/0003_Graph/0001_BreadthFirstSearch.cc
Original file line number Diff line number Diff line change
@@ -1,15 +1,80 @@
#include "../Headers/0003_Graph/0001_BreadthFirstSearch.h"
#include<iostream>
#include<queue>
#include<string>
using namespace std;

Node::Node(char val)
Node::Node(char value)
{
this->data = val;
this->data = value;
distance = INT_MAX;
color = WHITE;
parent = NULL;
}

bool CompareNodes::operator()(Node* temp_u, Node* temp_v)
Node* BFSGraph::MakeOrFindNode(char value)
{
return (temp_u->data < temp_v->data);
Node* node = NULL;
if (this->_nodeMap.find(value) == this->_nodeMap.end())
{
node = new Node(value);
this->_nodeMap[value] = node;
}
else
{
node = this->_nodeMap[value];
}
return node;
}

void BFSGraph::BreadthFirstSearch(Node* node)
{
node->color = WHITE;
node->distance = 0;
node->parent = NULL;

queue<Node*> nodeQueue;
nodeQueue.push(node);

while (nodeQueue.empty()!=true)
{
Node* currentNode = nodeQueue.front();
nodeQueue.pop();

for (auto adjacentNode : this->_adjlist[currentNode])
{
if (adjacentNode->color == WHITE)
{
adjacentNode->color = GRAY;
adjacentNode->parent = currentNode;
adjacentNode->distance = currentNode->distance + 1;
nodeQueue.push(adjacentNode);
}
}
currentNode->color = BLACK;
}
}

void BFSGraph::PushUndirectedEdge(char valueU, char valueV)
{
Node* nodeU = this->MakeOrFindNode(valueU);
Node* nodeV = this->MakeOrFindNode(valueV);

this->_adjlist[nodeU].push_back(nodeV);
this->_adjlist[nodeV].push_back(nodeU);
}

void BFSGraph::BFS(char value)
{
this->BreadthFirstSearch(this->_nodeMap[value]);
}

string BFSGraph::ShowBFSResult()
{
string result = "";
for (auto value : this->_nodeMap)
{
result = result + " " + value.first + "(" + to_string(value.second->distance) + ")";
}
return result;
}
28 changes: 28 additions & 0 deletions Tests/0003_Graph/0001_BreadthFirstSearchTest.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#include <gtest/gtest.h>
#include<string>
#include "../Headers/0003_Graph/0001_BreadthFirstSearch.h"

// Demonstrate some basic assertions.
namespace BreadthFirstSearchTest
{
TEST(BFSTesting, ShowBFSResultTest) {
BFSGraph graph;

graph.PushUndirectedEdge('s', 'r');
graph.PushUndirectedEdge('s', 'w');
graph.PushUndirectedEdge('r', 'v');
graph.PushUndirectedEdge('w', 't');
graph.PushUndirectedEdge('w', 'x');
graph.PushUndirectedEdge('t', 'x');
graph.PushUndirectedEdge('t', 'u');
graph.PushUndirectedEdge('x', 'u');
graph.PushUndirectedEdge('x', 'y');
graph.PushUndirectedEdge('u', 'y');

graph.BFS('s');

string actualResult = graph.ShowBFSResult();
string expectedResult = " r(1) s(0) t(2) u(3) v(2) w(1) x(2) y(3)";
EXPECT_EQ(actualResult, expectedResult);
}
}