Download as pdf or txt
Download as pdf or txt
You are on page 1of 16

ALGORITHM LAB REPORT

Section-ECSc-B

EXERCISE NO: 11 DATE OF EXERCISE: 02.11.2021


ROLL NUMBER: 1930187 SUB-GROUP NO.: 3
NAME IN CAPITAL : SHUBHAM KHUNTIA

LAB EXERCISE (LE):

11.1) Write a Program to implement Breadth First Search(BFS).

I. SOURCE CODE:

#include <iostream>
#include <bits/stdc++.h>

using namespace std;

vector<bool> v;
vector< vector <int> > g;

void bfsTraversal(int b)
{
queue<int> q;
q.push(b);
v[b] = true;

cout << "\n\nThe BFS Traversal is: ";

while (!q.empty())
{
int a = q.front();
q.pop();
for (auto j = g[a].begin(); j != g[a].end(); j++)
{
if (!v[*j])
{
v[*j] = true;
q.push(*j);
1
Algorithm Lab Report, Autumn 2021
}
}
cout << a << " ";
}
}

void makeEdge(int a, int b)


{
g[a].push_back(b); }

int main()
{
int n, e;
cout << "Enter the number of vertices: ";
cin >> n;
cout << "\n\nEnter the number of edges: ";
cin >> e;

v.assign(n, false);
g.assign(n, vector<int>());

int a, b, i;
cout << "Enter the edges with source and target vetex: \n ";

for (i = 0; i < e; i++)


{
cin >> a >> b;
makeEdge(a, b);
}

for (i = 0; i < n; i++)


{
if (!v[i])
{
bfsTraversal(i);
}
}
cout << "\n\n\n";
return 0;
}

2
Algorithm Lab Report, Autumn 2021
II. SCREENSHOTS

Test Case Output-1

3
Algorithm Lab Report, Autumn 2021
LAB EXERCISE (LE):
11.2) Write a Program to implement Depth First Search(DFS).

SOURCE CODE:

#include<iostream>
#include<conio.h>
#include<stdlib.h>
using namespace std;
int cost[10][10],i,j,k,n,stk[10],top,v,visit[10],visited[10];
int main()
{
int m;
cout <<"Enter no of vertices:";
cin >> n;
cout <<"Enter no of edges:";
cin >> m;
cout <<"\nEDGES \n";
for(k=1; k<=m; k++)
{
cin >>i>>j;
cost[i][j]=1;
}
cout <<"Enter initial vertex to traverse from:";
cin >>v;
cout <<"DFS ORDER OF VISITED VERTICES:";
cout << v <<" ";
visited[v]=1;
k=1;
while(k<n)
{
for(j=n; j>=1; j--)
if(cost[v][j]!=0 && visited[j]!=1 && visit[j]!=1)
{
visit[j]=1;
stk[top]=j;
top++;
}
v=stk[--top];
cout<<v << " ";
k++;
visit[v]=0;
visited[v]=1;
}
return 0;
}

4
Algorithm Lab Report, Autumn 2021
SCREENSHOTS
Test Case Output-1

5
Algorithm Lab Report, Autumn 2021
LAB EXERCISE (LE):

11.3) Write a Program to check whether a given graph is connected or not


using BFS method.

III. SOURCE CODE:


#include <iostream>
using namespace std;

void DFS(int **graph, int n, int v, bool *visited)


{
visited[v] = true;
for (int i = 0; i < n; i++)
{
if (graph[v][i] == 1 && !visited[i])
DFS(graph, n, i, visited);
}
}

void check_graph_connection_using_DFS_method(int **graph, int n)


{
bool *visited = new bool[n];
for (int i = 0; i < n; i++)
visited[i] = false;

DFS(graph, n, 0, visited);

for (int i = 0; i < n; i++)


{
if (!visited[i])
{
cout << "Graph is not connected" << endl;
return;
}
}
cout << "Graph is connected" << endl;
}

int main()
{
int n, m;
cin >> n >> m;
int **graph = new int *[n];
for (int i = 0; i < n; i++)
{
graph[i] = new int[n];
6
Algorithm Lab Report, Autumn 2021
for (int j = 0; j < n; j++)
graph[i][j] = 0;
}
for (int i = 0; i < m; i++)
{
int u, v;
cin >> u >> v;
graph[u][v] = 1;
graph[v][u] = 1;
}
check_graph_connection_using_DFS_method(graph, n);
return 0;
}

LAB EXERCISE (LE):

11.4) Write a Program to check whether a given graph is connected or not


using DFS method.

IV. SOURCE CODE:

#include <iostream>
using namespace std;

void BFS(int **graph, int n, int s, int *visited)


{
int i, j, k;
int queue[n];
int front = -1;
int rear = -1;
int u;
visited[s] = 1;
queue[++rear] = s;
while (front != rear)
{
u = queue[++front];
for (i = 0; i < n; i++)
{
if (graph[u][i] == 1 && visited[i] == 0)
{
visited[i] = 1;
queue[++rear] = i;
}
}
}
7
Algorithm Lab Report, Autumn 2021
}

void check_graph_connection_using_BFS(int **graph, int n)


{
int i, j, k;
int visited[n];
for (i = 0; i < n; i++)
visited[i] = 0;
BFS(graph, n, 0, visited);
for (i = 0; i < n; i++)
if (visited[i] == 0)
{
cout << "Graph is not connected" << endl;
return;
}
cout << "Graph is connected" << endl;
}

int main()
{
int n, i, j, k;
cout << "Enter the number of vertices: ";
cin >> n;
int **graph = new int *[n];
for (i = 0; i < n; i++)
graph[i] = new int[n];
for (i = 0; i < n; i++)
for (j = 0; j < n; j++)
graph[i][j] = 0;
cout << "Enter the adjacency matrix: " << endl;
for (i = 0; i < n; i++)
{
for (j = 0; j < n; j++)
{
cin >> graph[i][j];
}
}
check_graph_connection_using_BFS(graph, n);
for (i = 0; i < n; i++)
delete[] graph[i];
delete[] graph;
}

8
Algorithm Lab Report, Autumn 2021
V. SCREENSHOTS

Test Case Output-1

Test Case Output-2

9
Algorithm Lab Report, Autumn 2021
HOME EXERCISE(HE):

11.5) Write a program to print all the nodes reachable from a given starting
node in a given digraph using DFS/BFS method.

SOURCE CODE:
#include<stdio.h>
#include<stdlib.h>

int a[50][50], n, visited[50];


int q[20], front = -1,rear = -1;
int s[20], top = -1, count=0;

void bfs(int v)
{
int i, cur;
visited[v] = 1;
q[++rear] = v;
while(front!=rear)
{
cur = q[++front];
for(i=1;i<=n;i++)
{
if((a[cur][i]==1)&&(visited[i]==0))
{
q[++rear] = i;
visited[i] = 1;
printf("%d ", i);
}
}
}
}

void dfs(int v)
{
int i;
visited[v]=1;
s[++top] = v;
for(i=1;i<=n;i++)
{
if(a[v][i] == 1&& visited[i] == 0 )
{
printf("%d ", i);
dfs(i);
}
}
}

10
Algorithm Lab Report, Autumn 2021
int main()
{

int ch, start, i,j;


printf("\nEnter the number of vertices in graph: ");
scanf("%d",&n);
printf("\nEnter the adjacency matrix:\n");
for(i=1; i<=n; i++)
{
for(j=1;j<=n;j++)
scanf("%d",&a[i][j]);
}

for(i=1;i<=n;i++)
visited[i]=0;
printf("\nEnter the starting vertex: ");
scanf("%d",&start);

printf("\n==>1. BFS: Print all nodes reachable from a given starting node");
printf("\n==>2. DFS: Print all nodes reachable from a given starting node");
printf("\n==>3:Exit");
printf("\nEnter your choice: ");
scanf("%d", &ch);
switch(ch)
{
case 1: printf("\nNodes reachable from starting vertex %d are: ", start);
bfs(start);
for(i=1;i<=n;i++)
{
if(visited[i]==0)
printf("\nThe vertex that is not reachable is %d" ,i);
}
break;

case 2: printf("\nNodes reachable from starting vertex %d are:\n",start);


dfs(start);
break;
case 3: exit(0);
default: printf("\nPlease enter valid choice:");
}
}

11
Algorithm Lab Report, Autumn 2021
SCREENSHOTS

Test Case Output-1

Test Case Output-2

12
Algorithm Lab Report, Autumn 2021
11.6) Write a program to print all the nodes reachable from a given starting
node in a digraph using BFS method.
SOURCE CODE:
#include<stdio.h>
#define size 20
#define true 1
#define false 0
int queue[size],visit[20],rear=-1,front=0;
int n,s,adj[20][20],flag=0;
void insertq(int v)
{
queue[++rear]=v;
}

int deleteq()
{
return(queue[front++]);
}

int qempty()
{

if(rear<front)
return 1;

else
return 0;
}

void bfs(int v)
{
int w;
visit[v]=1;
insertq(v);

while(!qempty())
{
v=deleteq();
for(w=1;w<=n;w++)

if((adj[v][w]==1) && (visit[w]==0))


{
visit[w]=1;
flag=1;
printf("v%d\t",w);
insertq(w);
}
13
Algorithm Lab Report, Autumn 2021
}
}

void main()
{
int v,w;
printf("Enter the no.of vertices:\n");
scanf("%d",&n);
printf("Enter adjacency matrix:");
for(v=1;v<=n;v++)
{
for(w=1;w<=n;w++)
scanf("%d",&adj[v][w]);
}
printf("Enter the start vertex:");
scanf("%d",&s);
printf("Reachability of vertex %d\n",s);
for(v=1;v<=n;v++)
visit[v]=0;

bfs(s);

if(flag==0)
{
printf("No path found!!\n");
}
}

VI. SCREENSHOTS

Test Case Output-1

14
Algorithm Lab Report, Autumn 2021
Test Case Output-2

15
Algorithm Lab Report, Autumn 2021
DECLARATION
I hereby declare that,

 I have written the assignment in my own handwritting as mentioned in Handwritten


Code Section.
 I have typed my source code in code editor and taken my own test case output after
running of code .

Full Signature of the Student

16
Algorithm Lab Report, Autumn 2021

You might also like