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

Post on 21-Dec-2015

220 views 1 download

Tags:

transcript

Priority Queue / HeapDr. 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

P-3Memory Address

P-2Memory Address

P-1Memory Address

PMemory Address

PMemory Address

P-1Memory Address

P-2Memory Address

P-3Memory Address

Stack Queue

Priority Queue

• Operations• Insert (push, enqueue)

• With priority

• Remove (pop, dequeue)• With priority

• isEmpty• inspectFirst (peek)

Priority Queue

• Implementation• Array• Linked list• Heap

Priority Queue

12 12 45 5 23 89

Priority Queue Examples

• A*

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

Priority Queue Examples • Discrete Event Simulator

• Event + time

Heap

• Tree data structure• Often a binary tree

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

67

13 8

3 1

Heap

• Max / Min heap67

13 8

3 1

1

13 8

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

9 10

3 1 4 2

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

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

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

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

Building a Heap

• Build a heap

• Heapify

• HeapSort

Building a Heap

2 4 10 8 11

11 10 8 4 2

11

10 8

4 2

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

Building a Heap

1 963 12 67 211

1

3 6

9 12 67 211

211

63

12 1

211

67

1

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

Building a Heap

BuildMaxHeap(A)

size[A] = length[A]

for i = 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

• Complexity at h

•O(h)• (ch)

•O =

• O(n)

Heap Operations

• Extract Max

• Insert

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

Heap Operations

• O(h)

• O(logn)

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

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

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

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

5 6

1

5

5 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?