-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPokemon.c
353 lines (312 loc) · 6.77 KB
/
Pokemon.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
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
typedef struct node
{
int data; // id of the vertex
struct node *next;
} node;
typedef struct
{
int front, rear;
int size;
node **array;
} Queue;
// --------------------------------------------GRAPH DEFS---------------------------------------------------------
typedef struct graph
{
int numV;
node **adjl; // adjacency list
int *visit; // visited
} graph;
node *createNode(int val)
{
node *newVertex = (node *)malloc(sizeof(node));
newVertex->data = val;
newVertex->next = NULL;
return newVertex;
}
graph *createGraph(int vertices)
{
graph *g = (graph *)malloc(sizeof(graph));
g->numV = vertices;
g->adjl = (node **)malloc((vertices + 1) * sizeof(node *)); // adjacency list
g->visit = (int *)malloc((vertices + 1) * sizeof(int)); // array to check visited or not
for (int i = 0; i < vertices; i++)
{
g->adjl[i] = NULL;
g->visit[i] = 0;
}
return g;
}
void addEdge(graph *g, int src, int dest)
{
node *newNode = createNode(dest);
newNode->next = g->adjl[src]; // access the latest addition in the adjacency list
g->adjl[src] = newNode;
}
//-------------------------------------------------QUEUE--------------------------------------------------------------
// A utility function to create a new Queue
Queue *createQueue(int size)
{
Queue *queue = (Queue *)malloc(sizeof(Queue));
queue->front = queue->rear = -1;
queue->size = size;
queue->array = (node **)malloc(queue->size * sizeof(node *));
int i;
for (i = 0; i < size; ++i)
queue->array[i] = NULL;
return queue;
}
int isEmpty(Queue *queue)
{
return queue->front == -1;
}
int isFull(Queue *queue)
{
return queue->rear == queue->size - 1;
}
int hasOnlyOneItem(Queue *queue)
{
return queue->front == queue->rear;
}
void Enqueue(node *root, Queue *queue)
{
if (isFull(queue))
return;
queue->array[++queue->rear] = root;
if (isEmpty(queue))
++queue->front;
}
Queue *Dequeue(Queue *queue)
{
if (isEmpty(queue))
return NULL;
node *temp = queue->array[queue->front];
if (hasOnlyOneItem(queue))
queue->front = queue->rear = -1;
else
++queue->front;
return queue;
}
node *getFront(Queue *queue)
{
return queue->array[queue->front];
}
//---------------------------------------------PRIORITY Q-------------------------------------------------------
#define MAX 1000
int size;
typedef struct
{
int priority; // dist
node *vertex;
} heap;
int root()
{
return 1;
}
int parent(int n)
{
return n / 2;
}
int lchild(int n)
{
return 2 * n;
}
int rchild(int n)
{
return 2 * n + 1;
}
bool has_parent(int n)
{
return n != root();
}
bool is_node(int n)
{
return n <= size;
}
void swap(heap *a, heap *b)
{
heap temp = *a;
*a = *b;
*b = temp;
}
int get_min(heap h[])
{
if (size == 0)
{
printf("Empty priority queue!!");
exit(0);
}
return h[root()].priority;
}
void heap_up(heap h[], int n)
{
while (has_parent(n) && (h[parent(n)].priority > h[n].priority)) // > for min
{
swap(&h[parent(n)], &h[n]);
n = parent(n);
}
}
void push(heap h[], heap new_element)
{
if (size == MAX)
{
printf("Priority queue is full!!");
exit(0);
}
h[size + 1] = new_element;
size++;
heap_up(h, size);
}
void heap_dwn(heap h[], int n) // logn
{
while (is_node(lchild(n)))
{
int child = lchild(n);
if (is_node(rchild(n)) && (h[rchild(n)].priority < h[lchild(n)].priority)) // < for min
child = rchild(n);
if (h[n].priority > h[child].priority) // > for min
swap(&h[n], &h[child]);
else
break;
n = child;
}
}
void pop(heap h[]) // log n
{
if (size == 0)
{
printf("Empty priority queue!!");
exit(0);
}
h[1] = h[size]; // maintain complete tree
size--;
heap_dwn(h, root());
}
void build_heap(int arr[], heap h[], int n) // n
{
for (int i = 0; i < n; i++)
h[i + 1].priority = arr[i];
size = n;
for (int i = size / 2; i >= 1; i--)
heap_dwn(h, i);
}
void heap_sort(int arr[], int n) // nlogn
{
heap h[MAX + 1];
int sz = n;
build_heap(arr, h, n);
while (n)
{
arr[sz - n] = h[root()].priority; // n-1 for descending
pop(h);
n--;
};
}
//---------------------------------------------------------TRAVERSALS---------------------------------------------------------
void BFS(graph *g, node *src, int *dist, int vertices)
{
dist[src->data] = 0;
g->visit[src->data] = 1;
Queue *q = createQueue(vertices + 1);
Enqueue(src, q);
node *cur, *child;
while (!isEmpty(q))
{
cur = getFront(q);
q = Dequeue(q);
printf("%d\n", cur->data);
node *cnct = g->adjl[cur->data];
while (cnct != NULL)
{
child = cnct;
if (g->visit[child->data] != 1)
{
g->visit[child->data] = 1;
dist[child->data] = dist[cur->data] + 1;
Enqueue(child, q);
}
cnct = cnct->next;
}
}
}
void DFS(graph *g, node *v)
{
g->visit[v->data] = 1;
node *child;
node *cnct = g->adjl[v->data];
printf("%d\n", v->data);
while (cnct != NULL)
{
child = cnct;
if (g->visit[child->data] != 1)
{
DFS(g, child);
}
cnct = cnct->next;
}
}
int isCyclicUtil(int v, int *visit,
int *path, graph *g)
{
if (visit[v] == 0)
{
visit[v] = 1;
path[v] = 1;
node *cnct = g->adjl[v];
while (cnct != NULL)
{
if (visit[cnct->data] == 0 && isCyclicUtil(cnct->data, visit, path, g))
return 1;
else if (path[cnct->data])
return 1;
cnct = cnct->next;
}
}
path[v] = 0;
return 0;
}
int isCyclic(graph *g)
{
int *visit = (int *)malloc((g->numV) * sizeof(int));
int *path = (int *)malloc((g->numV) * sizeof(int));
for (int i = 0; i < g->numV; i++)
{
visit[i] = 0;
path[i] = 0;
}
for (int i = 0; i < g->numV; i++)
if (visit[i] == 0 && isCyclicUtil(i, visit, path, g))
return 1;
return 0;
}
int main()
{
int n, m, i;
int x, y, wt;
scanf("%d %d", &n, &m);
graph *g = createGraph(n);
for (i = 0; i < m; i++)
{
scanf("%d %d", &x, &y);
addEdge(g, x, y);
}
int *dist = (int *)malloc((n + 1) * sizeof(int));
if (isCyclic(g))
{
printf("unconquerable\n");
return 0;
}
else
{
printf("huh?\n");
return 0;
}
return 0;
}
// 4 4
// 1 0
// 2 0
// 3 1
// 3 2