Differences
This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| courses:cs211:winter2018:journals:patelk:chapter2 [2018/01/28 19:54] – patelk | courses:cs211:winter2018:journals:patelk:chapter2 [2018/01/28 20:18] (current) – [2.5 A More Complex Data Structure: Priority Queues] patelk | ||
|---|---|---|---|
| Line 216: | Line 216: | ||
| **The Problem** | **The Problem** | ||
| - | |||
| * Priority Queue: Elements have a priority value (key), and each time we need to select an element from S, we want to take the one with highest priority. | * Priority Queue: Elements have a priority value (key), and each time we need to select an element from S, we want to take the one with highest priority. | ||
| - Set of elements S | - Set of elements S | ||
| Line 224: | Line 223: | ||
| **A Data Structure for Implementing a Priority Queue** | **A Data Structure for Implementing a Priority Queue** | ||
| + | |||
| * List: have a pointer to the min; adding new elements is easy, but extracting the minimum requires O(n) scan to find the new minimum | * List: have a pointer to the min; adding new elements is easy, but extracting the minimum requires O(n) scan to find the new minimum | ||
| * Sorted Array: binary search to find position of insertion + shifting all elements -> O(nlogn) | * Sorted Array: binary search to find position of insertion + shifting all elements -> O(nlogn) | ||
| * Sorted Doubly Linked List: insertions are O(1), but O(n) to find position of insertion | * Sorted Doubly Linked List: insertions are O(1), but O(n) to find position of insertion | ||
| - | __The Heap__ | + | __The Heap: |
| * For a heap with bound N, we can use an array | * For a heap with bound N, we can use an array | ||
| * H[1] is the root | * H[1] is the root | ||
| - | * leftChild(i) = 2i | + | * leftChild(i) = 2i |
| + | * rightChild(i) = 2i+1 | ||
| **Implementing the Heap Operations** | **Implementing the Heap Operations** | ||
| - | | + | |
| + | | ||
| * Adding an Element: add element to the final position, then perform heapify-up recursively -> **O(logn)** | * Adding an Element: add element to the final position, then perform heapify-up recursively -> **O(logn)** | ||
| {{: | {{: | ||
| - | * Deleting an Element: move element w in position n to position i; key of element w may either be too small or too bit | + | * Deleting an Element: move element w in position n to position i; key of element w may either be too small or too bit -> **O(logn)** |
| * * If key is too small: use heapify-up to reestablish order | * * If key is too small: use heapify-up to reestablish order | ||
| * * If key is too large: use heapify-down to sway the element with one of its children | * * If key is too large: use heapify-down to sway the element with one of its children | ||
| {{: | {{: | ||
| + | |||
| + | **Implementing Priority Queues with Heaps** | ||
| + | * __StartHeap(N): | ||
| + | * __Insert(H, | ||
| + | * __FindMin(H): | ||
| + | * __Delete(H, | ||
| + | * __ExtractMin(H): | ||
| + | |||
| + | ---- | ||
| + | |||
| + | ==== Personal Thoughts ==== | ||
| + | |||
| + | This section was pretty straightforward and easy to follow. I think going over the concepts in class before reading this section of the textbook was helpful in clarifying things that maybe would have been otherwise confusing. I appreciated the summary of the operation run times as these can sometimes be difficult to recall. | ||
| + | Readability: | ||
| + | Interesting: | ||
| + | |||
