Download presentation
1
Dr. Andrew Wallace PhD BEng(hons) EurIng andrew.wallace@cs.umu.se
Priority Queue / Heap Dr. Andrew Wallace PhD BEng(hons) EurIng
2
Overview Priority Queue Priority Queue Examples Heap
Heap Implementation Building a Heap Heap Operations Variations of Heap
3
Priority Queue A priority queue is a queue with … err … priorities!
Special case: Queues Stacks Memory Address Memory Address P P-3 Memory Address Memory Address P-1 P-2 Memory Address Memory Address P-2 P-1 Memory Address Memory Address P-3 P Queue Stack
4
Priority Queue Operations Insert (push, enqueue) Remove (pop, dequeue)
With priority Remove (pop, dequeue) isEmpty inspectFirst (peek)
5
Priority Queue Implementation Array Linked list Heap
6
Priority Queue 12 12 45 5 23 89
7
Priority Queue Examples
Discrete Event Simulator
8
Priority Queue Examples
Path finding in robotics
9
Priority Queue Examples
Past path cost function Know distance from start Future path cost function Heuristics Priority queue Possible route segments
10
Priority Queue Examples
A*(G, start) create priority queue F create came_from create cost_so_far F.put(start) came_from[start] ← NULL cost_so_far ← 0 While not F.empty() n ← F.get() if n = goal return for next ← G.number_of_neighbours do new_cost ← cost_so_far[n] + G.cost(n, next) if next not in cost_so_far or new_cost < cost_so_far[next] cost_so_far[next] = new_cost priority = new_cost + Heuristic(goal, next) F.put(next, priority) came_from[next] ← current Return came_from, cost_so_far
11
Priority Queue Examples
Discrete Event Simulator Event + time
12
Heap Tree data structure Ordered relationship between child and parent
67 Tree data structure Often a binary tree Ordered relationship between child and parent Not between siblings Heap property 13 8 3 1
13
Heap 67 1 Max / Min heap 13 8 13 8 3 1 30 100
14
Heap Operations Create Insert Delete (max or min) isEmpty
Extract max or min
15
Heap Implementation Usually as an array Binary tree Almost complete 67
13 8 67 13 8 9 10 6 5 3 1 4 2 9 10 6 5 3 1 4 2
16
Priority queue as a heap
20 Root at index 1 Left at index 2 Right index 3 For node n, children at 2n and 2n+1 10 19 7 8 12 18 1 5 3 6 20 10 19 7 8 12 18 1 5 3 6
17
Priority queue as a heap
Insert Add at end New leaf on the tree Fix the priorities Compare with parent If smaller, swap Continue until priorities are fixed 20 10 19 7 8 17 12 18 1 5 3 6 12 17
18
Priority queue as a heap
RemoveMax Return data at index 1 Copy end of array to index 1 Compare and swap to restore priorities 20 17 19 10 17 19 7 8 12 18 1 5 3 6 17
19
Building a Heap Build a heap Heapify HeapSort
20
Building a Heap 11 10 8 2 4 10 8 11 4 2 11 10 8 4 2
21
Building a Heap In a binary tree, where do the leaves start?
11 In a binary tree, where do the leaves start? 𝒏 𝟐 to n n = 5, leaves start at 3 n = 6, leaves start at 4 Every leaf is a heap with one node! 10 8 4 2 1
22
Building a Heap 1 3 6 9 12 67 211 211 1 12 3 211 1 6 67 9 3 12 67 1 211 6
23
Building a Heap 30 5 MaxHeapify(A, i) l ← 2i r ← 2i+1 if l <= A.size and A[l] > A[i] then largest ← l else largest ← i if r <= A.size and A[r] > A[largest] then largest = r if largest not = i then swap(A[i], A[largest]) MaxHeapify(A, largest) 5 30 12
24
Building a Heap BuildMaxHeap(A) size[A] = length[A] for i = length[A] to i MaxHeapify(A, i)
25
Building a Heap 𝒏 𝟐 𝒉+𝟏 Complexity of BuildHeap
MaxHeapify is applied to all nodes at a given level How many nodes at each level? 𝒏 𝟐 𝒉+𝟏 Height h = 0, n= 7, number of nodes = 4 Height h = 1, n= 7, number of nodes = 2
26
Building a Heap 𝒏 𝟐 𝒉+𝟏 O(h) 𝒉=𝟎 𝒍𝒐𝒈𝒏 𝒏 𝟐 𝒉+𝟏 (ch) 𝒄𝒏 𝟐 𝒉=𝟎 𝒍𝒐𝒈𝒏 𝒉 𝟐𝒉
Complexity at h 𝒏 𝟐 𝒉+𝟏 O(h) 𝒉=𝟎 𝒍𝒐𝒈𝒏 𝒏 𝟐 𝒉+𝟏 (ch) 𝒄𝒏 𝟐 𝒉=𝟎 𝒍𝒐𝒈𝒏 𝒉 𝟐𝒉 O 𝒄𝒏 𝟐 𝒉=𝟎 ∞ 𝒉 𝟐𝒉 = O(n)
27
Heap Operations Extract Max Insert
28
Heap Operations 3 10 8 ExtractMax(A) max ← A[1] A[1] ← A[A.heap_size] A.heap_size ← A.heap_size – 1 MaxHeapify(A, 1) return max 8 3 6 2 1 5 3 10
29
Heap Operations O(h) O(logn)
30
Heap Operations Insert
10 8 6 Insert Insert at the end and copy upwards till heap property is satisfied Worst case O(logn) 5 7 3 2 12
31
Heap Operations 1 8 2 5 1 2 10 1 HeapSort(A, n) BuildMaxHeap(A) for i ← A.size to 2 swap(A[A.last], A[i]) MaxHeapify(A, i -1) 8 2 1 2 1 5 5 8 1 10 2 1 2 5 8 10
32
Heap Operations O(logn)
Heap Increase Key(A, i, key) A[i] ← key while i > 1 and A.parent(i) < A[i] do swap(A[i], A.parent(i)) i ← parent(i) 23 8 12 1 3 6 7
33
Variations of Heap Fibonacci heaps Binomial heaps
34
Variations of Heap Fibonacci heaps Set of trees Minimum trees
Fibonacci numbers used in the run time analysis 1 5 5 5 6 5 5
35
Variations of Heap Fibonacci heaps have no predefined order
However, number of children are low A child is root of a subtree at k
36
Questions?
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.