editing lecture 24
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
# Lecture 22 --- Priority Queues
|
||||
|
||||
<!--
|
||||
## Test 3 Information
|
||||
|
||||
- Test 3 will be held Thursday, November 16th, 2023 from 6-7:50pm. (Rooms will be different this time, Carnegie 113, LOW 4050, LOW 3051, Ricketts 211, Sage 5101, Sage 5510. AE 216 for accommodations.)
|
||||
@@ -12,17 +13,22 @@ you about your accommodations for the test.
|
||||
- OPTIONAL: you are allowed two physical pieces of 8.5x11” paper, that’s four “sides”. We will not collect these electronically and we will not pre-print them, you will have to bring these notes pages yourself if you want them. We will check at the start of the exam that you do not have more than two pieces of paper for your notes!
|
||||
- All students must bring their Rensselaer photo ID card.
|
||||
- Bring pencil(s) & eraser (pens are ok, but not recommended).
|
||||
-->
|
||||
|
||||
## Today’s Lecture
|
||||
|
||||
<!--
|
||||
- Range Based for Loop
|
||||
- Using STL’s for_each
|
||||
-->
|
||||
|
||||
- Definition of a Binary Heap
|
||||
- What’s a Priority Queue?
|
||||
- A Priority Queue as a Heap
|
||||
- A Heap as a Vector
|
||||
|
||||
## 22.1 Range Based for Loop
|
||||
<!--
|
||||
## 24.1 Range Based for Loop
|
||||
|
||||
- Executes a for loop over a range.
|
||||
|
||||
@@ -67,7 +73,7 @@ for (std::vector<int>& vec1 : result ) {
|
||||
}
|
||||
```
|
||||
|
||||
## 22.2 Using STL’s for_each
|
||||
## 24.2 Using STL’s for_each
|
||||
|
||||
- First, here’s a tiny helper function:
|
||||
```cpp
|
||||
@@ -100,15 +106,16 @@ std::for_each(my_data.begin(), my_data.end(), float_print);
|
||||
std::for_each(my_data.begin(), my_data.end(), [](float f){ std::cout << f << std::end; });
|
||||
```
|
||||
Lambda is new to the C++ language (part of C++11). But lambda is a core piece of many classic, older programming languages including Lisp and Scheme. Python lambdas and Perl anonymous subroutines are similar. (In fact lambda dates back to the 1930’s, before the first computers were built!) You’ll learn more about lambda more in later courses like CSCI 4430 Programming Languages!
|
||||
-->
|
||||
|
||||
## 22.3 Priority Queue
|
||||
## 24.1 Priority Queue
|
||||
|
||||
- Priority queues are used in prioritizing operations. Examples include a personal “to do” list, what order to do homework assignments, jobs on a shop floor, packet routing in a network, scheduling in an operating system, or events in a simulation.
|
||||
- Among the data structures we have studied, their interface is most similar to a queue, including the idea of a front or top and a tail or a back.
|
||||
- Each item is stored in a priority queue using an associated “priority” and therefore, the top item is the one with the lowest value of the priority score. The tail or back is never accessed through the public interface to a priority queue.
|
||||
- The main operations are insert or push, and pop (or delete_min).
|
||||
|
||||
## 22.4 Some Data Structure Options for Implementing a Priority Queue
|
||||
## 24.2 Some Data Structure Options for Implementing a Priority Queue
|
||||
|
||||
- Vector or list, either sorted or unsorted
|
||||
|
||||
@@ -120,7 +127,7 @@ Lambda is new to the C++ language (part of C++11). But lambda is a core piece of
|
||||
– This costs logarithmic time in the average case (and in the worst case as well if balancing is used).
|
||||
- The latter is the better solution, but we would like to improve upon it — for example, it might be more natural if the minimum priority value were stored at the root.
|
||||
|
||||
## 22.5 Definition: Binary Heaps
|
||||
## 24.3 Definition: Binary Heaps
|
||||
|
||||
- A binary heap is a complete binary tree such that at each internal node, p, the value stored is less than the value stored at either of p’s children.
|
||||
|
||||
@@ -128,13 +135,13 @@ Lambda is new to the C++ language (part of C++11). But lambda is a core piece of
|
||||
- Binary heaps will be drawn as binary trees, but implemented using vectors!
|
||||
- Alternatively, the heap could be organized such that the value stored at each internal node is greater than the values at its children.
|
||||
|
||||
## 22.6 Exercise: Drawing Binary Heaps
|
||||
## 24.4 Exercise: Drawing Binary Heaps
|
||||
|
||||
- Draw two different binary heaps with these values: 52 13 48 7 32 40 18 25 4
|
||||
|
||||
- Draw several other trees with these values which are not binary heaps.
|
||||
|
||||
## 22.7 STL priority_queue
|
||||
## 24.5 STL priority_queue
|
||||
|
||||
- The standard library (STL) priority_queue is implemented as a binary heap.
|
||||
- The STL priority_queue is a max heap.
|
||||
@@ -201,7 +208,7 @@ int main() {
|
||||
1 3 3 4 5
|
||||
```
|
||||
|
||||
## 22.8 Leetcode Exercises
|
||||
## 24.6 Leetcode Exercises
|
||||
|
||||
- [Leetcode problem 215: Kth Largest Element in an Array](https://leetcode.com/problems/kth-largest-element-in-an-array/). Solution: [p215_kth_largest_element.cpp](../../leetcode/p215_kth_largest_element.cpp).
|
||||
- [Leetcode problem 373: Find K Pairs with Smallest Sums](https://leetcode.com/problems/find-k-pairs-with-smallest-sums/). Solution: [p373_k_pairs_smallest_sums.cpp](../../leetcode/p373_k_pairs_smallest_sums.cpp).
|
||||
|
||||
Reference in New Issue
Block a user