-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindexed_min_priority_queue.cpp
191 lines (165 loc) · 4.41 KB
/
indexed_min_priority_queue.cpp
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
#include <bits/stdc++.h>
using namespace std;
template <class T>
class IndexedPriorityQueue{
public:
class Entry{
public:
int key;
T val;
Entry(int key, T val){
this->key = key;
this->val = val;
}
T get(int key){
return val;
}
void update(T val){
this->val = val;
}
};
IndexedPriorityQueue(){
size = 0;
this->nodes = vector<Entry>();
}
IndexedPriorityQueue(const vector<Entry>& nodes){
size = 0;
this->nodes = vector<Entry>();
build(nodes);
}
void build(const vector<Entry>& nodes){
int n = nodes.size();
for(auto node: nodes){
this->nodes.push_back(node);
key_to_position[node.key] = size;
size++;
}
for(int i = ((size/2)-1);i>=0;i--){
sink(i);
}
}
T top(){
return nodes[0].val;
}
void pop(){
int k = nodes[0].key;
key_to_position.erase(nodes[0].key);
exchange(0, size-1);
size--;
int pos = sink(0);
nodes.pop_back();
}
int push(const Entry& node){
int key = node.key;
nodes.push_back(node);
int pos = swim(size);
size++;
key_to_position[key] = pos;
return size-1;
}
void decrease(int id, int new_val){
nodes[key_to_position[id]].update(new_val);
swim(key_to_position[id]);
}
void print(){
for(auto node: nodes){
cout << node.key << " " << node.val << endl;
}
for(auto p: key_to_position){
cout << p.first << "->" << p.second << endl;
}
cout << endl;
}
bool empty(){
return (size == 0);
}
private:
int size;
vector<Entry> nodes; // Entry key will be the key users provide. For example; vertex number in a graph.
unordered_map<int, int> key_to_position; // mapping from key (provided by user) to position in the nodes vector.
inline int parent(int pos){
return (pos == 0) ? pos : (pos-1)/2;
}
inline int left(int pos){
return (2*pos + 1);
}
inline int right(int pos){
return (2*pos + 2);
}
void exchange(int a, int b){
if(a == b) return;
key_to_position[nodes[a].key] = b;
key_to_position[nodes[b].key] = a;
swap(nodes[a], nodes[b]);
}
int sink(int pos){
bool stop = false;
while(left(pos) < size and !stop){
int l = left(pos);
int r = right(pos);
int min_index = l;
if(l < size-1 and nodes[l].val > nodes[r].val) {
min_index = r;
}
if(nodes[min_index].val < nodes[pos].val) {
exchange(min_index, pos);
}
else {
min_index = pos;
stop = true;
}
pos = min_index;
}
return pos;
}
int swim(int pos){
while(parent(pos) != pos and nodes[parent(pos)].val > nodes[pos].val){
exchange(pos, parent(pos));
pos = parent(pos);
}
return pos;
}
};
// Test: output before both "###...#" should be same.
// Implementation reference : Algorithms by robert sedgewick and kevin wayne
int main()
{
IndexedPriorityQueue<int> pq;
pq.push(IndexedPriorityQueue<int> :: Entry(1, 3));
pq.push(IndexedPriorityQueue<int> :: Entry(2, 2));
pq.push(IndexedPriorityQueue<int> :: Entry(3, 15));
pq.push(IndexedPriorityQueue<int> :: Entry(4, 5));
pq.push(IndexedPriorityQueue<int> :: Entry(5, 4));
pq.push(IndexedPriorityQueue<int> :: Entry(6, 45));
pq.print();
pq.decrease(6, 1);
pq.decrease(3, 2);
pq.decrease(5, 2);
pq.print();
cout << endl << endl;
while(!pq.empty()){
cout << pq.top() << endl;
pq.pop();
}
cout << "####################################################################" << endl;
vector<IndexedPriorityQueue<int> :: Entry> nodes;
nodes.push_back(IndexedPriorityQueue<int> :: Entry(1, 3));
nodes.push_back(IndexedPriorityQueue<int> :: Entry(2, 2));
nodes.push_back(IndexedPriorityQueue<int> :: Entry(3, 15));
nodes.push_back(IndexedPriorityQueue<int> :: Entry(4, 5));
nodes.push_back(IndexedPriorityQueue<int> :: Entry(5, 4));
nodes.push_back(IndexedPriorityQueue<int> :: Entry(6, 45));
IndexedPriorityQueue<int> minheap(nodes);
minheap.print();
minheap.decrease(6, 1);
minheap.decrease(3, 2);
minheap.decrease(5, 2);
minheap.print();
cout << endl << endl;
while(!minheap.empty()){
cout << minheap.top() << endl;
minheap.pop();
}
cout << "####################################################################" << endl;
return 0;
}