-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdepth_first_search.c
87 lines (67 loc) · 1.09 KB
/
depth_first_search.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
#include<stdio.h>
#include<stdlib.h>
#include<stdbool.h>
#define MAX 4
int stack[4];
int top = -1;
int visited[4];
int isempty();
int isfull();
int pop();
int push(int data);
void main()
{
int graph[4][4]={{0,1,1,0},
{0,0,1,0},
{1,0,0,1},
{0,0,0,1}};
int vertex=0,i,j;
for(i=0;i<4;i++)
{
visited[i]=0;
}
push(vertex);
for(i=0;i<4;i++)
{
vertex=pop();
printf("%d ",vertex);
visited[vertex]=1;
//explore
for(j=0;j<4;j++)
{
if(graph[vertex][j]!=0)
{
if(visited[j]!=1)
{
push(j);
}
}
}
}
}
int isempty() {
if(top == -1)
return 1;
else
return 0;
}
int isfull() {
if(top == MAX)
return 1;
else
return 0;
}
int pop() {
int data;
if(!isempty()) {
data = stack[top];
top = top - 1;
return data;
}
}
int push(int data) {
if(!isfull()) {
top = top + 1;
stack[top] = data;
}
}