-
Notifications
You must be signed in to change notification settings - Fork 0
/
heap.c
506 lines (445 loc) · 12.4 KB
/
heap.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
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
/** @file heap.c
*
* heap data structure and operations
* Copyright (c) 2010, Gaurav Mathur <narainmg@gmail.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* See README and COPYING for more details.
*/
#include <stdio.h>
#include <stdlib.h>
#include <ds_types.h>
#include <heap.h>
#include <graph.h>
/**
* @brief graphviz description plugin for heap ds.
*
* @param[in] h The heap to operate in
* @param[in] filename The file to write the graphviz description to.
* @return HEAP_ERR_E
*/
HEAP_ERR_E heap_graphviz_description
(
HEAP_T* h,
char* filename
)
{
FILE* fp;
unsigned long idx = 0;
unsigned long weight;
if (NULL == filename)
{
fp = stderr;
}
else
{
fp = fopen (filename, "w");
if (NULL == fp)
return HEAP_ERR_ERR;
}
fprintf (fp, "graph G {\n");
fprintf (fp, "node [shape = circle, style=filled, color=\"sienna2\"];\n");
fprintf (fp, "size=\"12,8\"\n");
weight = h->heap_size;
if (HEAP_LEFT(idx) < h->heap_size)
fprintf (fp, "\t%lu -- %lu [headlabel=\"%lu\", weight=%lu];\n ",
HEAP_KEY(h, idx),
HEAP_KEY(h, HEAP_LEFT(idx)),
HEAP_LEFT(idx),
weight);
if (HEAP_RIGHT(idx) < h->heap_size)
fprintf (fp, "\t%lu -- %lu [headlabel=\"%lu\", weight=%lu];\n",
HEAP_KEY(h, idx),
HEAP_KEY(h, HEAP_RIGHT(idx)),
HEAP_RIGHT(idx),
weight);
for (idx = 1; idx < h->heap_size; idx++)
{
if (HEAP_LEFT(idx) < h->heap_size)
fprintf (fp, "\t%lu -- %lu [headlabel = \"%lu\"]; \n ",
HEAP_KEY(h, idx),
HEAP_KEY(h, HEAP_LEFT(idx)),
HEAP_LEFT(idx));
if (HEAP_RIGHT(idx) < h->heap_size)
fprintf (fp, "\t%lu -- %lu [headlabel = \"%lu\"];\n",
HEAP_KEY(h, idx),
HEAP_KEY(h, HEAP_RIGHT(idx)),
HEAP_RIGHT(idx));
}
fprintf (fp, "}\n");
if (NULL != filename)
fclose (fp);
return HEAP_ERR_OK;
}
/**
* @brief dump heap keys
*
* heap dump for graph nodes
*/
void heap_graph_dump (HEAP_T* h)
{
unsigned long idx;
fprintf (stdout, "[Dumping heap (heap_size=%lu)]\n", h->heap_size);
for (idx = 0; idx < h->heap_size; idx++)
{
fprintf (stdout, "i=%lu: key=%lu, vid=%lu\n",
(h->nodes[idx]->i)?*h->nodes[idx]->i:0, h->nodes[idx]->key,
((VTX_D_T*)h->nodes[idx]->data)->id.iid);
}
}
/**
* @brief dump heap keys
*/
void heap_dump (HEAP_T* h)
{
unsigned long idx;
fprintf (stdout, "[Dumping heap (heap_size=%lu)]\n", h->heap_size);
for (idx = 0; idx < h->heap_size; idx++)
{
fprintf (stdout, "i=%lu -> key=%lu\n",
idx, h->nodes[idx]->key);
}
}
/**
* @brief iterate over heap elements
*
* This routine can be used to iterator over the heap elements.
*
* @param[in] h
* @param[in] data
* @param[in] key
* @param[in] ctx Iterator context. Caller should set the context value to 0
* before starting the iterator
*/
HEAP_ERR_E heap_iter (HEAP_T* h, void** data, unsigned long* key, unsigned long* ctx)
{
unsigned long i;
if (NULL == ctx || NULL == key || NULL == data)
return HEAP_ERR_INVALID_ARGS;
i = *ctx;
if (i >= HEAP_SIZE(h))
return HEAP_ERR_ITERATOR_DONE;
*data = h->nodes[i]->data;
*key = h->nodes[i]->key;
*ctx += 1;
return HEAP_ERR_OK;
}
/**
* @brief CSLR MIN-HEAPIFY
*
* This routine creates a min-heap for the subtree rooted at index i. The
* index is 0 based.
*
* @param[in] h The heap to operate on
* @param[in] i The (0-based) subtree index
* @return HEAP_ERR_E
*/
HEAP_ERR_E heap_min_heapify (HEAP_T* h, unsigned long i)
{
unsigned long l = HEAP_LEFT(i);
unsigned long r = HEAP_RIGHT(i);
unsigned long smallest;
if (h->type == DS_HEAP_MAX)
return HEAP_ERR_WRONG_TYPE;
if (l < h->heap_size && (HEAP_KEY(h, l) < HEAP_KEY(h, i)))
smallest = l;
else
smallest = i;
if (r < h->heap_size && (HEAP_KEY(h, r) < HEAP_KEY(h, smallest)))
smallest = r;
if (smallest != i)
{
HEAP_SWAP_NODES(i,smallest);
heap_min_heapify (h, smallest);
}
return HEAP_ERR_OK;
}
/**
* @brief CSLR MAX-HEAPIFY
*
* This routine creates a max-heap for the subtree rooted at index i. The
* index is 0 based.
*
* @param[in] h The heap to operate on
* @param[in] i The (0-based) subtree index
* @return HEAP_ERR_E
*/
HEAP_ERR_E heap_max_heapify (HEAP_T* h, unsigned long i)
{
unsigned long l = HEAP_LEFT(i);
unsigned long r = HEAP_RIGHT(i);
unsigned long largest;
//fprintf (stdout, "i=%lu; l=%lu; r=%lu\n", i, l, r);
if (h->type == DS_HEAP_MIN)
return HEAP_ERR_WRONG_TYPE;
if (l < h->heap_size && (HEAP_KEY(h, l) > HEAP_KEY(h, i)))
largest = l;
else
largest = i;
if (r < h->heap_size && (HEAP_KEY(h, r) > HEAP_KEY(h, largest)))
largest = r;
if (largest != i)
{
HEAP_SWAP_NODES(i,largest);
heap_max_heapify (h, largest);
}
/* todo: set was_heapified to true */
return HEAP_ERR_OK;
}
/**
* @brief extract the maximum element from the heap
*
* @param[in] g The heap to operate on
* @param[out] data The application object attached with the key
* @param[out] key The application object key
* @return HEAP_ERR_E
*/
HEAP_ERR_E heap_extract_max (HEAP_T* h, void** data, unsigned long* key)
{
/* todo: check was_heapified */
if (h->type != DS_HEAP_MAX)
return HEAP_ERR_WRONG_TYPE;
if (h->heap_size < 1)
{
*data = NULL;
return HEAP_ERR_UNDERFLOW;
}
*data = h->nodes[0]->data;
*key = h->nodes[0]->key;
h->nodes[0]->data = h->nodes[h->heap_size-1]->data;
h->nodes[0]->key = h->nodes[h->heap_size-1]->key;
h->nodes[0]->i = h->nodes[h->heap_size-1]->i;
if (h->nodes[0]->i)
*h->nodes[0]->i = 0;
h->heap_size--;
heap_max_heapify (h, 0);
return HEAP_ERR_OK;
}
/**
* @brief extract the maximum element from the heap
*
* @param[in] g The heap to operate on
* @param[out] data The application object attached with the key
* @param[out] key The application object key
* @return HEAP_ERR_E
*/
HEAP_ERR_E heap_extract_min (HEAP_T* h, void** data, unsigned long* key)
{
/* todo: check was_heapified */
if (h->type != DS_HEAP_MIN)
return HEAP_ERR_WRONG_TYPE;
if (h->heap_size < 1)
{
*data = NULL;
return HEAP_ERR_UNDERFLOW;
}
*data = h->nodes[0]->data;
*key = h->nodes[0]->key;
h->nodes[0]->data = h->nodes[h->heap_size-1]->data;
h->nodes[0]->key = h->nodes[h->heap_size-1]->key;
h->nodes[0]->i = h->nodes[h->heap_size-1]->i;
if (h->nodes[0]->i)
*h->nodes[0]->i = 0;
h->heap_size--;
heap_min_heapify (h, 0);
return HEAP_ERR_OK;
}
/**
* @brief extract the maximum element from the heap
*
* @param[in] g The heap to operate on
* @param[out] data The application object attached with the key
* @param[out] key The application object key
* @return HEAP_ERR_E
*/
HEAP_ERR_E heap_maximum (HEAP_T* h, void** data, unsigned long* key)
{
if (h->type != DS_HEAP_MAX)
return HEAP_ERR_WRONG_TYPE;
if (h->heap_size < 1)
{
*data = NULL;
return HEAP_ERR_UNDERFLOW;
}
*data = h->nodes[0]->data;
*key = h->nodes[0]->key;
return HEAP_ERR_OK;
}
/**
* @brief decrease the key value of a heap element
*
* @param[in] g The heap to operate on
* @param[in] data The application object attached with the key
* @param[in] key The application object key
* @return HEAP_ERR_E
*/
HEAP_ERR_E heap_decrease_key (HEAP_T* h, unsigned long i, unsigned long key)
{
if (DS_HEAP_MIN != h->type)
return HEAP_ERR_WRONG_TYPE;
if (key == HEAP_NIL_KEY)
return HEAP_ERR_INVALID_KEY;
if (HEAP_KEY(h, i) != HEAP_NIL_KEY && key > HEAP_KEY(h, i))
return HEAP_ERR_LARGER_KEY;
HEAP_KEY(h, i) = key;
while (i > 0 && (HEAP_KEY(h, HEAP_PARENT(i)) > HEAP_KEY(h, i)))
{
HEAP_SWAP_NODES(i,HEAP_PARENT(i));
i = HEAP_PARENT(i);
}
return HEAP_ERR_OK;
}
/**
* @brief increase the key value of a heap element
*
* HEAP-INCREASE-KEY(A, i, key)
* 1 if key < A[i]
* 2 then error "new key is smaller than current key"
* 3 A[i] <- key
* 4 while i > 1 and A[PARENT(i)] < A[i]
* 5 do exchange A[i] <-> A[PARENT(i)]
* 6 i <- PARENT(i)
*
* @param[in] g The heap to operate on
* @param[in] data The application object attached with the key
* @param[in] key The application object key
* @return HEAP_ERR_E
*/
HEAP_ERR_E heap_increase_key (HEAP_T* h, unsigned long i, unsigned long key)
{
if (DS_HEAP_MAX != h->type)
return HEAP_ERR_WRONG_TYPE;
if (key == HEAP_NIL_KEY)
return HEAP_ERR_INVALID_KEY;
if (HEAP_KEY(h, i) != HEAP_NIL_KEY && key < HEAP_KEY(h, i))
return HEAP_ERR_SMALLER_KEY;
HEAP_KEY(h, i) = key;
while (i > 0 && (HEAP_KEY(h, HEAP_PARENT(i)) < HEAP_KEY(h, i)))
{
HEAP_SWAP_NODES(i,HEAP_PARENT(i));
i = HEAP_PARENT(i);
}
return HEAP_ERR_OK;
}
/**
* @brief add an element to the heap
*
* This routine adds a {key, data} pair to the heap. This routine is simply to
* add the elements to the heap, it does heapify the heap h.
*
* @param[in] h The heap to build
* @param[in] data Array of data elements
* @param[in] num Number of array elements
*/
static HEAP_ERR_E heap_add (HEAP_T* h, unsigned long key, void* data, unsigned long* i)
{
DS_HEAP_NODE_T* node;
node = malloc (sizeof (DS_HEAP_NODE_T));
if (NULL == node)
return HEAP_ERR_ERR;
node->data = data;
node->key = key;
if (NULL != i)
*i = h->heap_size;
node->i = i;
h->nodes[h->heap_size] = node;
h->heap_size++;
/* todo: set was_heapified to false */
return HEAP_ERR_OK;
}
/**
* @brief insert an element in a max heap
*
* This routine inserts an element in the heap. The element is a {key,data}
* pair.
*
* @param[in] h The heap to operate on
* @param[in] key The element key component
* @param[in] data The element data component
* @param[out] i The updated heap index for the {key, data} will be stored here.
* @return HEAP_ERR_E
*/
HEAP_ERR_E heap_max_insert (HEAP_T* h, unsigned long key, void* data, unsigned long* i)
{
if (h->type != DS_HEAP_MAX)
return HEAP_ERR_WRONG_TYPE;
heap_add (h, HEAP_NIL_KEY, data, i);
heap_increase_key (h, h->heap_size-1, key);
return HEAP_ERR_OK;
}
/**
*
*/
HEAP_ERR_E heap_min_insert (HEAP_T* h, unsigned long key, void* data, unsigned long* i)
{
if (h->type != DS_HEAP_MIN)
return HEAP_ERR_WRONG_TYPE;
heap_add (h, HEAP_NIL_KEY, data, i);
heap_decrease_key (h, h->heap_size-1, key);
return HEAP_ERR_OK;
}
/**
* @brief heapify heap data elements
*
* This routine heapifies the heap elements in the heap h.
*
* @param[in] h The heap to build
* @param[in] data Array of data elements
* @param[in] num Number of array elements
*/
HEAP_ERR_E heap_build (HEAP_T* h)
{
unsigned long i;
HEAP_ERR_E err;
/* todo: use was_heapified to skip this if already a heap */
for (i = h->heap_size/2; i >= 1; i--)
{
if (h->type == DS_HEAP_MAX)
{
if (HEAP_ERR_ERR == (err = heap_max_heapify (h, i-1)))
return HEAP_ERR_ERR;
}
else
{
if (HEAP_ERR_ERR == (err = heap_min_heapify (h, i-1)))
return HEAP_ERR_ERR;
}
}
return HEAP_ERR_OK;
}
/**
* @brief create and initialize a new heap
*
* This routine creates a new heap.
*
* @param[in] type The type of heap
* @param[in] length The heap length.
*/
HEAP_T* heap_create (DS_HEAP_TYPE_E type, unsigned long length)
{
HEAP_T* heap = malloc (sizeof (HEAP_T));
if (NULL == heap)
return NULL;
heap->length = length;
heap->heap_size = 0;
heap->type = type;
heap->nodes = malloc (length * sizeof (DS_HEAP_NODE_T*));
if (NULL == heap->nodes)
{
free (heap);
return NULL;
}
return heap;
}