Upload
nitesh
View
30
Download
0
Tags:
Embed Size (px)
DESCRIPTION
Heaps and Priority Queues Basic idea in this chapter: We prioritize data structures for special purposes Heap is a kind of tree structure Use a heaps to implement a priority queue. Content. Motivation: the need for priority queuing ? Fair/unfair? Background (queues and tree structures) - PowerPoint PPT Presentation
Citation preview
1heapsheaps
Heaps and Priority QueuesHeaps and Priority Queues
Basic idea in this chapter: Basic idea in this chapter:
We prioritize data structures for special We prioritize data structures for special
purposes purposes
Heap is a kind of tree structureHeap is a kind of tree structure
Use a heaps to implement a priority Use a heaps to implement a priority
queuequeue
2heapsheaps Dr.AlagozDr.Alagoz
ContentContent
• Motivation: the need for priority queuing ? Fair/unfair?Motivation: the need for priority queuing ? Fair/unfair?
• Background (queues and tree structures) Background (queues and tree structures)
• What is a heapWhat is a heap
• Heap types: Leftist, Binomial, d-type, skew Heap types: Leftist, Binomial, d-type, skew
• Merging two heapsMerging two heaps
• Analysis and implementation detailsAnalysis and implementation details
3heapsheaps Dr.AlagozDr.Alagoz
Priority Queue (Heaps)Priority Queue (Heaps)
•Priority Queue is a data structure Priority Queue is a data structure allowing at least the following two allowing at least the following two operations: operations:
•insertinsert same like same like enqueue enqueue in nonpriority in nonpriority queuesqueues
•deleteMin deleteMin is the priority queue equivalent of is the priority queue equivalent of queue’s queue’s dequeu dequeu operationoperation (i.e. find and (i.e. find and remove the minimum element in prioritized remove the minimum element in prioritized queue) queue)
•Efficient implementation of priority Efficient implementation of priority queue ADTsqueue ADTs
•Uses and implementation of priority Uses and implementation of priority queuesqueues
4heapsheaps Dr.AlagozDr.Alagoz
Priority Queue: Priority Queue: ApplicationsApplications
•External sortingExternal sorting
•Discrete-event simulationsDiscrete-event simulations•Very important for greedy algorithms Very important for greedy algorithms (CHP9) (CHP9)
and optimization problems (CHP10) and optimization problems (CHP10) •Example: ARAM system is greedy!!! Example: ARAM system is greedy!!!
5heapsheaps Dr.AlagozDr.Alagoz
Motivation … Motivation … ProblemProblem
• Queue: the fancy name for waiting in Queue: the fancy name for waiting in line line
1 4 2
6heapsheaps Dr.AlagozDr.Alagoz
Motivation … Motivation … Problem - At the Bank, bust Problem - At the Bank, bust stopstop• Queue at the counter or bus stopQueue at the counter or bus stop
• Give priority to Give priority to – ElderlyElderly
– PregnantPregnant
– With babyWith baby
– ……
1 4 2
7heapsheaps Dr.AlagozDr.Alagoz
Motivation … Motivation … Problem – HospitalProblem – Hospital
• Patients waiting for help in Emergency Patients waiting for help in Emergency RoomRoom
• Give priority to Give priority to – Severely woundedSeverely wounded– Bleading Bleading – … …
– the ones with cthe ones with crrash !!!ash !!!
1 4 2
8heapsheaps Dr.AlagozDr.Alagoz
Motivation … Motivation … Problem - Operating Problem - Operating SystemSystem
• Processes waiting for servicesProcesses waiting for services
– PrinterPrinter– Multiuser environment Multiuser environment – CPUCPU
• Give priority to Give priority to – I/O boundI/O bound– InterrupsInterrups– Eg. small jobs(1page print) may be given priority Eg. small jobs(1page print) may be given priority
over large jobs (100pages) …over large jobs (100pages) …
1 4 2
9heapsheaps Dr.AlagozDr.Alagoz
Review of QueuesReview of Queues
10heapsheaps Dr.AlagozDr.Alagoz
Review QueueReview QueueFIFO FIFO
– first come first come – first servefirst serve
• One entry placeOne entry place
• One exit placeOne exit place
• No change in No change in orderorder
• No priorityNo priority
• ARAM system is ARAM system is priority based!!!priority based!!!
public interface Queue extends Container {Object getHead();void enqueue(Object object);Object dequeue();
}
1 4 2
G/G/m/KG/G/m/K
– General arrival process General arrival process timetime
– General service process General service process timetime
– m serversm servers
– K buffer sizeK buffer size
• Example M/M/1: Example M/M/1:
Exponential arrival timeExponential arrival time
Exponential service timeExponential service time
Single server queueSingle server queue
11heapsheaps Dr.AlagozDr.Alagoz
Review Queue ADT…Review Queue ADT…
Sample operationSample operation
1enqueque(4)
1 4enqueque(2)
1 4 2enqueque(3)
1 4 2 3dequeque()
4 2 3dequeque()
2 3enqueque(1)
2 3 1enqueque(1)
2 3 1 1dequeque()
3 1 1dequeque()
1 1dequeque()
1
12heapsheaps Dr.AlagozDr.Alagoz
Review Queue …Review Queue …
PrioritiesPriorities - a few- a few
• If there is a need for change in the If there is a need for change in the orderorder– One queue for each priorityOne queue for each priority
a1 a4
b3 b1 b27 b3 b3
c6 c4 c9
AlgorithmAlgorithm• enqueueenqueue
– put in to proper put in to proper queuequeue
• dequeuedequeue– get from P1 firstget from P1 first– then get from P2then get from P2– then get from P3then get from P3
P1
P2
P3
13heapsheaps Dr.AlagozDr.Alagoz
Review Queue …Review Queue …
PrioritiesPriorities - more- more
• If there is a need for change in the orderIf there is a need for change in the order– One queue for each priorityOne queue for each priority
– Number of different priority categories is Number of different priority categories is knownknown
a1 a4
b3 b1 b27 b3 b3
c6 c4 c9
P1
P2
P3
AlgorithmAlgorithm• enqueueenqueue
– put in to proper put in to proper queuequeue
• dequeuedequeue– get from P1 firstget from P1 first– then get from P2then get from P2– then get from P3then get from P3– ……– then get from P123then get from P123
7 12 41P123
...
14heapsheaps Dr.AlagozDr.Alagoz
Review Queue …Review Queue …
PrioritiesPriorities – too many– too many
a1 a4
b3 b1 b27 b3 b3
c6 c4 c9
P1
P2
P3
AlgorithmAlgorithm• enqueueenqueue
– put in to proper put in to proper queuequeue
• dequeuedequeue– get from P1 firstget from P1 first– then get from P2then get from P2– then get from P3then get from P3– ……– then get from P123then get from P123
7 12 41P123
...
• If there is a need for change in the orderIf there is a need for change in the order– One queue for each priorityOne queue for each priority
– Number of different priority categories is Number of different priority categories is ununknownknown
15heapsheaps Dr.AlagozDr.Alagoz
Priority QueuesPriority Queues
16heapsheaps Dr.AlagozDr.Alagoz
Priority Queues …Priority Queues …AbstractionsAbstractions
• Priority queuePriority queue– A list of items A list of items
– Each item has an associated priorityEach item has an associated priority
• InsertionInsertion– Arbitrary orderArbitrary order
– Arbitrary priorityArbitrary priority
• DeletionDeletion– Item with the lowest (highest) priorityItem with the lowest (highest) priority
17heapsheaps Dr.AlagozDr.Alagoz
Priority Queues … Priority Queues … Abstract OperationsAbstract Operations
• enqueueenqueue– Puts an object in the containerPuts an object in the container
• findMinfindMin– Returns a reference to the smallest Returns a reference to the smallest
object in the containerobject in the container
• dequeueMin dequeueMin – Removes the smallest object from the Removes the smallest object from the
containercontainer
18heapsheaps Dr.AlagozDr.Alagoz
Simple ImplementationsSimple Implementationsfor priority queuefor priority queue
a) Simple link lista) Simple link list
b) Binary search tree b) Binary search tree
19heapsheaps Dr.AlagozDr.Alagoz
Simple Implementations … Simple Implementations … Using Simple Link ListsUsing Simple Link Lists
• Link is Unsorted ArrayLink is Unsorted Array– InsertInsert at the front in O(1) at the front in O(1)
– DeleteDelete after searching the min in O(n) after searching the min in O(n)
• Link is Sorted ArrayLink is Sorted Array– InsertInsert in the order in O(n) in the order in O(n)
– DeleteDelete at the end in O(1) at the end in O(1)
• Not: Use Unsorted Array, when there are Not: Use Unsorted Array, when there are more more insertionsinsertions than than deleteMindeleteMin. .
20heapsheaps Dr.AlagozDr.Alagoz
Simple Implementations … Simple Implementations … Using Binary Search TreeUsing Binary Search Tree
• Average running time for InsertAverage running time for Insert– O (log n)O (log n)
• Average running time for DeletionAverage running time for Deletion– O (log n)O (log n)
Now, lets talk about the heaps in class Now, lets talk about the heaps in class hierarchy hierarchy
a
b c
21heapsheaps Dr.AlagozDr.Alagoz
HeapHeap
A heap is a binary tree storing keys A heap is a binary tree storing keys at its nodes at its nodes
In order to understand the heap In order to understand the heap structures lets review general tree structures lets review general tree
structuresstructures
22heapsheaps Dr.AlagozDr.Alagoz
ReReviewviewN-ary TreeN-ary Tree
DefinitionDefinition
An An N-ary treeN-ary tree T is a finite set of nodes with the following properties: T is a finite set of nodes with the following properties:
1.1. Either the set is empty, Either the set is empty, T = T = ; or ; or
2.2. The set consists of a root, R, and exactly N distinct N-ary trees. That is, the The set consists of a root, R, and exactly N distinct N-ary trees. That is, the remaining nodes are partitioned into Nremaining nodes are partitioned into N0 subsets,0 subsets,TT0 0 , T, T11, … , T, … , TN-1N-1 , each of which is an N-ary tree such that , each of which is an N-ary tree such that T = {R , TT = {R , T0 0 , T, T11, … , T, … , TN-1N-1 }. }.
RemarkRemark The empty tree, T = The empty tree, T = , is a tree, is a tree
DefinitionDefinition The empty trees are called The empty trees are called external nodesexternal nodes because they have no subtrees and because they have no subtrees and therefore appear at the extremities of the tree. Conversely, the non-empty trees are therefore appear at the extremities of the tree. Conversely, the non-empty trees are called called internal nodesinternal nodes..
23heapsheaps Dr.AlagozDr.Alagoz
HeapHeap
DefinitionDefinition
A A (Min) Heap (Min) Heap is a tree, is a tree, T = {R , TT = {R , T0 0 , T, T11, … , T, … , Tn-1n-1 } } with the following properties: with the following properties:
1.1. Every subtree of T is a heap; and, Every subtree of T is a heap; and,
2.2. The root of T is less than or equal to the root of every The root of T is less than or equal to the root of every subtree of T. subtree of T. That is, R That is, R R Rii for all i, 0 for all i, 0 i i n , where R n , where Rii is the root of T is the root of Tii . .
R
R1 R2 RN…
Observations• Each node is less then all the subtrees of it.• No restrictions for the relative ordering of the subtrees.
24heapsheaps Dr.AlagozDr.Alagoz
Binary HeapBinary Heap
25heapsheaps Dr.AlagozDr.Alagoz
RecapRecapPerfect Binary TreePerfect Binary Tree
DefinitionDefinition
A A perfect binary treeperfect binary tree of height of height hh00 is a is a binary tree T = binary tree T = {R, T{R, TLL, T, TRR} }
with the following properties: with the following properties:
1.1. If h = 0, TIf h = 0, TLL = = and T and TRR = = . .
2.2. If h > 0, thenIf h > 0, thenTTLL is a perfect binary tree of height h-1. is a perfect binary tree of height h-1. TTRR is a perfect binary tree of height h-1. is a perfect binary tree of height h-1.
TheoremTheorem A perfect binary tree of height h has exactly 2A perfect binary tree of height h has exactly 2h+1h+1 - 1 nodes. - 1 nodes.
TheoremTheoremThe height of a perfect binary tree with n internal nodes is The height of a perfect binary tree with n internal nodes is log (n+1). log (n+1).
26heapsheaps Dr.AlagozDr.Alagoz
Complete Binary TreeComplete Binary Tree
DefinitionDefinition
A A complete binary tree complete binary tree of height h of height h0, is a 0, is a binary tree T={R, Tbinary tree T={R, TLL, T, TRR} } with the following properties: with the following properties:
1.1. If h=0, TIf h=0, TLL = = and T and TRR = = . .
2.2. For h>0 there are two possibilities: For h>0 there are two possibilities:
a.a. TTLL is a perfect binary tree of height h-2 and is a perfect binary tree of height h-2 and TTRR is a complete binary tree of height h-1 is a complete binary tree of height h-1
b.b. TTLL is a complete binary tree of height h-1 and is a complete binary tree of height h-1 and TTRR is a perfect binary tree of height h-2. is a perfect binary tree of height h-2.
27heapsheaps Dr.AlagozDr.Alagoz
Complete Binary Tree …Complete Binary Tree …• 11
– 11LL
• 22LL
• 22RR is a complete binary tree of height 2 is a complete binary tree of height 2– 55LL is a perfect binary tree of height 1 is a perfect binary tree of height 1– 55RR is a perfect binary tree of height 0 is a perfect binary tree of height 0
– 11RR
5L 5R
2R
28heapsheaps Dr.AlagozDr.Alagoz
Complete Binary Tree …Complete Binary Tree …• 11
– 11LL is a complete binary tree of height 3 is a complete binary tree of height 3 (2a)(2a)
• 22LL is a perfect binary tree of height 2 is a perfect binary tree of height 2
• 22RR is a complete binary tree of height 2 is a complete binary tree of height 2– 55LL is a perfect binary tree of height 1 is a perfect binary tree of height 1– 55RR is a perfect binary tree of height 0 is a perfect binary tree of height 0
– 11RR
2L 2R
1L
29heapsheaps Dr.AlagozDr.Alagoz
Complete Binary Tree …Complete Binary Tree …• 1 is a complete binary tree of height 4 1 is a complete binary tree of height 4 (2b)(2b)
– 11LL is a complete binary tree of height 3 is a complete binary tree of height 3 (2a)(2a)
• 22LL is a perfect binary tree of height 2 is a perfect binary tree of height 2
• 22RR is a complete binary tree of height 2 is a complete binary tree of height 2– 55LL is a perfect binary tree of height 1 is a perfect binary tree of height 1– 55RR is a perfect binary tree of height 0 is a perfect binary tree of height 0
– 11RR is a perfect binary tree of height 2 is a perfect binary tree of height 2
1L 1R
30heapsheaps Dr.AlagozDr.Alagoz
Complete Binary Tree …Complete Binary Tree …Array representationArray representation
a
b c
b = 2a
c = 2a+1
a = b/2a = c/2
31heapsheaps Dr.AlagozDr.Alagoz
Complete Binary Tree …Complete Binary Tree …
TheoremTheorem
A complete binary tree of height hA complete binary tree of height h0 0 contains contains at least 2at least 2hh and and at most 2at most 2h+1 h+1 - 1 nodes.- 1 nodes.
32heapsheaps Dr.AlagozDr.Alagoz
Complete Binary Tree …Complete Binary Tree …
TheoremTheorem
The internal path length of a The internal path length of a binary treebinary tree with n nodes with n nodes is at least as big as is at least as big as the internal path length of a the internal path length of a complete binary treecomplete binary tree with n with n nodes. nodes.
DefinitionDefinition
The The internal path lengthinternal path length of a tree is simply the sum of the of a tree is simply the sum of the depths (levels) of all the internal nodes in the treedepths (levels) of all the internal nodes in the tree
n
iidthLengthinternalPa
1
ddii is the depth of is the depth of the ithe ithth node node
n is the number n is the number of nodesof nodes
33heapsheaps Dr.AlagozDr.Alagoz
Complete N-ary TreeComplete N-ary Tree
InformallyInformally
• a complete tree is a tree in which a complete tree is a tree in which – all the levels are full except for the all the levels are full except for the
bottom level and bottom level and
– the bottom level is filled from left to the bottom level is filled from left to right.right.
34heapsheaps Dr.AlagozDr.Alagoz
Complete N-ary Tree …Complete N-ary Tree …
DefinitionDefinition
A A complete N-ary treecomplete N-ary tree of height of height hh00, is an , is an N-ary tree N-ary tree T = {R , TT = {R , T0 0 , T, T11, … , T, … , TN-1N-1 } }with the following properties with the following properties
1.1. If h=0, If h=0, TTii = = for all i, 0 for all i, 0 i < N. i < N.
2.2. For h>0 there exists a j, 0 For h>0 there exists a j, 0 j < N such that j < N such that a. Ta. Tii is a perfect binary tree of height h-1 for all i, 0 is a perfect binary tree of height h-1 for all i, 0 i i < j < j b. Tb. Tjj is a complete binary tree of height h-1 is a complete binary tree of height h-1 c. c. TTii is a perfect binary tree of height h-2 for all i, is a perfect binary tree of height h-2 for all i, j<i<N j<i<N
35heapsheaps Dr.AlagozDr.Alagoz
Complete N-ary Tree … Complete N-ary Tree … Array RepresentationArray Representation
c1
Children of node i
c1 = N(i-1)+2
c2 = N(i-1)+3
c3 = N(i-1)+4
…
cN = Ni+i
Parent of node i
(i-1)N
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
c2 cN…
i
36heapsheaps Dr.AlagozDr.Alagoz
Heaps (§)Heaps (§)
• A heap is a binary tree A heap is a binary tree storing keys at its nodes storing keys at its nodes and satisfying the following and satisfying the following properties:properties:– Heap-Order:Heap-Order: for every for every
internal node internal node vv other than other than the root,the root,keykey((vv)) keykey((parentparent((vv))))
– Complete Binary Tree:Complete Binary Tree: let let hh be the height of the be the height of the heapheap
• for for i i 0, … , 0, … , h h 1,1, there there are are 22ii nodes of depth nodes of depth ii
• at depth at depth hh 1 1, the internal , the internal nodes are to the left of nodes are to the left of the external nodesthe external nodes
2
65
79
• The last node of a The last node of a heap is the rightmost heap is the rightmost node of depth node of depth hh
last node
37heapsheaps Dr.AlagozDr.Alagoz
Height of a Heap (§)Height of a Heap (§)
• Theorem:Theorem: A heap storing A heap storing nn keys has height keys has height OO(log (log nn))
Proof: (we apply the complete binary tree property)Proof: (we apply the complete binary tree property)
– Let Let hh be the height of a heap storing be the height of a heap storing n n keyskeys
– Since there are Since there are 22ii keys at depth keys at depth ii 0, … , 0, … , h h 1 1 and at least one key and at least one key at depth at depth hh, we have , we have nn 1 1 2 2 4 4 … … 2 2hh1 1 11
– Thus, Thus, nn 22hh , i.e., , i.e., hh log log nn
1
2
2h1
1
keys
0
1
h1
h
depth
38heapsheaps Dr.AlagozDr.Alagoz
Heaps and Priority QueuesHeaps and Priority Queues
• We can use a heap to implement a priority queueWe can use a heap to implement a priority queue
• We store a (key, element) item at each internal nodeWe store a (key, element) item at each internal node
• We keep track of the position of the last nodeWe keep track of the position of the last node
• For simplicity, we show only the keys in the picturesFor simplicity, we show only the keys in the pictures(2, Sue)
(6, Mark)(5, Pat)
(9, Jeff) (7, Anna)
39heapsheaps Dr.AlagozDr.Alagoz
Insertion into a Heap Insertion into a Heap (§)(§)
• Method insertItem of Method insertItem of the priority queue ADT the priority queue ADT corresponds to the corresponds to the insertion of a key insertion of a key kk to to the heapthe heap
• The insertion algorithm The insertion algorithm consists of three stepsconsists of three steps
– Find the insertion node Find the insertion node zz (the new last node) (the new last node)
– Store Store kk at at zz
– Restore the heap-order Restore the heap-order property (discussed property (discussed next)next)
2
65
79
insertion node
2
65
79 1
z
z
40heapsheaps Dr.AlagozDr.Alagoz
Upheap (percolate up)Upheap (percolate up)
• After the insertion of a new key After the insertion of a new key kk, the heap-order property may be , the heap-order property may be violatedviolated
• Algorithm upheap restores the heap-order property by swapping Algorithm upheap restores the heap-order property by swapping kk along an upward path from the insertion nodealong an upward path from the insertion node
• Upheap terminates when the key Upheap terminates when the key kk reaches the root or a node reaches the root or a node whose parent has a key smaller than or equal to whose parent has a key smaller than or equal to kk
• Since a heap has height Since a heap has height OO(log (log nn)), upheap runs in , upheap runs in OO(log (log nn)) time time
2
15
79 6z
1
25
79 6z
41heapsheaps Dr.AlagozDr.Alagoz
Removal from a Heap (§)Removal from a Heap (§)
• Method removeMin of the Method removeMin of the priority queue ADT priority queue ADT corresponds to the corresponds to the removal of the root key removal of the root key from the heapfrom the heap
• The removal algorithm The removal algorithm consists of three stepsconsists of three steps
– Replace the root key Replace the root key with the key of the last with the key of the last node node ww
– Remove Remove ww
– Restore the heap-order Restore the heap-order property (discussed property (discussed next)next)
2
65
79
last node
w
7
65
9w
new last node
42heapsheaps Dr.AlagozDr.Alagoz
Downheap (percolate Downheap (percolate down)down)
• After replacing the root key with the key After replacing the root key with the key kk of the last node, the of the last node, the heap-order property may be violatedheap-order property may be violated
• Algorithm downheap restores the heap-order property by swapping Algorithm downheap restores the heap-order property by swapping key key kk along a downward path from the root along a downward path from the root
• Upheap terminates when key Upheap terminates when key kk reaches a leaf or a node whose reaches a leaf or a node whose children have keys greater than or equal to children have keys greater than or equal to kk
• Since a heap has height Since a heap has height OO(log (log nn)), downheap runs in , downheap runs in OO(log (log nn)) time time
7
65
9w
5
67
9w
43heapsheaps Dr.AlagozDr.Alagoz
Updating the Last NodeUpdating the Last Node
• The insertion node can be found by traversing a path of The insertion node can be found by traversing a path of OO(log (log nn) ) nodesnodes
– Go up until a left child or the root is reachedGo up until a left child or the root is reached
– If a left child is reached, go to the right childIf a left child is reached, go to the right child
– Go down left until a leaf is reachedGo down left until a leaf is reached
• Similar algorithm for updating the last node after a removalSimilar algorithm for updating the last node after a removal
•http://people.ksp.sk/~kuko/bak/index.html
44heapsheaps Dr.AlagozDr.Alagoz
Heap-Sort (§)Heap-Sort (§) Other Sorting types will be visited in CHP7 Other Sorting types will be visited in CHP7
• Consider a priority Consider a priority queue with queue with nn items items implemented by implemented by means of a heapmeans of a heap
– the space used is the space used is OO((nn))
– methods methods insertinsert and and removeMinremoveMin take take OO(log (log nn) ) timetime
– methods methods sizesize, , isEmptyisEmpty, and , and minmin take take time time OO(1) (1) timetime
• Using a heap-based Using a heap-based priority queue, we can priority queue, we can sort a sequence of sort a sequence of nn elements in elements in OO((nn log log nn) ) timetime
• The resulting algorithm The resulting algorithm is called heap-sortis called heap-sort
• Heap-sort is much Heap-sort is much faster than quadratic faster than quadratic sorting algorithms, sorting algorithms, such as insertion-sort such as insertion-sort and selection-sortand selection-sort
45heapsheaps Dr.AlagozDr.Alagoz
Vector-based Heap Vector-based Heap Implementation (§)Implementation (§)
• We can represent a heap with We can represent a heap with nn keys by means of a vector of keys by means of a vector of length length n n 1 1
• For the node at rank For the node at rank ii– the left child is at rank the left child is at rank 22ii
– the right child is at rank the right child is at rank 22i i 1 1
• Links between nodes are not Links between nodes are not explicitly storedexplicitly stored
• The cell of at rank The cell of at rank 00 is not used is not used
• Operation insert corresponds to Operation insert corresponds to inserting at rank inserting at rank n n 1 1
• Operation removeMin Operation removeMin corresponds to removing at rank corresponds to removing at rank nn
• Yields in-place heap-sortYields in-place heap-sort
2
65
79
2 5 6 9 7
1 2 3 4 50
46heapsheaps Dr.AlagozDr.Alagoz
Merging Two HeapsMerging Two Heaps
• We are given two two We are given two two heaps and a key heaps and a key kk
• We create a new heap We create a new heap with the root node with the root node storing storing kk and with the and with the two heaps as subtreestwo heaps as subtrees
• We perform downheap We perform downheap to restore the heap-to restore the heap-order property order property
7
3
58
2
64
3
58
2
64
2
3
58
4
67
47heapsheaps Dr.AlagozDr.Alagoz
• We can construct a heap We can construct a heap storing storing nn given keys in given keys in using a bottom-up using a bottom-up construction with construction with log log nn phasesphases
• In phase In phase ii, pairs of heaps , pairs of heaps with with 22i i 11 keys are keys are merged into heaps with merged into heaps with 22ii1111 keys keys
Bottom-up Heap Bottom-up Heap Construction (§)Construction (§)
2i 1 2i 1
2i11
48heapsheaps Dr.AlagozDr.Alagoz
Priority Queues … Priority Queues … Priority Queue InterfacePriority Queue Interface
• enqueueenqueue
– Puts an object in the Puts an object in the priorityQueuepriorityQueue
• findMinfindMin
– Returns a reference to Returns a reference to the smallest object in the smallest object in the priorityQueuethe priorityQueue
• dequeueMindequeueMin
– Removes the smallest Removes the smallest object from the object from the priorityQueuepriorityQueue
public interface PriorityQueue extends Container {
void enqueue(Comparable object);Comparable findMin();Comparable dequeueMin();
}
49heapsheaps Dr.AlagozDr.Alagoz
Priority Queues … Priority Queues … Mergeable Priority Queue InterfaceMergeable Priority Queue Interface
public interface MergeablePriorityQueue extends PriorityQueue {
void merge(MergeablePriorityQueue queue);}
50heapsheaps Dr.AlagozDr.Alagoz
Priority Queues … Priority Queues … Mostly used Heap typesMostly used Heap types
• Binary HeapBinary Heap
• d-heaps d-heaps
• Leftist HeapLeftist Heap
• Binomial HeapBinomial Heap
• Others include skew heaps, Others include skew heaps, fibonacci heaps, soft heap etc fibonacci heaps, soft heap etc
51heapsheaps Dr.AlagozDr.Alagoz
Binary Heap Binary Heap Implementation(*)Implementation(*)
52heapsheaps Dr.AlagozDr.Alagoz
Binary Heap Implementation Binary Heap Implementation (*)(*)
ConstructorsConstructorspublic class BinaryHeap {…
/** * Construct the binary heap. */public BinaryHeap() {
this(DEFAULT_CAPACITY);}
/** * Construct the binary heap. * @param capacity the capacity of the binary heap. */public BinaryHeap(int capacity) {
currentSize = 0;array = new Comparable[capacity + 1];
}
…}
53heapsheaps Dr.AlagozDr.Alagoz
Binary Heap Implementation (*)…Binary Heap Implementation (*)…enqueueenqueue
54heapsheaps Dr.AlagozDr.Alagoz
Binary Heap Implementation (*)…Binary Heap Implementation (*)…enqueueenqueuepublic class BinaryHeap {…
/** * Insert into the priority queue, maintaining heap order. * Duplicates are allowed. * @param x the item to insert. * @exception Overflow if container is full. */public void insert(Comparable x) throws Overflow {
if (isFull())throw new Overflow();
// Percolate upint hole = ++currentSize;for (; hole > 1 && x.compareTo(array[hole / 2]) < 0; hole /= 2)
array[hole] = array[hole / 2];array[hole] = x;
}
…}
55heapsheaps Dr.AlagozDr.Alagoz
Binary Heap Implementation (*)…Binary Heap Implementation (*)…enqueueenqueue
public class BinaryHeap {…
/** * Insert into the priority queue, maintaining heap order. * Duplicates are allowed. * @param x the item to insert. * @exception Overflow if container is full. */public void insert(Comparable x) throws Overflow {
if (isFull())throw new Overflow();
// Percolate upint hole = ++currentSize;for (; hole > 1 && x.compareTo(array[hole / 2]) < 0; hole /= 2)
array[hole] = array[hole / 2];array[hole] = x;
}
…}
•weiss•Fig 6.6•Fig 6.7
Worst-case: O(log n)
56heapsheaps Dr.AlagozDr.Alagoz
Binary Heap Implementation (*)…Binary Heap Implementation (*)…enqueueenqueue - Exacution - Exacution
• insert 14
57heapsheaps Dr.AlagozDr.Alagoz
Binary Heap Binary Heap Implementation(*) …Implementation(*) …enqueueenqueue - Exacution - Exacution
• insert 14
58heapsheaps Dr.AlagozDr.Alagoz
Binary Heap Implementation Binary Heap Implementation (*) …(*) …enqueueenqueue - Exacution - Exacution
• insert 14
59heapsheaps Dr.AlagozDr.Alagoz
Binary Heap Implementation Binary Heap Implementation (*)(*)enqueueenqueue - Exacution - Exacution
• insert 14
60heapsheaps Dr.AlagozDr.Alagoz
Binary Heap ImplementationBinary Heap Implementation(*)(*)
dequeuedequeue
61heapsheaps Dr.AlagozDr.Alagoz
Binary Heap ImplementationBinary Heap Implementation(*)(*)
dequeuedequeuepublic class BinaryHeap {…/** * Remove the smallest item from the priority queue. * @return the smallest item, or null, if empty. */public Comparable deleteMin() {
if (isEmpty())return null;
Comparable minItem = findMin();array[1] = array[currentSize--];percolateDown(1);
return minItem;}…}
62heapsheaps Dr.AlagozDr.Alagoz
Binary Heap ImplementationBinary Heap Implementation(*)(*)
dequeuedequeuepublic class BinaryHeap {…/** * Remove the smallest item from the priority queue. * @return the smallest item, or null, if empty. */public Comparable deleteMin() {
if (isEmpty())return null;
Comparable minItem = findMin();array[1] = array[currentSize--];percolateDown(1);
return minItem;}…}
public class BinaryHeap {…/** * Internal method to percolate down in the heap. * @param hole the index at which the percolate begins. */private void percolateDown(int hole) {
int child;Comparable tmp = array[hole];
for (; hole * 2 <= currentSize; hole = child) {child = hole * 2; // leftif (child != currentSize && array[child + 1].compareTo(array[child]) < 0)
child++;if (array[child].compareTo(tmp) < 0)
array[hole] = array[child];else
break;}array[hole] = tmp;
}…}
63heapsheaps Dr.AlagozDr.Alagoz
Binary Heap ImplementationBinary Heap Implementation(*)(*)
dequeuedequeuepublic class BinaryHeap {…/** * Remove the smallest item from the priority queue. * @return the smallest item, or null, if empty. */public Comparable deleteMin() {
if (isEmpty())return null;
Comparable minItem = findMin();array[1] = array[currentSize--];percolateDown(1);
return minItem;}…}
public class BinaryHeap {…/** * Internal method to percolate down in the heap. * @param hole the index at which the percolate begins. */private void percolateDown(int hole) {
int child;Comparable tmp = array[hole];
for (; hole * 2 <= currentSize; hole = child) {child = hole * 2; // leftif (child != currentSize && array[child + 1].compareTo(array[child]) < 0)
child++;if (array[child].compareTo(tmp) < 0)
array[hole] = array[child];else
break;}array[hole] = tmp;
}…}
Worst-case: O(log n)
64heapsheaps Dr.AlagozDr.Alagoz
Binary Heap Implementation Binary Heap Implementation (*)(*)
dequeuedequeue – Execution – Execution
65heapsheaps Dr.AlagozDr.Alagoz
Binary Heap Binary Heap Implementation(*)Implementation(*)dequeuedequeue – Execution – Execution
66heapsheaps Dr.AlagozDr.Alagoz
Binary Heap Binary Heap Implementation(*)Implementation(*)dequeuedequeue – Execution – Execution
67heapsheaps Dr.AlagozDr.Alagoz
Binary Heap Implementation Binary Heap Implementation (*)(*)dequeuedequeue – Execution – Execution
68heapsheaps Dr.AlagozDr.Alagoz
Binary Heap Implementation Binary Heap Implementation (*)(*)dequeuedequeue – Execution – Execution
69heapsheaps Dr.AlagozDr.Alagoz
Binary Heap Implementation Binary Heap Implementation ……dequeuedequeue – Execution – Execution
70heapsheaps Dr.AlagozDr.Alagoz
Binary Heap Implementation Binary Heap Implementation (*)(*)
buildHeapbuildHeappublic class BinaryHeap {…
/** * Establish heap order property from an arbitrary * arrangement of items. Runs in linear time. */private void buildHeap() {
for (int i = currentSize / 2; i > 0; i--)percolateDown(i);
}
…}
public class BinaryHeap {…/** * Internal method to percolate down in the heap. * @param hole the index at which the percolate begins. */private void percolateDown(int hole) {
int child;Comparable tmp = array[hole];
for (; hole * 2 <= currentSize; hole = child) {child = hole * 2; // leftif (child != currentSize && array[child + 1].compareTo(array[child]) < 0)
child++;if (array[child].compareTo(tmp) < 0)
array[hole] = array[child];else
break;}array[hole] = tmp;
}…}
71heapsheaps Dr.AlagozDr.Alagoz
Binary Heap Implementation Binary Heap Implementation (*)(*)
buildHeapbuildHeappublic class BinaryHeap {…
/** * Establish heap order property from an arbitrary * arrangement of items. Runs in linear time. */private void buildHeap() {
for (int i = currentSize / 2; i > 0; i--)percolateDown(i);
}
…}
public class BinaryHeap {…/** * Internal method to percolate down in the heap. * @param hole the index at which the percolate begins. */private void percolateDown(int hole) {
int child;Comparable tmp = array[hole];
for (; hole * 2 <= currentSize; hole = child) {child = hole * 2; // leftif (child != currentSize && array[child + 1].compareTo(array[child]) < 0)
child++;if (array[child].compareTo(tmp) < 0)
array[hole] = array[child];else
break;}array[hole] = tmp;
}…}
72heapsheaps Dr.AlagozDr.Alagoz
Binary Heap Implementation Binary Heap Implementation (*)(*)
buildHeapbuildHeap
public class BinaryHeap {…/** * Internal method to percolate down in the heap. * @param hole the index at which the percolate begins. */private void percolateDown(int hole) {
int child;Comparable tmp = array[hole];
for (; hole * 2 <= currentSize; hole = child) {child = hole * 2; // leftif (child != currentSize && array[child + 1].compareTo(array[child]) < 0)
child++;if (array[child].compareTo(tmp) < 0)
array[hole] = array[child];else
break;}array[hole] = tmp;
}…}
•Weiss•Fig 6.15•6.16•6.17•6.18
73heapsheaps Dr.AlagozDr.Alagoz
Binary Heap Implementation (*)…Binary Heap Implementation (*)…findMinfindMin
public class BinaryHeap {…/** * Find the smallest item in the priority queue. * @return the smallest item, or null, if empty. */public Comparable findMin() {
if (isEmpty())return null;
return array[1];}…}
74heapsheaps Dr.AlagozDr.Alagoz
Binary Heap Implementation (*)Binary Heap Implementation (*)Best Practices: Best Practices:
Header SampleHeader Sample// BinaryHeap class//// CONSTRUCTION: with optional capacity (that defaults to 100)//// ******************PUBLIC OPERATIONS*********************// void insert( x ) --> Insert x// Comparable deleteMin( )--> Return and remove smallest item// Comparable findMin( ) --> Return smallest item// boolean isEmpty( ) --> Return true if empty; else false// boolean isFull( ) --> Return true if full; else false// void makeEmpty( ) --> Remove all items// ******************ERRORS********************************// Throws Overflow if capacity exceeded
/** * Implements a binary heap. * Note that all "matching" is based on the compareTo method. * @author Mark Allen Weiss */public class BinaryHeap {…}
75heapsheaps Dr.AlagozDr.Alagoz
Binary Heap Implementation (*)…Binary Heap Implementation (*)…Best Practices: Best Practices: Test SampleTest Sample
• Use main() Use main() for testingfor testing
public class BinaryHeap {…// Test programpublic static void main(String[] args) {
int numItems = 10000;BinaryHeap h = new BinaryHeap(numItems);int i = 37;
try {for (i = 37; i != 0; i = (i + 37) % numItems)
h.insert(new MyInteger(i));for (i = 1; i < numItems; i++)
if (((MyInteger) (h.deleteMin())).intValue() != i)System.out.println("Oops! " + i);
for (i = 37; i != 0; i = (i + 37) % numItems)h.insert(new MyInteger(i));
h.insert(new MyInteger(0));i = 9999999;h.insert(new MyInteger(i));for (i = 1; i <= numItems; i++)
if (((MyInteger) (h.deleteMin())).intValue() != i)System.out.println("Oops! " + i + " ");
} catch (Overflow e) {System.out.println("Overflow (expected)! " + i);
}}}
Building Binary Heap is Building Binary Heap is O(n)O(n)A heap could be built by successive insertions. This approach A heap could be built by successive insertions. This approach requires requires OO((nnlglgnn) time because each insertion takes ) time because each insertion takes OO(lg(lgnn) time ) time and there are n elements. However this is not the optimal and there are n elements. However this is not the optimal method! method!
The optimal method starts by randomly putting the elements The optimal method starts by randomly putting the elements on a binary tree (which could be represented by an array). on a binary tree (which could be represented by an array). Then starting from the lowest level and moving upwards until Then starting from the lowest level and moving upwards until the heap property is restored by shifting the root of the subtree the heap property is restored by shifting the root of the subtree downward as in the deletion algorithm. More specifically if all downward as in the deletion algorithm. More specifically if all the subtrees starting at some height h (measured from the the subtrees starting at some height h (measured from the bottom) have already been "heapified", the trees at depth h+1 bottom) have already been "heapified", the trees at depth h+1 can be heapified by sending their root down, along the path of can be heapified by sending their root down, along the path of maximum children. This process takes maximum children. This process takes OO((hh) operations (swaps). ) operations (swaps). In this method most of the heapification takes place in the In this method most of the heapification takes place in the lower levels. The number of nodes at height lower levels. The number of nodes at height hh is . Therefore, is . Therefore, the cost of heapifying all subtrees is:the cost of heapifying all subtrees is:
A heap could be built by successive insertions. This approach A heap could be built by successive insertions. This approach requires requires OO((nnlglgnn) time because each insertion takes ) time because each insertion takes OO(lg(lgnn) time ) time and there are n elements. However this is not the optimal and there are n elements. However this is not the optimal method! method!
The optimal method starts by randomly putting the elements The optimal method starts by randomly putting the elements on a binary tree (which could be represented by an array). on a binary tree (which could be represented by an array). Then starting from the lowest level and moving upwards until Then starting from the lowest level and moving upwards until the heap property is restored by shifting the root of the subtree the heap property is restored by shifting the root of the subtree downward as in the deletion algorithm. More specifically if all downward as in the deletion algorithm. More specifically if all the subtrees starting at some height h (measured from the the subtrees starting at some height h (measured from the bottom) have already been "heapified", the trees at depth h+1 bottom) have already been "heapified", the trees at depth h+1 can be heapified by sending their root down, along the path of can be heapified by sending their root down, along the path of maximum children. This process takes maximum children. This process takes OO((hh) operations (swaps). ) operations (swaps). In this method most of the heapification takes place in the In this method most of the heapification takes place in the lower levels. The number of nodes at height lower levels. The number of nodes at height hh is . Therefore, is . Therefore, the cost of heapifying all subtrees is:the cost of heapifying all subtrees is:
77heapsheaps Dr.AlagozDr.Alagoz
Binary Heap Implementation …Binary Heap Implementation …Complexity SummaryComplexity Summary
OperationOperation ComplexityComplexity
enqueueenqueue O(logO(log22 n) n)
dequeuedequeue O(logO(log22 n) n)
builtbuilt O(n)O(n)
• BinaryBinary
78heapsheaps Dr.AlagozDr.Alagoz
Priority Queues … Priority Queues … Mostly used Heap typesMostly used Heap types
• Binary HeapBinary Heap
• d-heaps d-heaps
• Leftist HeapLeftist Heap
• Binomial HeapBinomial Heap
• Others include skew heaps, Others include skew heaps, fibonacci heaps, soft heap etc fibonacci heaps, soft heap etc
79heapsheaps Dr.AlagozDr.Alagoz
d-Heapd-HeapInsert operation is O(logInsert operation is O(logdd n) n)
DeleteMin operation is O(dlogDeleteMin operation is O(dlogdd n) n)
Because the minimum of d-children should be Because the minimum of d-children should be found which takes (d-1) comparison. found which takes (d-1) comparison.
For practical applications where the # of insertions For practical applications where the # of insertions is much greater than that of deletions, d-heap is much greater than that of deletions, d-heap design may be performed to speed-up…design may be performed to speed-up…
But, both find operation and combining two d-heap But, both find operation and combining two d-heap are rather difficult compared to binary heap….. are rather difficult compared to binary heap…..
80heapsheaps Dr.AlagozDr.Alagoz
Leftist HeapsLeftist Heaps• Idea: merging requires copying one array Idea: merging requires copying one array
into another, which takes O(N) time for into another, which takes O(N) time for equal-sized heaps…equal-sized heaps…
• Can we do it better for mCan we do it better for mergeable prergeable priority iority queues ? queues ? – Leftist heaps: a special form of binary heaps Leftist heaps: a special form of binary heaps
that attempt to be very unbalanced…. that attempt to be very unbalanced….
– Establish a binary tree that is formed on the Establish a binary tree that is formed on the left path so that insert and merge operations left path so that insert and merge operations can be easily done at the right path….can be easily done at the right path….
– Playing on the right requires reforming the Playing on the right requires reforming the leftist property..leftist property..
81heapsheaps
Leftist heap: s- valueLeftist heap: s- value
Dr.AlagozDr.Alagoz
•Definition: s-value of a node is the distance from that node to the nearest leaf. s-value calculation includes both the starting node and the leaf in the s-value.
Eg. s-value of the top node (with value 4) is 2 because there are 2 nodes on the way to the leaf (the starting node, and the leaf itself). For the other three nodes, the s-value is 1 because those nodes are leaves themselves.
82heapsheaps Dr.AlagozDr.Alagoz
Leftist Heaps :Leftist Heaps : Null PathNull PathDefinitionDefinition
Consider an arbitrary node x in some binary tree T. Consider an arbitrary node x in some binary tree T. The The null path lengthnull path length of node x is the shortest path in T from x of node x is the shortest path in T from x to an external node of T. or the length of the shortest path to an external node of T. or the length of the shortest path from a node X to a node without two-children…Nullpath length from a node X to a node without two-children…Nullpath length of any node is more than the minimum of the null path lengths of any node is more than the minimum of the null path lengths of its children of its children
DefinitionDefinition
The The null path length null path length of an empty tree is zero.of an empty tree is zero.
The The null path lengthnull path length of a non-empty binary tree T={R, T of a non-empty binary tree T={R, TLL, T, TRR} is } is the null path length its root R. the null path length its root R.
83heapsheaps Dr.AlagozDr.Alagoz
Leftist TreesLeftist TreesRemarkRemark A leftist tree is a tree in which the shortest path to an A leftist tree is a tree in which the shortest path to an external node is always on the right. That is, for every node X external node is always on the right. That is, for every node X in the heap, the null path length of the left child is AT LEAST as in the heap, the null path length of the left child is AT LEAST as large as that of the right childlarge as that of the right child
84heapsheaps Dr.AlagozDr.Alagoz
Leftist Trees … Leftist Trees … Length of the Right PathLength of the Right Path
TheoremTheorem
Consider a leftist tree Consider a leftist tree TT which contains which contains nn internal internal nodes. The path leading from the root of nodes. The path leading from the root of TT downwards to the rightmost external node contains downwards to the rightmost external node contains at most at most log2(n+1)log2(n+1) nodes. nodes.
85heapsheaps Dr.AlagozDr.Alagoz
Merging Leftist HeapsMerging Leftist Heaps
• if h1 is empty, swap h1 and if h1 is empty, swap h1 and h2h2
• otherwise, assume the root otherwise, assume the root of h2 is larger than the root of h2 is larger than the root of h1:of h1:
• recursively merge h2 with recursively merge h2 with the right subheap of h1the right subheap of h1
• if the right subheap of h1 if the right subheap of h1 now has a larger null path now has a larger null path length then its left subheap, length then its left subheap, swap the left and right swap the left and right subheapssubheaps
• if h2 initially has the smaller if h2 initially has the smaller root, exchange h1 and h2 root, exchange h1 and h2 and proceed as aboveand proceed as above
86heapsheaps Dr.AlagozDr.Alagoz
Leftist Trees …(*) Leftist Trees …(*) ImplementationImplementation - - LeftHeapNodeLeftHeapNode
// Basic node stored in leftist heaps// Note that this class is not accessible outside// of package DataStructures
class LeftHeapNode { // Constructors LeftHeapNode(Comparable theElement) { this(theElement, null, null); }
LeftHeapNode(Comparable theElement, LeftHeapNode lt, LeftHeapNode rt) { element = theElement; left = lt; right = rt; npl = 0; }
// Friendly data; accessible by other package routines Comparable element; // The data in the node LeftHeapNode left; // Left child LeftHeapNode right; // Right child int npl; // null path length}
87heapsheaps Dr.AlagozDr.Alagoz
Leftist Trees …(*) Leftist Trees …(*) Implementation – MergingImplementation – Mergingpublic class LeftistHeap { … /** * Merge rhs into the priority queue. * rhs becomes empty. rhs must be different from this. * @param rhs the other leftist heap. */ public void merge(LeftistHeap rhs) { if (this == rhs) // Avoid aliasing problems return;
root = merge(root, rhs.root); rhs.root = null; }
/** * Internal static method to merge two roots. * Deals with deviant cases and calls recursive merge1. */ private static LeftHeapNode merge(LeftHeapNode h1, LeftHeapNode h2) { if (h1 == null) return h2; if (h2 == null) return h1; if (h1.element.compareTo(h2.element) < 0) return merge1(h1, h2); else return merge1(h2, h1); } …}
88heapsheaps Dr.AlagozDr.Alagoz
public class LeftistHeap { … /** * Merge rhs into the priority queue. * rhs becomes empty. rhs must be different from this. * @param rhs the other leftist heap. */ public void merge(LeftistHeap rhs) { if (this == rhs) // Avoid aliasing problems return;
root = merge(root, rhs.root); rhs.root = null; }
/** * Internal static method to merge two roots. * Deals with deviant cases and calls recursive merge1. */ private static LeftHeapNode merge(LeftHeapNode h1, LeftHeapNode h2) { if (h1 == null) return h2; if (h2 == null) return h1; if (h1.element.compareTo(h2.element) < 0) return merge1(h1, h2); else return merge1(h2, h1); } …
Leftist Trees …(*) Leftist Trees …(*) Implementation – MergingImplementation – Merging
… /** * Internal static method to merge two roots. * Assumes trees are not empty, * and h1's root contains smallest item. */ private static LeftHeapNode merge1(LeftHeapNode h1, LeftHeapNode h2) { if (h1.left == null) // Single node h1.left = h2; // Other fields in h1 already accurate else { h1.right = merge(h1.right, h2); if (h1.left.npl < h1.right.npl) swapChildren(h1); h1.npl = h1.right.npl + 1; } return h1; }
/** * Swaps t's two children. */ private static void swapChildren(LeftHeapNode t) { LeftHeapNode tmp = t.left; t.left = t.right; t.right = tmp; } …}
89heapsheaps Dr.AlagozDr.Alagoz
Leftist Trees …(*) Leftist Trees …(*) Implementation – enqueue()Implementation – enqueue()
public class LeftistHeap {
…
/** * Insert into the priority queue, * maintaining heap order. * @param x the item to insert. */ public void insert(Comparable x) { root = merge(new LeftHeapNode(x), root); } …}
90heapsheaps Dr.AlagozDr.Alagoz
Leftist Trees … (*) Leftist Trees … (*) Implementation – findMin()Implementation – findMin()
public class LeftistHeap { …
/** * Find the smallest item in the priority queue. * @return the smallest item, or null, if empty. */ public Comparable findMin() { if (isEmpty()) return null; return root.element; } …}
91heapsheaps Dr.AlagozDr.Alagoz
Leftist Trees …(*) Leftist Trees …(*) Implementation – Implementation – dequeueMin()dequeueMin()
public class LeftistHeap { …
/** * Remove the smallest item from the priority queue. * @return the smallest item, or null, if empty. */ public Comparable deleteMin() { if (isEmpty()) return null;
Comparable minItem = root.element; root = merge(root.left, root.right);
return minItem; } …}
92heapsheaps Dr.AlagozDr.Alagoz
Leftist Trees … Leftist Trees … Complexity SummaryComplexity Summary
OperationOperation ComplexityComplexity
mergemerge O(logO(log22 n n11 + log + log22 nn22))
enqueueenqueue O(logO(log22 n) n)
dequeuedequeue O(logO(log22 n) n)
findMinfindMin O(O(11))
93heapsheaps Dr.AlagozDr.Alagoz
Binomial HeapsBinomial Heaps
94heapsheaps Dr.AlagozDr.Alagoz
Binomial QueuesBinomial Queues• can we do better than leftist-heap with can we do better than leftist-heap with
O(logN) complexity for insertion, deletion O(logN) complexity for insertion, deletion and merge?and merge?
• Note: Binary-heap supports O(1) for Note: Binary-heap supports O(1) for insertion..insertion..
• Binomial heaps with O(logN) worst-case for Binomial heaps with O(logN) worst-case for insertion, deletion and merging, and O(1) insertion, deletion and merging, and O(1) average time for insertion….average time for insertion….– binomial treesbinomial trees
– forestsforests
– merging of binomial queuesmerging of binomial queues
95heapsheaps Dr.AlagozDr.Alagoz
Binomial Queues …Binomial Queues …
DefinitionDefinition
The The binomial treebinomial tree of order k of order k 0 0 with root R is the tree Bwith root R is the tree Bkk defined defined as followsas follows
1.1. If k = 0, BIf k = 0, B00 = {R}. = {R}. i.e., the binomial tree of order i.e., the binomial tree of order zero consists of a single node, zero consists of a single node, R.R.
2.2. If k > 0, BIf k > 0, Bkk = {R, B = {R, B00, B, B11, … , B, … , Bk-k-
11}. }. i.e., the binomial tree of order i.e., the binomial tree of order k > 0 comprises the root R, k > 0 comprises the root R, and k binomial subtrees, B0, and k binomial subtrees, B0, B1, . . . , B1, . . . , BBk-1k-1..
96heapsheaps Dr.AlagozDr.Alagoz
Binomial Queues …Binomial Queues …Different ViewsDifferent Views
97heapsheaps Dr.AlagozDr.Alagoz
Binomial Queues …Binomial Queues …Number of NodesNumber of Nodes
TheoremTheorem
The binomial tree of order k, BThe binomial tree of order k, Bkk, , contains 2contains 2kk nodes. nodes.
98heapsheaps Dr.AlagozDr.Alagoz
Binomial Queues …Binomial Queues …HeightHeight
TheoremTheorem
The height of the binomial tree of order The height of the binomial tree of order k, Bk, Bkk, is k., is k.
•http://www.cse.yorku.ca/~aaw/Sotirios/BinomialHeap.html
99heapsheaps Dr.AlagozDr.Alagoz
Binomial Queues …Binomial Queues …Binomial TheoremBinomial Theorem
TheoremTheorem
The nThe nthth power of the binomial x+y for n power of the binomial x+y for n 0 is given by0 is given by
where where is called is called binomial coefficientbinomial coefficient
n
i
inin yxi
nyx
0
)!(!
!
ini
n
i
n
100heapsheaps Dr.AlagozDr.Alagoz
Binomial Queues …Binomial Queues …Why Binomial ?Why Binomial ?
TheoremTheorem
The number of nodes at level The number of nodes at level ll in B in Bkk, , the binomial treethe binomial tree of order k, where 0 ≤ of order k, where 0 ≤ ll ≤ ≤ k, k, is given by the binomialis given by the binomial coecoeffifficientcient
l
n
levellevel
0 1 2 3 4 ...0 1 2 3 4 ...
11
1 11 1
1 2 11 2 1
1 3 3 11 3 3 1
11 4 6 4 14 6 4 1
......
kk
00
11
22
33
44
......
101heapsheaps Dr.AlagozDr.Alagoz
• Binomial trees only come in sizes that are a power of 2
• How to represent arbitrary number, n, of items?• Consider the binary representation of the number n:
where bi {0, 1} is the ith bit
• To hold n items use a forest of binomial trees:Fn = {Bi : bi = 1};
Binomial Queues …Binomial Queues …Any Size ?Any Size ?
n
i
iibn
2log
0
2
102heapsheaps Dr.AlagozDr.Alagoz
Binomial Queues …Binomial Queues …Merging Binomial QueuesMerging Binomial Queues
• Merging two binomial queues is like doing binary addition
BB44 BB33 BB11 BB00 FF22
77
2727 11 11 00 11 11
++ BB33 BB11 FF11
00
++ 1010 ++ 11 00 11 00
BB55 BB22 BB00 FF33
77
3737 11 00 00 11 00 11
103heapsheaps Dr.AlagozDr.Alagoz
Binomial Queues …Binomial Queues …Merging Binomial Queues ...Merging Binomial Queues ...
104heapsheaps Dr.AlagozDr.Alagoz
HW in CHP6: HW in CHP6: 4, 6, 14, 15, 22,26,274, 6, 14, 15, 22,26,27
6.1 6.1 Yes. When an element is inserted, we compare it to the Yes. When an element is inserted, we compare it to the current minimum and change the minimum if the new current minimum and change the minimum if the new element is smaller. element is smaller. deleteMin deleteMin operations are expensive in operations are expensive in this scheme.this scheme.
6.26.2 11
3 23 2
6 7 5 46 7 5 4
15 14 12 9 10 11 13 815 14 12 9 10 11 13 8
11
3 23 2
12 6 4 812 6 4 8
15 14 9 7 5 11 13 1015 14 9 7 5 11 13 10
105heapsheaps Dr.AlagozDr.Alagoz
ReferencesReferences
• Data Structures and Algorithms Data Structures and Algorithms with Object-Oriented Design Patterns in Javawith Object-Oriented Design Patterns in JavaPreiss Preiss http://www.brpreiss.com/books/opus5/http://www.brpreiss.com/books/opus5/
• Data Structures and AlgorithmsData Structures and Algorithmswith Object-Oriented Design Patters in C++with Object-Oriented Design Patters in C++PreissPreissWiley, 1999 Wiley, 1999
• Data Structures and Algorithm Analysis in JavaData Structures and Algorithm Analysis in JavaWeissWeissAddison-Wesley, 1999Addison-Wesley, 1999
106heapsheaps Dr.AlagozDr.Alagoz
Debuging ToolsDebuging Tools(*)(*)
107heapsheaps Dr.AlagozDr.Alagoz
Debuging ToolsDebuging Tools public static void main(String[] args) { int numItems = 10000; BinaryHeap h = new BinaryHeap(numItems); switch (1) { case 1 : try { h.insert(new MyInteger(4)); h.printTree(); h.insert(new MyInteger(2)); h.printTree(); h.insert(new MyInteger(6)); h.printTree(); h.insert(new MyInteger(7)); h.printTree(); h.insert(new MyInteger(1)); h.printTree(); } catch (Overflow e1) { // TODO Auto-generated catch block e1.printStackTrace(); } break; case 2 : ... break; default : break; } }
______binary tree______ ./-4\ .______binary tree______ . / -4 \ ./-2\ .______binary tree______ . / -4 \ ./-2\ . / -6 \ .______binary tree______ . / -7 \ . / -4 \ ./-2\ . / -6 \ .______binary tree______ . / -7 \ . / -2 \ . / -4 \ ./-1\ . / -6 \ .
108heapsheaps Dr.AlagozDr.Alagoz
Debuging ToolsDebuging Tools public void printTree() { System.out.println("______binary tree______"); if (isEmpty()) System.out.println("Empty tree"); else printTree(1, 0); }
public void printTree(String name) { System.out.println("______" + name + "______"); if (isEmpty()) System.out.println("Empty tree"); else printTree(1, 0); }
private void printTree(int t, int depth) { String space = ""; for (int i = 0; i < depth; i++) { space += " "; } if (t <= currentSize) { int d = ++depth; printTree(2 * t, d); System.out.println(space + "/"); System.out.print(space + "-"); System.out.println(array[t]); System.out.println(space + "\\"); printTree(2 * t + 1, d); } else { System.out.println(space + "."); } }
109heapsheaps Dr.AlagozDr.Alagoz
Debuging ToolsDebuging Tools public void printTree() { System.out.println("______binary tree______"); if (isEmpty()) System.out.println("Empty tree"); else printTree(1, 0); }
public void printTree(String name) { System.out.println("______" + name + "______"); if (isEmpty()) System.out.println("Empty tree"); else printTree(1, 0); }
private void printTree(int t, int depth) { String space = ""; for (int i = 0; i < depth; i++) { space += " "; } if (t <= currentSize) { int d = ++depth; printTree(2 * t, d); System.out.println(space + "/"); System.out.print(space + "-"); System.out.println(array[t]); System.out.println(space + "\\"); printTree(2 * t + 1, d); } else { System.out.println(space + "."); } }
...
int numItems = 10000;
BinaryHeap h = new BinaryHeap(numItems);h.insert(new MyInteger(4));
h.insert(new MyInteger(2));
h.insert(new MyInteger(6));
h.insert(new MyInteger(7));
h.insert(new MyInteger(1));
h.printTree();...
______binary tree______ . / -7 \ . / -2 \ . / -4 \ ./-1\ . / -6 \ .
110heapsheaps Dr.AlagozDr.Alagoz
Class Hierarchy:Class Hierarchy: Priority Queues- Heap (*) Priority Queues- Heap (*)
ContainerContainer
PriorityQueuePriorityQueue TreeTree
BinaryHeaBinaryHeapp
MergeablePriorityQueMergeablePriorityQueueue
BinomialQueBinomialQueueue
LeftistHeapLeftistHeap
BinaryTreBinaryTreee
GeneralTreGeneralTreee
BinomialTreBinomialTreee
111heapsheaps Dr.AlagozDr.Alagoz
Leftist Trees … Leftist Trees … Leftist HeapsLeftist Heaps
• A heap-ordered treeA heap-ordered tree– The positions of the keys in the treeThe positions of the keys in the tree
• A leftist treeA leftist tree– The shape of the treeThe shape of the tree
112heapsheaps Dr.AlagozDr.Alagoz
ExampleExample
1516 124 76 2023
25
1516
5
124
11
76
27
2023
113heapsheaps Dr.AlagozDr.Alagoz
Example (contd.)Example (contd.)
25
1516
5
124
11
96
27
2023
15
2516
4
125
6
911
23
2027
114heapsheaps Dr.AlagozDr.Alagoz
Example (contd.)Example (contd.)
7
15
2516
4
125
8
6
911
23
2027
4
15
2516
5
127
6
8
911
23
2027
115heapsheaps Dr.AlagozDr.Alagoz
Example (end)Example (end)
4
15
2516
5
127
10
6
8
911
23
2027
5
15
2516
7
1210
4
6
8
911
23
2027
116heapsheaps Dr.AlagozDr.Alagoz
AnalysisAnalysis
• We visualize the worst-case time of a downheap with a proxy path We visualize the worst-case time of a downheap with a proxy path that goes first right and then repeatedly goes left until the bottom that goes first right and then repeatedly goes left until the bottom of the heap (this path may differ from the actual downheap path)of the heap (this path may differ from the actual downheap path)
• Since each node is traversed by at most two proxy paths, the Since each node is traversed by at most two proxy paths, the total number of nodes of the proxy paths is total number of nodes of the proxy paths is OO((nn))
• Thus, bottom-up heap construction runs in Thus, bottom-up heap construction runs in OO((nn) ) time time
• Bottom-up heap construction is faster than Bottom-up heap construction is faster than nn successive insertions successive insertions and speeds up the first phase of heap-sortand speeds up the first phase of heap-sort