SlideShare une entreprise Scribd logo
1  sur  11
Télécharger pour lire hors ligne
Princeton University • COS 423 • Theory of Algorithms • Spring 2002 • Kevin Wayne
Binary and Binomial Heaps
These lecture slides are adapted
from CLRS, Chapters 6, 19.
2
Priority Queues
Supports the following operations.
s Insert element x.
s Return min element.
s Return and delete minimum element.
s Decrease key of element x to k.
Applications.
s Dijkstra’s shortest path algorithm.
s Prim’s MST algorithm.
s Event-driven simulation.
s Huffman encoding.
s Heapsort.
s . . .
3
Priority Queues in Action
PQinit()
for each v ∈ V
key(v) ← ∞
PQinsert(v)
key(s) ← 0
while (!PQisempty())
v = PQdelmin()
for each w ∈ Q s.t (v,w) ∈ E
if π(w) > π(v) + c(v,w)
PQdecrease(w, π(v) + c(v,w))
Dijkstra’s Shortest Path Algorithm
4
Dijkstra/Prim
1 make-heap
|V| insert
|V| delete-min
|E| decrease-key
Priority Queues
make-heap
Operation
insert
find-min
delete-min
union
decrease-key
delete
1
Binary
log N
1
log N
N
log N
log N
1
Binomial
log N
log N
log N
log N
log N
log N
1
Fibonacci *
1
1
log N
1
1
log N
1
Relaxed
1
1
log N
1
1
log N
1
Linked List
1
N
N
1
1
N
is-empty 1 1 1 11
Heaps
O(|E| + |V| log |V|)O(|E| log |V|)O(|V|2)
5
Binary Heap: Definition
Binary heap.
s Almost complete binary tree.
– filled on all levels, except last, where filled from left to right
s Min-heap ordered.
– every child greater than (or equal to) parent
06
14
78 18
81 7791
45
5347
6484 9983
6
Binary Heap: Properties
Properties.
s Min element is in root.
s Heap with N elements has height = log2 N.
06
14
78 18
81 7791
45
5347
6484 9983
N = 14
Height = 3
7
Binary Heaps: Array Implementation
Implementing binary heaps.
s Use an array: no need for explicit parent or child pointers.
– Parent(i) = i/2
– Left(i) = 2i
– Right(i) = 2i + 1
06
14
78 18
81 7791
45
5347
6484 9983
1
2 3
4 5 6 7
8 9 10 11 12 13 14
8
Binary Heap: Insertion
Insert element x into heap.
s Insert into next available slot.
s Bubble up until it’s heap ordered.
– Peter principle: nodes rise to level of incompetence
06
14
78 18
81 7791
45
5347
6484 9983 42 next free slot
9
Binary Heap: Insertion
Insert element x into heap.
s Insert into next available slot.
s Bubble up until it’s heap ordered.
– Peter principle: nodes rise to level of incompetence
06
14
78 18
81 7791
45
5347
6484 9983 4242
swap with parent
10
Binary Heap: Insertion
Insert element x into heap.
s Insert into next available slot.
s Bubble up until it’s heap ordered.
– Peter principle: nodes rise to level of incompetence
06
14
78 18
81 7791
45
4247
6484 9983 4253
swap with parent
11
Binary Heap: Insertion
Insert element x into heap.
s Insert into next available slot.
s Bubble up until it’s heap ordered.
– Peter principle: nodes rise to level of incompetence
s O(log N) operations.
06
14
78 18
81 7791
42
4547
6484 9983 53
stop: heap ordered
12
Binary Heap: Decrease Key
Decrease key of element x to k.
s Bubble up until it’s heap ordered.
s O(log N) operations.
06
14
78 18
81 7791
42
4547
6484 9983 53
13
Binary Heap: Delete Min
Delete minimum element from heap.
s Exchange root with rightmost leaf.
s Bubble root down until it’s heap ordered.
– power struggle principle: better subordinate is promoted
06
14
78 18
81 7791
42
4547
6484 9983 53
14
Binary Heap: Delete Min
Delete minimum element from heap.
s Exchange root with rightmost leaf.
s Bubble root down until it’s heap ordered.
– power struggle principle: better subordinate is promoted
53
14
78 18
81 7791
42
4547
6484 9983 06
15
Binary Heap: Delete Min
Delete minimum element from heap.
s Exchange root with rightmost leaf.
s Bubble root down until it’s heap ordered.
– power struggle principle: better subordinate is promoted
53
14
78 18
81 7791
42
4547
6484 9983
exchange with left child
16
Binary Heap: Delete Min
Delete minimum element from heap.
s Exchange root with rightmost leaf.
s Bubble root down until it’s heap ordered.
– power struggle principle: better subordinate is promoted
14
53
78 18
81 7791
42
4547
6484 9983
exchange with right child
17
Binary Heap: Delete Min
Delete minimum element from heap.
s Exchange root with rightmost leaf.
s Bubble root down until it’s heap ordered.
– power struggle principle: better subordinate is promoted
s O(log N) operations.
14
18
78 53
81 7791
42
4547
6484 9983
stop: heap ordered
18
Binary Heap: Heapsort
Heapsort.
s Insert N items into binary heap.
s Perform N delete-min operations.
s O(N log N) sort.
s No extra storage.
19
H1
H2
14
78 18
81 7791
11
6253
6484 9941
Binary Heap: Union
Union.
s Combine two binary heaps H1 and H2 into a single heap.
s No easy solution.
– Ω(N) operations apparently required
s Can support fast union with fancier heaps.
20
Priority Queues
make-heap
Operation
insert
find-min
delete-min
union
decrease-key
delete
1
Binary
log N
1
log N
N
log N
log N
1
Binomial
log N
log N
log N
log N
log N
log N
1
Fibonacci *
1
1
log N
1
1
log N
1
Relaxed
1
1
log N
1
1
log N
1
Linked List
1
N
N
1
1
N
is-empty 1 1 1 11
Heaps
21
Binomial Tree
Binomial tree.
s Recursive definition:
Bk-1
Bk-1
B0
Bk
B0 B1 B2 B3 B4
22
Binomial Tree
Useful properties of order k binomial tree Bk.
s Number of nodes = 2k.
s Height = k.
s Degree of root = k.
s Deleting root yields binomial
trees Bk-1, … , B0.
Proof.
s By induction on k.
B0 B1 B2 B3 B4
B1
Bk
Bk+1
B2
B0
23
Binomial Tree
A property useful for naming the data structure.
s Bk has nodes at depth i.
B4






i
k
6
2
4
=





depth 2
depth 3
depth 4
depth 0
depth 1
24
Binomial Heap
Binomial heap. Vuillemin, 1978.
s Sequence of binomial trees that satisfy binomial heap property.
– each tree is min-heap ordered
– 0 or 1 binomial tree of order k
B4 B0B1
55
45 32
30
24
23 22
50
48 31 17
448 29 10
6
37
3 18
25
Binomial Heap: Implementation
Implementation.
s Represent trees using left-child, right sibling pointers.
– three links per node (parent, left, right)
s Roots of trees connected with singly linked list.
– degrees of trees strictly decreasing from left to right
50
48 31 17
4410
6
37
3 18
29
6
37
3 18
48
3150
10
4417
heap
29
Leftist Power-of-2 HeapBinomial Heap
Parent
Left
Right
26
Binomial Heap: Properties
Properties of N-node binomial heap.
s Min key contained in root of B0, B1, . . . , Bk.
s Contains binomial tree Bi iff bi = 1 where bn⋅ b2b1b0 is binary
representation of N.
s At most log2 N + 1 binomial trees.
s Height ≤ log2 N.
B4 B0B1
55
45 32
30
24
23 22
50
48 31 17
448 29 10
6
37
3 18
N = 19
# trees = 3
height = 4
binary = 10011
27
Binomial Heap: Union
Create heap H that is union of heaps H’ and H’’.
s "Mergeable heaps."
s Easy if H’ and H’’ are each order k binomial trees.
– connect roots of H’ and H’’
– choose smaller key to be root of H
H’’
55
45 32
30
24
23 22
50
48 31 17
448 29 10
6
H’
28
Binomial Heap: Union
001 1
100 1+
011 1
11
1
1
0
1
19 + 7 = 26
55
45 32
30
24
23 22
50
48 31 17
448 29 10
6
37
3 18
41
3328
15
25
7 12
+
29
Binomial Heap: Union
55
45 32
30
24
23 22
50
48 31 17
448 29 10
6
37
3 18
41
3328
15
25
7 12
+
30
Binomial Heap: Union
55
45 32
30
24
23 22
50
48 31 17
448 29 10
6
37
3
41
3328
15
25
7
+
12
18
18
12
31
55
45 32
30
24
23 22
50
48 31 17
448 29 10
6
37
3
41
3328
15
25
7
+
12
18
25
377
3
18
12
18
12
32
55
45 32
30
24
23 22
50
48 31 17
448 29 10
6
37
3
41
3328
15
25
7
12
+
18
25
377
3
41
28 33 25
3715 7
3
18
12
18
12
33
55
45 32
30
24
23 22
50
48 31 17
448 29 10
6
37
3
41
3328
15
25
7
+
18
12
41
28 33 25
3715 7
3
12
18
25
377
3
41
28 33 25
3715 7
3
18
12
34
55
45 32
30
24
23 22
50
48 31 17
448 29 10
6
37
3
41
3328
15
25
7
+
18
12
41
28 33 25
3715 7
3
12
18
25
377
3
41
28 33 25
3715 7
3
55
45 32
30
24
23 22
50
48 31 17
448 29 10
6
18
12
35
Binomial Heap: Union
Create heap H that is union of heaps H’ and H’’.
s Analogous to binary addition.
Running time. O(log N)
s Proportional to number of trees in root lists ≤ 2( log2 N + 1).
001 1
100 1+
011 1
11
1
1
0
1
19 + 7 = 26
36
3
37
6 18
55
45 32
30
24
23 22
50
48 31 17
448 29 10
H
Binomial Heap: Delete Min
Delete node with minimum key in binomial heap H.
s Find root x with min key in root list of H, and delete
s H’ ← broken binomial trees
s H ← Union(H’, H)
Running time. O(log N)
37
Binomial Heap: Delete Min
Delete node with minimum key in binomial heap H.
s Find root x with min key in root list of H, and delete
s H’ ← broken binomial trees
s H ← Union(H’, H)
Running time. O(log N)
55
45 32
30
24
23 22
50
48 31 17
37
6 18
448 29 10
H
H’ 38
3
37
6 18
55
x 32
30
24
23 22
50
48 31 17
448 29 10
H
Binomial Heap: Decrease Key
Decrease key of node x in binomial heap H.
s Suppose x is in binomial tree Bk.
s Bubble node x up the tree if x is too small.
Running time. O(log N)
s Proportional to depth of node x ≤ log2 N .
depth = 3
39
Binomial Heap: Delete
Delete node x in binomial heap H.
s Decrease key of x to -∞.
s Delete min.
Running time. O(log N)
40
Binomial Heap: Insert
Insert a new node x into binomial heap H.
s H’ ← MakeHeap(x)
s H ← Union(H’, H)
Running time. O(log N)
3
37
6 18
55
45 32
30
24
23 22
50
48 31 17
448 29 10
H
x
H’
41
Binomial Heap: Sequence of Inserts
Insert a new node x into binomial heap H.
s If N = .......0, then only 1 steps.
s If N = ......01, then only 2 steps.
s If N = .....011, then only 3 steps.
s If N = ....0111, then only 4 steps.
Inserting 1 item can take Ω(log N) time.
s If N = 11...111, then log2 N steps.
But, inserting sequence of N items takes O(N) time!
s (N/2)(1) + (N/4)(2) + (N/8)(3) + . . . ≤ 2N
s Amortized analysis.
s Basis for getting most operations
down to constant time.
50
48 31 17
4429 10
3
37
6 x
2
2
1
2
2
2 1
1
≤
−−=∑ −
=
NN
N
n
n
Nn
42
Priority Queues
make-heap
Operation
insert
find-min
delete-min
union
decrease-key
delete
1
Binary
log N
1
log N
N
log N
log N
1
Binomial
log N
log N
log N
log N
log N
log N
1
Fibonacci *
1
1
log N
1
1
log N
1
Relaxed
1
1
log N
1
1
log N
1
Linked List
1
N
N
1
1
N
is-empty 1 1 1 11
Heaps
just did this

Contenu connexe

Tendances

Reed Solomon encoder and decoder \ ريد سلمون
Reed Solomon encoder and decoder \ ريد سلمونReed Solomon encoder and decoder \ ريد سلمون
Reed Solomon encoder and decoder \ ريد سلمونMuhammed Abdulmahdi
 
Gaussian Integration
Gaussian IntegrationGaussian Integration
Gaussian IntegrationReza Rahimi
 
Gaussian quadratures
Gaussian quadraturesGaussian quadratures
Gaussian quadraturesTarun Gehlot
 
Solution manual for theory and applications of digital speech processing lawr...
Solution manual for theory and applications of digital speech processing lawr...Solution manual for theory and applications of digital speech processing lawr...
Solution manual for theory and applications of digital speech processing lawr...zammok
 
Natural and Clamped Cubic Splines
Natural and Clamped Cubic SplinesNatural and Clamped Cubic Splines
Natural and Clamped Cubic SplinesMark Brandao
 
Numerical differentiation integration
Numerical differentiation integrationNumerical differentiation integration
Numerical differentiation integrationTarun Gehlot
 
lecture 4
lecture 4lecture 4
lecture 4sajinsc
 
01 knapsack using backtracking
01 knapsack using backtracking01 knapsack using backtracking
01 knapsack using backtrackingmandlapure
 
Dancing Links: an educational pearl
Dancing Links: an educational pearlDancing Links: an educational pearl
Dancing Links: an educational pearlESUG
 
Num Integration
Num IntegrationNum Integration
Num Integrationmuhdisys
 
Applications of datastructures
Applications of datastructuresApplications of datastructures
Applications of datastructuresRatsietsi Mokete
 
Econometric Analysis 8th Edition Greene Solutions Manual
Econometric Analysis 8th Edition Greene Solutions ManualEconometric Analysis 8th Edition Greene Solutions Manual
Econometric Analysis 8th Edition Greene Solutions ManualLewisSimmonss
 
8.7 numerical integration
8.7 numerical integration8.7 numerical integration
8.7 numerical integrationdicosmo178
 
Binomial heap (a concept of Data Structure)
Binomial heap (a concept of Data Structure)Binomial heap (a concept of Data Structure)
Binomial heap (a concept of Data Structure)Saliha Bilal
 

Tendances (19)

Reed Solomon encoder and decoder \ ريد سلمون
Reed Solomon encoder and decoder \ ريد سلمونReed Solomon encoder and decoder \ ريد سلمون
Reed Solomon encoder and decoder \ ريد سلمون
 
Gaussian Integration
Gaussian IntegrationGaussian Integration
Gaussian Integration
 
Gaussian quadratures
Gaussian quadraturesGaussian quadratures
Gaussian quadratures
 
Solution manual for theory and applications of digital speech processing lawr...
Solution manual for theory and applications of digital speech processing lawr...Solution manual for theory and applications of digital speech processing lawr...
Solution manual for theory and applications of digital speech processing lawr...
 
Lec9
Lec9Lec9
Lec9
 
CLIM Undergraduate Workshop: (Attachment) Performing Extreme Value Analysis (...
CLIM Undergraduate Workshop: (Attachment) Performing Extreme Value Analysis (...CLIM Undergraduate Workshop: (Attachment) Performing Extreme Value Analysis (...
CLIM Undergraduate Workshop: (Attachment) Performing Extreme Value Analysis (...
 
Natural and Clamped Cubic Splines
Natural and Clamped Cubic SplinesNatural and Clamped Cubic Splines
Natural and Clamped Cubic Splines
 
Numerical differentiation integration
Numerical differentiation integrationNumerical differentiation integration
Numerical differentiation integration
 
lecture 4
lecture 4lecture 4
lecture 4
 
01 knapsack using backtracking
01 knapsack using backtracking01 knapsack using backtracking
01 knapsack using backtracking
 
Dancing Links: an educational pearl
Dancing Links: an educational pearlDancing Links: an educational pearl
Dancing Links: an educational pearl
 
Num Integration
Num IntegrationNum Integration
Num Integration
 
Applications of datastructures
Applications of datastructuresApplications of datastructures
Applications of datastructures
 
Lec5
Lec5Lec5
Lec5
 
Econometric Analysis 8th Edition Greene Solutions Manual
Econometric Analysis 8th Edition Greene Solutions ManualEconometric Analysis 8th Edition Greene Solutions Manual
Econometric Analysis 8th Edition Greene Solutions Manual
 
DEV Project
DEV ProjectDEV Project
DEV Project
 
Soft Heaps
Soft HeapsSoft Heaps
Soft Heaps
 
8.7 numerical integration
8.7 numerical integration8.7 numerical integration
8.7 numerical integration
 
Binomial heap (a concept of Data Structure)
Binomial heap (a concept of Data Structure)Binomial heap (a concept of Data Structure)
Binomial heap (a concept of Data Structure)
 

Similaire à Heaps 4up

Applicationof datastructures
Applicationof datastructuresApplicationof datastructures
Applicationof datastructuresHitesh Wagle
 
Dynamic Programming.pptx
Dynamic Programming.pptxDynamic Programming.pptx
Dynamic Programming.pptxThanga Ramya S
 
03-data-structures.pdf
03-data-structures.pdf03-data-structures.pdf
03-data-structures.pdfNash229987
 
binomial & fibonaccci heap by priya
binomial & fibonaccci heap by priyabinomial & fibonaccci heap by priya
binomial & fibonaccci heap by priyaShohani Priya
 
zkStudyClub: PLONKUP & Reinforced Concrete [Luke Pearson, Joshua Fitzgerald, ...
zkStudyClub: PLONKUP & Reinforced Concrete [Luke Pearson, Joshua Fitzgerald, ...zkStudyClub: PLONKUP & Reinforced Concrete [Luke Pearson, Joshua Fitzgerald, ...
zkStudyClub: PLONKUP & Reinforced Concrete [Luke Pearson, Joshua Fitzgerald, ...Alex Pruden
 
A Simple Algorithm for Minimal Unsatisfiable core
A Simple Algorithm for Minimal Unsatisfiable coreA Simple Algorithm for Minimal Unsatisfiable core
A Simple Algorithm for Minimal Unsatisfiable corePvc Pvc
 
Binary heap (Priority Queue).ppt
Binary heap (Priority Queue).pptBinary heap (Priority Queue).ppt
Binary heap (Priority Queue).pptMano Arun
 
10.m way search tree
10.m way search tree10.m way search tree
10.m way search treeChandan Singh
 
20101017 program analysis_for_security_livshits_lecture02_compilers
20101017 program analysis_for_security_livshits_lecture02_compilers20101017 program analysis_for_security_livshits_lecture02_compilers
20101017 program analysis_for_security_livshits_lecture02_compilersComputer Science Club
 

Similaire à Heaps 4up (20)

b-tree.ppt
b-tree.pptb-tree.ppt
b-tree.ppt
 
Binomial heaps
Binomial heapsBinomial heaps
Binomial heaps
 
Applicationof datastructures
Applicationof datastructuresApplicationof datastructures
Applicationof datastructures
 
Divide and conquer
Divide and conquerDivide and conquer
Divide and conquer
 
Dynamic Programming.pptx
Dynamic Programming.pptxDynamic Programming.pptx
Dynamic Programming.pptx
 
03-data-structures.pdf
03-data-structures.pdf03-data-structures.pdf
03-data-structures.pdf
 
binomial & fibonaccci heap by priya
binomial & fibonaccci heap by priyabinomial & fibonaccci heap by priya
binomial & fibonaccci heap by priya
 
B tree
B  treeB  tree
B tree
 
AOA ppt.ppt
AOA ppt.pptAOA ppt.ppt
AOA ppt.ppt
 
Rand final
Rand finalRand final
Rand final
 
CLIM Fall 2017 Course: Statistics for Climate Research, Estimating Curves and...
CLIM Fall 2017 Course: Statistics for Climate Research, Estimating Curves and...CLIM Fall 2017 Course: Statistics for Climate Research, Estimating Curves and...
CLIM Fall 2017 Course: Statistics for Climate Research, Estimating Curves and...
 
Lecture3
Lecture3Lecture3
Lecture3
 
BTree.pptx
BTree.pptxBTree.pptx
BTree.pptx
 
zkStudyClub: PLONKUP & Reinforced Concrete [Luke Pearson, Joshua Fitzgerald, ...
zkStudyClub: PLONKUP & Reinforced Concrete [Luke Pearson, Joshua Fitzgerald, ...zkStudyClub: PLONKUP & Reinforced Concrete [Luke Pearson, Joshua Fitzgerald, ...
zkStudyClub: PLONKUP & Reinforced Concrete [Luke Pearson, Joshua Fitzgerald, ...
 
Btreesprogramme
BtreesprogrammeBtreesprogramme
Btreesprogramme
 
A Simple Algorithm for Minimal Unsatisfiable core
A Simple Algorithm for Minimal Unsatisfiable coreA Simple Algorithm for Minimal Unsatisfiable core
A Simple Algorithm for Minimal Unsatisfiable core
 
Binary heap (Priority Queue).ppt
Binary heap (Priority Queue).pptBinary heap (Priority Queue).ppt
Binary heap (Priority Queue).ppt
 
10.m way search tree
10.m way search tree10.m way search tree
10.m way search tree
 
20101017 program analysis_for_security_livshits_lecture02_compilers
20101017 program analysis_for_security_livshits_lecture02_compilers20101017 program analysis_for_security_livshits_lecture02_compilers
20101017 program analysis_for_security_livshits_lecture02_compilers
 
bv_cvxslides (1).pdf
bv_cvxslides (1).pdfbv_cvxslides (1).pdf
bv_cvxslides (1).pdf
 

Dernier

Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsJoaquim Jorge
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...Neo4j
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Drew Madelung
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...Martijn de Jong
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Igalia
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationRadu Cotescu
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfEnterprise Knowledge
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024The Digital Insurer
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptxHampshireHUG
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfsudhanshuwaghmare1
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxMalak Abu Hammad
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024Rafal Los
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountPuma Security, LLC
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking MenDelhi Call girls
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking MenDelhi Call girls
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUK Journal
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationSafe Software
 
What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?Antenna Manufacturer Coco
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationMichael W. Hawkins
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking MenDelhi Call girls
 

Dernier (20)

Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptx
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path Mount
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
 

Heaps 4up

  • 1. Princeton University • COS 423 • Theory of Algorithms • Spring 2002 • Kevin Wayne Binary and Binomial Heaps These lecture slides are adapted from CLRS, Chapters 6, 19. 2 Priority Queues Supports the following operations. s Insert element x. s Return min element. s Return and delete minimum element. s Decrease key of element x to k. Applications. s Dijkstra’s shortest path algorithm. s Prim’s MST algorithm. s Event-driven simulation. s Huffman encoding. s Heapsort. s . . . 3 Priority Queues in Action PQinit() for each v ∈ V key(v) ← ∞ PQinsert(v) key(s) ← 0 while (!PQisempty()) v = PQdelmin() for each w ∈ Q s.t (v,w) ∈ E if π(w) > π(v) + c(v,w) PQdecrease(w, π(v) + c(v,w)) Dijkstra’s Shortest Path Algorithm 4 Dijkstra/Prim 1 make-heap |V| insert |V| delete-min |E| decrease-key Priority Queues make-heap Operation insert find-min delete-min union decrease-key delete 1 Binary log N 1 log N N log N log N 1 Binomial log N log N log N log N log N log N 1 Fibonacci * 1 1 log N 1 1 log N 1 Relaxed 1 1 log N 1 1 log N 1 Linked List 1 N N 1 1 N is-empty 1 1 1 11 Heaps O(|E| + |V| log |V|)O(|E| log |V|)O(|V|2)
  • 2. 5 Binary Heap: Definition Binary heap. s Almost complete binary tree. – filled on all levels, except last, where filled from left to right s Min-heap ordered. – every child greater than (or equal to) parent 06 14 78 18 81 7791 45 5347 6484 9983 6 Binary Heap: Properties Properties. s Min element is in root. s Heap with N elements has height = log2 N. 06 14 78 18 81 7791 45 5347 6484 9983 N = 14 Height = 3 7 Binary Heaps: Array Implementation Implementing binary heaps. s Use an array: no need for explicit parent or child pointers. – Parent(i) = i/2 – Left(i) = 2i – Right(i) = 2i + 1 06 14 78 18 81 7791 45 5347 6484 9983 1 2 3 4 5 6 7 8 9 10 11 12 13 14 8 Binary Heap: Insertion Insert element x into heap. s Insert into next available slot. s Bubble up until it’s heap ordered. – Peter principle: nodes rise to level of incompetence 06 14 78 18 81 7791 45 5347 6484 9983 42 next free slot
  • 3. 9 Binary Heap: Insertion Insert element x into heap. s Insert into next available slot. s Bubble up until it’s heap ordered. – Peter principle: nodes rise to level of incompetence 06 14 78 18 81 7791 45 5347 6484 9983 4242 swap with parent 10 Binary Heap: Insertion Insert element x into heap. s Insert into next available slot. s Bubble up until it’s heap ordered. – Peter principle: nodes rise to level of incompetence 06 14 78 18 81 7791 45 4247 6484 9983 4253 swap with parent 11 Binary Heap: Insertion Insert element x into heap. s Insert into next available slot. s Bubble up until it’s heap ordered. – Peter principle: nodes rise to level of incompetence s O(log N) operations. 06 14 78 18 81 7791 42 4547 6484 9983 53 stop: heap ordered 12 Binary Heap: Decrease Key Decrease key of element x to k. s Bubble up until it’s heap ordered. s O(log N) operations. 06 14 78 18 81 7791 42 4547 6484 9983 53
  • 4. 13 Binary Heap: Delete Min Delete minimum element from heap. s Exchange root with rightmost leaf. s Bubble root down until it’s heap ordered. – power struggle principle: better subordinate is promoted 06 14 78 18 81 7791 42 4547 6484 9983 53 14 Binary Heap: Delete Min Delete minimum element from heap. s Exchange root with rightmost leaf. s Bubble root down until it’s heap ordered. – power struggle principle: better subordinate is promoted 53 14 78 18 81 7791 42 4547 6484 9983 06 15 Binary Heap: Delete Min Delete minimum element from heap. s Exchange root with rightmost leaf. s Bubble root down until it’s heap ordered. – power struggle principle: better subordinate is promoted 53 14 78 18 81 7791 42 4547 6484 9983 exchange with left child 16 Binary Heap: Delete Min Delete minimum element from heap. s Exchange root with rightmost leaf. s Bubble root down until it’s heap ordered. – power struggle principle: better subordinate is promoted 14 53 78 18 81 7791 42 4547 6484 9983 exchange with right child
  • 5. 17 Binary Heap: Delete Min Delete minimum element from heap. s Exchange root with rightmost leaf. s Bubble root down until it’s heap ordered. – power struggle principle: better subordinate is promoted s O(log N) operations. 14 18 78 53 81 7791 42 4547 6484 9983 stop: heap ordered 18 Binary Heap: Heapsort Heapsort. s Insert N items into binary heap. s Perform N delete-min operations. s O(N log N) sort. s No extra storage. 19 H1 H2 14 78 18 81 7791 11 6253 6484 9941 Binary Heap: Union Union. s Combine two binary heaps H1 and H2 into a single heap. s No easy solution. – Ω(N) operations apparently required s Can support fast union with fancier heaps. 20 Priority Queues make-heap Operation insert find-min delete-min union decrease-key delete 1 Binary log N 1 log N N log N log N 1 Binomial log N log N log N log N log N log N 1 Fibonacci * 1 1 log N 1 1 log N 1 Relaxed 1 1 log N 1 1 log N 1 Linked List 1 N N 1 1 N is-empty 1 1 1 11 Heaps
  • 6. 21 Binomial Tree Binomial tree. s Recursive definition: Bk-1 Bk-1 B0 Bk B0 B1 B2 B3 B4 22 Binomial Tree Useful properties of order k binomial tree Bk. s Number of nodes = 2k. s Height = k. s Degree of root = k. s Deleting root yields binomial trees Bk-1, … , B0. Proof. s By induction on k. B0 B1 B2 B3 B4 B1 Bk Bk+1 B2 B0 23 Binomial Tree A property useful for naming the data structure. s Bk has nodes at depth i. B4       i k 6 2 4 =      depth 2 depth 3 depth 4 depth 0 depth 1 24 Binomial Heap Binomial heap. Vuillemin, 1978. s Sequence of binomial trees that satisfy binomial heap property. – each tree is min-heap ordered – 0 or 1 binomial tree of order k B4 B0B1 55 45 32 30 24 23 22 50 48 31 17 448 29 10 6 37 3 18
  • 7. 25 Binomial Heap: Implementation Implementation. s Represent trees using left-child, right sibling pointers. – three links per node (parent, left, right) s Roots of trees connected with singly linked list. – degrees of trees strictly decreasing from left to right 50 48 31 17 4410 6 37 3 18 29 6 37 3 18 48 3150 10 4417 heap 29 Leftist Power-of-2 HeapBinomial Heap Parent Left Right 26 Binomial Heap: Properties Properties of N-node binomial heap. s Min key contained in root of B0, B1, . . . , Bk. s Contains binomial tree Bi iff bi = 1 where bn⋅ b2b1b0 is binary representation of N. s At most log2 N + 1 binomial trees. s Height ≤ log2 N. B4 B0B1 55 45 32 30 24 23 22 50 48 31 17 448 29 10 6 37 3 18 N = 19 # trees = 3 height = 4 binary = 10011 27 Binomial Heap: Union Create heap H that is union of heaps H’ and H’’. s "Mergeable heaps." s Easy if H’ and H’’ are each order k binomial trees. – connect roots of H’ and H’’ – choose smaller key to be root of H H’’ 55 45 32 30 24 23 22 50 48 31 17 448 29 10 6 H’ 28 Binomial Heap: Union 001 1 100 1+ 011 1 11 1 1 0 1 19 + 7 = 26 55 45 32 30 24 23 22 50 48 31 17 448 29 10 6 37 3 18 41 3328 15 25 7 12 +
  • 8. 29 Binomial Heap: Union 55 45 32 30 24 23 22 50 48 31 17 448 29 10 6 37 3 18 41 3328 15 25 7 12 + 30 Binomial Heap: Union 55 45 32 30 24 23 22 50 48 31 17 448 29 10 6 37 3 41 3328 15 25 7 + 12 18 18 12 31 55 45 32 30 24 23 22 50 48 31 17 448 29 10 6 37 3 41 3328 15 25 7 + 12 18 25 377 3 18 12 18 12 32 55 45 32 30 24 23 22 50 48 31 17 448 29 10 6 37 3 41 3328 15 25 7 12 + 18 25 377 3 41 28 33 25 3715 7 3 18 12 18 12
  • 9. 33 55 45 32 30 24 23 22 50 48 31 17 448 29 10 6 37 3 41 3328 15 25 7 + 18 12 41 28 33 25 3715 7 3 12 18 25 377 3 41 28 33 25 3715 7 3 18 12 34 55 45 32 30 24 23 22 50 48 31 17 448 29 10 6 37 3 41 3328 15 25 7 + 18 12 41 28 33 25 3715 7 3 12 18 25 377 3 41 28 33 25 3715 7 3 55 45 32 30 24 23 22 50 48 31 17 448 29 10 6 18 12 35 Binomial Heap: Union Create heap H that is union of heaps H’ and H’’. s Analogous to binary addition. Running time. O(log N) s Proportional to number of trees in root lists ≤ 2( log2 N + 1). 001 1 100 1+ 011 1 11 1 1 0 1 19 + 7 = 26 36 3 37 6 18 55 45 32 30 24 23 22 50 48 31 17 448 29 10 H Binomial Heap: Delete Min Delete node with minimum key in binomial heap H. s Find root x with min key in root list of H, and delete s H’ ← broken binomial trees s H ← Union(H’, H) Running time. O(log N)
  • 10. 37 Binomial Heap: Delete Min Delete node with minimum key in binomial heap H. s Find root x with min key in root list of H, and delete s H’ ← broken binomial trees s H ← Union(H’, H) Running time. O(log N) 55 45 32 30 24 23 22 50 48 31 17 37 6 18 448 29 10 H H’ 38 3 37 6 18 55 x 32 30 24 23 22 50 48 31 17 448 29 10 H Binomial Heap: Decrease Key Decrease key of node x in binomial heap H. s Suppose x is in binomial tree Bk. s Bubble node x up the tree if x is too small. Running time. O(log N) s Proportional to depth of node x ≤ log2 N . depth = 3 39 Binomial Heap: Delete Delete node x in binomial heap H. s Decrease key of x to -∞. s Delete min. Running time. O(log N) 40 Binomial Heap: Insert Insert a new node x into binomial heap H. s H’ ← MakeHeap(x) s H ← Union(H’, H) Running time. O(log N) 3 37 6 18 55 45 32 30 24 23 22 50 48 31 17 448 29 10 H x H’
  • 11. 41 Binomial Heap: Sequence of Inserts Insert a new node x into binomial heap H. s If N = .......0, then only 1 steps. s If N = ......01, then only 2 steps. s If N = .....011, then only 3 steps. s If N = ....0111, then only 4 steps. Inserting 1 item can take Ω(log N) time. s If N = 11...111, then log2 N steps. But, inserting sequence of N items takes O(N) time! s (N/2)(1) + (N/4)(2) + (N/8)(3) + . . . ≤ 2N s Amortized analysis. s Basis for getting most operations down to constant time. 50 48 31 17 4429 10 3 37 6 x 2 2 1 2 2 2 1 1 ≤ −−=∑ − = NN N n n Nn 42 Priority Queues make-heap Operation insert find-min delete-min union decrease-key delete 1 Binary log N 1 log N N log N log N 1 Binomial log N log N log N log N log N log N 1 Fibonacci * 1 1 log N 1 1 log N 1 Relaxed 1 1 log N 1 1 log N 1 Linked List 1 N N 1 1 N is-empty 1 1 1 11 Heaps just did this