-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathDFS-BFS.c
95 lines (89 loc) · 1.87 KB
/
DFS-BFS.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
#include <stdio.h>
#define MAX 5
int matrix[][5]={{0,0,0,1,0}
,{0,0,1,0,1}
,{0,1,0,1,1}
,{1,0,1,0,1}
,{0,1,1,1,0}};
void BFS(int a);
void insert(int a);
void delete();
void display_Matrix();
int queue_array[MAX];
int rear = - 1;
int front = - 1;
int visitedb[]={0,0,0,0,0};
int visitedd[]={0,0,0,0,0};
main()
{
display_Matrix();
printf("\n\t");
printf("\n\tBFS::");
BFS(1);
printf("\n\tDFS::");
DFS(1);
}
void BFS(int src){
insert(src);
visitedb[src-1]=1;
printf(" -> %d",src);
while(front!=-1){
int v=Delete();
for(int i=0;i<5;i++){
if(matrix[v-1][i]!=0 && visitedb[i]==0){
insert(i);
visitedb[i]=1;
printf(" -> %d",i+1);
}
}
}
}
void DFS(int src){
visitedd[src-1]=1;
printf(" -> %d",src);
for(int i=0;i<5;i++){
if(matrix[src-1][i]!=0 && visitedd[i]==0){
DFS(i+1);
}
}
}
void display_Matrix(){
printf("\tAdjacency Matrix :: \n");
printf("\t | 1 2 3 4 5\n");
printf("\t-------------------");
for(int i=0;i<5;i++){
printf("\n\t %d |",i+1);
for(int j=0;j<5;j++){
printf(" %d",matrix[i][j]);
}
}
}
void insert(int val)
{
if (rear == MAX - 1)
printf("Queue Overflow \n");
else
{
if (front == - 1)
front = 0;
rear = rear + 1;
queue_array[rear] = val;
}
}
int Delete()
{
if (front == - 1 || front > rear)
{
printf("Queue Underflow \n");
return ;
}
else
{
int v=queue_array[front];
if(front==rear){
front=rear=-1;
}else
front = front + 1;
return v;
}
}