Dr. Andrew Wallace PhD BEng(hons) EurIng andrew.wallace@cs.umu.se Priority Queue / Heap Dr. Andrew Wallace PhD BEng(hons) EurIng andrew.wallace@cs.umu.se
Overview Priority Queue Priority Queue Examples Heap Heap Implementation Building a Heap Heap Operations Variations of Heap
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
Priority Queue Operations Insert (push, enqueue) Remove (pop, dequeue) With priority Remove (pop, dequeue) isEmpty inspectFirst (peek)
Priority Queue Implementation Array Linked list Heap
Priority Queue 12 12 45 5 23 89
Priority Queue Examples Discrete Event Simulator
Priority Queue Examples Path finding in robotics
Priority Queue Examples Past path cost function Know distance from start Future path cost function Heuristics Priority queue Possible route segments
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
Priority Queue Examples Discrete Event Simulator Event + time
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
Heap 67 1 Max / Min heap 13 8 13 8 3 1 30 100
Heap Operations Create Insert Delete (max or min) isEmpty Extract max or min
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
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
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
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
Building a Heap Build a heap Heapify HeapSort
Building a Heap 11 10 8 2 4 10 8 11 4 2 11 10 8 4 2
Building a Heap In a binary tree, where do the leaves start? 11 In a binary tree, where do the leaves start? 𝒏 𝟐 + 1 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
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
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
Building a Heap BuildMaxHeap(A) size[A] = length[A] for i = length[A] to i MaxHeapify(A, i)
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
Building a Heap 𝒏 𝟐 𝒉+𝟏 O(h) 𝒉=𝟎 𝒍𝒐𝒈𝒏 𝒏 𝟐 𝒉+𝟏 (ch) 𝒄𝒏 𝟐 𝒉=𝟎 𝒍𝒐𝒈𝒏 𝒉 𝟐𝒉 Complexity at h 𝒏 𝟐 𝒉+𝟏 O(h) 𝒉=𝟎 𝒍𝒐𝒈𝒏 𝒏 𝟐 𝒉+𝟏 (ch) 𝒄𝒏 𝟐 𝒉=𝟎 𝒍𝒐𝒈𝒏 𝒉 𝟐𝒉 O 𝒄𝒏 𝟐 𝒉=𝟎 ∞ 𝒉 𝟐𝒉 = O(n)
Heap Operations Extract Max Insert
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
Heap Operations O(h) O(logn)
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
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
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
Variations of Heap Fibonacci heaps Binomial heaps
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
Variations of Heap Fibonacci heaps have no predefined order However, number of children are low A child is root of a subtree at k
Questions?