-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBFS DFS ADJ MATRIX
More file actions
74 lines (72 loc) · 1.68 KB
/
BFS DFS ADJ MATRIX
File metadata and controls
74 lines (72 loc) · 1.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#include<stdio.h>
#include<stdlib.h>
#define MAX 5
void BFS(int adj[][MAX],int visited[],int start){
int queue[MAX], i,k,rear=-1,front=-1;
for(k=0;k<MAX;k++){
visited[k]=0;
}
queue[++rear]=start;
++front;
visited[start]=1;
while(rear>=front){
start=queue[front++];
printf("%c-",start+65);
for(i=0;i<MAX;i++){
if(adj[start][i]&&visited[i]==0){
queue[++rear]=i;
visited[i]=1;
}
}
}
}
void DFS(int adj[][MAX],int visited[],int start){
int stack[MAX],top=-1,i,k;
for(k=0;k<MAX;k++){
visited[k]=0;
}
stack[++top]=start;
visited[start]=1;
while(top!=-1){
start=stack[top--];
printf("%c-",start+65);
for(i=0;i<MAX;i++){
if(adj[start][i]&&visited[i]==0){
stack[++top]=i;
visited[i]=1;
break;
}
}
}
}
int main(){
int visited[MAX]={0};
int adj[MAX][MAX],i,j;
int choice,size;
while(1){
printf("enter choice 1:enter values of graph,2:BFS,3:DFS,4:exit");
scanf("%d",&choice);
switch(choice){
case 1:
printf("enter the adjacemcey matrix");
for(i=0;i<MAX;i++){
for(j=0;j<MAX;j++){
scanf("%d",&adj[i][j]);
}
}
break;
case 2:
printf("BFS traversal is");
BFS(adj,visited,0);
break;
case 3:
printf("DFS traversal is");
DFS(adj,visited,0);
break;
case 4:
exit(0);
default:
printf("invalid ");
}
}
}