UEC2023156 Assig 10
UEC2023156 Assig 10
Assignment-11 Graphs
U.no - UEC2023156
Batch- A3
Code:-
#include<stdio.h>
#define MAX 10
typedef struct Q
{ int R,F; int
data[MAX];
}Q; int empty (Q *P); int
full(Q *P); void
enqueue(Q *P,int x); int
dequeue(Q *P); void
BFS(int); void DFS(int);
int G[MAX][MAX]; int n=0;
int visited[MAX];
void main()
{ int i,j,v,op,nedges;
for(i=0;i<n;i++)
for(j=0;j<n;j++)
G[i][j] =0; printf("\nEnter the Graph as
list of edges(
Starting vertex No is 0");
for(v=0;v<nedges;v++)
{
printf("\nEnter the next edge(Start vertex,
end vertex);"); scanf("%d%d",
&i,&j);
G[i][j] = G[j][i]= 1;
}
do{
printf("\n\n1. DFS\n2. BFS\n3. Display the
graph\n 4. Exit"); printf("\nEnter your choice");
scanf("%d",&op);
switch(op)
{ case 1: printf("\nEnter the starting
vertex
for DFS"); scanf("%d",&v);
for(i=0;i<n;i++)
visited[i] = 0;
DFS(v);
break;
case 2: printf("Enter the starting vertex
for BFS"); scanf("%d",&v);
BFS(v);
break;
case 3: printf("Adjacency Matrix : \n");
for(i=0;i<n;i++)
{ printf("\n");
for(j=0;j<n;j++)
printf("%d\t",G[i][j]);
}
break;
}
}while(op!= 4);
}
void BFS(int v)
{ int
visited[MAX],i;
Q q;
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++)
if (visited[i] ==0 && G[v][i]!= 0)
{ enqueue(&q,i);
visited[i] = 1;
printf("\n%d",i);
}
}
}
int full(Q*P)
{ if(P-> R == MAX-
1) return(1);
return(0);
}
void DFS(int i)
{ int j;
printf("\n%d", i); visited[i] =1;
for(j=0;j<n;j++) if(!visited[j] &&
G[i][j] ==1)
DFS(j);
}
Output:-
Enter no of vertices : 4
1. DFS
2. BFS
3. Display the graph
4. Exit Enter your choice3 Adjacency Matrix :
0 1 1 0
1 0 1 1
1 1 0 1
0 1 1 0
1. DFS
2. BFS
3. Display the graph
4. Exit
Enter your choice1
0
1
2
3
1. DFS
2. BFS
3. Display the graph
4. Exit
Enter your choice2
Enter the starting vertex for BFS0
visit :
0
1
2
3
1. DFS
2. BFS
3. Display the graph
4. Exit
Enter your choice4