Top Banner
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. 9/10
58

Priority Queues 9/10 · • Leftist heaps • Binomial heaps • Fibonacci heaps . Priority queues are important in, among other things, operating systems (process control in multitasking

Jul 09, 2020

Download

Documents

dariahiddleston
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Priority Queues 9/10 · • Leftist heaps • Binomial heaps • Fibonacci heaps . Priority queues are important in, among other things, operating systems (process control in multitasking

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.

9/10

Page 2: Priority Queues 9/10 · • Leftist heaps • Binomial heaps • Fibonacci heaps . Priority queues are important in, among other things, operating systems (process control in multitasking

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.

Page 3: Priority Queues 9/10 · • Leftist heaps • Binomial heaps • Fibonacci heaps . Priority queues are important in, among other things, operating systems (process control in multitasking

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”.

Page 4: Priority Queues 9/10 · • Leftist heaps • Binomial heaps • Fibonacci heaps . Priority queues are important in, among other things, operating systems (process control in multitasking

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

0 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

Page 5: Priority Queues 9/10 · • Leftist heaps • Binomial heaps • Fibonacci heaps . Priority queues are important in, among other things, operating systems (process control in multitasking

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

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

1 2 3 4

14

Binary heaps

Page 6: Priority Queues 9/10 · • Leftist heaps • Binomial heaps • Fibonacci heaps . Priority queues are important in, among other things, operating systems (process control in multitasking

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

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

1 2 3 4

31

”percolateUp()”

Binary heaps

Page 7: Priority Queues 9/10 · • Leftist heaps • Binomial heaps • Fibonacci heaps . Priority queues are important in, among other things, operating systems (process control in multitasking

14 16

19 21

65 26 32

19 68

1

2

3

4

deleteMin()

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

31

Binary heaps

Page 8: Priority Queues 9/10 · • Leftist heaps • Binomial heaps • Fibonacci heaps . Priority queues are important in, among other things, operating systems (process control in multitasking

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

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

1 2 3 4

Binary heaps

Page 9: Priority Queues 9/10 · • Leftist heaps • Binomial heaps • Fibonacci heaps . Priority queues are important in, among other things, operating systems (process control in multitasking

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

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

1 2 3 4

”percolateDown()”

Binary heaps

Page 10: Priority Queues 9/10 · • Leftist heaps • Binomial heaps • Fibonacci heaps . Priority queues are important in, among other things, operating systems (process control in multitasking

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

Page 11: Priority Queues 9/10 · • Leftist heaps • Binomial heaps • Fibonacci heaps . Priority queues are important in, among other things, operating systems (process control in multitasking

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

Page 12: Priority Queues 9/10 · • Leftist heaps • Binomial heaps • Fibonacci heaps . Priority queues are important in, among other things, operating systems (process control in multitasking

Leftist heaps

1 0

0 0

0

NOT LEFTIST

LEFTIST

1

1 0

0 1

0

1

0

Page 13: Priority Queues 9/10 · • Leftist heaps • Binomial heaps • Fibonacci heaps . Priority queues are important in, among other things, operating systems (process control in multitasking

Leftist heaps

10 8

21 14

23

17

26

12 7

18 24

33

37 18

merge() 3 6

Page 14: Priority Queues 9/10 · • Leftist heaps • Binomial heaps • Fibonacci heaps . Priority queues are important in, among other things, operating systems (process control in multitasking

Leftist heaps

10 8

21 14

23

17

26

12 7

18 24

33

37 18

merge() 3 6

Page 15: Priority Queues 9/10 · • Leftist heaps • Binomial heaps • Fibonacci heaps . Priority queues are important in, among other things, operating systems (process control in multitasking

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

Page 16: Priority Queues 9/10 · • Leftist heaps • Binomial heaps • Fibonacci heaps . Priority queues are important in, among other things, operating systems (process control in multitasking

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

Page 17: Priority Queues 9/10 · • Leftist heaps • Binomial heaps • Fibonacci heaps . Priority queues are important in, among other things, operating systems (process control in multitasking

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

Page 18: Priority Queues 9/10 · • Leftist heaps • Binomial heaps • Fibonacci heaps . Priority queues are important in, among other things, operating systems (process control in multitasking

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

Page 19: Priority Queues 9/10 · • Leftist heaps • Binomial heaps • Fibonacci heaps . Priority queues are important in, among other things, operating systems (process control in multitasking

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

Page 20: Priority Queues 9/10 · • Leftist heaps • Binomial heaps • Fibonacci heaps . Priority queues are important in, among other things, operating systems (process control in multitasking

Leftist heaps

5 3

7 6

10

5 3

7 6

10

deleteMin()

insert(3) merge()

11

merge()

1

Page 21: Priority Queues 9/10 · • Leftist heaps • Binomial heaps • Fibonacci heaps . Priority queues are important in, among other things, operating systems (process control in multitasking

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 9/10 · • Leftist heaps • Binomial heaps • Fibonacci heaps . Priority queues are important in, among other things, operating systems (process control in multitasking

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

Page 23: Priority Queues 9/10 · • Leftist heaps • Binomial heaps • Fibonacci heaps . Priority queues are important in, among other things, operating systems (process control in multitasking

Binomial trees

B0

Binomial heaps

Page 24: Priority Queues 9/10 · • Leftist heaps • Binomial heaps • Fibonacci heaps . Priority queues are important in, among other things, operating systems (process control in multitasking

Binomial trees

B0 B1

Binomial heaps

Page 25: Priority Queues 9/10 · • Leftist heaps • Binomial heaps • Fibonacci heaps . Priority queues are important in, among other things, operating systems (process control in multitasking

Binomial trees

B0 B1

B2

Binomial heaps

Page 26: Priority Queues 9/10 · • Leftist heaps • Binomial heaps • Fibonacci heaps . Priority queues are important in, among other things, operating systems (process control in multitasking

Binomial trees

B0 B1

B2

B3

Binomial heaps

Page 27: Priority Queues 9/10 · • Leftist heaps • Binomial heaps • Fibonacci heaps . Priority queues are important in, among other things, operating systems (process control in multitasking

Binomial trees

B0 B1

B2

B3

B4

Binomial heaps

Page 28: Priority Queues 9/10 · • Leftist heaps • Binomial heaps • Fibonacci heaps . Priority queues are important in, among other things, operating systems (process control in multitasking

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.

dk

Binomial heaps

Page 29: Priority Queues 9/10 · • Leftist heaps • Binomial heaps • Fibonacci heaps . Priority queues are important in, among other things, operating systems (process control in multitasking

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

Page 30: Priority Queues 9/10 · • Leftist heaps • Binomial heaps • Fibonacci heaps . Priority queues are important in, among other things, operating systems (process control in multitasking

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

Page 31: Priority Queues 9/10 · • Leftist heaps • Binomial heaps • Fibonacci heaps . Priority queues are important in, among other things, operating systems (process control in multitasking

merge() 16

18

12

21 24

65

14

26

23

51 24

65

13

Binomial heaps

Page 32: Priority Queues 9/10 · • Leftist heaps • Binomial heaps • Fibonacci heaps . Priority queues are important in, among other things, operating systems (process control in multitasking

merge() 16

18

12

21 24

65

14

26

23

51 24

65

13

Binomial heaps

Page 33: Priority Queues 9/10 · • Leftist heaps • Binomial heaps • Fibonacci heaps . Priority queues are important in, among other things, operating systems (process control in multitasking

merge() 16

18

12

21 24

65

14

26

23

51 24

65

13

13

Binomial heaps

Page 34: Priority Queues 9/10 · • Leftist heaps • Binomial heaps • Fibonacci heaps . Priority queues are important in, among other things, operating systems (process control in multitasking

merge() 16

18

12

21 24

65

14

26

23

51 24

65

13

13

16

18

14

26

Binomial heaps

Page 35: Priority Queues 9/10 · • Leftist heaps • Binomial heaps • Fibonacci heaps . Priority queues are important in, among other things, operating systems (process control in multitasking

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

Page 36: Priority Queues 9/10 · • Leftist heaps • Binomial heaps • Fibonacci heaps . Priority queues are important in, among other things, operating systems (process control in multitasking

deleteMin() 13

16

18

14

26

12

21 24

65

23

51 24

65

Binomial heaps

Page 37: Priority Queues 9/10 · • Leftist heaps • Binomial heaps • Fibonacci heaps . Priority queues are important in, among other things, operating systems (process control in multitasking

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

Page 38: Priority Queues 9/10 · • Leftist heaps • Binomial heaps • Fibonacci heaps . Priority queues are important in, among other things, operating systems (process control in multitasking

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

Page 39: Priority Queues 9/10 · • Leftist heaps • Binomial heaps • Fibonacci heaps . Priority queues are important in, among other things, operating systems (process control in multitasking

Implementation

13

16

18

14

26

12

21 24

65

23

51 24

65

Doubly linked, circular lists

Binomial heaps

Page 40: Priority Queues 9/10 · • Leftist heaps • Binomial heaps • Fibonacci heaps . Priority queues are important in, among other things, operating systems (process control in multitasking

Implementation

13

16

18

14

26

12

21 24

65

23

51 24

65

23

61 27

88

Doubly linked, circular lists

Binomial heaps

Page 41: Priority Queues 9/10 · • Leftist heaps • Binomial heaps • Fibonacci heaps . Priority queues are important in, among other things, operating systems (process control in multitasking

Implementation

13

16

18

14

26

12

21 24

65

23

51 24

65

23

61 27

88

Doubly linked, circular lists

Binomial heaps

Page 42: Priority Queues 9/10 · • Leftist heaps • Binomial heaps • Fibonacci heaps . Priority queues are important in, among other things, operating systems (process control in multitasking

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

Page 43: Priority Queues 9/10 · • Leftist heaps • Binomial heaps • Fibonacci heaps . Priority queues are important in, among other things, operating systems (process control in multitasking

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

Page 44: Priority Queues 9/10 · • Leftist heaps • Binomial heaps • Fibonacci heaps . Priority queues are important in, among other things, operating systems (process control in multitasking

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

2

11

12 17

18

4

5

8 6

11

0

10 18

31

21

15

Fibonacci heaps

Page 45: Priority Queues 9/10 · • Leftist heaps • Binomial heaps • Fibonacci heaps . Priority queues are important in, among other things, operating systems (process control in multitasking

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

2

11

12 17

18

4

5

8 6

11

0

10 18

31

21

15

Leftist

Ikke leftist

Fibonacci heaps

Page 46: Priority Queues 9/10 · • Leftist heaps • Binomial heaps • Fibonacci heaps . Priority queues are important in, among other things, operating systems (process control in multitasking

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

2

11

12 17

18

4

5

8 6

11

0

10 18

31

21

15

Leftist

Ikke leftist

Fibonacci heaps

Page 47: Priority Queues 9/10 · • Leftist heaps • Binomial heaps • Fibonacci heaps . Priority queues are important in, among other things, operating systems (process control in multitasking

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

2

4

12 17

18

11

5

8 6

11

0

10 18

31

21

15

Leftist

Leftist

Fibonacci heaps

Page 48: Priority Queues 9/10 · • Leftist heaps • Binomial heaps • Fibonacci heaps . Priority queues are important in, among other things, operating systems (process control in multitasking

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

2

4

12 17

18

11

5

8 6

11

0

10

18

31

21

15

Fibonacci heaps

Page 49: Priority Queues 9/10 · • Leftist heaps • Binomial heaps • Fibonacci heaps . Priority queues are important in, among other things, operating systems (process control in multitasking

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

Page 50: Priority Queues 9/10 · • Leftist heaps • Binomial heaps • Fibonacci heaps . Priority queues are important in, among other things, operating systems (process control in multitasking

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

Page 51: Priority Queues 9/10 · • Leftist heaps • Binomial heaps • Fibonacci heaps . Priority queues are important in, among other things, operating systems (process control in multitasking

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

Page 52: Priority Queues 9/10 · • Leftist heaps • Binomial heaps • Fibonacci heaps . Priority queues are important in, among other things, operating systems (process control in multitasking

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

Page 53: Priority Queues 9/10 · • Leftist heaps • Binomial heaps • Fibonacci heaps . Priority queues are important in, among other things, operating systems (process control in multitasking

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

Page 54: Priority Queues 9/10 · • Leftist heaps • Binomial heaps • Fibonacci heaps . Priority queues are important in, among other things, operating systems (process control in multitasking

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

Page 55: Priority Queues 9/10 · • Leftist heaps • Binomial heaps • Fibonacci heaps . Priority queues are important in, among other things, operating systems (process control in multitasking

We use lazy merging / lazy binomial queue.

18 7

8

5

8 6

11

8

9

4

9 6

32

Fibonacci heaps

Page 56: Priority Queues 9/10 · • Leftist heaps • Binomial heaps • Fibonacci heaps . Priority queues are important in, among other things, operating systems (process control in multitasking

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

Page 57: Priority Queues 9/10 · • Leftist heaps • Binomial heaps • Fibonacci heaps . Priority queues are important in, among other things, operating systems (process control in multitasking

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 deleteMin()’s) The trees are put in lists, one per size, and we begin merging, starting with the smallest.

Fibonacci heaps

Page 58: Priority Queues 9/10 · • Leftist heaps • Binomial heaps • Fibonacci heaps . Priority queues are important in, among other things, operating systems (process control in multitasking

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