-
Notifications
You must be signed in to change notification settings - Fork 0
/
dslab11.c
100 lines (84 loc) · 1.72 KB
/
dslab11.c
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#include <stdio.h>
#include <stdlib.h>
int a[10][10],n,src,visited1[10]={0},visited2[10]={0};
void create();
void bfs(int src);
void dfs(int src);
int main(){
int ch,src,i;
while(1){
printf("\n\t MENU\n");
printf("1. Create \n 2. BFS \n 3. DFS \n 4. Exit\n");
printf("Enter Your Choice:\n");
scanf("%d",&ch);
switch(ch){
case 1:
create();
break;
case 2: printf("BFS Traversals:\n");
printf("Enter Source Vertex:\n");
scanf("%d",&src);
bfs(src);
break;
case 3: printf("DFS Traversal:\n");
printf("Enter Source Vertex:\n");
scanf("%d",&src);
dfs(src);
for(i=0;i<n;i++){
if(visited2[i] == 0){
printf("Graph is Not Connected.\n");
exit(0);
}
}
printf("Graph is Connected.\n");
break;
case 4: exit(0);
default:printf("Invalid Choice\n");
}
}
}
void create(){
int i,j;
printf("Enter No Vertices:\n");
scanf("%d",&n);
printf("Enter Adjacency matrix:\n");
for(i=0;i<n;i++){
for(j=1;j<=n;j++){
scanf("%d",&a[i][j]);
}
}
}
void bfs(int src){
int q[10],f=0,r=-1,i,j;
visited1[src] = 1;
q[++r] = src;
printf("Nodes Reachables are:\n");
while(f<= r){
i = q[f];
f++;
printf("%d \t",i);
for(j=0;j<n;j++){
if(a[i][j]==0 && visited1[j]==0){
visited1[j] = 1;
q[++r] = j;
}
}
}
printf("\n Nodes not Reachables are:\n");
for(i=0;i<n;i++){
if(visited1[i] == 0){
printf("%d \t",i);
}
visited1[i] = 0;
}
}
void dfs(int src){
int j;
visited2[src] = 1;
for(j=0;j<n;j++){
if(a[src][j] == 1 && visited2[j] == 0){
printf("%d -----> %d \n",src,j);
dfs(j);
}
}
}