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

152 DSS 8 To 9

Uploaded by

rparthipan2686
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)
19 views8 pages

152 DSS 8 To 9

Uploaded by

rparthipan2686
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

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING 23CSR201 – DATA STRUCTURES

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]);

ROLL NO 717823P152
DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING 23CSR201 – DATA STRUCTURES

}
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.

ROLL NO:717823P152
DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING 23CSR201 – DATA STRUCTURES

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

ROLL NO: 717823P152


DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING 23CSR201 – DATA STRUCTURES

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

ROLL NO : 717823P152
DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING 23CSR201 – DATA STRUCTURES

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.

ROLL NO: 717823P152


DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING 23CSR201 – DATA STRUCTURES

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;
}
}

ROLL NO: 717823P152


DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING 23CSR201 – DATA STRUCTURES

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;
}

ROLL NO: 717823P152


DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING 23CSR201 – DATA STRUCTURES

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.

ROLL NO: 717823P152

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy