priority queues - universitetet i oslo · fibonacci heaps very elegant, and in theory efficient,...

58
Priority Queues • Binary heaper • Leftist heaper • Binomial heaps • Fibonacci heaps Priority queues are important in, among other things, operating systems (process control i multitasking systems) search algorithms (A A* D* etc ) (process control i multitasking systems), search algorithms (A, A , D , etc.), and simulation.

Upload: others

Post on 09-Jul-2020

0 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Priority Queues - Universitetet i oslo · Fibonacci heaps Very elegant, and in theory efficient, way to implement heaps: Most operastions have O(1) amortized running time.(Fredman

Priority Queues

• Binary heaper• Leftist heaper• Binomial heapsp• Fibonacci heaps

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

Page 2: Priority Queues - Universitetet i oslo · Fibonacci heaps Very elegant, and in theory efficient, way to implement heaps: Most operastions have O(1) amortized running time.(Fredman

Priority QueuesPriority queues are data stuctures 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:

b ild () B ild f t ( 1) f l t• 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.

Page 3: Priority Queues - Universitetet i oslo · Fibonacci heaps Very elegant, and in theory efficient, way to implement heaps: Most operastions have O(1) amortized running time.(Fredman

Priority QueuesAn 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))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”almost sorted .

Page 4: Priority Queues - Universitetet i oslo · Fibonacci heaps Very elegant, and in theory efficient, way to implement heaps: Most operastions have O(1) amortized running time.(Fredman

Binary heapsA binary heap is organized as a complete binary tree. (All leves are full, execptpossibly 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.

1a

2

3⎣i / 2⎦ b c

d e f g2i 2i+14h i j

a b c d e f g h i j

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

1 2 3 4

Page 5: Priority Queues - Universitetet i oslo · Fibonacci heaps Very elegant, and in theory efficient, way to implement heaps: Most operastions have O(1) amortized running time.(Fredman

Binary heaps

13 1insert(14)

21 16 2

24 31 19 68 3

65 26 32 414

13 21 16 24 31 19 68 65 26 32 14

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

1 2 3 4

Page 6: Priority Queues - Universitetet i oslo · Fibonacci heaps Very elegant, and in theory efficient, way to implement heaps: Most operastions have O(1) amortized running time.(Fredman

Binary heaps

13 1insert(14)

14 16 2

23 21 19 68 3

65 26 32 431

13 14 16 24 21 19 68 65 26 32 31

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

1 2 3 4

”percolateUp()”

Page 7: Priority Queues - Universitetet i oslo · Fibonacci heaps Very elegant, and in theory efficient, way to implement heaps: Most operastions have O(1) amortized running time.(Fredman

Binary heaps

1deleteMin()

14 16 2

19 21 19 68 3

65 26 32 431

14 16 19 21 19 68 65 26 32 31

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

1 2 3 4

Page 8: Priority Queues - Universitetet i oslo · Fibonacci heaps Very elegant, and in theory efficient, way to implement heaps: Most operastions have O(1) amortized running time.(Fredman

Binary heaps

31 1deleteMin()

14 16 2

19 21 19 68 3

65 26 32 4

31 14 16 19 21 19 68 65 26 32

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

1 2 3 4

Page 9: Priority Queues - Universitetet i oslo · Fibonacci heaps Very elegant, and in theory efficient, way to implement heaps: Most operastions have O(1) amortized running time.(Fredman

Binary heaps

14 1deleteMin()

19 16 2

26 21 19 68 3

65 31 32 4

14 19 16 19 21 26 68 65 31 32

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

1 2 3 4

”percolateDown()”

Page 10: Priority Queues - Universitetet i oslo · Fibonacci heaps Very elegant, and in theory efficient, way to implement heaps: Most operastions have O(1) amortized running time.(Fredman

Binary heapsworst 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)(N = number of elements)

Page 11: Priority Queues - Universitetet i oslo · Fibonacci heaps Very elegant, and in theory efficient, way to implement heaps: Most operastions have O(1) amortized running time.(Fredman

Leftist heaps

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 node without 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.

Page 12: Priority Queues - Universitetet i oslo · Fibonacci heaps Very elegant, and in theory efficient, way to implement heaps: Most operastions have O(1) amortized running time.(Fredman

Leftist heaps

1

1 0

1

0 0

0 LEFTIST

1 0

1

0 1

0 0

NOT LEFTIST

Page 13: Priority Queues - Universitetet i oslo · Fibonacci heaps Very elegant, and in theory efficient, way to implement heaps: Most operastions have O(1) amortized running time.(Fredman

Leftist heapsmerge() 3 6

10 8 12 7

21 14 17 18 24 37 18

23 26 33

Page 14: Priority Queues - Universitetet i oslo · Fibonacci heaps Very elegant, and in theory efficient, way to implement heaps: Most operastions have O(1) amortized running time.(Fredman

Leftist heapsmerge() 3 6

10 8 12 7

21 14 17 18 24 37 18

23 26 33

Page 15: Priority Queues - Universitetet i oslo · Fibonacci heaps Very elegant, and in theory efficient, way to implement heaps: Most operastions have O(1) amortized running time.(Fredman

Leftist heapsmerge() 3 6

10 8 12 7

21 14 17 18 24 37 18

23 26 33

63

7

18

6

108

12

18 24

3721 14

2317

18 24

33

23

26

Page 16: Priority Queues - Universitetet i oslo · Fibonacci heaps Very elegant, and in theory efficient, way to implement heaps: Most operastions have O(1) amortized running time.(Fredman

Leftist heapsmerge() 3 6

10 8 12 7

21 14 17 18 24 37 18

23 26 33

63

7

18

6

108

12

18 24

3721 14

2317

18 24

33

23

26

Page 17: Priority Queues - Universitetet i oslo · Fibonacci heaps Very elegant, and in theory efficient, way to implement heaps: Most operastions have O(1) amortized running time.(Fredman

Leftist heapsmerge() 3 6

10 8 12 7

21 14 17 18 24 37 18

23 26 33

63

7

18

6

108

12

18 24

3721 14

2317

18 24

33

23

26

Page 18: Priority Queues - Universitetet i oslo · Fibonacci heaps Very elegant, and in theory efficient, way to implement heaps: Most operastions have O(1) amortized running time.(Fredman

Leftist heapsmerge() 3 6

10 8 12 7

21 14 17 18 24 37 18

23 26 33

63

7

37

6

10

12

18 2418

821 14

23 1718 24

33

23 17

26

Page 19: Priority Queues - Universitetet i oslo · Fibonacci heaps Very elegant, and in theory efficient, way to implement heaps: Most operastions have O(1) amortized running time.(Fredman

Leftist heapsmerge() 3 6

10 8 12 7

21 14 17 18 24 37 18

23 26 33

3

12 76 10

18 2437

18

8

17

21 14

23

3317

26

23

Page 20: Priority Queues - Universitetet i oslo · Fibonacci heaps Very elegant, and in theory efficient, way to implement heaps: Most operastions have O(1) amortized running time.(Fredman

Leftist heaps

5 3

7 6

insert(3) merge()

10

deleteMin() 1

5 3

7 6 11

merge()

7 6

10

11

Page 21: Priority Queues - Universitetet i oslo · Fibonacci heaps Very elegant, and in theory efficient, way to implement heaps: Most operastions have O(1) amortized running time.(Fredman

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.

Page 22: Priority Queues - Universitetet i oslo · Fibonacci heaps Very elegant, and in theory efficient, way to implement heaps: Most operastions have O(1) amortized running time.(Fredman

Binomial heaps

Leftist heaps:merge(), insert() og deleteMin() in O(log N) time w.c.

Binary heaps:insert() in O(1) time on average.

Binomial heapsmerge(), 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 hheap.

Page 23: Priority Queues - Universitetet i oslo · Fibonacci heaps Very elegant, and in theory efficient, way to implement heaps: Most operastions have O(1) amortized running time.(Fredman

Binomial heapsBinomial trees

B0B0

Page 24: Priority Queues - Universitetet i oslo · Fibonacci heaps Very elegant, and in theory efficient, way to implement heaps: Most operastions have O(1) amortized running time.(Fredman

Binomial heapsBinomial trees

B0B0B1

Page 25: Priority Queues - Universitetet i oslo · Fibonacci heaps Very elegant, and in theory efficient, way to implement heaps: Most operastions have O(1) amortized running time.(Fredman

Binomial heapsBinomial trees

B0B0B1

B2

Page 26: Priority Queues - Universitetet i oslo · Fibonacci heaps Very elegant, and in theory efficient, way to implement heaps: Most operastions have O(1) amortized running time.(Fredman

Binomial heapsBinomial trees

B0B0B1

B2

B3

Page 27: Priority Queues - Universitetet i oslo · Fibonacci heaps Very elegant, and in theory efficient, way to implement heaps: Most operastions have O(1) amortized running time.(Fredman

Binomial heapsBinomial trees

B0B0B1

B2

B3

B4B4

Page 28: Priority Queues - Universitetet i oslo · Fibonacci heaps Very elegant, and in theory efficient, way to implement heaps: Most operastions have O(1) amortized running time.(Fredman

Binomial heapsBinomial trees

B0B0B1

B2

B3

A t f h i ht k h

B4

A tree of height k has:

2k nodes in total,B4

nodes on level d.⎟⎠⎞⎜

⎝⎛dk

Page 29: Priority Queues - Universitetet i oslo · Fibonacci heaps Very elegant, and in theory efficient, way to implement heaps: Most operastions have O(1) amortized running time.(Fredman

Binomial heapsBinomial heap

16

18

12

21 24

65

Maximum one tree of each size:

6 l t 6 bi 011 (0 2 4) B B BX6 elements: 6 binary = 011 (0+2+4) B0 B1 B2X

Page 30: Priority Queues - Universitetet i oslo · Fibonacci heaps Very elegant, and in theory efficient, way to implement heaps: Most operastions have O(1) amortized running time.(Fredman

Binomial heapsBinomial heap

16

18

12

21 24

65

Maximum one tree of each size:

6 l t 6 bi 011 (0 2 4) B B BX6 elements: 6 binary = 011 (0+2+4) B0 B1 B2X

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

Page 31: Priority Queues - Universitetet i oslo · Fibonacci heaps Very elegant, and in theory efficient, way to implement heaps: Most operastions have O(1) amortized running time.(Fredman

Binomial heaps

merge() 16 12 14 2313

18 21 24

65

26 51 24

6565 65

Page 32: Priority Queues - Universitetet i oslo · Fibonacci heaps Very elegant, and in theory efficient, way to implement heaps: Most operastions have O(1) amortized running time.(Fredman

Binomial heaps

merge() 16 12 14 2313

18 21 24

65

26 51 24

6565 65

Page 33: Priority Queues - Universitetet i oslo · Fibonacci heaps Very elegant, and in theory efficient, way to implement heaps: Most operastions have O(1) amortized running time.(Fredman

Binomial heaps

merge() 16 12 14 2313

18 21 24

65

26 51 24

6565 65

1313

Page 34: Priority Queues - Universitetet i oslo · Fibonacci heaps Very elegant, and in theory efficient, way to implement heaps: Most operastions have O(1) amortized running time.(Fredman

Binomial heaps

merge() 16 12 14 2313

18 21 24

65

26 51 24

6565 65

13 1413

16

14

26

18

Page 35: Priority Queues - Universitetet i oslo · Fibonacci heaps Very elegant, and in theory efficient, way to implement heaps: Most operastions have O(1) amortized running time.(Fredman

Binomial heaps

merge() 16 12 14 2313

18 21 24

65

26 51 24

6565 65

13 122313

14

12

21 2424

23

51

16

18

266565

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

18

Page 36: Priority Queues - Universitetet i oslo · Fibonacci heaps Very elegant, and in theory efficient, way to implement heaps: Most operastions have O(1) amortized running time.(Fredman

Binomial heaps

deleteMin() 13 1223

16

14

26

21 2451 24

16

18

266565

Page 37: Priority Queues - Universitetet i oslo · Fibonacci heaps Very elegant, and in theory efficient, way to implement heaps: Most operastions have O(1) amortized running time.(Fredman

Binomial heaps

deleteMin() 13 1223

16

14

26

21 2451 24

16

18

266565

13 1421 2423merge()

16

18

266551 24

65

Page 38: Priority Queues - Universitetet i oslo · Fibonacci heaps Very elegant, and in theory efficient, way to implement heaps: Most operastions have O(1) amortized running time.(Fredman

Binomial heapsworst 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)

Page 39: Priority Queues - Universitetet i oslo · Fibonacci heaps Very elegant, and in theory efficient, way to implement heaps: Most operastions have O(1) amortized running time.(Fredman

Binomial heapsImplementation Doubly linked, circular lists

13

14

12

21 24

23

51 24

16

14

26

21 24

65

51 24

65

18

Page 40: Priority Queues - Universitetet i oslo · Fibonacci heaps Very elegant, and in theory efficient, way to implement heaps: Most operastions have O(1) amortized running time.(Fredman

Binomial heapsImplementation Doubly linked, circular lists

13

14

12

21 24

23

51 24

16

14

26

21 24

65

51 24

65

18

2323

61 27

88

Page 41: Priority Queues - Universitetet i oslo · Fibonacci heaps Very elegant, and in theory efficient, way to implement heaps: Most operastions have O(1) amortized running time.(Fredman

Binomial heapsImplementation Doubly linked, circular lists

13

14

12

21 24

23

51 24

16

14

26

21 24

65

51 24

65

18

2323

61 27

88

Page 42: Priority Queues - Universitetet i oslo · Fibonacci heaps Very elegant, and in theory efficient, way to implement heaps: Most operastions have O(1) amortized running time.(Fredman

Fibonacci heapsVery elegant, and in theory efficient, way to implement heaps: Most operastionshave O(1) amortized running time. (Fredman & Tarjan ’87)

insert(), decreaseKey() og merge() O(1) amortized time

deleteMin() O(log N) amortized time

Combines elements from leftist heaps and binomial heaps.

A bit compicated 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 p p p g (many decreaseKey() operations), also used in spanning tree algorithms.

Page 43: Priority Queues - Universitetet i oslo · Fibonacci heaps Very elegant, and in theory efficient, way to implement heaps: Most operastions have O(1) amortized running time.(Fredman

Fibonacci heapsWe include a smart decreaseKey() method from leftist heaps.

2

114

12 1759

188 61018

1131

21

15

21

Page 44: Priority Queues - Universitetet i oslo · Fibonacci heaps Very elegant, and in theory efficient, way to implement heaps: Most operastions have O(1) amortized running time.(Fredman

Fibonacci heapsWe include a smart decreaseKey() method from leftist heaps.

2

114

12 1750

188 61018

1131

21

15

21

Page 45: Priority Queues - Universitetet i oslo · Fibonacci heaps Very elegant, and in theory efficient, way to implement heaps: Most operastions have O(1) amortized running time.(Fredman

Fibonacci heapsWe include a smart decreaseKey() method from leftist heaps.

2

114

0

12 175

1018

31

188 6

31

Leftist

11

21

15

21

Ikke leftist

Page 46: Priority Queues - Universitetet i oslo · Fibonacci heaps Very elegant, and in theory efficient, way to implement heaps: Most operastions have O(1) amortized running time.(Fredman

Fibonacci heapsWe include a smart decreaseKey() method from leftist heaps.

2

114

0

12 175

1018

31

188 6

31

Leftist

11

21

15

21

Ikke leftist

Page 47: Priority Queues - Universitetet i oslo · Fibonacci heaps Very elegant, and in theory efficient, way to implement heaps: Most operastions have O(1) amortized running time.(Fredman

Fibonacci heapsWe include a smart decreaseKey() method from leftist heaps.

2

411

0

12 17 5

1018

31

18 8 6

31

Leftist

11

21

15

21

Leftist

Page 48: Priority Queues - Universitetet i oslo · Fibonacci heaps Very elegant, and in theory efficient, way to implement heaps: Most operastions have O(1) amortized running time.(Fredman

Fibonacci heapsWe include a smart decreaseKey() method from leftist heaps.

0

2

411

18

12 17 5 10

31

18 8 6

11

21

15

21

Page 49: Priority Queues - Universitetet i oslo · Fibonacci heaps Very elegant, and in theory efficient, way to implement heaps: Most operastions have O(1) amortized running time.(Fredman

Fibonacci heapsWe include a smart decreaseKey() method from leftist heaps.

The method must be modified a bit as we wish to use trees that are binomialThe method must be modified a bit, as we wish to use trees that are binomial trees, or partial binomial trees.

N d k d th fi t ti hild i d- Nodes are marked the first time child is removed. - The second time a node gets a child removed, it is cut off, and becomes theroot of a separate tree

38 718

min

38

24

7

23 17

18

39 2141

26

35

463052

35

Page 50: Priority Queues - Universitetet i oslo · Fibonacci heaps Very elegant, and in theory efficient, way to implement heaps: Most operastions have O(1) amortized running time.(Fredman

Fibonacci heapsWe include a smart decreaseKey() method from leftist heaps.

The method must be modified a bit as we wish to use trees that are binomialThe method must be modified a bit, as we wish to use trees that are binomial trees, or partial binomial trees.

N d k d th fi t ti hild i d- Nodes are marked the first time child is removed. - The second time a node gets a child removed, it is cut off, and becomes theroot of a separate tree

38 718

min

38

24

7

23 17

18

39 2141

26

35

53052

35

Page 51: Priority Queues - Universitetet i oslo · Fibonacci heaps Very elegant, and in theory efficient, way to implement heaps: Most operastions have O(1) amortized running time.(Fredman

Fibonacci heapsWe include a smart decreaseKey() method from leftist heaps.

The method must be modified a bit as we wish to use trees that are binomialThe method must be modified a bit, as we wish to use trees that are binomial trees, or partial binomial trees.

N d k d th fi t ti hild i d- Nodes are marked the first time child is removed. - The second time a node gets a child removed, it is cut off, and becomes theroot of a separate tree

38 5718

min

38

24

57

23 17

18

39 2141

26

35

3052

35

Page 52: Priority Queues - Universitetet i oslo · Fibonacci heaps Very elegant, and in theory efficient, way to implement heaps: Most operastions have O(1) amortized running time.(Fredman

Fibonacci heapsWe include a smart decreaseKey() method from leftist heaps.

The method must be modified a bit as we wish to use trees that are binomialThe method must be modified a bit, as we wish to use trees that are binomial trees, or partial binomial trees.

N d k d th fi t ti hild i d- Nodes are marked the first time child is removed. - The second time a node gets a child removed, it is cut off, and becomes theroot of a separate tree

38 5718

min

38

24

57

23 17

18

39 2141

13

35

3052

35

Page 53: Priority Queues - Universitetet i oslo · Fibonacci heaps Very elegant, and in theory efficient, way to implement heaps: Most operastions have O(1) amortized running time.(Fredman

Fibonacci heapsWe include a smart decreaseKey() method from leftist heaps.

The method must be modified a bit as we wish to use trees that are binomialThe method must be modified a bit, as we wish to use trees that are binomial trees, or partial binomial trees.

N d k d th fi t ti hild i d- Nodes are marked the first time child is removed. - The second time a node gets a child removed, it is cut off, and becomes theroot of a separate tree

38 135718

min

38 13

3524

57

23 17

18

39 2141

3052

Page 54: Priority Queues - Universitetet i oslo · Fibonacci heaps Very elegant, and in theory efficient, way to implement heaps: Most operastions have O(1) amortized running time.(Fredman

Fibonacci heapsWe include a smart decreaseKey() method from leftist heaps.

The method must be modified a bit as we wish to use trees that are binomialThe method must be modified a bit, as we wish to use trees that are binomial trees, or partial binomial trees.

N d k d th fi t ti hild i d- Nodes are marked the first time child is removed. - The second time a node gets a child removed, it is cut off, and becomes theroot of a separate tree

38 13 245718

min

38 13

35

2457

23 17

18

39 2141

3052

Page 55: Priority Queues - Universitetet i oslo · Fibonacci heaps Very elegant, and in theory efficient, way to implement heaps: Most operastions have O(1) amortized running time.(Fredman

Fibonacci heapsWe use lazy merging / lazy binomial queue.

18 7

8

5

8 6

8

9

4

9 68 8 6

11

9 9 6

32

Page 56: Priority Queues - Universitetet i oslo · Fibonacci heaps Very elegant, and in theory efficient, way to implement heaps: Most operastions have O(1) amortized running time.(Fredman

Fibonacci heapsWe use lazy merging / lazy binomial queue.

18 7

8

5

8 6

8

9

4

9 68 8 6

11

9 9 6

32

18 7 5 8 4

8 8 6 9 9 6

11 32

Page 57: Priority Queues - Universitetet i oslo · Fibonacci heaps Very elegant, and in theory efficient, way to implement heaps: Most operastions have O(1) amortized running time.(Fredman

Fibonacci heapsThe problem with our decreaseKey()-method and lazy merging is that wehave to clean up afterwards. This is done in by the deleteMin()-method whichbecomes expensive (O(log N) amortized time):becomes expensive (O(log N) amortized time):

All trees are examined we start with the smallest and merge two and two soAll 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 (RecallEach root has a number of children this is used as the size of the tree. (Recallhow we construct binomial trees, and that they may be partial as a result ofdeleteMin()’s)

The trees are put in lists, one per size, and we begin merging, starting with thesmallest.

Page 58: Priority Queues - Universitetet i oslo · Fibonacci heaps Very elegant, and in theory efficient, way to implement heaps: Most operastions have O(1) amortized running time.(Fredman

Fibonacci heapsAmortized 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)