39
CS221: Algorithms and Data Structures Lecture #3 Mind Your Priority Queues Steve Wolfman 2011W2 1

CS221: Algorithms and Data Structures Lecture #3 Mind Your Priority Queues Steve Wolfman 2011W2 1

Embed Size (px)

Citation preview

Page 1: CS221: Algorithms and Data Structures Lecture #3 Mind Your Priority Queues Steve Wolfman 2011W2 1

CS221: Algorithms and Data Structures

Lecture #3Mind Your Priority Queues

Steve Wolfman

2011W2

1

Page 2: CS221: Algorithms and Data Structures Lecture #3 Mind Your Priority Queues Steve Wolfman 2011W2 1

Today’s Outline

• Trees, Briefly• Priority Queue ADT• Heaps

– Implementing Priority Queue ADT

– Focus on Create: Heapify

– Brief introduction to d-Heaps

2

Page 3: CS221: Algorithms and Data Structures Lecture #3 Mind Your Priority Queues Steve Wolfman 2011W2 1

Trees

• Family Trees

• Organization Charts

• Classification trees– what kind of flower is this?

– is this mushroom poisonous?

• File directory structure– folders, subfolders in Windows

– directories, subdirectories in UNIX

• Function call structure (i.e., a record of everything that goes in the call stack)

3

Page 4: CS221: Algorithms and Data Structures Lecture #3 Mind Your Priority Queues Steve Wolfman 2011W2 1

Tree TerminologyA

E

B

D F

C

G

IH

LJ MK N

root:leaf:child:parent:sibling:ancestor:descendent:subtree:

4

Page 5: CS221: Algorithms and Data Structures Lecture #3 Mind Your Priority Queues Steve Wolfman 2011W2 1

Tree Terminology ReferenceA

E

B

D F

C

G

IH

LJ MK N

root: the single node with no parentleaf: a node with no childrenchild: a node pointed to by meparent: the node that points to mesibling: another child of my parentancestor: my parent or my parent’s ancestordescendent: my child or my child’s descendentsubtree: a node and its descendents

We sometimes use degenerate versionsof these definitions that allow NULL asthe empty tree. (This can be very handy for recursive base cases!)5

Page 6: CS221: Algorithms and Data Structures Lecture #3 Mind Your Priority Queues Steve Wolfman 2011W2 1

More Tree TerminologyA

E

B

D F

C

G

IH

LJ MK N

depth: # of edges along path from root to nodedepth of H?a.0b.1c.2d.3e.4

6

Page 7: CS221: Algorithms and Data Structures Lecture #3 Mind Your Priority Queues Steve Wolfman 2011W2 1

More Tree TerminologyA

E

B

D F

C

G

IH

LJ MK N

height: # of edges along longest path from node to leaf or, for whole tree, from root to leafheight of tree?a.0b.1c.2d.3e.4

7

Page 8: CS221: Algorithms and Data Structures Lecture #3 Mind Your Priority Queues Steve Wolfman 2011W2 1

More Tree TerminologyA

E

B

D F

C

G

IH

LJ MK N

degree: # of children of a nodedegree of B?a.0b.1c.2d.3e.4

8

Page 9: CS221: Algorithms and Data Structures Lecture #3 Mind Your Priority Queues Steve Wolfman 2011W2 1

More Tree TerminologyA

E

B

D F

C

G

IH

LJ MK N

branching factor: maximum degree of any node in the tree

2 for binary trees, our usual concern; 5 for this weird tree

9

Page 10: CS221: Algorithms and Data Structures Lecture #3 Mind Your Priority Queues Steve Wolfman 2011W2 1

One More Tree Terminology Slide

JIH

GFED

CB

A

binary: branching factor of 2 (each child has at most 2 children)

n-ary: branching factor of n

complete: “packed” binary tree; as many nodes as possible for its height

nearly complete: complete plus some nodes on the left at the bottom10

Page 11: CS221: Algorithms and Data Structures Lecture #3 Mind Your Priority Queues Steve Wolfman 2011W2 1

Tree Calculations

• Find the longest undirected path in a tree

• A tree is an empty tree or a node with some number of subtrees

• Path might be:–

t

11

Page 12: CS221: Algorithms and Data Structures Lecture #3 Mind Your Priority Queues Steve Wolfman 2011W2 1

Tree Calculations Example

A

E

B

D F

C

G

IH

LJ MK N

12

Page 13: CS221: Algorithms and Data Structures Lecture #3 Mind Your Priority Queues Steve Wolfman 2011W2 1

Today’s Outline

• Trees, Briefly• Priority Queue ADT• Heaps

– Implementing Priority Queue ADT

– Focus on Create: Heapify

– Brief introduction to d-Heaps

13

Page 14: CS221: Algorithms and Data Structures Lecture #3 Mind Your Priority Queues Steve Wolfman 2011W2 1

Back to Queues

• Some applications– ordering CPU jobs

– simulating events

– picking the next search site

• Problems?– short jobs should go first

– earliest (simulated time) events should go first

– most promising sites should be searched first

14

Page 15: CS221: Algorithms and Data Structures Lecture #3 Mind Your Priority Queues Steve Wolfman 2011W2 1

Priority Queue ADT

• Priority Queue operations– create

– destroy

– insert

– deleteMin

– is_empty

• Priority Queue 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

F(7) E(5) D(100) A(4)

B(6)

insert deleteMinG(9) C(3)

15

Page 16: CS221: Algorithms and Data Structures Lecture #3 Mind Your Priority Queues Steve Wolfman 2011W2 1

Applications of the Priority Q

• Call queues for help lines (or don’t you think so?)

• Hold jobs for a printer in order of length

• Simulate events

• Sort numbers

• Store packets on network routers in order of urgency

• Select symbols for compression

• Anything greedy: an algorithm that makes the “locally best choice” at each step

16

Your call will not be answered in the order it was received.

Page 17: CS221: Algorithms and Data Structures Lecture #3 Mind Your Priority Queues Steve Wolfman 2011W2 1

Naïve Priority Q Data Structures

• Unsorted list:– insert:

– deleteMin:

• Sorted list:– insert:

– deleteMin:

17

a. O(lg n)b. O(n)c. O(n lg n)d. O(n2)e. Something else

Page 18: CS221: Algorithms and Data Structures Lecture #3 Mind Your Priority Queues Steve Wolfman 2011W2 1

Today’s Outline

• Trees, Briefly• Priority Queue ADT• Heaps

– Implementing Priority Queue ADT

– Focus on Create: Heapify

– Brief introduction to d-Heaps

18

Page 19: CS221: Algorithms and Data Structures Lecture #3 Mind Your Priority Queues Steve Wolfman 2011W2 1

Binary Heap Priority Q Data Structure

201412911

81067

54

2

• Heap-order property– parent’s key is less than or

equal to children’s keys

– result: minimum is always at the top

• Structure property– “nearly complete tree”

– result: depth is always O(log n); next open location always known

WARNING: this has NO SIMILARITY to the “heap” you hear about when people say “things you create with new go on the heap”.

Look! Invariants!

19

Page 20: CS221: Algorithms and Data Structures Lecture #3 Mind Your Priority Queues Steve Wolfman 2011W2 1

201412911

81067

54

2

2 4 5 7 6 10 8 11 9 12 14 20

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

0

1 2

3 4 5 6

7 8

9 10 11

Nifty Storage Trick• Calculations:

– child:

– parent:

– root:

– next free:

0

20

Page 21: CS221: Algorithms and Data Structures Lecture #3 Mind Your Priority Queues Steve Wolfman 2011W2 1

DeleteMin

201412911

81067

54

?2

201412911

81067

54

2

pqueue.deleteMin()

Invariants violated! DOOOM!!! 21

Page 22: CS221: Algorithms and Data Structures Lecture #3 Mind Your Priority Queues Steve Wolfman 2011W2 1

Percolate Down

201412911

81067

54

?

201412911

81067

5?

4

201412911

810?7

56

4

201420911

810127

56

4

22

Page 23: CS221: Algorithms and Data Structures Lecture #3 Mind Your Priority Queues Steve Wolfman 2011W2 1

Finally…

1420911

810127

56

4

23

Page 24: CS221: Algorithms and Data Structures Lecture #3 Mind Your Priority Queues Steve Wolfman 2011W2 1

DeleteMin CodeObject deleteMin() {

assert(!isEmpty());

returnVal = Heap[0];

size--;

newPos =

percolateDown(0,

Heap[size]);

Heap[newPos] =

Heap[size];

return returnVal;

}

int percolateDown(int hole, Object val) {while (2*hole + 1 < size) { left = 2*hole + 1; right = left + 1; if (right < size && Heap[right] < Heap[left]) target = right; else target = left;

if (Heap[target] < val) { Heap[hole] = Heap[target]; hole = target; } else break; } return hole;}

runtime:

24

Page 25: CS221: Algorithms and Data Structures Lecture #3 Mind Your Priority Queues Steve Wolfman 2011W2 1

DeleteMin Code (Better!)Do yours this way!!!

Object deleteMin() {

assert(!isEmpty());

returnVal = Heap[0];

size--;

newPos =

percolateDown(0,

Heap[size]);

Heap[newPos] =

Heap[size];

return returnVal;

}

int percolateDown(int hole, Object val) {while (numChildren(hole) > 0) { left = getLeftChild(hole); right = getRightChild(hole); if (numChildren(hole) > 1 && Heap[right] < Heap[left]) target = right; else target = left;

if (Heap[target] < val) { Heap[hole] = Heap[target]; hole = target; } else break; } return hole;}

runtime:

25

Page 26: CS221: Algorithms and Data Structures Lecture #3 Mind Your Priority Queues Steve Wolfman 2011W2 1

Insert

201412911

81067

54

2

201412911

81067

54

2

pqueue.insert(3)

3

Invariant violated! What will we do? 26

Page 27: CS221: Algorithms and Data Structures Lecture #3 Mind Your Priority Queues Steve Wolfman 2011W2 1

Percolate Up

201412911

81067

54

2

3 201412911

8367

54

2

10

201412911

8567

34

2

10 201412911

8567

34

2

10 27

Page 28: CS221: Algorithms and Data Structures Lecture #3 Mind Your Priority Queues Steve Wolfman 2011W2 1

Insert Codevoid insert(Object o) {

assert(!isFull());

size++;

newPos =

percolateUp(size-1,o);

Heap[newPos] = o;

}

int percolateUp(int hole, Object val) { while (!isRoot(hole) && val < Heap[parent(hole)] Heap[hole] = Heap[parent(hole)]; hole = parent(hole); } return hole;}

runtime:

28Note: parent(hole) == (hole-1)/2

Page 29: CS221: Algorithms and Data Structures Lecture #3 Mind Your Priority Queues Steve Wolfman 2011W2 1

Today’s Outline

• Trees, Briefly• Priority Queue ADT• Heaps

– Implementing Priority Queue ADT

– Focus on Create: Heapify

– Brief introduction to d-Heaps

29

Page 30: CS221: Algorithms and Data Structures Lecture #3 Mind Your Priority Queues Steve Wolfman 2011W2 1

Closer Look at Creating Heaps

To create a heap given a list of items:

Create an empty heap.

For each item: insert into heap.

Time complexity?

a. O(lg n)

b. O(n)

c. O(n lg n)

d. O(n2)

e. None of these

111210

35

39, 4, 8, 1, 7, 2

30

Page 31: CS221: Algorithms and Data Structures Lecture #3 Mind Your Priority Queues Steve Wolfman 2011W2 1

A Better BuildHeapFloyd’s Method. Thank you, Floyd.

5 11 3 10 6 9 4 8 1 7 212

pretend it’s a heap and fix the heap-order property!

27184

96103

115

12Invariant violated!

Where can the orderinvariant be violated in general?a.Anywhereb.Non-leavesc.Non-roots 31

Page 32: CS221: Algorithms and Data Structures Lecture #3 Mind Your Priority Queues Steve Wolfman 2011W2 1

Build(this)Heap

67184

92103

115

12

671084

9213

115

12

1171084

9613

25

12

1171084

9653

21

12

32

Page 33: CS221: Algorithms and Data Structures Lecture #3 Mind Your Priority Queues Steve Wolfman 2011W2 1

Finally…

11710812

9654

23

1

runtime:

33

Page 34: CS221: Algorithms and Data Structures Lecture #3 Mind Your Priority Queues Steve Wolfman 2011W2 1

Build(any)Heap

This is as many violations as we can get.How do we fix them? Let’s play colouring games!

34

“amortized analysis!”

Page 35: CS221: Algorithms and Data Structures Lecture #3 Mind Your Priority Queues Steve Wolfman 2011W2 1

Today’s Outline

• Trees, Briefly• Priority Queue ADT• Heaps

– Implementing Priority Queue ADT

– Focus on Create: Heapify

– Brief introduction to d-Heaps

35

Page 36: CS221: Algorithms and Data Structures Lecture #3 Mind Your Priority Queues Steve Wolfman 2011W2 1

Thinking about Binary Heaps• Observations

– finding a child/parent index is a multiply/divide by two– operations jump widely through the heap– deleteMins look at all (two) children of some nodes– inserts only care about parents of some nodes– inserts are at least as common as deleteMins

• Realities– division and multiplication by powers of two are fast– looking at one new piece of data sucks in a cache line– with huge data sets, disk accesses dominate

36BUT… don’t solve a performance

problem until you know you have one!

Page 37: CS221: Algorithms and Data Structures Lecture #3 Mind Your Priority Queues Steve Wolfman 2011W2 1

4

9654

23

1

8 1012

7

11

Solution: d-Heaps

• Nodes have (up to) d children• Still representable by array• Good choices for d:

– optimize (non-asymptotic) performance based on ratio of inserts/removes

– make d a power of two for efficiency

– fit one set of children in a cache line

– fit one set of children on a memory page/disk block

3 7 2 8 5 12 11 10 6 91

d-heap mnemonic:d is for degree!37

Page 38: CS221: Algorithms and Data Structures Lecture #3 Mind Your Priority Queues Steve Wolfman 2011W2 1

To Do

• Read: KW Section 8.5• Keep working on homework!• Start preparing for the exam… it’s not that far

away!

38

Page 39: CS221: Algorithms and Data Structures Lecture #3 Mind Your Priority Queues Steve Wolfman 2011W2 1

Coming Up

• Sorting, sorting, and more sorting!• Midterm (Wed Feb 15, 5-7PM, loc’n TBD)

39