Depth-First-Search and Breadth-First-Search Traversal of Graph | DFS and BFS of Graph | Representation of Graph using Adjacency List using Linked List
Problem Statement:- Represent a given graph using an adjacency list to perform DFS and BFS. Use the map of the area around the college as the graph. Identify the prominent landmarks as nodes and perform DFS and BFS on that.
Note:- Scroll horizontally to see the full line of code.
#include <iostream>
#include <string.h>
using namespace std;
class flagarray
{
string data;
int flag;
public:
flagarray()
{
this->data = "";
this->flag = 0;
}
flagarray(string data, int flag)
{
this->data = data;
this->flag = flag;
}
friend class stack;
friend class queue;
friend class node;
friend class graph;
};
class stack;
class queue;
class stack
{
int top = -1;
string arr[30];
public:
bool isFull();
bool isEmpty();
void push(string data);
string Top();
string pop();
};
void stack::push(string data)
{
if (!isFull())
{
top++;
arr[top] = data;
}
else
{
cout << "Stack is full!!! in push" << endl;
}
}
string stack::Top()
{
if (!isEmpty())
{
return arr[top];
}
else
{
cout << "Stack is Empty!!!" << endl;
return "Empty";
}
}
bool stack::isFull()
{
return (top >= 29);
}
bool stack::isEmpty()
{
return (top == -1);
}
string stack::pop()
{
if (!isEmpty())
{
return arr[top--];
}
else
{
cout << "Stack is Empty!!!" << endl;
return "Empty";
}
}
class queue
{
int front = -1, rear = -1;
string arr[30];
public:
bool isFull();
bool isEmpty();
void push(string data);
string Front();
string pop();
string Rear();
};
bool queue::isFull()
{
return (rear > 29);
}
bool queue::isEmpty()
{
return (front > rear);
}
void queue::push(string data)
{
if (!isFull())
{
if (front == -1)
{
front++;
}
arr[++rear] = data;
}
else
{
cout << "Queue is Full!!!" << endl;
}
}
string queue::Front()
{
if (!isEmpty())
{
return arr[front];
}
else
{
cout << "Queue is Empty" << endl;
return "Empty";
}
}
string queue::Rear()
{
if (!isEmpty())
{
return arr[rear];
}
else
{
cout << "Queue is Empty" << endl;
return "Empty";
}
}
string queue::pop()
{
if (!isEmpty())
{
return arr[front++];
}
else
{
cout << "Queue is Empty" << endl;
return "Empty";
}
}
class node
{
string data;
node *next;
public:
node(string data)
{
this->data = data;
next = NULL;
}
friend class graph;
friend class stack;
friend class queue;
};
class graph
{
flagarray *BFS;
flagarray *DFS;
node **array;
int vertexCount;
public:
graph(int vertexCount)
{
this->vertexCount = vertexCount;
array = new node *[vertexCount];
BFS = new flagarray[vertexCount];
DFS = new flagarray[vertexCount];
for (int i = 0; i < vertexCount; i++)
{
array[i] = NULL;
}
}
void createGraph();
void display();
int indexInBFS(string data);
int indexInDFS(string data);
int indexInArray(string data);
void bfs();
void dfs();
friend class stack;
friend class queue;
};
int graph::indexInBFS(string data)
{
for (int i = 0; i < vertexCount; i++)
{
if (BFS[i].data == data)
{
return i;
}
}
return -1;
}
int graph::indexInArray(string data)
{
for (int i = 0; i < vertexCount; i++)
{
if (array[i]->data == data)
{
return i;
}
}
return -1;
}
int graph::indexInDFS(string data)
{
for (int i = 0; i < vertexCount; i++)
{
if (DFS[i].data == data)
{
return i;
}
}
return -1;
}
void graph::createGraph()
{
cout << "Enter the no. of Edges";
int n;
cin >> n;
for (int i = 0; i < n; i++)
{
cout << "Edge " << i + 1 << " : " << endl;
string node1, node2;
cout << "Enter node 1 of edge: ";
cin >> node1;
cout << "Enter node 2 of edge: ";
cin >> node2;
if (!array[0])
{
array[0] = new node(node1);
array[1] = new node(node2);
BFS[0].data = node1;
DFS[0].data = node1;
BFS[1].data = node2;
DFS[1].data = node2;
array[0]->next = new node(node2);
array[1]->next = new node(node1);
}
else
{
int index1 = -1, index2 = -1;
int index = 0;
while (array[index] && index < vertexCount)
{
if (array[index]->data == node1)
{
index1 = index;
}
if (array[index]->data == node2)
{
index2 = index;
}
index++;
}
if (index1 == -1)
{
int j = 0;
while (array[j])
{
j++;
}
array[j] = new node(node1);
BFS[i].data = node1;
DFS[i].data = node1;
array[j]->next = new node(node2);
}
else
{
node *current = array[index1];
while (current->next)
{
current = current->next;
}
current->next = new node(node2);
}
if (index2 == -1)
{
int j = 0;
while (array[j])
{
j++;
}
array[j] = new node(node2);
BFS[j].data = node2;
DFS[j].data = node2;
array[j]->next = new node(node1);
}
else
{
node *current = array[index2];
while (current->next)
{
current = current->next;
}
current->next = new node(node1);
}
}
}
}
void graph::display()
{
if (array[0])
{
for (int i = 0; i < vertexCount; i++)
{
if (array[i])
{
node *current = array[i];
cout << current->data;
if (current->next)
{
current = current->next;
}
while (current)
{
cout << " --> " << current->data;
current = current->next;
}
cout << endl;
}
}
}
else
{
cout << "Graph is Empty!!!" << endl;
}
}
void graph::dfs()
{
stack dfsStack;
node *current = array[0];
dfsStack.push(current->data);
DFS[indexInDFS(current->data)].flag = 1;
while (!dfsStack.isEmpty())
{
string temp = dfsStack.pop();
cout << temp << " ";
current = array[indexInArray(temp)];
while (current)
{
if (!DFS[indexInDFS(current->data)].flag)
{
dfsStack.push(current->data);
DFS[indexInDFS(current->data)].flag = 1;
current = current->next;
}
else
{
current = current->next;
}
}
}
}
void graph::bfs()
{
queue bfsQueue;
node *current = array[0];
bfsQueue.push(current->data);
BFS[indexInBFS(current->data)].flag = 1;
while (!bfsQueue.isEmpty())
{
string temp = bfsQueue.pop();
cout << temp << " ";
current = array[indexInArray(temp)];
while (current)
{
if (!BFS[indexInBFS(current->data)].flag)
{
bfsQueue.push(current->data);
BFS[indexInBFS(current->data)].flag = 1;
current = current->next;
}
else
{
current = current->next;
}
}
}
}
int main()
{
int n;
cout << "Enter the no. of Vertices: ";
cin >> n;
graph k(n);
k.createGraph();
char cont = 'y';
while (cont == 'y')
{
cout << "<--------Menu-------->" << endl;
cout << "1. Show Adjacency List" << endl;
cout << "2. BFS Traversal" << endl;
cout << "3. DFS Traversal" << endl;
int choice;
cout << "Enter the choice: ";
cin >> choice;
switch (choice)
{
case 1:
cout << "Adjacency List for Graph : " << endl;
k.display();
break;
case 2:
cout << "BFS Traveral of Graph: ";
k.bfs();
cout << endl;
break;
case 3:
cout << "DFS Traversal of Graph: ";
k.dfs();
cout << endl;
break;
default:
cout << "Invalid Choice!!!" << endl;
break;
}
cout << "Do you want to continue ? (y/n) : ";
cin >> cont;
}
cout << "Program Ended Successfully!!!" << endl;
}
Comments
Post a Comment