36
Priority Queue / Heap Dr. Andrew Wallace PhD BEng(hons) EurIng [email protected]

Priority Queue / Heap Dr. Andrew Wallace PhD BEng(hons) EurIng [email protected]

Embed Size (px)

Citation preview

Page 1: Priority Queue / Heap Dr. Andrew Wallace PhD BEng(hons) EurIng andrew.wallace@cs.umu.se

Priority Queue / HeapDr. Andrew Wallace PhD BEng(hons) EurIng

[email protected]

Page 2: 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

Page 3: Priority Queue / Heap Dr. Andrew Wallace PhD BEng(hons) EurIng andrew.wallace@cs.umu.se

Priority Queue

• A priority queue is a queue with … err … priorities!

• Special case:• Queues• Stacks

P-3Memory Address

P-2Memory Address

P-1Memory Address

PMemory Address

PMemory Address

P-1Memory Address

P-2Memory Address

P-3Memory Address

Stack Queue

Page 4: Priority Queue / Heap Dr. Andrew Wallace PhD BEng(hons) EurIng andrew.wallace@cs.umu.se

Priority Queue

• Operations• Insert (push, enqueue)

• With priority

• Remove (pop, dequeue)• With priority

• isEmpty• inspectFirst (peek)

Page 5: Priority Queue / Heap Dr. Andrew Wallace PhD BEng(hons) EurIng andrew.wallace@cs.umu.se

Priority Queue

• Implementation• Array• Linked list• Heap

Page 6: Priority Queue / Heap Dr. Andrew Wallace PhD BEng(hons) EurIng andrew.wallace@cs.umu.se

Priority Queue

12 12 45 5 23 89

Page 7: Priority Queue / Heap Dr. Andrew Wallace PhD BEng(hons) EurIng andrew.wallace@cs.umu.se

Priority Queue Examples

• A*

• Discrete Event Simulator

Page 8: Priority Queue / Heap Dr. Andrew Wallace PhD BEng(hons) EurIng andrew.wallace@cs.umu.se

Priority Queue Examples

• Path finding in robotics

Page 9: Priority Queue / Heap Dr. Andrew Wallace PhD BEng(hons) EurIng andrew.wallace@cs.umu.se

Priority Queue Examples

• Past path cost function• Know distance from start

• Future path cost function• Heuristics

• Priority queue• Possible route segments

Page 10: Priority Queue / Heap Dr. Andrew Wallace PhD BEng(hons) EurIng andrew.wallace@cs.umu.se

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_neighboursdo 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

Page 11: Priority Queue / Heap Dr. Andrew Wallace PhD BEng(hons) EurIng andrew.wallace@cs.umu.se

Priority Queue Examples • Discrete Event Simulator

• Event + time

Page 12: Priority Queue / Heap Dr. Andrew Wallace PhD BEng(hons) EurIng andrew.wallace@cs.umu.se

Heap

• Tree data structure• Often a binary tree

• Ordered relationship between child and parent• Not between siblings• Heap property

67

13 8

3 1

Page 13: Priority Queue / Heap Dr. Andrew Wallace PhD BEng(hons) EurIng andrew.wallace@cs.umu.se

Heap

• Max / Min heap67

13 8

3 1

1

13 8

30 100

Page 14: Priority Queue / Heap Dr. Andrew Wallace PhD BEng(hons) EurIng andrew.wallace@cs.umu.se

Heap

• Operations• Create• Insert• Delete (max or min)• isEmpty• Extract max or min

Page 15: Priority Queue / Heap Dr. Andrew Wallace PhD BEng(hons) EurIng andrew.wallace@cs.umu.se

Heap Implementation

• Usually as an array

• Binary tree

• Almost complete

67

13 8

9 10

3 1 4 2

67 13 8 9 10 6 5 3 1 4 2 6 5

Page 16: Priority Queue / Heap Dr. Andrew Wallace PhD BEng(hons) EurIng andrew.wallace@cs.umu.se

Priority queue as a heap

• Root at index 1

• Left at index 2

• Right index 3

• For node n, children at 2n and 2n+1

20 10 19 7 8 12 18 1 5 3 6

3

10 19

7 8 12 18

1 5

20

6

Page 17: Priority Queue / Heap Dr. Andrew Wallace PhD BEng(hons) EurIng andrew.wallace@cs.umu.se

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 12 18 1 5 3 6 1717 12

Page 18: Priority Queue / Heap Dr. Andrew Wallace PhD BEng(hons) EurIng andrew.wallace@cs.umu.se

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 10 19 7 8 12 18 1 5 3 6 171719 17

Page 19: Priority Queue / Heap Dr. Andrew Wallace PhD BEng(hons) EurIng andrew.wallace@cs.umu.se

Building a Heap

• Build a heap

• Heapify

• HeapSort

Page 20: Priority Queue / Heap Dr. Andrew Wallace PhD BEng(hons) EurIng andrew.wallace@cs.umu.se

Building a Heap

2 4 10 8 11

11 10 8 4 2

11

10 8

4 2

Page 21: Priority Queue / Heap Dr. Andrew Wallace PhD BEng(hons) EurIng andrew.wallace@cs.umu.se

Building a Heap

• 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!

11

10 8

4 2 1

Page 22: Priority Queue / Heap Dr. Andrew Wallace PhD BEng(hons) EurIng andrew.wallace@cs.umu.se

Building a Heap

1 963 12 67 211

1

3 6

9 12 67 211

211

63

12 1

211

67

1

Page 23: Priority Queue / Heap Dr. Andrew Wallace PhD BEng(hons) EurIng andrew.wallace@cs.umu.se

Building a Heap

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 125

30

Page 24: Priority Queue / Heap Dr. Andrew Wallace PhD BEng(hons) EurIng andrew.wallace@cs.umu.se

Building a Heap

BuildMaxHeap(A)

size[A] = length[A]

for i = to i

MaxHeapify(A, i)

Page 25: Priority Queue / Heap Dr. Andrew Wallace PhD BEng(hons) EurIng andrew.wallace@cs.umu.se

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

Page 26: Priority Queue / Heap Dr. Andrew Wallace PhD BEng(hons) EurIng andrew.wallace@cs.umu.se

Building a Heap

• Complexity at h

•O(h)• (ch)

•O =

• O(n)

Page 27: Priority Queue / Heap Dr. Andrew Wallace PhD BEng(hons) EurIng andrew.wallace@cs.umu.se

Heap Operations

• Extract Max

• Insert

Page 28: Priority Queue / Heap Dr. Andrew Wallace PhD BEng(hons) EurIng andrew.wallace@cs.umu.se

Heap Operations

ExtractMax(A)

max A[1]

A[1] A[A.heap_size]

A.heap_size A.heap_size – 1

MaxHeapify(A, 1)

return max

10

8 6

2 1 5 3

10

38

3

Page 29: Priority Queue / Heap Dr. Andrew Wallace PhD BEng(hons) EurIng andrew.wallace@cs.umu.se

Heap Operations

• O(h)

• O(logn)

Page 30: Priority Queue / Heap Dr. Andrew Wallace PhD BEng(hons) EurIng andrew.wallace@cs.umu.se

Heap Operations

• Insert

• Insert at the end and copy upwards till heap property is satisfied

• Worst case O(logn)

10

8 6

5 7 3 2

12

Page 31: Priority Queue / Heap Dr. Andrew Wallace PhD BEng(hons) EurIng andrew.wallace@cs.umu.se

Heap Operations

HeapSort(A, n)

BuildMaxHeap(A)

for i A.size to 2

swap(A[A.last], A[i])

MaxHeapify(A, i -1)

10

8 5

1 210

28

2

1

8

8

5

15

1

5

2

12

1

21 10

Page 32: Priority Queue / Heap Dr. Andrew Wallace PhD BEng(hons) EurIng andrew.wallace@cs.umu.se

Heap Operations

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)

O(logn)

23

8 12

1 3 6 7

Page 33: Priority Queue / Heap Dr. Andrew Wallace PhD BEng(hons) EurIng andrew.wallace@cs.umu.se

Variations of Heap

• Fibonacci heaps

• Binomial heaps

Page 34: Priority Queue / Heap Dr. Andrew Wallace PhD BEng(hons) EurIng andrew.wallace@cs.umu.se

Variations of Heap

• Fibonacci heaps

• Set of trees

• Minimum trees

• Fibonacci numbers used in the run time analysis

5 6

1

5

5 5

5

Page 35: Priority Queue / Heap Dr. Andrew Wallace PhD BEng(hons) EurIng andrew.wallace@cs.umu.se

Variations of Heap

• Fibonacci heaps have no predefined order

• However, number of children are low

• A child is root of a subtree at k

Page 36: Priority Queue / Heap Dr. Andrew Wallace PhD BEng(hons) EurIng andrew.wallace@cs.umu.se

Questions?