-
Notifications
You must be signed in to change notification settings - Fork 0
/
(GRAPH)BREADTHFIRSTSEARCH.c
84 lines (55 loc) · 981 Bytes
/
(GRAPH)BREADTHFIRSTSEARCH.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
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include "Queue.h"
void BFS(int a[][7],int start,int n){
int i=start;
int visited[8]={0};
// struct queue q;
printf("%d",i);
visited[i]=1;
enqueue(&q,i);
while(!isEmpty(q)){
int u=dequeue(&q);
for(int v=1;v<=n;v++){
if(a[u][v]==1 && visited[v]==0)
{
printf("%d",v);
visited[v]=1;
enqueue(&q,v);
}
}
}
}
// ***************** DEPTH FIRST SEARCH ***************************
// u->starting vertex
void DFS(int a[][7],int start,int n){
static int visited[8]={0};
int u=start;
if(visited[u]==0)
{
printf("%d",u);
visited[u]=1;
for(int v=1;v<=n;v++){
if(a[u][v]==1 && visited[v]!=1)
// means there is an edge
{
DFS(a,v,n);
}
}
}
}
int main(){
int a[7][7]={
{0,0,0,0,0,0,0},
{0,0,1,1,0,0,0},
{0,1,0,0,1,0,0},
{0,1,0,0,1,0,0},
{0,0,1,1,0,1,1},
{0,0,0,0,1,0,0},
{0,0,0,0,1,0,0}
};
// BFS(a,1,7);
DFS(a,1,7);
return 0;
}