forked from harish3600/Currency-Conversion-System
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpriority_queue.c
More file actions
176 lines (157 loc) · 4.36 KB
/
priority_queue.c
File metadata and controls
176 lines (157 loc) · 4.36 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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <malloc.h>
#include <assert.h>
#include <limits.h>
#include <time.h>
#include <math.h>
#include "priority_queue.h"
#define infinite 100005
// creates a priority queue with a particular size
pqueue create_pqueue(length size)
{
pqueue pq = (pqueue)malloc(sizeof(min_heap));
pq->size = size;
pq->heap = (element_type *)malloc(size * sizeof(element_type));
}
// checks if the heap is empty or not
int is_pqueue_empty(pqueue pq)
{
if (pq->heap_size <= 0)
return 1;
else
return 0;
}
// swaps two elements of the heap
void swap(element_type *a, element_type *b, length i[])
{
length temp_id = i[(*a).vertex_id];
i[(*a).vertex_id] = i[(*b).vertex_id];
i[(*b).vertex_id] = temp_id;
element_type t;
t.key = (*a).key;
t.vertex_id = (*a).vertex_id;
(*a).key = (*b).key;
(*a).vertex_id = (*b).vertex_id;
(*b).key = t.key;
(*b).vertex_id = t.vertex_id;
}
//function to get right child of an element in the heap
length get_right_child(pqueue pq, length index)
{
if ((((2 * index) + 2) < pq->size) && (index >= 0))
return (2 * index) + 2;
return -1;
}
//function to get left child of an element in the heap
length get_left_child(pqueue pq, length index)
{
if (((2 * index) + 1< pq->size) && (index >= 0))
return (2 * index)+1;
return -1;
}
//function to get the parent of an element in the heap
length get_parent(pqueue pq, length index)
{
if ((index > 0) && (index < pq->size))
{
return (index-1) / 2;
}
return -1;
}
void min_heapify(pqueue pq, length index, length i[])
{
length left_child_index = get_left_child(pq, index);
length right_child_index = get_right_child(pq, index);
// finding smallest among index, left child and right child
length smallest = index;
if ((left_child_index < pq->heap_size) && (left_child_index >= 0))
{
if (pq->heap[left_child_index].key < pq->heap[smallest].key)
{
smallest = left_child_index;
}
}
if ((right_child_index < pq->heap_size && (right_child_index >= 0)))
{
if (pq->heap[right_child_index].key < pq->heap[smallest].key)
{
smallest = right_child_index;
}
}
// smallest is not the element, element is not a heap
if (smallest != index)
{
swap(&pq->heap[index], &pq->heap[smallest], i);
min_heapify(pq, smallest, i);
}
}
// builds the heap
void build_min_heap(pqueue pq, length i[])
{
length j;
for (j = (pq->heap_size / 2)-1; j >= 0; j--)
{
min_heapify(pq, j, i);
}
}
// returns the minimum element (top element) of the heap
element_type minimum(pqueue pq)
{
return pq->heap[1];
}
// extracts the minimum element of the heap and deletes it
element_type extract_min(pqueue pq, length i[])
{
element_type ex_min = pq->heap[0];
pq->heap[0].key = pq->heap[pq->heap_size-1].key;
pq->heap[0].vertex_id = pq->heap[pq->heap_size-1].vertex_id;
i[pq->heap[0].vertex_id] = 0;
pq->heap_size--;
min_heapify(pq, 0, i);
return ex_min;
}
// adjusts the position of the element in the heap according to its key value
// Driver function
void decrease_key(pqueue pq, length vertex_id, length key, length i[])
{
element_type e;
e.vertex_id = vertex_id;
e.key = key;
decrease_key_helper(pq, i[e.vertex_id], e, i);
}
// Helper function
void decrease_key_helper(pqueue pq, length index, element_type e, length i[])
{
pq->heap[index].key = e.key;
pq->heap[index].vertex_id = e.vertex_id;
i[e.vertex_id] = index;
while ((index > 0) && (pq->heap[get_parent(pq, index)].key > pq->heap[index].key))
{
swap(&pq->heap[index], &pq->heap[get_parent(pq, index)], i);
index = get_parent(pq, index);
}
}
// inserts an element in the heap
void insert_pqueue(pqueue pq, length vertex_id, length key, length i[])
{
element_type e;
e.vertex_id = vertex_id;
e.key = key;
pq->heap[pq->heap_size].key = key;
pq->heap[pq->heap_size].vertex_id = vertex_id;
i[vertex_id] = pq->heap_size;
pq->heap_size++;
decrease_key_helper(pq, pq->heap_size-1, e, i);
}
// prints the elements of the heap
void print_heap(pqueue pq)
{
length i;
for (i = 0; i < pq->heap_size; i++)
{
printf("(%ld) %ld ", pq->heap[i].vertex_id, pq->heap[i].key);
}
printf("\n");
}