SlideShare une entreprise Scribd logo
1  sur  24
Heaps & Adaptable Priority
Queues
By – Priyanka Rana
Session 2 review
In list-based implementation of priority queue:
If we use unsorted lists:
 insertion takes O(1) time
 deletion takes O(n) time.
If we use sorted lists:
 insertion takes O(n) time
 deletion takes O(1) time.
Session 2 Review
Selection-sort:
Phase 1 -> fast running time.
Phase 2 -> slow running time.
Insertion-sort:
Phase 1 -> slow running time.
Phase 2 -> fast running time.
Balancing the running time
Heap
Heap Data structure
Is a binary tree T that stores a collection of entries at its nodes
and satisfies two additional properties:
Order Property
Structure Property
Heap properties
Order Property:
For every internal node v, other than the root, key(v) ≥
key(parent(v)).
Structure Property:
A heap T with height h is a complete binary tree, i.e.:
for depth/level i < h, there are 2i nodes.
at depth h−1, the internal nodes are to the left of the external
nodes.
Heap data structure
The last node of a heap is the rightmost node of the
maximum depth.
2
65
89 Last node
Height of heap
A heap storing n keys has height h = [lg n]
For each depth i < h , there are 2i nodes and
at least one node at depth h. Thus,
n ≥ 20 + 21+ 22 + ... + 2h-1 + 1
≥ 1 + 2 + 4 + ... + 2h-1 + 1
≥ 2h
Which gives, h ≤ lgn.
Array list representation of heap
For the node at rank i
the left child is at rank 2i.
the right child is at rank 2i+1.
The cell at rank 0 is not used
A heap with n keys is represented by means of a array size n+1.
2
65
79
0 1 2 3 4 5
2 5 6 79
Insertion Algorithm
The insertion algorithm consists of three steps:
Find the insertion node z (i.e. the new last node).
Store k at z. (Here k=1)
Restore the heap-order property (discussed next).
2
65
79 2
5
79
z
Insertion Node
6
1
Up-heap bubbling
1
5
79
2
6
2
5
79
1
6
Removal from a heap
The removal algorithm consists of three steps:
 Replace the root key with the key of the last node
w.
 Remove w.
 Restore the heap-order property (discussed next).
2
65
79 7
5
9
w
Last Node
6
New Last Node
Down-heap bubbling
5
7
9
6
7
5
w9
6
Heap-sort
A heap-based priority queue, sorts a sequence of n
elements in O(nlgn) time.
performs n series of removeMin() operation.
Since each call of removeMin() takes O(lgn) time, sorting a
heap with n elements takes O(nlgn) time.
Merging two heaps
2
6
3
8 5
72
47
4
Exercise
At which nodes of a heap can an entry with the
largest key be stored?
Adaptable Priority Queues
Priority queues which are adaptable.
When priority queue methods are not sufficient.
For example, in a standby airline passenger application.
Adaptable Priority Queue ADT
remove(e):
Remove from P and return e.
replaceKey(e, k):
Replace with k and return the key of entry e of P.
replaceValue(e, x):
 Replace with x and return the value of entry e of P.
Exercise
Operation Output
Insert(5,A) e1
Insert(3,B) e2
Insert(7,C) e3
min() e2
Key(e2) 3
remove(e1) e1
replaceKey(e2,9) 3
replaceValue(e3,D
)
C
Remove(e2) e2
isEmpty() “false”
(3,B)
,
(5,A),(7,C),(9,B),(7,D)
Location aware entry
mechanism for finding the position of entry e of Priority Queue P.
This position is called the location of the entry
implementation of an entry that keeps track of its position is called
location-aware entry.
List implementation of an adaptable
PQ
A location-aware list entry is an object storing
key
value
position (or rank) of the entry in the list.
Heap Implementation of an Adaptable PQ
A location-aware list entry is an object storing
key
value
position of the entry in the heap.
Performance of Adaptable PQ Implementations
Method Unsorted List Sorted List Heap
Size, isEmpty() O(1) O(1) O(1)
insert O(1) O(n) O(n)
min O(n) O(1) O(1)
removeMin O(n) O(1) O(lg n)
remove O(1) O(1) O(lg n)
replaceKey O(1) O(n) O(lg n)
replaceValue O(1) O(1) O(1)
THANK YOU

Contenu connexe

Tendances

resolution in the propositional calculus
resolution in the propositional calculusresolution in the propositional calculus
resolution in the propositional calculusAnju Kanjirathingal
 
Mining Frequent Patterns, Association and Correlations
Mining Frequent Patterns, Association and CorrelationsMining Frequent Patterns, Association and Correlations
Mining Frequent Patterns, Association and CorrelationsJustin Cletus
 
Association rule mining
Association rule miningAssociation rule mining
Association rule miningAcad
 
Data decomposition techniques
Data decomposition techniquesData decomposition techniques
Data decomposition techniquesMohamed Ramadan
 
3. Relational Models in DBMS
3. Relational Models in DBMS3. Relational Models in DBMS
3. Relational Models in DBMSkoolkampus
 
Interaction Modeling
Interaction ModelingInteraction Modeling
Interaction ModelingHemant Sharma
 
Risk management(software engineering)
Risk management(software engineering)Risk management(software engineering)
Risk management(software engineering)Priya Tomar
 
Predicate calculus
Predicate calculusPredicate calculus
Predicate calculusRajendran
 
Unit 2(advanced class modeling & state diagram)
Unit  2(advanced class modeling & state diagram)Unit  2(advanced class modeling & state diagram)
Unit 2(advanced class modeling & state diagram)Manoj Reddy
 
Partial redundancy elimination
Partial redundancy eliminationPartial redundancy elimination
Partial redundancy eliminationIshucs
 
Chart and graphs in R programming language
Chart and graphs in R programming language Chart and graphs in R programming language
Chart and graphs in R programming language CHANDAN KUMAR
 
03 algorithm properties
03 algorithm properties03 algorithm properties
03 algorithm propertiesLincoln School
 
3.7 outlier analysis
3.7 outlier analysis3.7 outlier analysis
3.7 outlier analysisKrish_ver2
 
Methods for handling deadlocks
Methods for handling deadlocksMethods for handling deadlocks
Methods for handling deadlocksA. S. M. Shafi
 

Tendances (20)

resolution in the propositional calculus
resolution in the propositional calculusresolution in the propositional calculus
resolution in the propositional calculus
 
3 classification
3  classification3  classification
3 classification
 
Mining Frequent Patterns, Association and Correlations
Mining Frequent Patterns, Association and CorrelationsMining Frequent Patterns, Association and Correlations
Mining Frequent Patterns, Association and Correlations
 
Association rule mining
Association rule miningAssociation rule mining
Association rule mining
 
Data decomposition techniques
Data decomposition techniquesData decomposition techniques
Data decomposition techniques
 
Branch and bound
Branch and boundBranch and bound
Branch and bound
 
3. Relational Models in DBMS
3. Relational Models in DBMS3. Relational Models in DBMS
3. Relational Models in DBMS
 
Interaction Modeling
Interaction ModelingInteraction Modeling
Interaction Modeling
 
Risk management(software engineering)
Risk management(software engineering)Risk management(software engineering)
Risk management(software engineering)
 
Predicate calculus
Predicate calculusPredicate calculus
Predicate calculus
 
Unit 2(advanced class modeling & state diagram)
Unit  2(advanced class modeling & state diagram)Unit  2(advanced class modeling & state diagram)
Unit 2(advanced class modeling & state diagram)
 
Partial redundancy elimination
Partial redundancy eliminationPartial redundancy elimination
Partial redundancy elimination
 
Chart and graphs in R programming language
Chart and graphs in R programming language Chart and graphs in R programming language
Chart and graphs in R programming language
 
Applications of expert system
Applications of expert systemApplications of expert system
Applications of expert system
 
Data reduction
Data reductionData reduction
Data reduction
 
Ooad unit – 1 introduction
Ooad unit – 1 introductionOoad unit – 1 introduction
Ooad unit – 1 introduction
 
03 algorithm properties
03 algorithm properties03 algorithm properties
03 algorithm properties
 
Predicate logic
 Predicate logic Predicate logic
Predicate logic
 
3.7 outlier analysis
3.7 outlier analysis3.7 outlier analysis
3.7 outlier analysis
 
Methods for handling deadlocks
Methods for handling deadlocksMethods for handling deadlocks
Methods for handling deadlocks
 

Similaire à Heaps & Adaptable priority Queues

Similaire à Heaps & Adaptable priority Queues (20)

Sorting
SortingSorting
Sorting
 
03-data-structures.pdf
03-data-structures.pdf03-data-structures.pdf
03-data-structures.pdf
 
Computer Network Assignment Help
Computer Network Assignment HelpComputer Network Assignment Help
Computer Network Assignment Help
 
Daa chapter5
Daa chapter5Daa chapter5
Daa chapter5
 
lecture 6
lecture 6lecture 6
lecture 6
 
Introduction to data structures and complexity.pptx
Introduction to data structures and complexity.pptxIntroduction to data structures and complexity.pptx
Introduction to data structures and complexity.pptx
 
Lec3
Lec3Lec3
Lec3
 
Lec3
Lec3Lec3
Lec3
 
Analysis and design of algorithms part2
Analysis and design of algorithms part2Analysis and design of algorithms part2
Analysis and design of algorithms part2
 
Algorithms Design Exam Help
Algorithms Design Exam HelpAlgorithms Design Exam Help
Algorithms Design Exam Help
 
Algorithms Design Homework Help
Algorithms Design Homework HelpAlgorithms Design Homework Help
Algorithms Design Homework Help
 
01 - DAA - PPT.pptx
01 - DAA - PPT.pptx01 - DAA - PPT.pptx
01 - DAA - PPT.pptx
 
Introduction To Stack
Introduction To StackIntroduction To Stack
Introduction To Stack
 
Review session2
Review session2Review session2
Review session2
 
DAA-Divide and Conquer methodology, DAA 2024
DAA-Divide and Conquer methodology, DAA 2024DAA-Divide and Conquer methodology, DAA 2024
DAA-Divide and Conquer methodology, DAA 2024
 
Algorithm: Quick-Sort
Algorithm: Quick-SortAlgorithm: Quick-Sort
Algorithm: Quick-Sort
 
Queue
QueueQueue
Queue
 
Unit 7 sorting
Unit 7   sortingUnit 7   sorting
Unit 7 sorting
 
Introduction to Algorithms
Introduction to AlgorithmsIntroduction to Algorithms
Introduction to Algorithms
 
Priority queues
Priority queuesPriority queues
Priority queues
 

Plus de Priyanka Rana

Insertion & Selection Sort - using Priority Queues
Insertion & Selection Sort - using Priority QueuesInsertion & Selection Sort - using Priority Queues
Insertion & Selection Sort - using Priority QueuesPriyanka Rana
 
Content package - Mobile computing
Content package - Mobile computingContent package - Mobile computing
Content package - Mobile computingPriyanka Rana
 
Scrum retrospective
Scrum retrospective Scrum retrospective
Scrum retrospective Priyanka Rana
 
Convergent divergent thinking &amp; wireframeprototyping
Convergent divergent thinking &amp; wireframeprototypingConvergent divergent thinking &amp; wireframeprototyping
Convergent divergent thinking &amp; wireframeprototypingPriyanka Rana
 
Sketching&storyboarding
Sketching&storyboardingSketching&storyboarding
Sketching&storyboardingPriyanka Rana
 
Building better prototype
Building better prototypeBuilding better prototype
Building better prototypePriyanka Rana
 
Mobile presence & location based marketing
Mobile presence & location based marketingMobile presence & location based marketing
Mobile presence & location based marketingPriyanka Rana
 
User friendliness of website
User friendliness of websiteUser friendliness of website
User friendliness of websitePriyanka Rana
 
E-commerce Marketing & advertising
E-commerce Marketing & advertisingE-commerce Marketing & advertising
E-commerce Marketing & advertisingPriyanka Rana
 
E commerce business proposal
E commerce business proposalE commerce business proposal
E commerce business proposalPriyanka Rana
 
E-strategic Management-1
E-strategic Management-1E-strategic Management-1
E-strategic Management-1Priyanka Rana
 
Trees - Non Linear Data Structure
Trees - Non Linear Data StructureTrees - Non Linear Data Structure
Trees - Non Linear Data StructurePriyanka Rana
 
Algorithm analysis basics - Seven Functions/Big-Oh/Omega/Theta
Algorithm analysis basics - Seven Functions/Big-Oh/Omega/ThetaAlgorithm analysis basics - Seven Functions/Big-Oh/Omega/Theta
Algorithm analysis basics - Seven Functions/Big-Oh/Omega/ThetaPriyanka Rana
 
Recursion - Algorithms and Data Structures
Recursion - Algorithms and Data StructuresRecursion - Algorithms and Data Structures
Recursion - Algorithms and Data StructuresPriyanka Rana
 

Plus de Priyanka Rana (19)

Insertion & Selection Sort - using Priority Queues
Insertion & Selection Sort - using Priority QueuesInsertion & Selection Sort - using Priority Queues
Insertion & Selection Sort - using Priority Queues
 
Scrum values
Scrum valuesScrum values
Scrum values
 
Usability testing
Usability testing  Usability testing
Usability testing
 
Content package - Mobile computing
Content package - Mobile computingContent package - Mobile computing
Content package - Mobile computing
 
Scrum retrospective
Scrum retrospective Scrum retrospective
Scrum retrospective
 
Convergent divergent thinking &amp; wireframeprototyping
Convergent divergent thinking &amp; wireframeprototypingConvergent divergent thinking &amp; wireframeprototyping
Convergent divergent thinking &amp; wireframeprototyping
 
Sketching&storyboarding
Sketching&storyboardingSketching&storyboarding
Sketching&storyboarding
 
Building better prototype
Building better prototypeBuilding better prototype
Building better prototype
 
Mobile presence & location based marketing
Mobile presence & location based marketingMobile presence & location based marketing
Mobile presence & location based marketing
 
User friendliness of website
User friendliness of websiteUser friendliness of website
User friendliness of website
 
E-commerce Marketing & advertising
E-commerce Marketing & advertisingE-commerce Marketing & advertising
E-commerce Marketing & advertising
 
E commerce business proposal
E commerce business proposalE commerce business proposal
E commerce business proposal
 
E-strategic Management-1
E-strategic Management-1E-strategic Management-1
E-strategic Management-1
 
Maps&hash tables
Maps&hash tablesMaps&hash tables
Maps&hash tables
 
Priority queues
Priority queuesPriority queues
Priority queues
 
Trees - Non Linear Data Structure
Trees - Non Linear Data StructureTrees - Non Linear Data Structure
Trees - Non Linear Data Structure
 
Algorithm analysis basics - Seven Functions/Big-Oh/Omega/Theta
Algorithm analysis basics - Seven Functions/Big-Oh/Omega/ThetaAlgorithm analysis basics - Seven Functions/Big-Oh/Omega/Theta
Algorithm analysis basics - Seven Functions/Big-Oh/Omega/Theta
 
Recursion - Algorithms and Data Structures
Recursion - Algorithms and Data StructuresRecursion - Algorithms and Data Structures
Recursion - Algorithms and Data Structures
 
Linked list
Linked listLinked list
Linked list
 

Dernier

Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...apidays
 
Ransomware_Q4_2023. The report. [EN].pdf
Ransomware_Q4_2023. The report. [EN].pdfRansomware_Q4_2023. The report. [EN].pdf
Ransomware_Q4_2023. The report. [EN].pdfOverkill Security
 
Cyberprint. Dark Pink Apt Group [EN].pdf
Cyberprint. Dark Pink Apt Group [EN].pdfCyberprint. Dark Pink Apt Group [EN].pdf
Cyberprint. Dark Pink Apt Group [EN].pdfOverkill Security
 
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Victor Rentea
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century educationjfdjdjcjdnsjd
 
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
 
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...apidays
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native ApplicationsWSO2
 
[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdfSandro Moreira
 
DBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDropbox
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProduct Anonymous
 
AXA XL - Insurer Innovation Award Americas 2024
AXA XL - Insurer Innovation Award Americas 2024AXA XL - Insurer Innovation Award Americas 2024
AXA XL - Insurer Innovation Award Americas 2024The Digital Insurer
 
Corporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxCorporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxRustici Software
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...DianaGray10
 
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Victor Rentea
 
FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024The Digital Insurer
 
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 AmsterdamDEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 AmsterdamUiPathCommunity
 
CNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In PakistanCNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In Pakistandanishmna97
 
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Jeffrey Haguewood
 
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdfRising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdfOrbitshub
 

Dernier (20)

Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
 
Ransomware_Q4_2023. The report. [EN].pdf
Ransomware_Q4_2023. The report. [EN].pdfRansomware_Q4_2023. The report. [EN].pdf
Ransomware_Q4_2023. The report. [EN].pdf
 
Cyberprint. Dark Pink Apt Group [EN].pdf
Cyberprint. Dark Pink Apt Group [EN].pdfCyberprint. Dark Pink Apt Group [EN].pdf
Cyberprint. Dark Pink Apt Group [EN].pdf
 
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
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
 
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
 
[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf
 
DBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor Presentation
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
AXA XL - Insurer Innovation Award Americas 2024
AXA XL - Insurer Innovation Award Americas 2024AXA XL - Insurer Innovation Award Americas 2024
AXA XL - Insurer Innovation Award Americas 2024
 
Corporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxCorporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptx
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
 
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
 
FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024
 
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 AmsterdamDEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
 
CNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In PakistanCNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In Pakistan
 
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
 
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdfRising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
 

Heaps & Adaptable priority Queues

  • 1. Heaps & Adaptable Priority Queues By – Priyanka Rana
  • 2. Session 2 review In list-based implementation of priority queue: If we use unsorted lists:  insertion takes O(1) time  deletion takes O(n) time. If we use sorted lists:  insertion takes O(n) time  deletion takes O(1) time.
  • 3. Session 2 Review Selection-sort: Phase 1 -> fast running time. Phase 2 -> slow running time. Insertion-sort: Phase 1 -> slow running time. Phase 2 -> fast running time.
  • 5. Heap Data structure Is a binary tree T that stores a collection of entries at its nodes and satisfies two additional properties: Order Property Structure Property
  • 6. Heap properties Order Property: For every internal node v, other than the root, key(v) ≥ key(parent(v)). Structure Property: A heap T with height h is a complete binary tree, i.e.: for depth/level i < h, there are 2i nodes. at depth h−1, the internal nodes are to the left of the external nodes.
  • 7. Heap data structure The last node of a heap is the rightmost node of the maximum depth. 2 65 89 Last node
  • 8. Height of heap A heap storing n keys has height h = [lg n] For each depth i < h , there are 2i nodes and at least one node at depth h. Thus, n ≥ 20 + 21+ 22 + ... + 2h-1 + 1 ≥ 1 + 2 + 4 + ... + 2h-1 + 1 ≥ 2h Which gives, h ≤ lgn.
  • 9. Array list representation of heap For the node at rank i the left child is at rank 2i. the right child is at rank 2i+1. The cell at rank 0 is not used A heap with n keys is represented by means of a array size n+1. 2 65 79 0 1 2 3 4 5 2 5 6 79
  • 10. Insertion Algorithm The insertion algorithm consists of three steps: Find the insertion node z (i.e. the new last node). Store k at z. (Here k=1) Restore the heap-order property (discussed next). 2 65 79 2 5 79 z Insertion Node 6 1
  • 12. Removal from a heap The removal algorithm consists of three steps:  Replace the root key with the key of the last node w.  Remove w.  Restore the heap-order property (discussed next). 2 65 79 7 5 9 w Last Node 6 New Last Node
  • 14. Heap-sort A heap-based priority queue, sorts a sequence of n elements in O(nlgn) time. performs n series of removeMin() operation. Since each call of removeMin() takes O(lgn) time, sorting a heap with n elements takes O(nlgn) time.
  • 16. Exercise At which nodes of a heap can an entry with the largest key be stored?
  • 17. Adaptable Priority Queues Priority queues which are adaptable. When priority queue methods are not sufficient. For example, in a standby airline passenger application.
  • 18. Adaptable Priority Queue ADT remove(e): Remove from P and return e. replaceKey(e, k): Replace with k and return the key of entry e of P. replaceValue(e, x):  Replace with x and return the value of entry e of P.
  • 19. Exercise Operation Output Insert(5,A) e1 Insert(3,B) e2 Insert(7,C) e3 min() e2 Key(e2) 3 remove(e1) e1 replaceKey(e2,9) 3 replaceValue(e3,D ) C Remove(e2) e2 isEmpty() “false” (3,B) , (5,A),(7,C),(9,B),(7,D)
  • 20. Location aware entry mechanism for finding the position of entry e of Priority Queue P. This position is called the location of the entry implementation of an entry that keeps track of its position is called location-aware entry.
  • 21. List implementation of an adaptable PQ A location-aware list entry is an object storing key value position (or rank) of the entry in the list.
  • 22. Heap Implementation of an Adaptable PQ A location-aware list entry is an object storing key value position of the entry in the heap.
  • 23. Performance of Adaptable PQ Implementations Method Unsorted List Sorted List Heap Size, isEmpty() O(1) O(1) O(1) insert O(1) O(n) O(n) min O(n) O(1) O(1) removeMin O(n) O(1) O(lg n) remove O(1) O(1) O(lg n) replaceKey O(1) O(n) O(lg n) replaceValue O(1) O(1) O(1)

Notes de l'éditeur

  1. &amp;lt;number&amp;gt;
  2. &amp;lt;number&amp;gt;
  3. &amp;lt;number&amp;gt;
  4. &amp;lt;number&amp;gt;
  5. &amp;lt;number&amp;gt;
  6. Order property is about every internal node whose key value must be greater than the key value of its parent. Depth: number of ancestors a node has Height: the height of a tree is given as the maximum depth of any node // structure property considers a heap of height &amp;quot;h&amp;quot; a complete binary tree when: its depth is less than its height and there are 2 raise to power of i nodes. another condition is that at height h-1 , internal nodes are to the left of the external nodes that // also means in a way that last node of a heap is the rightmost node of the maximum depth. &amp;lt;number&amp;gt;
  7. &amp;lt;number&amp;gt;
  8. this is about height of the heap, which is said to be as log n where n is the number of entries, now here we consider //the structural property of heap which says that at depth i ,// heap has 2 raise to power of i nodes. and //hence using mathematical operations// and in the end taking// log on both sides it is easily proved that height is log of number of entries. &amp;lt;number&amp;gt;
  9. We store a (key and value) for each entry item at each internal node, and keep track of the position of the last node. &amp;lt;number&amp;gt;
  10. Insertion into heap has three steps. // // // After the insertion of a new node, the heap-order property may be violated. we need to restore it by some swappings of the values stored at z with the entry stored in its ancestor nodes.// &amp;lt;number&amp;gt;
  11. The upward movement of the newly inserted entry //by means of swaps is called up-heap bubbling. Since the heap has height lgn, up-heap runs in O(lgn) time. &amp;lt;number&amp;gt;
  12. removal from a heap has three steps as well, // // // After replacing the root key with key k of the last node, again the heap-order property gets violated. Then we need to restore the heap-order by swapping the entries stored at root with the entries stored in its descendent nodes.// here The downward movement of the root entry by means of swaps is call down-heap bubbling. as the heap has height lgn, down-heap runs in O(lgn) time. &amp;lt;number&amp;gt;
  13. Using a heap-based priority queue, we can sort a sequence of n elements in O(nlgn) time, we have already covered sorting in detail, heap sort is also similar. //The algorithm simply performs n series of removeMin() operation. //Since each call of removeMin() takes O(lgn) time, sorting a heap with n elements takes O(nlgn) time. Traaversing takes O(logn) time --. Sorting takes O(nlog(n)) time &amp;lt;number&amp;gt;
  14. lets see how two are merged together, lets say We are given two heaps and a key k. //We create a new heap with the root node storing k and with the two heaps as subtrees.// We perform down-heap bubbling to restore the heap-order property. &amp;lt;number&amp;gt;
  15. &amp;lt;number&amp;gt;
  16. APQ are the priority queues which are adaptable, //There are some situations where the basic methods of the priority queue ADT (given in previous section) are not sufficient. //For example, in a standby airline passenger application: A passenger may request to be removed from the waiting list.A passenger presents a document (e.g. gold frequent flyer card) that implies to update his/her order in the waiting list.a passenger notices her name is misspelled on the ticket and asks it to be corrected. So, we need the priority queues be adaptable. &amp;lt;number&amp;gt;
  17. &amp;lt;number&amp;gt;
  18. &amp;lt;number&amp;gt;
  19. &amp;lt;number&amp;gt;
  20. &amp;lt;number&amp;gt;