Repository navigation
Expand file tree
/
Copy pathDFS_BFS_List.cpp
More file actions
189 lines (167 loc) · 2.66 KB
/
Copy pathDFS_BFS_List.cpp
File metadata and controls
189 lines (167 loc) · 2.66 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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
//Linked List DFS & BFS
#include <iostream>
#define MAX 50
using namespace std;
int arr[8][8] =
{
0,1,1,0,0,0,0,0,
1,0,0,1,1,0,0,0,
1,0,0,0,0,1,1,0,
0,1,0,0,0,0,0,1,
0,1,0,0,0,0,0,1,
0,0,1,0,0,0,0,1,
0,0,1,0,0,0,0,1,
0,0,0,1,1,1,1,0
};
typedef struct Q
{
int data[MAX];
int front, rear;
}Q;
typedef struct node
{
node *next;
int vertext;
}node;
void enqueue(Q *, int);
int dequeue(Q *);
int empty(Q *);
void BFS(int v);
void DFS(int v);
void initGraph();
void insert(int num1, int num2);
int visited[MAX];
node *graph[MAX]; //head of linked list
int n;
void BFS(int v)
{
int i, visited[MAX];
Q q;
node *p;
q.rear = q.front = -1;
for (int i = 0; i < MAX; i++)
visited[i] = 0; //false·Î ÃʱâÈ
enqueue(&q, v);
cout << v << " ";
visited[v] = 1; //check true
while (!empty(&q))
{
v = dequeue(&q);
for (p = graph[v]; p != NULL; p = p->next)
{
if (visited[p->vertext] == 0)
{
enqueue(&q, p->vertext);
visited[p->vertext] = 1;
cout << p->vertext <<" ";
}
}
}
}
int empty(Q *p)
{
if (p->rear == -1)
return 1;
else return 0;
}
void enqueue(Q *p, int x)
{
if (p->rear == -1)
{
p->front = p->rear = 0;
p->data[p->rear] = x;
}
else
{
p->rear = p->rear + 1;
p->data[p->rear] = x;
}
}
int dequeue(Q *p)
{
int x;
x = p->data[p->front];
if (p->rear == p->front)
{
p->rear = -1;
p->front = -1;
}
else
p->front = p->front + 1;
return x;
}
void initGraph()
{
int i, num1, num2;
for (int i = 0; i < MAX; i++)
graph[i] = NULL;
}
void insert(int num1, int num2)
{
node *p, *q;
q = new node();
q->vertext = num2;
q->next = NULL;
if (graph[num1] == NULL)
graph[num1] = q;
else
{
p = graph[num1];
while (p->next != NULL)
p = p->next;
p->next = q;
}
}
void DFS(int v)
{
node *p;
cout << v << " ";
p = graph[v];
visited[v] = 1;
while (p != NULL)
{
v = p->vertext;
if (!visited[v])
DFS(v);
p = p->next;
}
}
void main()
{
cout << "Print Adjavent Matrix" << endl;
for (int i = 0; i < 8; i++)
{
for (int j = 0; j <8; j++)
{
cout << arr[i][j] << " ";
}
cout << endl;
}
cout << endl;
initGraph();
insert(0, 1);
insert(0, 2);
insert(1, 0);
insert(1, 3);
insert(1, 4);
insert(2, 0);
insert(2, 5);
insert(2, 6);
insert(3, 1);
insert(3, 7);
insert(4, 1);
insert(4, 7);
insert(5, 2);
insert(5, 7);
insert(6, 2);
insert(6, 7);
insert(7, 3);
insert(7, 4);
insert(7, 5);
insert(7, 6);
cout << "BFS : ";
BFS(0);
cout << endl;
cout <<"DFS : ";
DFS(0);
}