data structuresdata structures priority queue. recall queues fifo:first-in, first-out some contexts...

47
Data Structures Priority Queue

Upload: duane-ball

Post on 19-Jan-2016

220 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Data StructuresData Structures Priority Queue. Recall Queues FIFO:First-In, First-Out Some contexts where this seems right? Some contexts where some things

Data Structures

Priority Queue

Page 2: Data StructuresData Structures Priority Queue. Recall Queues FIFO:First-In, First-Out Some contexts where this seems right? Some contexts where some things

Recall Queues

• FIFO: First-In, First-Out

• Some contexts where this seems right?

• Some contexts where some things should be allowed to skip ahead in the line?

Page 3: Data StructuresData Structures Priority Queue. Recall Queues FIFO:First-In, First-Out Some contexts where this seems right? Some contexts where some things

Queues that Allow Line Jumping

• Need a new ADT• Operations: Insert an Item,

Remove the “Best” Item

insert deleteMin

2615 23

12 1845 3 7

Page 4: Data StructuresData Structures Priority Queue. Recall Queues FIFO:First-In, First-Out Some contexts where this seems right? Some contexts where some things

Priority Queue ADT1. PQueue data : collection of data with priority

2. PQueue operations– insert– deleteMin

3. PQueue property: for two elements in the queue, x and y, if x has a lower priority value than y, x will be deleted before y

Page 5: Data StructuresData Structures Priority Queue. Recall Queues FIFO:First-In, First-Out Some contexts where this seems right? Some contexts where some things

Applications of the Priority Queue

• Select print jobs in order of decreasing length• Forward packets on routers in order of

urgency• Select most frequent symbols for compression• Sort numbers, picking minimum first• Anything greedy

Page 6: Data StructuresData Structures Priority Queue. Recall Queues FIFO:First-In, First-Out Some contexts where this seems right? Some contexts where some things

Potential Implementations

insert deleteMin

Unsorted list (Array)

Unsorted list (Linked-List)

Sorted list (Array)

Sorted list (Linked-List)

Binary Search Tree

AVL Trees

Page 7: Data StructuresData Structures Priority Queue. Recall Queues FIFO:First-In, First-Out Some contexts where this seems right? Some contexts where some things

Potential Implementations

insert deleteMin

Unsorted list (Array) O(1) O(n)

Unsorted list (Linked-List) O(1) O(n)

Sorted list (Array) O(n) O(1)

Sorted list (Linked-List) O(n) O(1)

Binary Search Tree O(n) worst O(n) worst

AVL Trees O(log n) O(log n)

Page 8: Data StructuresData Structures Priority Queue. Recall Queues FIFO:First-In, First-Out Some contexts where this seems right? Some contexts where some things

Recall From Lists, Queues, Stacks

• Use an ADT that corresponds to your needs

• The right ADT is efficient, while an overly general ADT provides functionality you aren’t using, but are paying for anyways

• Heaps provide O(log n) worst case for both insert and deleteMin, O(1) average insert

Page 9: Data StructuresData Structures Priority Queue. Recall Queues FIFO:First-In, First-Out Some contexts where this seems right? Some contexts where some things

Heap Structure Property• A binary heap is a complete binary tree – binary

tree that is completely filled, with the possible exception of the bottom level, which is filled left to right.

Page 10: Data StructuresData Structures Priority Queue. Recall Queues FIFO:First-In, First-Out Some contexts where this seems right? Some contexts where some things

Representing Complete Binary Trees in an Array

GED

CB

A

F

From node i:

7

1

2 3

4 5 6

J KH I L

left child: right child:parent:

98 10 11 12

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

implicit (array) implementation:A B C D E F G H I J K L

Page 11: Data StructuresData Structures Priority Queue. Recall Queues FIFO:First-In, First-Out Some contexts where this seems right? Some contexts where some things

Heap Order Property

Heap order property: For every non-root node X, the value in the parent of X is less than (or equal to) the value in X.

8020

10

996040

20

10

50 700

80

853015

not a heap

Page 12: Data StructuresData Structures Priority Queue. Recall Queues FIFO:First-In, First-Out Some contexts where this seems right? Some contexts where some things

Heap Operations• findMin• insert(val)• deleteMin

10

996040

8020

50

85

700 65

Page 13: Data StructuresData Structures Priority Queue. Recall Queues FIFO:First-In, First-Out Some contexts where this seems right? Some contexts where some things

Heap – Deletemin

Basic Idea:1. Remove root (that is always the min!)2. Put “last” leaf node at root3. Find smallest child of node4. Swap node with its smallest child if needed.5. Repeat steps 3 & 4 until no swaps needed.

Page 14: Data StructuresData Structures Priority Queue. Recall Queues FIFO:First-In, First-Out Some contexts where this seems right? Some contexts where some things

Building a Heap

• At every point, the new item may need to percolate all the way through the heap

• Adding the items one at a time is O(n log n) in the worst case.

• Can we do better?

Page 15: Data StructuresData Structures Priority Queue. Recall Queues FIFO:First-In, First-Out Some contexts where this seems right? Some contexts where some things

BuildHeap: Floyd’s Method

8 1 7 212 5 11 3 106 9 4

Add elements arbitrarily to form a complete tree. Pretend it’s a heap and fix the heap-order property!

4 8 1 7 2

96103

115

12

Page 16: Data StructuresData Structures Priority Queue. Recall Queues FIFO:First-In, First-Out Some contexts where this seems right? Some contexts where some things

BuildHeap: Floyd’s Method

115

12

4 8 1 7 2

96103

Page 17: Data StructuresData Structures Priority Queue. Recall Queues FIFO:First-In, First-Out Some contexts where this seems right? Some contexts where some things

92103

115

12

4 8 1 7 6

Page 18: Data StructuresData Structures Priority Queue. Recall Queues FIFO:First-In, First-Out Some contexts where this seems right? Some contexts where some things

92103

115

12

9213

115

12

4 8 1 7 6 4 8 10 7 6

Page 19: Data StructuresData Structures Priority Queue. Recall Queues FIFO:First-In, First-Out Some contexts where this seems right? Some contexts where some things

92103

115

12

9213

115

12

4 8 1 7 6 4 8 10 7 6

8 10 7

9613

25

12

4 11

Page 20: Data StructuresData Structures Priority Queue. Recall Queues FIFO:First-In, First-Out Some contexts where this seems right? Some contexts where some things

92103

115

12

9213

115

12

4 8 1 7 6 4 8 10 7 6

8 10 7

9613

25

12

8 10 711

9653

21

12

4 411

Page 21: Data StructuresData Structures Priority Queue. Recall Queues FIFO:First-In, First-Out Some contexts where this seems right? Some contexts where some things

Finally…

23

1

12 8 10 711

9654

Page 22: Data StructuresData Structures Priority Queue. Recall Queues FIFO:First-In, First-Out Some contexts where this seems right? Some contexts where some things

Note they’re not the same

2323

O(N log N)

1

O(N)

1

9654

12 8 10 711

9645

12 8 10 7 11

But that doesn’t matter, they’re both heaps

Page 23: Data StructuresData Structures Priority Queue. Recall Queues FIFO:First-In, First-Out Some contexts where this seems right? Some contexts where some things

32

1

7

Extension: d-Heaps

How does height compare to binary heap?

• Each node has d children• Still representable by array• Good choices for d:– choose a power of two– fit one set of children in a

cache line/memory page/disk block

4 8 5 12 11 10 6 9

1 2 7 3 4 8 5 12 11 10 69

Page 24: Data StructuresData Structures Priority Queue. Recall Queues FIFO:First-In, First-Out Some contexts where this seems right? Some contexts where some things

Operations on d-Heap

• Insert: runtime = O(logd n) worst

• deleteMin: runtime =

Does this help insert or deleteMin more?

O(d logd n), worst/ave

Page 25: Data StructuresData Structures Priority Queue. Recall Queues FIFO:First-In, First-Out Some contexts where this seems right? Some contexts where some things

Analysis of Priority Queue

• The run time for updating an item or adding a new item to a priority queue that has N items

Implementation Insert Delete Min Find MinUnordered array 1 N NOrdered array N 1 1AVL tree (RB tree) log N log N log NBinary heap log N log N 1d-ary heap logd N d logd N 1Fibonacci heap 1 log N 1

Page 26: Data StructuresData Structures Priority Queue. Recall Queues FIFO:First-In, First-Out Some contexts where this seems right? Some contexts where some things

723

30

17

35

26 46

24

39

4118 52

3

44

Fibonacci Heaps: Structure

Fibonacci heap.Set of heap-ordered trees.Maintain pointer to minimum element.Set of marked nodes.

roots heap-ordered tree

Page 27: Data StructuresData Structures Priority Queue. Recall Queues FIFO:First-In, First-Out Some contexts where this seems right? Some contexts where some things

723

30

17

35

26 46

24

39

4118 52

3

44

Fibonacci Heaps: Structure

Fibonacci heap.Set of heap-ordered trees.Maintain pointer to minimum element.Set of marked nodes.

min

find-min takes O(1) time

Page 28: Data StructuresData Structures Priority Queue. Recall Queues FIFO:First-In, First-Out Some contexts where this seems right? Some contexts where some things

28

Fibonacci Heaps: Insert

Insert.Create a new singleton tree.Add to root list; update min pointer (if necessary).

723

30

17

35

26 46

24

39

4118 52

3

44

21

insert 21

min

Heap H

Page 29: Data StructuresData Structures Priority Queue. Recall Queues FIFO:First-In, First-Out Some contexts where this seems right? Some contexts where some things

Fibonacci Heaps: Insert

Insert.Create a new singleton tree.Add to root list; update min pointer (if necessary).

39

41

723

18 52

3

30

17

35

26 46

24

44

21

min

insert 21

Page 30: Data StructuresData Structures Priority Queue. Recall Queues FIFO:First-In, First-Out Some contexts where this seems right? Some contexts where some things

Fibonacci Heaps: Insert Analysis

Actual cost. O(1)

Change in potential. +1

39

41

7

18 52

3

30

17

35

26 46

24

44

2123

min

Page 31: Data StructuresData Structures Priority Queue. Recall Queues FIFO:First-In, First-Out Some contexts where this seems right? Some contexts where some things

Linking Operation

Linking operation. Make larger root be a child of smaller root.

39

4118 52

3

4477

56 24

15

39

4118 52

3

44

77

56 24

15

smaller rootlarger root still heap-ordered

Page 32: Data StructuresData Structures Priority Queue. Recall Queues FIFO:First-In, First-Out Some contexts where this seems right? Some contexts where some things

Fibonacci Heaps: Delete Min

Delete min.Delete min; meld its children into root list; update min.Consolidate trees so that no two roots have same rank.

39

4118 52

3

44

1723

30

7

35

26 46

24

min

Page 33: Data StructuresData Structures Priority Queue. Recall Queues FIFO:First-In, First-Out Some contexts where this seems right? Some contexts where some things

Fibonacci Heaps: Delete Min

Delete min.Delete min; meld its children into root list; update min.Consolidate trees so that no two roots have same rank.

39

411723 18 52

30

7

35

26 46

24

44

min

Page 34: Data StructuresData Structures Priority Queue. Recall Queues FIFO:First-In, First-Out Some contexts where this seems right? Some contexts where some things

Fibonacci Heaps: Delete Min

Delete min.Delete min; meld its children into root list; update min.Consolidate trees so that no two roots have same rank.

39

411723 18 52

30

7

35

26 46

24

44

min current

Page 35: Data StructuresData Structures Priority Queue. Recall Queues FIFO:First-In, First-Out Some contexts where this seems right? Some contexts where some things

Fibonacci Heaps: Delete Min

Delete min.

39

411723 18 52

30

7

35

26 46

24

44

0 1 2 3

currentmin

rank

Page 36: Data StructuresData Structures Priority Queue. Recall Queues FIFO:First-In, First-Out Some contexts where this seems right? Some contexts where some things

Fibonacci Heaps: Delete Min

Delete min.

39

411723 18 52

30

7

35

26 46

24

44

0 1 2 3

min current

rank

Page 37: Data StructuresData Structures Priority Queue. Recall Queues FIFO:First-In, First-Out Some contexts where this seems right? Some contexts where some things

Fibonacci Heaps: Delete Min

Delete min.

39

411723 18 52

30

7

35

26 46

24

44

0 1 2 3

min

current

rank

Page 38: Data StructuresData Structures Priority Queue. Recall Queues FIFO:First-In, First-Out Some contexts where this seems right? Some contexts where some things

Fibonacci Heaps: Delete Min

Delete min.

39

411723 18 52

30

7

35

26 46

24

44

0 1 2 3

min

current

rank

link 23 into 17

Page 39: Data StructuresData Structures Priority Queue. Recall Queues FIFO:First-In, First-Out Some contexts where this seems right? Some contexts where some things

Fibonacci Heaps: Delete Min

Delete min.

39

4117

23

18 52

30

7

35

26 46

24

44

0 1 2 3

min

current

rank

link 17 into 7

Page 40: Data StructuresData Structures Priority Queue. Recall Queues FIFO:First-In, First-Out Some contexts where this seems right? Some contexts where some things

Fibonacci Heaps: Delete Min

Delete min.

39

417

30

18 52

17

35

26 46

24

44

0 1 2 3

23

current

min

rank

link 24 into 7

Page 41: Data StructuresData Structures Priority Queue. Recall Queues FIFO:First-In, First-Out Some contexts where this seems right? Some contexts where some things

Fibonacci Heaps: Delete Min

Delete min.

39

417

30

18 52

23

17

35

26 46

24 44

0 1 2 3

min

current

rank

Page 42: Data StructuresData Structures Priority Queue. Recall Queues FIFO:First-In, First-Out Some contexts where this seems right? Some contexts where some things

Fibonacci Heaps: Delete Min

Delete min.

39

417

30

18 52

23

17

35

26 46

24 44

0 1 2 3

min

current

rank

Page 43: Data StructuresData Structures Priority Queue. Recall Queues FIFO:First-In, First-Out Some contexts where this seems right? Some contexts where some things

Fibonacci Heaps: Delete Min

Delete min.

39

417

30

18 52

23

17

35

26 46

24 44

0 1 2 3

min

current

rank

Page 44: Data StructuresData Structures Priority Queue. Recall Queues FIFO:First-In, First-Out Some contexts where this seems right? Some contexts where some things

Fibonacci Heaps: Delete Min

Delete min.

39

417

30

18 52

23

17

35

26 46

24 44

0 1 2 3

min

current

rank

link 41 into 18

Page 45: Data StructuresData Structures Priority Queue. Recall Queues FIFO:First-In, First-Out Some contexts where this seems right? Some contexts where some things

Fibonacci Heaps: Delete Min

Delete min.

3941

7

30

1852

23

17

35

26 46

24

44

0 1 2 3

min

current

rank

Page 46: Data StructuresData Structures Priority Queue. Recall Queues FIFO:First-In, First-Out Some contexts where this seems right? Some contexts where some things

Fibonacci Heaps: Delete Min

Delete min.

7

30

52

23

17

35

26 46

24

0 1 2 3

min

rank

3941

18

44

current

Page 47: Data StructuresData Structures Priority Queue. Recall Queues FIFO:First-In, First-Out Some contexts where this seems right? Some contexts where some things

Fibonacci Heaps: Delete Min

Delete min.Delete min; meld its children into root list; update min.Consolidate trees so that no two roots have same rank.

7

30

52

23

17

35

26 46

24

min

3941

18

44

stop