Pune Institute of Computer Technology PUNE - 411043 Department of Electronics & Telecommunication
Pune Institute of Computer Technology PUNE - 411043 Department of Electronics & Telecommunication
PUNE - 411043
Department of Electronics & Telecommunication
ASSESMENT YEAR: 2019-2020 CLASS : SE
SUBJECT: DATA STRUCTURES and ALGORITHMS
EXPT No: LAB Ref: SE/2019/ Starting date:
Roll No:22324 Submission date:
Title: Creation of Graph
Theory:
/*BFS and DFS on a graph represented using adjacency matrix 22324*/
#include<conio.h>
#include<stdio.h>
#define MAX 10
typedef struct Q
int R,F;
int data[MAX];
}Q;
void BFS(int);
P:f-LTL-UG/03/R1 Page 1 of 12
void DFS(int);
int G[MAX][MAX];
int n=0;
int visited[MAX];
void main()
int i,j,v,op,nedges;
scanf("%d",&n);
scanf("%d",&nedges);
for(i=0;i<n;i++)
for(j=0;j<n;j++)
G[i][j]=0;
for(v=0;v<nedges;v++)
scanf("%d%d",&i,&j);
do{
P:f-LTL-UG/03/R1 Page 2 of 12
scanf("%d",&op);
switch(op)
scanf("%d",&v);
for(i=0;i<n;i++)
visited[i]=0;
DFS(v);break;
scanf("%d",&v);
BFS(v);break;
for(i=0;i<n;i++)
printf("\n");
for(j=0;j<n;j++)
printf("%d ",G[i][j]);
break;
}while(op!=4);
void BFS(int v)
int visited[MAX],i;
Q q;
P:f-LTL-UG/03/R1 Page 3 of 12
q.R=q.F=-1;
for(i=0;i<n;i++)
visited[i]=0;
enqueue(&q,v);
printf("\n visit:\n%d",v);
visited[v]=1;
while(!empty(&q))
v=dequeue(&q);
for(i=0;i<n;i++)
enqueue(&q,i);
visited[i]=1;
printf("\n%d",i);
if(P->R==-1)
return(1);
return(0);
P:f-LTL-UG/03/R1 Page 4 of 12
if(P->R==MAX-1)
return(1);
return(0);
if(P->R==-1)
P->R=P->F=0;
P->data[P->R]=x;
else
P->R=P->R+1;
P->data[P->R]=x;
int x;
x=P->data[P->F];
if(P->R==P->F)
P->R=-1;
P->F=-1;
else
P:f-LTL-UG/03/R1 Page 5 of 12
P->F=P->F+1;
return(x);
void DFS(int i)
int j;
printf("\n%d",i);
visited[i]=1;
for(j=0;j<n;j++)
DFS(j);
P:f-LTL-UG/03/R1 Page 6 of 12
FLOW CHART (Extra sheet may be attached)
P:f-LTL-UG/03/R1 Page 7 of 12
ALGORITHM (Extra sheet may be attached)
P:f-LTL-UG/03/R1 Page 8 of 12
ERROR (if any)
CONCLUSION :
REFERENCES :
Date:
P:f-LTL-UG/03/R1 Page 9 of 12
PUNE INSTITUTE OF COMPUTER TECHNOLOGY
PUNE - 411043
Department of Electronics & Telecommunication
ASSESMENTYEAR: 2019-2020 CLASS : SE
SUBJECT: DATA STRUCTURES and ALGORITHMS
EXPTNo: LAB Ref:SE/2019/ Starting date:
Roll No:22324 Submission date:
Title:
Output:
1)DFS
2)BFS
3)Display the Graph
4)QUIT
Enter Your choice : 1
2
1
3
4
1)DFS
2)BFS
P:f-LTL-UG/03/R1 Page 10 of 12
3)Display the Graph
4)QUIT
Enter Your choice : 2
visit:
2
1
3
4
1)DFS
2)BFS
3)Display the Graph
4)QUIT
Enter Your choice : 3
Adjacency Matrix :
0 0 0 0 0
0 0 1 1 1
0 1 0 1 0
0 1 1 0 1
0 1 0 1 0
1)DFS
2)BFS
3)Display the Graph
4)QUIT
Enter Your choice :
P:f-LTL-UG/03/R1 Page 11 of 12
P:f-LTL-UG/03/R1 Page 12 of 12