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

Content Beyond Syllabus

Uploaded by

sandybbc12106
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)
196 views

Content Beyond Syllabus

Uploaded by

sandybbc12106
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/ 8

Exp.

No: 8 (a)
IMPLEMENTATION OF DEPTH FRIST SEARCH (DFS)
Date :

AIM:

To write a C program for implementation of Depth First Search algorithm on the given graph.

PSEUDO CODE:

Algorithm DFS
BEGIN
visited[v]=1;
printf("%d ",v);
for(i=1;i<=n;i++)
if(adj[v][i]==1)
if(visited[i]==0)
DFS(i,n);
END
Algorithm Graph_Matrix
BEGIN
printf("Enter the adjacency matrix:\n");
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
scanf("%d",&adj[i][j]);
END

SOURCE CODE:
#include<stdio.h>
int visited[100]={0},adj[100][100]={0};
void DFS(int v,int n)
{
int i;
visited[v]=1;
printf("%d ",v);
for(i=1;i<=n;i++)
if(adj[v][i]==1)
if(visited[i]==0)
DFS(i,n);
}
void Graph_Matrix(int n)
{
int i,j;
printf("Enter the adjacency matrix:\n");
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
scanf("%d",&adj[i][j]);
}
void main()
{
int i,n;
printf("Enter the number of vertices: ");
scanf("%d",&n);
Graph_Matrix(n);
printMatrix(n);
printf("DFS Order: ");
for(i=1;i<=n;i++)
if(visited[i]==0)
DFS(i,n);
printf("\n");
}

OUTPUT:

RESULT:

Thus the C program to implement Depth First Search algorithm on the given graph is executed
successfully and the output was verified.
Exp. No: 8 (b)
IMPLEMENTATION OF BREADTH FRIST SEARCH (BFS)
Date :

AIM:
To write a C program for implemention of Breadth First Search algorithm on the given graph.

PSEUDO CODE:

Algorithm BFS
BEGIN
visited[v]=1;
printf("%d ",v);
enqueue(v);
while(front<=rear)
BEGIN
k=dequeue();
for(i=1;i<=n;i++)
if((adj[k][i]==1)&&(visited[i]==0))
BEGIN
visited[i]=1;
printf("%d ",i);
enqueue(i);
END
END
END

Algorithm Graph_Matrix
BEGIN
printf("Enter the adjacency matrix:\n");
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
scanf("%d",&adj[i][j]);
END

Algorithm enqueue
BEGIN
if((rear==-1)&&(front==-1))
front=rear=0;
else
rear=rear+1;
queue[rear]=ele;
END

Algorithm dequeue
BEGIN
if(rear==-1&&front==-1)
printf("Cannot Dequeue\n");
else
BEGIN
value=queue[front];
front++;
return value;
END
END

SOURCE CODE:
#include<stdio.h>
void enqueue(int );
int front=-1,rear=-1,queue[100];
int visited[100]={0},adj[100][100]={0};
void BFS(int v,int n)
{
int i,k;
visited[v]=1;
printf("%d ",v);
enqueue(v);
while(front<=rear)
{
k=dequeue();
for(i=1;i<=n;i++)
if((adj[k][i]==1)&&(visited[i]==0))
{
visited[i]=1;
printf("%d ",i);
enqueue(i);
}
}
}
void Graph_Matrix(int n) {
int i,j;
printf("Enter the adjacency matrix:\n");
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
scanf("%d",&adj[i][j]);
}
void enqueue(int ele)
{
if((rear==-1)&&(front==-1))
front=rear=0;
else
rear=rear+1;
queue[rear]=ele;
}
int dequeue()
{
int value;
if(rear==-1&&front==-1)
printf("Cannot Dequeue\n");
else
{
value=queue[front];
front++;
return value;
}
}
void main()
{
int i,n,vertex;
printf("Enter the number of vertices: ");
scanf("%d",&n);
Graph_Matrix(n);
printf("BFS Order: ");
for(i=1;i<=n;i++)
if(visited[i]==0)
BFS(i,n);
}

OUTPUT:

RESULT:

Thus the program to implement Breadth First Search algorithm on the given graph is executed successfully
and the output is verified.
Exp. No: 9 (a)
PRIM’S ALGORITHM
Date :

AIM:

To write a program in C for implementation of Prim’s Algorithm in a graph.


PSEUDOCODE:

//Algorithm: prims()
//Input: No. of vertices and Adjacency matrix
//Output: Cost of Minimum Spanning treeWhile
ne<n
for(i=1,min=999;i<=n;i++)
for(j=1;j<=n;j++) if(cost[i][j]<min)
if(visited[i]!=0)
min=cost[i][j]a=i
b=j if(visited[a]==0||visited[b]==0)
mincost+=minvisited[b]=1
cost[a][b]=cost[b][a]=999
SOURCE CODE:

#include<stdio.h>
int a,b,n,i,j,ne=1,min,mincost=0;int
cost[10][10];
int visited[10]={0};int
main(){

printf("\n\t***PRIMS ALGORITHM***\n");printf("Enter
the number of nodes: "); scanf("%d",&n);
printf("\nEnter the adjacent matrix\n");;
for(i=1;i<=n;i++){
for(j=1;j<=n;j++){
scanf("%d",&cost[i][j]);if(cost[i][j]==0)
cost[i][j]=999;
}
}
visited[1]=1;printf("\n");
while(ne<n)
{
for(i=1,min=999;i<=n;i++)
for(j=1;j<=n;j++) if(cost[i][j]<min)
if(visited[i]!=0)
{
min=cost[i][j];a=i;
b=j;
}
if(visited[a]==0||visited[b]==0)
{
printf("Edge %d :(%d %d) cost: %d\n",ne++,a,b,min);mincost+=min;
visited[b]=1;
}
cost[a][b]=cost[b][a]=999;
}
printf("\nCost of Minimum Spanning Tree is: %d",mincost);
return 0;
}
OUTPUT:
***PRIMS ALGORITHM***
Enter the number of nodes: 4

Enter the adjacent matrix


0 8 1 6
8 0 5 0
1 5 0 5
6 0 5 0

Edge 1 :(1 3) cost: 1

Edge 2 :(3 2) cost: 5

Edge 3 :(3 4) cost: 5

Cost of Minimum Spanning Tree is: 11

RESULT:

Thus, the C program for implementation of Prim’s Algorithm in a graph is executed successfully
and output was Verified.

You might also like