chapter 6 heaps - william & mary
TRANSCRIPT
Chapter 6
Heaps
Introduction
2
âsome systems applications require that items be processed
in specialized ways
âprinting
âmay not be best to place on a queue
âsome jobs may be more ________________
âsmall 1-page jobs should be printed before a 100-page
job
âoperating system _________________
âprocesses run only for a slice of time
âqueue: FIFO
âsome jobs may take too long
âother jobs are more important and should not wait
Heap Model
3
âspecialized queue required
âheap (priority queue)
âprovides at least
â_______________
âdeleteMin: finds, returns, and removes min (or max)
âother operations common
âused in other applications
âexternal sorting
â algorithms
âdiscrete event simulation
Heap Implementation
4
âheaps can be implemented with
â , with insertions at head
â insert đ 1
âdeleteMin đ đ
âordered linked list (worse due to number of )
â insert đ đ
âdeleteMin đ 1
âbinary search tree
â insert đ log đ
âdeleteMin đ log đ
âsince only min is deleted, tree will be _______________
âoverkill since other included operations not required
Binary Heap
5
âbinary heap common
âsimply termed heap
â two properties
â________________
âheap _____________
âoperations can one of the properties
âoperation must continue until heap properties have been
restored, which is typically simple
Binary Heap
6
âstructure property
âcompletely filled
âexcept level, which is filled from left to right
âcomplete binary tree has between 2â and 2â+1 â 1 nodes
âheight: log đ
âcan be represented with an array (no necessary)
âarray position đ
âleft child in 2đ
âright child in 2đ + 1
âparent in đ/2
âoperations simple
âmaximum heap size must be ______________________
Binary Heap
7
Binary Heap
8
âheap-order property
âallows operations to be performed quickly
âwant 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 operations
â insert
âdeleteMin
â insert
âneed to maintain structure property
âcreate in next available location (at bottom of tree)
âplace new value there if possible
âotherwise, slide into hole and bubble up hole
âcontinue until new value can be inserted
â termed strategy
Binary Heap
10
âexample: insert 14
Heap Operations
11
â insert (cont.)
âcould have used repeated swaps, but a swap requires
_____________ assignments
â if element percolated up đ levels
âswap method: 3đ assignments
ânon-swap method: đ + 1 assignments
â if new element is smaller than all others in heap, hole will
percolate 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 time
âcould place a copy of new value in position ____
Heap Operations
12
â insert (cont.)
âcould require as much as đ log đ
âon average, percolation terminates ___________
âon average 2.607 comparisons are required
Heap Operations
13
âdeleteMin
â finding minimum easy
â minimum more difficult
âhole is created at root
â element in complete binary tree must move
â if last element can be placed in hole, done
âotherwise, slide holeâs child into hole
âhole slides down one level
ârepeat until last element can be placed in hole
âworst case: đ log đ
âaverage case: đ log đ
Binary Heap
14
âexample: deleteMin (13)
Binary Heap
15
âexample: deleteMin (13) (cont.)
Heap Operations
16
âother heap operations
â finding minimum ___________
â finding maximum not possible without scan
through entire heap
âmaximum in one of the leaves
âcould use separate data structure, such as _____________
Heap Operations
17
âother heap operations (cont.)
âdecreaseKey
â lowers value at position đ by given amount
âpercolate up
âexample: change process for more run time
â increaseKey
â increases value at position đ by given amount
âpercolate down
âexample: drop process priority if taking ______________
â remove
âdecreaseKey to root, then deleteMin
âexample: process is by user
Heap Operations
18
âother heap operations (cont.)
âbuildHeap
âplace đ items into the tree in any ____________
âmaintains property
âuse percolateDown(đ) from node đ
Binary Heap
19
âexample: start with random placement
âstart with percolateDown(7)
âeach dashed line represents 2 comparisons
Binary Heap
20
âexample: (cont.)
Heap Applications
21
âselection problem
â from a list of đ elements, find đth largest
âoriginal algorithm
â list and index đth element
âwith simple sort, đ đ2
âalternative algorithm
âread ____ elements into array and sort them
âsmallest is in đth position
âother elements processed one by one, placing them into
the array
ârunning time đ đ â đ
âif đ = đ/2 , đ đ2
Heap Applications
22
âselection problem (cont.)
â if heap is used
âbuild heap of đ elements
âperform đ deleteMin operations
â element extracted is đth smallest element
â if đ = đ/2 , ÏŽ đ log đ
âanother algorithm using heap
âas in previous algorithm, but put đ elements in heap
â is in đth position
âother elements processed one by one, placing them into
the array
ârunning time ÏŽ đ log đ
Heap Applications
23
âevent simulation
âbank where customers arrive and wait until one of đ tellers
is available
âcustomer and based
on probability distribution function
âcompute statistics on the length of time a customer must
wait, or the length of the line
âneed to consider event that will occur in the least amount
of time
âheap can be used to order events
đ-Heaps
24
âđ-heap
âsame as heap, but all nodes have đ children
â tends to be more than binary heap
â running time reduced to đ logđ đ
âdeleteMin more expensive since more _______________
required
âuseful when heap is too large to fit entirely into main
memory
â4-heaps may outperform 2-heaps (binary heaps)
đ-Heaps
25
âexample: đ-heap with đ = 3
Leftist Heaps
26
âone weakness of heaps so far is two heaps
is difficult
â three data structures that can help
â heaps
â heaps
âbinomial queues
Leftist Heaps
27
â leftist heaps
âcan be difficult to design a data structure for ____________
that uses an array, but runs efficiently
â linked data structure therefore required
â leftist heap
âstructure and ordering properties of binary heaps
âdifference is that heap is not ______________________
âvery unbalanced is desired
Leftist Heaps
28
â leftist heaps (cont.)
ânull path length (npl): length of shortest path from current
node to a node without children
ânpl of a node with zero or one child is 0
ânpl of a null pointer is -1
ânpl of each node is 1 more than the minimum of the null
path lengths of its ________________
â leftist heap property
ânpl of the left child is at least as large as that of the right
child
âbiases tree to left subtree
Leftist Heaps
29
âexample: leftist heap
Leftist Heaps
30
â leftist heaps operations
âall work should be done on the right path, which is
guaranteed to be short
â and may destroy the leftist
heap property
ânot difficult to fix
Leftist Heaps
31
â leftist heaps operations (cont.)
âmerging
â insertion special case of merging a 1-node heap with a
larger heap
â if either of the two heaps is , return the other
heap
âotherwise, compare __________
â merge heap with the larger root with
the 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
âmust swap children anytime subtree non-leftist
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 heap
â relationship of skew heap to leftist heap is analogous to splay
trees and trees
âskew heaps
âbinary trees with heap order
âbut no structural constraint
âno information kept about null path length
âright path can be arbitrarily _________
âworst case of all operations: đ đ
â for đ operations, total worst case: đ đ log đ , or đ log đ
__________________
Skew Heaps
37
âskew heaps (cont.)
â fundamental operation is merging
âafter merging, for leftists heaps, check both children for
structure and swap children if needed
â in skew heaps, swap children
âexcept nodes on right paths do not swap
children
âno extra required to maintain path lengths
âno tests required to determine when to swap children
Skew Heaps
38
âskew heaps example
âmerge two skew heaps
âtree with larger root will merge onto tree with smaller root
Skew Heaps
39
âskew heaps example (cont.)
â recursively merge đ»2 with the subheap of đ»1 rooted at 8
âheap happens to be ______________
Skew Heaps
40
âskew heaps example (cont.)
âmake this heap the new left child of đ»1 and the old left child
of đ»1 becomes the new right child
Binomial Queues
41
âbinomial queues
âkeep a collection of heap-ordered trees, known as a
____________
âat most one binomial tree of every ____________
âheap order imposed on each binomial tree
âcan represent any priority queue
âexample: a priority queue of size 13 can be represented
by the forest đ”3, đ”2, đ”0 or 1101
âworst case of all operations:đ log đ
Binomial Queues
42
âbinomial queues example
Binomial Queues
43
âbinomial queue of size 6 example
Binomial Queues
44
âbinomial queue operations
âminimum element found by scanning of all trees
âfound in đ log đ
âcan keep ongoing information to reduce to đ 1
âmerging two queues
âmerge takes đ log đ
Binomial Queues
45
âmerge example
âmerge of two đ”1 trees
Binomial Queues
46
âmerge example (cont.)
ânow we have three binomial trees of height 3; keep one
and merge the other two (with two smallest root)
Binomial Queues
47
âmerge values 1â7 example
â insert 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