-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdfsbfs.c
90 lines (83 loc) · 1.6 KB
/
dfsbfs.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
#include<stdio.h>
#include<stdlib.h>
int source,v,e,g[20][20],visited[20],q[20];
int front=-1;
int rear=-1;
void enqueue(int data){
if(rear==-1){
front=0;
}
q[++rear]=data;
}
int dequeue(){
if(front==-1){
return 02;
}
else{
return q[front--];
}
}
void dfs(int i){
int j;
visited[i]=1;
printf("%3d",i);
for(j=1;j<=v;j++)
{
if(g[i][j]==1 && visited[j]==0)
dfs(j);
}
}
void bfs(int s){
int i;
visited[s]=1;
printf("%3d", s);
enqueue(s);
while(front!=-1){
s=dequeue();
for(i=1;i<=v;i++)
{
if (g[s][i]==1 && visited[i]==0)
{
printf("%3d",i);
visited[i]=1;
enqueue(i);
}
}
}
}
void main(){
int i,j,v1,v2,opt;
printf("enter the no: of vertices of the graph");
scanf("%d",&v);
for(int i=1;i<=v;i++){
for(int j=1;j<=v;j++){
g[i][j]=0;
}
}
printf("enter the number of edges ");
scanf("%d",&e);
for(int i=1;i<=e;i++)
{
printf("enter the edges(v1 v2): ");
scanf("%d %d", &v1,&v2);
g[v1][v2]=1;
g[v2][v1]=1;
}
printf("adjacency matrix\n");
for(int i=1;i<=v;i++){
for(j=1;j<=v;j++){
printf("%3d",g[i][j]);
}
printf("\n");
}
for (int i=1;i<=v;i++){
visited[i]=0;}
printf("source");
scanf("%d",&source);
printf("dfs:\n");
dfs(source);
for (int i=1;i<=v;i++){
visited[i]=0;}
printf("\nbfs:\n");
bfs(source);
}