Skip to content

Commit c9e369f

Browse files
authored
GH-174: Priority Queue (Heap) in C++ (#175)
1 parent 067410b commit c9e369f

File tree

5 files changed

+290
-1
lines changed

5 files changed

+290
-1
lines changed

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,12 @@ A data structure is a data organization, management, and storage format that is
9595
</td>
9696
</tr>
9797
<tr>
98+
<td>Priority Queue (Heap)</td>
99+
<td>
100+
<a href="/concepts/cpp/priority_queue.md"><code>cpp🐀</code></a>
101+
</td>
102+
</tr>
103+
<tr>
98104
<td colspan="2" align="center"><b>Algorithms</b></td>
99105
</tr>
100106
<tr>
Loading
Lines changed: 220 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,220 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"metadata": {},
6+
"source": [
7+
"# Priority Queue \n",
8+
"\n",
9+
"A priority queue is a container that provides constant time lookup of the largest (by default) element, at the expense of logarithmic insertion and extraction.\n",
10+
"\n",
11+
"Real world examples of priority queues include:\n",
12+
"\n",
13+
"- The task queue in an operating system\n",
14+
"- The call queue in a call center\n",
15+
"- The list of pending requests at a web server, sorted by their priority\n"
16+
]
17+
},
18+
{
19+
"cell_type": "markdown",
20+
"metadata": {},
21+
"source": [
22+
"## Baisc usage of priority queue\n",
23+
"\n",
24+
"C++ has built-in object [`priority_queue`](https://cplusplus.com/reference/queue/priority_queue/) in `queue` library."
25+
]
26+
},
27+
{
28+
"cell_type": "code",
29+
"execution_count": null,
30+
"metadata": {
31+
"vscode": {
32+
"languageId": "cpp"
33+
}
34+
},
35+
"outputs": [],
36+
"source": [
37+
"#include <queue>\n",
38+
"#include <vector>\n",
39+
"#include <iostream>\n",
40+
"\n",
41+
"using namespace std;"
42+
]
43+
},
44+
{
45+
"cell_type": "code",
46+
"execution_count": 13,
47+
"metadata": {
48+
"vscode": {
49+
"languageId": "cpp"
50+
}
51+
},
52+
"outputs": [
53+
{
54+
"name": "stdout",
55+
"output_type": "stream",
56+
"text": [
57+
"5\n",
58+
"3\n",
59+
"1\n"
60+
]
61+
}
62+
],
63+
"source": [
64+
"priority_queue<int> pq;\n",
65+
"\n",
66+
"pq.push(1);\n",
67+
"pq.push(5);\n",
68+
"pq.push(3);\n",
69+
"\n",
70+
"while (!pq.empty()) {\n",
71+
" cout << pq.top() << endl;\n",
72+
" pq.pop();\n",
73+
"}"
74+
]
75+
},
76+
{
77+
"cell_type": "markdown",
78+
"metadata": {},
79+
"source": [
80+
"## Min priority queue\n",
81+
"\n",
82+
"If you want min priority queue, you can use `greater<int>` as the third argument."
83+
]
84+
},
85+
{
86+
"cell_type": "code",
87+
"execution_count": 14,
88+
"metadata": {
89+
"vscode": {
90+
"languageId": "cpp"
91+
}
92+
},
93+
"outputs": [
94+
{
95+
"name": "stdout",
96+
"output_type": "stream",
97+
"text": [
98+
"1\n",
99+
"3\n",
100+
"5\n"
101+
]
102+
}
103+
],
104+
"source": [
105+
"priority_queue<int, vector<int>, greater<int>> pq;\n",
106+
"\n",
107+
"pq.push(1);\n",
108+
"pq.push(5);\n",
109+
"pq.push(3);\n",
110+
"\n",
111+
"while (!pq.empty()) {\n",
112+
" cout << pq.top() << endl;\n",
113+
" pq.pop();\n",
114+
"}"
115+
]
116+
},
117+
{
118+
"cell_type": "markdown",
119+
"metadata": {},
120+
"source": [
121+
"## Priority queue with custom comparator\n",
122+
"\n",
123+
"Custom Comparision with `priority_queue` is a little bit interesting. You can use custom struct as the third argument."
124+
]
125+
},
126+
{
127+
"cell_type": "code",
128+
"execution_count": 15,
129+
"metadata": {
130+
"vscode": {
131+
"languageId": "cpp"
132+
}
133+
},
134+
"outputs": [],
135+
"source": [
136+
"struct Node {\n",
137+
" int val;\n",
138+
" int priority;\n",
139+
"};\n",
140+
"\n",
141+
"struct compare{\n",
142+
" public:\n",
143+
" bool operator()(Node& a,Node& b)\n",
144+
" {\n",
145+
" return a.priority < b.priority;\n",
146+
" }\n",
147+
"};"
148+
]
149+
},
150+
{
151+
"cell_type": "code",
152+
"execution_count": 16,
153+
"metadata": {
154+
"vscode": {
155+
"languageId": "cpp"
156+
}
157+
},
158+
"outputs": [],
159+
"source": [
160+
"priority_queue<Node, vector<Node>, compare> pq;\n",
161+
"pq.push({1, 2});\n",
162+
"pq.push({5, 6});\n",
163+
"pq.push({10, 3});"
164+
]
165+
},
166+
{
167+
"cell_type": "code",
168+
"execution_count": 17,
169+
"metadata": {
170+
"vscode": {
171+
"languageId": "cpp"
172+
}
173+
},
174+
"outputs": [
175+
{
176+
"name": "stdout",
177+
"output_type": "stream",
178+
"text": [
179+
"5, 6\n",
180+
"10, 3\n",
181+
"1, 2\n"
182+
]
183+
}
184+
],
185+
"source": [
186+
"while(!pq.empty()){\n",
187+
" Node tmp = pq.top();\n",
188+
" cout << tmp.val << \", \" << tmp.priority << endl;\n",
189+
" pq.pop();\n",
190+
"}"
191+
]
192+
},
193+
{
194+
"cell_type": "markdown",
195+
"metadata": {},
196+
"source": [
197+
"## References\n",
198+
"\n",
199+
"* [Declaring a priority_queue in c++ with a custom comparator](https://stackoverflow.com/questions/16111337/declaring-a-priority-queue-in-c-with-a-custom-comparator)"
200+
]
201+
}
202+
],
203+
"metadata": {
204+
"kernelspec": {
205+
"display_name": "C++17",
206+
"language": "C++17",
207+
"name": "xcpp17"
208+
},
209+
"language_info": {
210+
"codemirror_mode": "text/x-c++src",
211+
"file_extension": ".cpp",
212+
"mimetype": "text/x-c++src",
213+
"name": "c++",
214+
"version": "17"
215+
},
216+
"orig_nbformat": 4
217+
},
218+
"nbformat": 4,
219+
"nbformat_minor": 2
220+
}

concepts/cpp/priority_queue.md

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
# Priority Queue
2+
3+
<a href="https://github.com/rain1024/datastructures-algorithms-competitive-programming/blob/main/concepts/cpp/labs/priority_queue.ipynb">
4+
<img src="https://img.shields.io/badge/🧪-notebook-brightgreen">
5+
</a>
6+
7+
A priority queue is an abstract data type that allows the storage of elements with priority. The elements are stored in such a way that the element with the highest priority is always at the front of the queue, and the element with the lowest priority is at the back of the queue.
8+
9+
![](../abstract/data-structures/priority_queue.png)
10+
11+
12+
## 🎨 Priority Queue Design
13+
14+
**Methods**
15+
16+
| | Definition |
17+
|----------------|-------------------------------------------------------------|
18+
| enqueue | Add element to the queue |
19+
| dequeue | remove the element with the highest priority from the queue |
20+
21+
22+
## 💻 Priority Queue Implementation in C++
23+
24+
C++ has built-in object [`priority_queue`](https://cplusplus.com/reference/queue/priority_queue/) in `queue` library.
25+
26+
```cpp
27+
#include <iostream>
28+
#include <queue>
29+
30+
int main(){
31+
priority_queue<int> pq;
32+
33+
pq.push(1);
34+
pq.push(5);
35+
pq.push(3);
36+
37+
while (!pq.empty()) {
38+
cout << pq.top() << endl;
39+
pq.pop();
40+
}
41+
42+
return 0;
43+
}
44+
45+
```
46+
47+
## 📈 Complexity Analysis of Priority Queue
48+
49+
50+
| Operation | Complexity |
51+
|-----------------|---------------------|
52+
| push | $O(log N)$ |
53+
| pop | $O(log N)$ |
54+
| top | $O(1)$ |
55+
56+
## 🔗 Further Reading
57+
58+
* [Priority Queue (data structures)](https://en.wikipedia.org/wiki/Priority_queue), wikipedia
59+
* ▶️ [Priority Queue (1/5) Introduction](https://www.youtube.com/watch?v=wptevk0bshY&ab_channel=WilliamFiset), WilliamFiset, 2017
60+
* ▶️ [Priority Queue (2/5) Min Heaps and Max Heaps](https://www.youtube.com/watch?v=HCEr35qpawQ&ab_channel=WilliamFiset), WilliamFiset, 2017
61+
* ▶️ [Priority Queue (3/5) Inserting Elements](https://www.youtube.com/watch?v=QOJ-CmQiXko&list=PLDV1Zeh2NRsB6SWUrDFW2RmDotAfPbeHu&index=16&ab_channel=WilliamFiset), WilliamFiset, 2017
62+
* ▶️ [Priority Queue (4/5) Removing Elements](https://www.youtube.com/watch?v=eVq8CmoC1x8&list=PLDV1Zeh2NRsB6SWUrDFW2RmDotAfPbeHu&index=17&ab_channel=WilliamFiset), WilliamFiset, 2017
63+
* ▶️ [Priority Queue (5/5) Code](https://www.youtube.com/watch?v=GLIRnUhknP0&list=PLDV1Zeh2NRsB6SWUrDFW2RmDotAfPbeHu&index=18&ab_channel=WilliamFiset), WilliamFiset, 2017

concepts/cpp/tree.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ int main()
7272
}
7373
```
7474
75-
## 📈 Complexity Analysis of [Awesome Type]
75+
## 📈 Complexity Analysis of Tree
7676
7777
7878
| Operation | Complexity |

0 commit comments

Comments
 (0)