-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathp8.c
More file actions
94 lines (89 loc) · 2.09 KB
/
p8.c
File metadata and controls
94 lines (89 loc) · 2.09 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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#include<stdio.h>
#include<stdlib.h>
int st[10],top=-1,u[10],v[10],a[10][10];
int n,q[10],front=0,rear=-1;
void dfs(int s)
{
int i;
v[s]=1;
st[++top]=s;
for(i=1;i<=n;i++)
{
if(a[s][i]==1&&v[i]==0)
{
printf("officer %d->officer %d\n",i,s);
dfs(i);
}
}
}
void bfs(int s)
{
int i,m;
u[s]=1;
q[++rear]=s;
printf("reachbale officers using bfs method from given officer %d\n",s);
while(front<=rear)
{
m=q[front++];
for(i=1;i<=n;i++)
{
if(a[m][i]==1&&u[i]==0)
{
q[++rear]=i;
printf("officer %d\n",i);
u[i]=1;
}
}
}
}
void main()
{
int i,n,ch,j,s;
while(1)
{
printf("\n1.create a graph\n2.dfs method\n3.bfs method\n4.exit\n");
printf("enter the choice\n");
scanf("%d",&ch);
switch(ch)
{
case 1:printf("enter the elements\n");
scanf("%d",&n);
printf("matrix\n");
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
scanf("%d",&a[i][j]);
}
}
break;
case 2:printf("dfs\n");
printf("enter the source\n");
scanf("%d",&s);
printf("reachable officer using dfs method from given officer %d\n",s);
dfs(s);
for(i=1;i<=n;i++)
{
if(v[i]==0)
{
printf("%d is not visited and it is dissconnected graph \n",i);
}
}
break;
case 3:printf("bfs\n");
printf("enter the source\n");
scanf("%d",&s);
bfs(s);
for(i=1;i<=n;i++)
{
if(u[i]==0)
{
printf("%d is not visited and it is dissconnected graph \n",i);
}
}
break;
case 4:exit(0);
default:printf("invalid choice\n");
}
}
}