0% found this document useful (0 votes)
26 views

Data Structures

The document describes an experiment to illustrate graph traversal using breadth-first search and depth-first search, providing pseudocode that takes user input to create an adjacency matrix representing a graph and then uses functions to perform BFS and DFS on the graph, printing the results. It includes the full C++ code implementing the BFS and DFS algorithms on the graph.

Uploaded by

Harshit
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views

Data Structures

The document describes an experiment to illustrate graph traversal using breadth-first search and depth-first search, providing pseudocode that takes user input to create an adjacency matrix representing a graph and then uses functions to perform BFS and DFS on the graph, printing the results. It includes the full C++ code implementing the BFS and DFS algorithms on the graph.

Uploaded by

Harshit
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

DEPARTMENT OF

COMPUTER SCIENCE & ENGINEERING

Experiment -3.3

Student Name: kartik Soni UID: 21BCS3022


Branch: CSE Section/Group: 711-A
Semester: 3rd Subject Code:21CSH-211
Subject Name: Data Structure

Aim of the practical:


Write a program to illustrate the traversal of graph using
a) Breadth-first search
b) Depth-first search

Algorithm:

i) Start
ii) Take number of vertices and edges as input from user.
iii) Make an adjacency matrix of edges.
iv) Now call a dfs and bfs function which will print the graph in depth-first
search and breadth first search. v) End

Program code:

#include<iostream>
#include<queue>
using namespace std;
void printBFS(int** edges, int n, int sv, bool* visited)
{ queue<int> pendingVertices;
pendingVertices.push(sv); visited[sv]
= true;
while(!pendingVertices.empty())
{
int currentVertex = pendingVertices.front();
pendingVertices.pop(); cout <<
currentVertex << endl; for(int i=0; i < n;
i++){
if(i == currentVertex)
{ continue;
}
if(edges[currentVertex][i] == 1 && !visited[i])
{
pendingVertices.push(i);
visited[i] = true;
}
}
}
}
void BFS(int** edges, int n)
{ bool* visited = new bool[n];
for(int i=0; i<n; i++)
{ visited[i] = false;
} for(int i=0; i < n;
i++)
{ if(!visited[i])
{ printBFS(edges, n, i, visited);
}
}

delete [] visited;

// DFS

void printDFS(int** edges, int n, int sv, bool* visited)


{ cout << sv << endl;
visited[sv] = true; for
(int i = 0; i < n; i++)
{ if(i == sv)
{ continue;
}
if(edges[sv][i] == 1)
{ if(visited[i])
{
continue; } printDFS(edges,
n, i, visited);
}
}
}
void DFS(int** edges, int n)
{ bool* visited = new bool[n];
for(int i=0; i<n; i++)
{ visited[i] = false;
} for(int i=0; i < n;
i++)
{ if(!visited[i]){
printDFS(edges, n, i, visited);
}
}
delete [] visited;
} int
main()
{ int n;
int e;
cout<<"Enter the number of vertices and edges: "<<endl;
cin >> n >> e; int** edges = new int*[n]; for (int i = 0; i
< n; i++)
{ edges[i] = new int[n];
for(int j=0; j<n; j++)
{ edges[i][j] =
0;
}}
cout<<"Enter the edges: "<<endl;
for(int i = 0; i < e; i++)
{ int f, s; cin >> f
>> s;
edges[f][s] = 1;
edges[s][f] = 1;
} cout << "DFS" <<
endl; DFS(edges, n);
cout << "BFS" << endl;
BFS(edges, n); for(int
i=0; i < n; i++)
{ delete [] edges[i];
} delete []
edges;
return 0;
}

Output:

You might also like