Professional Documents
Culture Documents
ALGO LAB Exercise
ALGO LAB Exercise
Section-ECSc-B
I. SOURCE CODE:
#include <iostream>
#include <bits/stdc++.h>
vector<bool> v;
vector< vector <int> > g;
void bfsTraversal(int b)
{
queue<int> q;
q.push(b);
v[b] = true;
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 << " ";
}
}
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 ";
2
Algorithm Lab Report, Autumn 2021
II. SCREENSHOTS
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):
DFS(graph, n, 0, visited);
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;
}
#include <iostream>
using namespace std;
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
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>
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()
{
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;
11
Algorithm Lab Report, Autumn 2021
SCREENSHOTS
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++)
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
14
Algorithm Lab Report, Autumn 2021
Test Case Output-2
15
Algorithm Lab Report, Autumn 2021
DECLARATION
I hereby declare that,
16
Algorithm Lab Report, Autumn 2021