some systems applications require that items be …tadavis/cs303/ch06sm.pdf · chapter 6 heaps...
TRANSCRIPT
Chapter 6Heaps
Introduction
2
some systems applications require that items be processedin specialized ways
printingmay not be best to place on a queuesome jobs may be more ________________small 1-page jobs should be printed before a 100-pagejob
operating system _________________processes run only for a slice of timequeue: FIFO
some jobs may take too longother jobs are more important and should not wait
Heap Model
3
specialized queue requiredheap (priority queue)provides at least
_______________deleteMin: finds, returns, and removes min (or max)other operations common
used in other applicationsexternal sorting
algorithmsdiscrete event simulation
Heap Implementation
4
heaps can be implemented with, with insertions at head
insertdeleteMin
ordered linked list (worse due to number of )insertdeleteMin
binary search treeinsertdeleteMinsince only min is deleted, tree will be _______________overkill since other included operations not required
Binary Heap
5
binary heap commonsimply termed heap
two properties________________heap _____________
operations can one of the propertiesoperation must continue until heap properties have beenrestored, which is typically simple
Binary Heap
6
structure propertycompletely filled
except level, which is filled from left to rightcomplete binary tree has between and nodesheight:
can be represented with an array (no necessary)array position
left child inright child inparent in
operations simplemaximum heap size must be ______________________
Binary Heap
7
Binary Heap
8
heap-order propertyallows operations to be performed quicklywant to be able to find minimum quickly
smallest element at ___________any should also be a heap
any node should be than all its descendants
Heap Operations
9
two main heap operationsinsertdeleteMin
insertneed to maintain structure propertycreate in next available location (at bottom of tree)place new value there if possibleotherwise, slide into hole and bubble up holecontinue until new value can be insertedtermed strategy
Binary Heap
10
example: insert 14
Heap Operations
11
insert (cont.)could have used repeated swaps, but a swap requires_____________ assignmentsif element percolated up levels
swap method: assignmentsnon-swap method: assignments
if new element is smaller than all others in heap, hole willpercolate to the __________hole will be at index 1 and we will break out of the loop
if extra check for 1 in loop, adds unnecessary timecould place a copy of new value in position ____
Heap Operations
12
insert (cont.)could require as much ason average, percolation terminates ___________
on average 2.607 comparisons are required
Heap Operations
13
deleteMinfinding minimum easy
minimum more difficulthole is created at root
element in complete binary tree must moveif last element can be placed in hole, done
child into holehole slides down one levelrepeat until last element can be placed in hole
worst case:average case:
Binary Heap
14
example: deleteMin (13)
Binary Heap
15
example: deleteMin (13) (cont.)
Heap Operations
16
other heap operationsfinding minimum ___________finding maximum not possible without scanthrough entire heap
maximum in one of the leavescould use separate data structure, such as _____________
Heap Operations
17
other heap operations (cont.)decreaseKey
lowers value at position by given amountpercolate up
example: change process for more run timeincreaseKey
increases value at position by given amountpercolate down
example: drop process priority if taking ______________remove
decreaseKey to root, then deleteMinexample: process is by user
Heap Operations
18
other heap operations (cont.)buildHeap
place items into the tree in any ____________maintains propertyuse percolateDown( ) from node
Binary Heap
19
example: start with random placementstart with percolateDown(7)each dashed line represents 2 comparisons
Binary Heap
20
example: (cont.)
Heap Applications
21
selection problemfrom a list of elements, find th largestoriginal algorithm
list and index th elementwith simple sort,
alternative algorithmread ____ elements into array and sort themsmallest is in th positionother elements processed one by one, placing them intothe arrayrunning time
if ,
Heap Applications
22
selection problem (cont.)if heap is used
build heap of elementsperform deleteMin operations
element extracted is th smallest elementif ,
another algorithm using heapas in previous algorithm, but put elements in heap
is in th positionother elements processed one by one, placing them intothe arrayrunning time
Heap Applications
23
event simulationbank where customers arrive and wait until one of tellersis available
customer and basedon probability distribution functioncompute statistics on the length of time a customer mustwait, or the length of the lineneed to consider event that will occur in the least amountof time
heap can be used to order events
-Heaps
24
-heapsame as heap, but all nodes have childrentends to be more than binary heaprunning time reduced todeleteMin more expensive since more _______________requireduseful when heap is too large to fit entirely into mainmemory4-heaps may outperform 2-heaps (binary heaps)
-Heaps
25
example: -heap with
Leftist Heaps
26
one weakness of heaps so far is two heapsis difficultthree data structures that can help
heapsheaps
binomial queues
Leftist Heaps
27
leftist heapscan be difficult to design a data structure for ____________that uses an array, but runs efficiently
linked data structure therefore requiredleftist heap
structure and ordering properties of binary heapsdifference is that heap is not ______________________
very unbalanced is desired
Leftist Heaps
28
leftist heaps (cont.)null path length (npl): length of shortest path from currentnode to a node without children
npl of a node with zero or one child is 0npl of a null pointer is -1
npl of each node is 1 more than the minimum of the nullpath lengths of its ________________leftist heap property
npl of the left child is at least as large as that of the rightchildbiases tree to left subtree
Leftist Heaps
29
example: leftist heap
Leftist Heaps
30
leftist heaps operationsall work should be done on the right path, which isguaranteed to be short
and may destroy the leftistheap property
not difficult to fix
Leftist Heaps
31
leftist heaps operations (cont.)merging
insertion special case of merging a 1-node heap with alarger heapif either of the two heaps is , return the otherheapotherwise, compare __________
merge heap with the larger root withthe right subheap of the heap with the smaller root
Leftist Heaps
32
leftist heaps operations (cont.)merging example
Leftist Heaps
33
leftist heaps operations (cont.)merging example
Leftist Heaps
34
leftist heaps operations (cont.)merging example
result is not leftist: left npl = 1, right npl = 2
Leftist Heaps
35
leftist heaps operations (cont.)merging example
fix by swapping children
Skew Heaps
36
self-adjusting version of leftist heaprelationship of skew heap to leftist heap is analogous to splaytrees and treesskew heaps
binary trees with heap orderbut no structural constraint
no information kept about null path lengthright path can be arbitrarily _________
worst case of all operations:for operations, total worst case: , or__________________
Skew Heaps
37
skew heaps (cont.)fundamental operation is mergingafter merging, for leftists heaps, check both children forstructure and swap children if neededin skew heaps, swap children
except nodes on right paths do not swapchildrenno extra required to maintain path lengthsno tests required to determine when to swap children
Skew Heaps
38
skew heaps examplemerge two skew heaps
tree with larger root will merge onto tree with smaller root
Skew Heaps
39
skew heaps example (cont.)recursively merge with the subheap of rooted at 8
heap happens to be ______________
Skew Heaps
40
skew heaps example (cont.)make this heap the new left child of and the old left childof becomes the new right child
Binomial Queues
41
binomial queueskeep a collection of heap-ordered trees, known as a____________at most one binomial tree of every ____________
heap order imposed on each binomial treecan represent any priority queueexample: a priority queue of size 13 can be representedby the forest or 1101
worst case of all operations:
Binomial Queues
42
binomial queues example
Binomial Queues
43
binomial queue of size 6 example
Binomial Queues
44
binomial queue operationsminimum element found by scanning of all trees
found incan keep ongoing information to reduce to
merging two queuesmerge takes
Binomial Queues
45
merge example
merge of two trees
Binomial Queues
46
merge example (cont.)now we have three binomial trees of height 3; keep oneand merge the other two
Binomial Queues
47
merge values 1 7 exampleinsert 1
insert 2
insert 3
insert 4
Binomial Queues
48
merge values 1 7 example (cont.)insert 5
insert 6
insert 7
Binomial Queues
49
deleteMin example
separate tree with minimum root from rest of tree
remaining trees after removing min
Binomial Queues
50
deleteMin example (cont.)after merge