Data StructuresData Structures Priority Queue. Recall Queues FIFO:First-In, First-Out Some contexts...

Post on 19-Jan-2016

222 views 0 download

Tags:

Transcript of Data StructuresData Structures Priority Queue. Recall Queues FIFO:First-In, First-Out Some contexts...

Data Structures

Priority Queue

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?

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

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

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

Potential Implementations

insert deleteMin

Unsorted list (Array)

Unsorted list (Linked-List)

Sorted list (Array)

Sorted list (Linked-List)

Binary Search Tree

AVL Trees

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)

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

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.

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

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

Heap Operations• findMin• insert(val)• deleteMin

10

996040

8020

50

85

700 65

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.

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?

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

BuildHeap: Floyd’s Method

115

12

4 8 1 7 2

96103

92103

115

12

4 8 1 7 6

92103

115

12

9213

115

12

4 8 1 7 6 4 8 10 7 6

92103

115

12

9213

115

12

4 8 1 7 6 4 8 10 7 6

8 10 7

9613

25

12

4 11

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

Finally…

23

1

12 8 10 711

9654

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

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

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

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

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

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

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

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

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

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

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

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

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

Fibonacci Heaps: Delete Min

Delete min.

39

411723 18 52

30

7

35

26 46

24

44

0 1 2 3

currentmin

rank

Fibonacci Heaps: Delete Min

Delete min.

39

411723 18 52

30

7

35

26 46

24

44

0 1 2 3

min current

rank

Fibonacci Heaps: Delete Min

Delete min.

39

411723 18 52

30

7

35

26 46

24

44

0 1 2 3

min

current

rank

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

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

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

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

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

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

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

Fibonacci Heaps: Delete Min

Delete min.

3941

7

30

1852

23

17

35

26 46

24

44

0 1 2 3

min

current

rank

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

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