Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
96 views8 pages

DFS AND BFS 11 - 12

Danish Tameez completed Lab 11 which involved implementing depth first search (DFS) and breadth first search (BFS) algorithms on adjacency matrix and adjacency list graph representations. For the adjacency matrix, DFS and BFS functions were added to the Graph class. For the adjacency list, separate implementations of BFS and DFS were done by adding respective functions to the Graph class and calling them from main. The outputs of running BFS and DFS on sample graphs are also shown.

Uploaded by

D A N I S H
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
96 views8 pages

DFS AND BFS 11 - 12

Danish Tameez completed Lab 11 which involved implementing depth first search (DFS) and breadth first search (BFS) algorithms on adjacency matrix and adjacency list graph representations. For the adjacency matrix, DFS and BFS functions were added to the Graph class. For the adjacency list, separate implementations of BFS and DFS were done by adding respective functions to the Graph class and calling them from main. The outputs of running BFS and DFS on sample graphs are also shown.

Uploaded by

D A N I S H
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

Data Structures and Algorithms

Lab Journal - Lab 11

Name: Danish Tameez

Enrollment #: 01-235181-009

Class/Section: BSIT-3A

Question 1:
Code:
DFS AND BSF FOR matrix

AdjMat.h:
#include <iostream>
#include <conio.h>
#include<string>
#include<iomanip>
using namespace std;
class Graph
{
private:
bool** adjacencyMatrix;
int vertexCount;
public:
Graph(int vertexCount);
void addEdge(int i, int j) ;
void removeEdge(int i, int j);
bool isEdge(int i, int j) ;
void display();
void BFS(int s);
bool isConnected(int u, int v);
void DFS(int x, int required);
};

AdjMat.cpp:
#include "matrix.h"
#include <queue>
#include <stack>
Graph::Graph(int VertexCount)
{
vertexCount=VertexCount;
adjacencyMatrix = new bool *[VertexCount];
for (int i = 0; i < VertexCount; i++)
{
adjacencyMatrix[i] = new bool[VertexCount];
}
for (int i = 0; i < VertexCount; i++)
{
for (int j = 0; j <VertexCount; j++)
{
adjacencyMatrix[i][j] = 0;
}
}
}
void Graph::addEdge(int x, int y)
{
adjacencyMatrix[x-1][y-1] = adjacencyMatrix[y-1][x-1] = 1;
}
void Graph::removeEdge(int i, int j)
{
adjacencyMatrix[i][j] = false;
adjacencyMatrix[j][i] = false;
}
bool Graph::isEdge(int i, int j)
{
if (adjacencyMatrix[i][j] == 1)
{
return true;
}
else{
return false;}
}
bool Graph::isConnected(int u, int v)
{
return (adjacencyMatrix[u-1][v-1] == 1);
}
void Graph::display()
{
for (int i = 0; i < vertexCount; i++)
{
cout<<i<<" : ";
for (int j = 0; j < vertexCount; j++)
{
cout << adjacencyMatrix[i][j] << " ";
}
cout << endl;
}
}
void Graph::DFS(int x, int required)
{
stack<int> s;
bool *visited = new bool[vertexCount+1];
int i;
for(i = 0; i <= vertexCount; i++)
visited[i] = false;
s.push(x);
visited[x] = true;
if(x == required) return;
cout << "Depth first Search starting from vertex ";
cout << x << " : " << endl;
while(!s.empty()){
int k = s.top();
s.pop();
if(k == required) break;
cout<<k<<" ";
for (i = vertexCount; i >= 0 ; --i)
if (isConnected(k, i) && !visited[i]) {
s.push(i);
visited[i] = true;
}
}
cout<<endl;
delete [] visited;
}
void Graph::BFS(int s)
{
queue<int> Q;
bool *explored = new bool[vertexCount+1];
for (int i = 1; i <= vertexCount; ++i)
explored[i] = false;
Q.push(s);
explored[s] = true;
cout << "Breadth first Search starting from vertex ";
cout << s << " : " << endl;
while (!Q.empty()) {
int v =Q.front();
Q.pop();
cout << v << " ";
for (int w = 1; w <= vertexCount; ++w)
if (isConnected(v, w) && !explored[w]) {
Q.push(w);
explored[w] = true;
}
}
cout << endl;
delete [] explored;
}

Main.cpp:
#include <iostream>
#include <conio.h>
#include "graph.h"
#include "matrix.h"
using namespace std;

void main()
{
Graph g(8);
g.addEdge(1, 2);
g.addEdge(1, 3);
g.addEdge(2, 4);
g.addEdge(3, 4);
g.addEdge(3, 6);
g.addEdge(4 ,7);
g.addEdge(5, 6);
g.addEdge(5, 7);
g.DFS(1, 8);
g.BFS(1);
system("pause");
}
OUTPUT:

Question 2:
BFS FOR adjlist

Code:
Graph.h:
#include<iostream>
#include <conio.h>
#include <list>
#include <queue>
#include <stack>

using namespace std;

class Graph
{
int V;
list<int> *adj;
public:
Graph(int V);
void addEdge(int v, int w);
void BFS(int s);
};

Graph.cpp:
#include "adjlistbfs.h"
#include <queue>
#include <stack>

Graph::Graph(int V)
{
this->V = V;
adj = new list<int>[V];
}

void Graph::addEdge(int v, int w)


{
adj[v].push_back(w);
}

void Graph::BFS(int s)
{

bool *visited = new bool[V];


for (int i = 0; i < V; i++)
visited[i] = false;

list<int> queue;

visited[s] = true;
queue.push_back(s);

list<int>::iterator i;

while (!queue.empty())
{

s = queue.front();
cout << s << " ";
queue.pop_front();

for (i = adj[s].begin(); i != adj[s].end(); ++i)


{
if (!visited[*i])
{
visited[*i] = true;
queue.push_back(*i);
}
}
}
}

Main.cpp:
#include "adjlistbfs.h"

int main()
{

Graph g(4);
g.addEdge(0, 2);
g.addEdge(0, 0);
g.addEdge(1, 0);
g.addEdge(2, 0);
g.addEdge(2, 2);
g.addEdge(3, 3);

cout << "BFS START FROM 0 \n";


g.BFS(0);
_getch();

return 0;
}

Output:

DFS FOR adjlist

Code:
Graph.h:
#include<iostream>
#include<conio.h>
#include<list>
#include <queue>
#include <stack>

using namespace std;

class Graph
{
int V;

list<int> *adj;

void DFSUtil(int v, bool visited[]);


public:
Graph(int V);

void addEdge(int v, int w);

void DFS(int v);


};
Graph.cpp:
#include "adjlistbfs.h"
#include <queue>
#include <stack>

Graph::Graph(int V)
{
this->V = V;
adj = new list<int>[V];
}

void Graph::addEdge(int v, int w)


{
adj[v].push_back(w);
}

void Graph::DFSUtil(int v, bool visited[])


{

visited[v] = true;
cout << v << " ";

list<int>::iterator i;
for (i = adj[v].begin(); i != adj[v].end(); ++i)
if (!visited[*i])
DFSUtil(*i, visited);
}

void Graph::DFS(int v)
{

bool *visited = new bool[V];


for (int i = 0; i < V; i++)
visited[i] = false;

DFSUtil(v, visited);
}

Main.cpp:
#include "adjlistbfs.h"

int main()
{

Graph g(4);
g.addEdge(0, 1);
g.addEdge(0, 2);
g.addEdge(1, 0);
g.addEdge(2, 2);
g.addEdge(2, 3);
g.addEdge(3, 1);

cout << "BFS START FROM 0 \n";


g.DFS(0);
_getch();
return 0;
}

Output:

You might also like