Priority Queues 3rd October 2019 Priority Queues Binary heaps - - PowerPoint PPT Presentation

priority queues
SMART_READER_LITE
LIVE PREVIEW

Priority Queues 3rd October 2019 Priority Queues Binary heaps - - PowerPoint PPT Presentation

Priority Queues 3rd October 2019 Priority Queues Binary heaps Leftist heaps Binomial heaps Fibonacci heaps Priority queues are important in, among other things, operating systems (process control in multitasking systems),


slide-1
SLIDE 1

Priority Queues

3rd October 2019

slide-2
SLIDE 2

Priority Queues

  • Binary heaps
  • Leftist heaps
  • Binomial heaps
  • Fibonacci heaps

Priority queues are important in, among other things, operating systems (process control in multitasking systems), search algorithms (A, A*, D*, etc.), and simulation.

slide-3
SLIDE 3

Priority Queues

Priority queues are data structures that hold elements with some kind of priority (key) in a queue-like structure, implementing the following operations:

  • insert() – Inserting an element into the queue.
  • deleteMin() – Removing the element with the highest priority.

And maybe also:

  • buildHeap() – Build a queue from a set (>1) of elements.
  • increaseKey()/DecreaseKey() – Change priority.
  • delete() – Removing an element from the queue.
  • merge() – Merge two queues.
slide-4
SLIDE 4

Priority Queues

An unsorted linked list can be used. insert() inserts an element at the head of the list (O(1)), and deleteMin() searches the list for the element with the highest priority and removes it (O(n)). A sorted list can also be used (reversed running times). – Not very efficient implementations. To make an efficient priority queue, it is enough to keeps the elements “almost sorted”.

slide-5
SLIDE 5

A binary heap is organized as a complete binary tree. (All levels are full, except possibly the last.) In a binary heap the element in the root must have a key less than or equal to the key of its children, in addition each sub-tree must be a binary heap.

a b c d e f g h i j

1 2 3 4 5 6 7 8 9 10 11 12 13

1 2 3 4

1 2 3 4 ëi / 2û

a b c d e h i j f g

Binary heaps

2i 2i+1

slide-6
SLIDE 6

13 21 16 24 31 65 26 32 19 68

1 2 3 4 insert(14)

13 21 16 24 31 19 68 65 26 32 14

1 2 3 4 5 6 7 8 9 10 11 12 13

1 2 3 4

14

Binary heaps

slide-7
SLIDE 7

13 14 16 23 21 65 26 32 19 68

1 2 3 4 insert(14)

13 14 16 24 21 19 68 65 26 32 31

1 2 3 4 5 6 7 8 9 10 11 12 13

1 2 3 4

31

”percolateUp()”

Binary heaps

slide-8
SLIDE 8

14 16 19 21 65 26 32 19 68

1 2 3 4 deleteMin()

14 16 19 21 19 68 65 26 32 31

1 2 3 4 5 6 7 8 9 10 11 12 13

1 2 3 4

31

Binary heaps

slide-9
SLIDE 9

31 14 16 19 21 65 26 32 19 68

1 2 3 4 deleteMin()

31 14 16 19 21 19 68 65 26 32

1 2 3 4 5 6 7 8 9 10 11 12 13

1 2 3 4

Binary heaps

slide-10
SLIDE 10

14 19 16 26 21 65 31 32 19 68

1 2 3 4 deleteMin()

14 19 16 19 21 26 68 65 31 32

1 2 3 4 5 6 7 8 9 10 11 12 13

1 2 3 4

”percolateDown()”

Binary heaps

slide-11
SLIDE 11

worst case average insert() O(log N) O(1) deleteMin() O(log N) O(log N) buildHeap() O(N) (Insert elements into the array unsorted, and run percolateDown() on each root in the resulting heap (the tree), bottom up) (The sum of the heights of a binary tree with N nodes is O(N).) merge() O(N) (N = number of elements)

Binary heaps

slide-12
SLIDE 12

To implement an efficient merge(), we move away from arrays, and implement so-called leftist heaps as pure trees. The idea is to make the heap (the tree) as skewed as possible, and do all the work on a short (right) branch, leaving the long (left) branch untouched. A leftist heap is still a binary tree with the heap structure (key in root is lower than key in children), but with an extra skewness requirement. For all nodes X in our tree, we define the null-path-length(X) as the distance from X to a descendant with less than two children (i.e. 0 or 1). The skewness requirement is that for every node the null path length of its left child be at least as large as the null path length of the right child. For the empty tree we define the null-path-length to be -1, as a special case.

Leftist heaps

slide-13
SLIDE 13

Leftist heaps

1

NOT LEFTIST LEFTIST

1 1 1 1

slide-14
SLIDE 14

Leftist heaps

10 8 21 14 23 17 26 12 7 18 24 33 37 18

merge()

3 6

slide-15
SLIDE 15

Leftist heaps

10 8 21 14 23 17 26 12 7 18 24 33 37 18

merge()

3 6

slide-16
SLIDE 16

Leftist heaps

10 8 21 14 23 17 26 12 7 18 24 33 37 18

merge()

3 6 10 8 21 14 23 17 26 3 12 7 18 24 33 37 18 6

slide-17
SLIDE 17

Leftist heaps

10 8 21 14 23 17 26 12 7 18 24 33 37 18

merge()

3 6 10 8 21 14 23 17 26 3 12 7 18 24 33 37 18 6

Flip L/R if not leftist

slide-18
SLIDE 18

Leftist heaps

10 8 21 14 23 17 26 12 7 18 24 33 37 18

merge()

3 6 10 8 21 14 23 17 26 3 12 7 18 24 33 37 18 6

Flip L/R if not leftist

slide-19
SLIDE 19

Leftist heaps

10 8 21 14 23 17 26 12 7 18 24 33 37 18

merge()

3 6 10 8 21 14 23 17 26 3 12 7 18 24 33 37 18 6

Flip L/R if not leftist

slide-20
SLIDE 20

Leftist heaps

10 8 21 14 23 17 26 12 7 18 24 33 37 18

merge()

3 6 8 17 26 3 12 7 18 24 33 37 18 6 10 21 14 23

slide-21
SLIDE 21

Leftist heaps

5 3 7 6 10 5 3 7 6 10

deleteMin() insert(3) merge()

11

merge()

1

slide-22
SLIDE 22

Leftist heaps

worst case merge() O(log N) insert() O(log N) deleteMin() O(log N) buildHeap() O(N) (N = number of elements) In a leftist heap with N nodes, the right path is at most ëlog (N+1)û long.

slide-23
SLIDE 23

Leftist heaps: merge(), insert() and deleteMin() in O(log N) time w.c. Binary heaps: insert() in O(1) time on average. Binomial heaps merge(), insert() og deleteMin() in O(log N) time w.c. insert() O(1) time on average Binomial heaps are collections of trees (sometimes called a forest), each tree a heap.

Binomial heaps

slide-24
SLIDE 24

Binomial trees B0

Binomial heaps

slide-25
SLIDE 25

Binomial trees B0 B1

Binomial heaps

slide-26
SLIDE 26

Binomial trees B0 B1 B2

Binomial heaps

slide-27
SLIDE 27

Binomial trees B0 B1 B2 B3

Binomial heaps

slide-28
SLIDE 28

Binomial trees B0 B1 B2 B3 B4

Binomial heaps

slide-29
SLIDE 29

Binomial trees B0 B1 B2 B3 B4 Bi = 2 x Bi-1, root of one tree connected as a child of the root of the other tree. A tree of height k has: 2k nodes in total, nodes on level d.

÷ ø ö ç è æ d k

Binomial heaps

slide-30
SLIDE 30

Binomial heap

16 18 12 21 24 65

Binomial heaps

Maximum one tree of each size: 6 elements: 6 binary = 011 (0+2+4) B0 B1 B2 X

slide-31
SLIDE 31

Binomial heap

16 18 12 21 24 65

The length of the root list in a heap of N elements is O(log N). (Doubly linked, circular list.)

Binomial heaps

Maximum one tree of each size: 6 elements: 6 binary = 011 (0+2+4) B0 B1 B2 X

slide-32
SLIDE 32

merge()

16 18 12 21 24 65 14 26 23 51 24 65 13

Binomial heaps

slide-33
SLIDE 33

merge()

16 18 12 21 24 65 14 26 23 51 24 65 13

Binomial heaps

slide-34
SLIDE 34

merge()

16 18 12 21 24 65 14 26 23 51 24 65 13 13

Binomial heaps

slide-35
SLIDE 35

merge()

16 18 12 21 24 65 14 26 23 51 24 65 13 13 16 18 14 26

Binomial heaps

slide-36
SLIDE 36

merge()

16 18 12 21 24 65 14 26 23 51 24 65 13 13

Binomial heaps

The trees (the root list) is kept sorted on height.

16 18 14 26 12 21 24 65 24 65 23 51

slide-37
SLIDE 37

deleteMin()

13 16 18 14 26 12 21 24 65 23 51 24 65

Binomial heaps

slide-38
SLIDE 38

deleteMin()

13 16 18 14 26 21 24 65 23 51 24 65

merge()

13 16 18 14 26 12 21 24 65 23 51 24 65

Binomial heaps

slide-39
SLIDE 39

worst case average case merge() O(log N) O(log N) insert() O(log N) O(1) deleteMin() O(log N) O(log N) buildHeap() O(N) O(N) (Run N insert() on an initially empty heap.) (N = number of elements)

Binomial heaps

slide-40
SLIDE 40

Implementation

13 16 18 14 26 12 21 24 65 23 51 24 65

Doubly linked, circular lists

Binomial heaps

slide-41
SLIDE 41

Implementation

13 16 18 14 26 12 21 24 65 23 51 24 65 23 61 27 88

Doubly linked, circular lists

Binomial heaps

slide-42
SLIDE 42

Implementation

13 16 18 14 26 12 21 24 65 23 51 24 65 23 61 27 88

Doubly linked, circular lists

Binomial heaps

slide-43
SLIDE 43

Very elegant, and in theory efficient, way to implement heaps: Most operations have O(1) amortized running time. (Fredman & Tarjan ’87) insert(), decreaseKey() and merge() O(1) amortized time deleteMin() O(log N) amortized time Combines elements from leftist heaps and binomial heaps. A bit complicated to implement, and certain hidden constants are a bit high. Best suited when there are few deleteMin() compared to the other operations. The data structure was developed for a shortest path algorithm (with many decreaseKey() operations), also used in spanning tree algorithms.

Fibonacci heaps

slide-44
SLIDE 44

We include a smart decreaseKey() method from leftist heaps.

2 11 12 17 18 4 5 8 6 11 9 10 18 31 21 15

Fibonacci heaps

slide-45
SLIDE 45

We include a smart decreaseKey() method from leftist heaps.

2 11 12 17 18 4 5 8 6 11 10 18 31 21 15

Fibonacci heaps

slide-46
SLIDE 46

We include a smart decreaseKey() method from leftist heaps.

2 11 12 17 18 4 5 8 6 11 10 18 31 21 15

Leftist Ikke leftist

Fibonacci heaps

slide-47
SLIDE 47

We include a smart decreaseKey() method from leftist heaps.

2 11 12 17 18 4 5 8 6 11 10 18 31 21 15

Leftist Ikke leftist

Fibonacci heaps

slide-48
SLIDE 48

We include a smart decreaseKey() method from leftist heaps.

2 4 12 17 18 11 5 8 6 11 10 18 31 21 15

Leftist Leftist

Fibonacci heaps

slide-49
SLIDE 49

We include a smart decreaseKey() method from leftist heaps.

2 4 12 17 18 11 5 8 6 11 10 18 31 21 15

Fibonacci heaps

slide-50
SLIDE 50

We include a smart decreaseKey() method from leftist heaps. The method must be modified a bit, as we wish to use trees that are binomial trees, or partial binomial trees.

  • Nodes are marked the first time child is removed.
  • The second time a node gets a child removed, it is cut off, and becomes the

root of a separate tree

38 26 35 24 46 7 23 17 30 18 39 21 52 41

min

Fibonacci heaps

slide-51
SLIDE 51

We include a smart decreaseKey() method from leftist heaps. The method must be modified a bit, as we wish to use trees that are binomial trees, or partial binomial trees.

  • Nodes are marked the first time child is removed.
  • The second time a node gets a child removed, it is cut off, and becomes the

root of a separate tree

38 26 35 24 5 7 23 17 30 18 39 21 52 41

min

Fibonacci heaps

slide-52
SLIDE 52

We include a smart decreaseKey() method from leftist heaps. The method must be modified a bit, as we wish to use trees that are binomial trees, or partial binomial trees.

  • Nodes are marked the first time child is removed.
  • The second time a node gets a child removed, it is cut off, and becomes the

root of a separate tree

38 26 35 24 5 7 23 17 30 18 39 21 52 41

min

Fibonacci heaps

slide-53
SLIDE 53

We include a smart decreaseKey() method from leftist heaps. The method must be modified a bit, as we wish to use trees that are binomial trees, or partial binomial trees.

  • Nodes are marked the first time child is removed.
  • The second time a node gets a child removed, it is cut off, and becomes the

root of a separate tree

38 13 35 24 5 7 23 17 30 18 39 21 52 41

Fibonacci heaps

min

slide-54
SLIDE 54

We include a smart decreaseKey() method from leftist heaps. The method must be modified a bit, as we wish to use trees that are binomial trees, or partial binomial trees.

  • Nodes are marked the first time child is removed.
  • The second time a node gets a child removed, it is cut off, and becomes the

root of a separate tree

38 13 35 24 5 7 23 17 30 18 39 21 52 41

Fibonacci heaps

min

slide-55
SLIDE 55

We include a smart decreaseKey() method from leftist heaps. The method must be modified a bit, as we wish to use trees that are binomial trees, or partial binomial trees.

  • Nodes are marked the first time child is removed.
  • The second time a node gets a child removed, it is cut off, and becomes the

root of a separate tree

38 13 35 24 5 7 23 17 30 18 39 21 52 41

Fibonacci heaps

min

slide-56
SLIDE 56

We use lazy merging / lazy binomial queue.

18 7 8 5 8 6 11 8 9 4 9 6 32

Fibonacci heaps

slide-57
SLIDE 57

We use lazy merging / lazy binomial queue.

18 7 8 5 8 6 11 8 9 4 9 6 32 18 7 8 5 8 6 11 8 9 4 9 6 32

Fibonacci heaps

slide-58
SLIDE 58

The problem with our decreaseKey()-method and lazy merging is that we have to clean up afterwards. This is done in by the deleteMin()-method which becomes expensive (O(log N) amortized time): All trees are examined, we start with the smallest, and merge two and two, so that we get at most one tree of each size. Each root has a number of children – this is used as the size of the tree. (Recall how we construct binomial trees, and that they may be partial as a result of decreaseKey()’s) The trees are put in lists, one per size, and we begin merging, starting with the smallest.

Fibonacci heaps

slide-59
SLIDE 59

Amortized time insert() O(1) decreaseKey() O(1) merge() O(1) deleteMin() O(log N) buildHeap() O(N) (Run N insert() on an initially empty heap.) (N = number of elements)

Fibonacci heaps