SlideShare a Scribd company logo
1 of 20
Priority Queues
(Algorithms and Data Structures)
By
Priyanka Rana
By – Priyanka Rana
Priority Queues
Which assets (stock, company, real-estate) the company should
purchase for investment.
Priority Queues
Many factors determine the future value of an asset.
Let’s call this “Utility”.
Utility(asset) = priority or importance that Utility() return the
minimum value.
Use Utility() as key or order of assets in the list.
Definition Priority Queue
Is an Abstract Data Type.
Storing collection of prioritized elements.
Supports
arbitrary element insertion.
removal of elements in order of priority.
Priority Queue ADT
Each entry is a pair (key, value).
Key: used to identify or weigh that element.
Two distinct entries in a priority queue can have the same key.
Mathematical concept of total order relation, must satisfy:
Reflexive property: k ≤ k.
Antisymmetric property: if k1 ≤ k2 and k2 ≤ k1 , then k1 = k2.
Transitive property: if k1 ≤ k2 and k2 ≤ k3, then k1 ≤ k3.
Applications
Standby flyers.
Auctions.
Stock market.
Main methods of Priority Queue ADT
size():
Return the number of entries in the queue.
isEmpty():
Test whether the queue is empty.
min():
Return (do not remove) an entry with smallest key.
insert(k, x):
Insert an entry with key k and value x.
removeMin():
Remove and return the entry with smallest key.
Sorting with a Priority Queue
Sorting a collection S of n comparable elements
with a priority queue Q, called PriorityQueueSort.
Hint: Using insert() and removeMin()
Exercise
Operation Output
Insert(5,A) nil
Insert(4,B) nil
Insert(7,I) nil
removeMin() (4,B)
insert(3,J) Nil
removeMin() (3,J)
insert(6,L) nil
removeMin() (5,A)
Implementing a Priority Queue
• With an unsorted list :
• insert() – adds entry at last -> takes O(1)
• removeMin() – requires complete scan -> takes O(n)
• “fast insertion and slow removal”
4 5 2 3 1
 With a Sorted List:
 Insert() – requires complete scan -> takes O(n)
 removeMin() – always access the first element -> takes O(1)
 fast removal and slow insertions.
1 2 3 4 5
Method Unsorted List Sorted List
size, isEmpty O(1) O(1)
insert O(1) O(n)
Min, removeMin O(n) O(1)
Sorting using priority Queues
• Selection – Sort
• Insertion - Sort
Selection Sort – 7 4 8 2 5 3 9
7 4 8 2 5 3 9
2 4 8 7 5 3 9
2 3 8 7 5 4 9
2 3 4 7 5 8 9
2 3 4 5 7 8 9
2 3 4 5 7 8 9
2 3 4 5 7 8 9
Selection- Sorting using priority Queues
Sequence S Priority Queue P
Input (7,4,8,2,5,3,9) ()
Phase 1 (a)
(b)
.
.
.
(g)
(4,8,2,5,3,9)
(8,2,5,3,9)
.
.
.
()
(7)
(7,4)
.
.
.
(7,4,8,2,5,3,9)
Phase 2 (a)
(b)
(c)
(d)
(e)
(f)
(g)
(2)
(2,3)
(2,3,4)
(2,3,4,5)
(2,3,4,5,7)
(2,3,4,5,7,8)
(2,3,4,5,7,8,9)
(7,4,8,2,5,3,9)
(7,4,8,5,9)
(7,8,5,9)
(7,8,9)
(8,9)
(9)
()
Running Time
• First removeMin takes O(n) time
• Second removeMin takes O(n-1) time) and so on
• Till last n operation takes O(1) time
= O(n2)O(n
Insertion Sort – 7 4 8 2 5 3 9
7
4 7
4 7 8
2 4 7 8
2 3 4 5 7 8 9
2 3 4 5 7 8
2 4 5 7 8
Insertion -Sorting using priority Queues
Sequence S Priority Queue P
Input (7,4,8,2,5,3,9) ()
Phase 1 (a)
(b)
(c)
(d)
(e)
(f)
(g)
(4,8,2,5,3,9)
(8,2,5,3,9)
(2,5,3,9)
(5,3,9)
(3,9)
(9)
()
(7)
(4,7)
(4,7,8)
(2,4,7,8)
(2,4,5,7,8)
(2,3,4,5,7,8)
(2,3,4,5,7,8,9)
Phase 2 (a)
(b)
●
.
(2)
(2,3)
● .
● .
● .
(2,3,4,5,7,8,9)
(3,4,5,7,8,9)
(4,5,7,8,9)
.
.
.
()
Running Time
• First element -no comparison
• Second element – 1 comparison
• Third element – 2 comparisons and so on.
= O(n2)O(n
THANK YOU

More Related Content

What's hot

Binary Search Tree in Data Structure
Binary Search Tree in Data StructureBinary Search Tree in Data Structure
Binary Search Tree in Data StructureDharita Chokshi
 
Queue data structure
Queue data structureQueue data structure
Queue data structureanooppjoseph
 
Array data structure
Array data structureArray data structure
Array data structuremaamir farooq
 
Data Structures - Lecture 9 [Stack & Queue using Linked List]
 Data Structures - Lecture 9 [Stack & Queue using Linked List] Data Structures - Lecture 9 [Stack & Queue using Linked List]
Data Structures - Lecture 9 [Stack & Queue using Linked List]Muhammad Hammad Waseem
 
Introduction to stack
Introduction to stackIntroduction to stack
Introduction to stackvaibhav2910
 
STACKS IN DATASTRUCTURE
STACKS IN DATASTRUCTURESTACKS IN DATASTRUCTURE
STACKS IN DATASTRUCTUREArchie Jamwal
 
Applications of stack
Applications of stackApplications of stack
Applications of stackeShikshak
 
Queue Implementation Using Array & Linked List
Queue Implementation Using Array & Linked ListQueue Implementation Using Array & Linked List
Queue Implementation Using Array & Linked ListPTCL
 
queue & its applications
queue & its applicationsqueue & its applications
queue & its applicationssomendra kumar
 
Bubble sort | Data structure |
Bubble sort | Data structure |Bubble sort | Data structure |
Bubble sort | Data structure |MdSaiful14
 
Data Structure and Algorithms Linked List
Data Structure and Algorithms Linked ListData Structure and Algorithms Linked List
Data Structure and Algorithms Linked ListManishPrajapati78
 
Conversion of Infix to Prefix and Postfix with Stack
Conversion of Infix to Prefix and Postfix with StackConversion of Infix to Prefix and Postfix with Stack
Conversion of Infix to Prefix and Postfix with Stacksahil kumar
 
Ppt on Linked list,stack,queue
Ppt on Linked list,stack,queuePpt on Linked list,stack,queue
Ppt on Linked list,stack,queueSrajan Shukla
 

What's hot (20)

Binary Search Tree in Data Structure
Binary Search Tree in Data StructureBinary Search Tree in Data Structure
Binary Search Tree in Data Structure
 
Queue data structure
Queue data structureQueue data structure
Queue data structure
 
Infix to postfix conversion
Infix to postfix conversionInfix to postfix conversion
Infix to postfix conversion
 
Array data structure
Array data structureArray data structure
Array data structure
 
Data Structures - Lecture 9 [Stack & Queue using Linked List]
 Data Structures - Lecture 9 [Stack & Queue using Linked List] Data Structures - Lecture 9 [Stack & Queue using Linked List]
Data Structures - Lecture 9 [Stack & Queue using Linked List]
 
Introduction to stack
Introduction to stackIntroduction to stack
Introduction to stack
 
Tree in data structure
Tree in data structureTree in data structure
Tree in data structure
 
STACKS IN DATASTRUCTURE
STACKS IN DATASTRUCTURESTACKS IN DATASTRUCTURE
STACKS IN DATASTRUCTURE
 
Stack
StackStack
Stack
 
Applications of stack
Applications of stackApplications of stack
Applications of stack
 
Queue Implementation Using Array & Linked List
Queue Implementation Using Array & Linked ListQueue Implementation Using Array & Linked List
Queue Implementation Using Array & Linked List
 
queue & its applications
queue & its applicationsqueue & its applications
queue & its applications
 
Bubble sort | Data structure |
Bubble sort | Data structure |Bubble sort | Data structure |
Bubble sort | Data structure |
 
Data Structure and Algorithms Linked List
Data Structure and Algorithms Linked ListData Structure and Algorithms Linked List
Data Structure and Algorithms Linked List
 
sorting and its types
sorting and its typessorting and its types
sorting and its types
 
Stack a Data Structure
Stack a Data StructureStack a Data Structure
Stack a Data Structure
 
Graph traversals in Data Structures
Graph traversals in Data StructuresGraph traversals in Data Structures
Graph traversals in Data Structures
 
Conversion of Infix to Prefix and Postfix with Stack
Conversion of Infix to Prefix and Postfix with StackConversion of Infix to Prefix and Postfix with Stack
Conversion of Infix to Prefix and Postfix with Stack
 
Ppt on Linked list,stack,queue
Ppt on Linked list,stack,queuePpt on Linked list,stack,queue
Ppt on Linked list,stack,queue
 
Linked List
Linked ListLinked List
Linked List
 

Similar to Priority queues

Ist year Msc,2nd sem module1
Ist year Msc,2nd sem module1Ist year Msc,2nd sem module1
Ist year Msc,2nd sem module1blessyboban92
 
Algorithms - "Chapter 2 getting started"
Algorithms - "Chapter 2 getting started"Algorithms - "Chapter 2 getting started"
Algorithms - "Chapter 2 getting started"Ra'Fat Al-Msie'deen
 
Ch03_stacks_and_queues.ppt
Ch03_stacks_and_queues.pptCh03_stacks_and_queues.ppt
Ch03_stacks_and_queues.pptOliverKane3
 
Analysis and design of algorithms part2
Analysis and design of algorithms part2Analysis and design of algorithms part2
Analysis and design of algorithms part2Deepak John
 
Data Structures by Maneesh Boddu
Data Structures by Maneesh BodduData Structures by Maneesh Boddu
Data Structures by Maneesh Boddumaneesh boddu
 
Stack and queue
Stack and queueStack and queue
Stack and queueLavanyaJ28
 
Quick sort Algorithm Discussion And Analysis
Quick sort Algorithm Discussion And AnalysisQuick sort Algorithm Discussion And Analysis
Quick sort Algorithm Discussion And AnalysisSNJ Chaudhary
 
Week2-stacks-queues.pptx
Week2-stacks-queues.pptxWeek2-stacks-queues.pptx
Week2-stacks-queues.pptxVandanaBharti21
 
Lect11 Sorting
Lect11 SortingLect11 Sorting
Lect11 Sortingryokollll
 
standard template library(STL) in C++
standard template library(STL) in C++standard template library(STL) in C++
standard template library(STL) in C++•sreejith •sree
 
Queue data structure
Queue data structureQueue data structure
Queue data structureMekk Mhmd
 

Similar to Priority queues (20)

Ist year Msc,2nd sem module1
Ist year Msc,2nd sem module1Ist year Msc,2nd sem module1
Ist year Msc,2nd sem module1
 
Algorithms - "Chapter 2 getting started"
Algorithms - "Chapter 2 getting started"Algorithms - "Chapter 2 getting started"
Algorithms - "Chapter 2 getting started"
 
Ch03_stacks_and_queues.ppt
Ch03_stacks_and_queues.pptCh03_stacks_and_queues.ppt
Ch03_stacks_and_queues.ppt
 
Queue
QueueQueue
Queue
 
Sorting algorithms
Sorting algorithmsSorting algorithms
Sorting algorithms
 
Analysis and design of algorithms part2
Analysis and design of algorithms part2Analysis and design of algorithms part2
Analysis and design of algorithms part2
 
Data Structures by Maneesh Boddu
Data Structures by Maneesh BodduData Structures by Maneesh Boddu
Data Structures by Maneesh Boddu
 
Sorting
SortingSorting
Sorting
 
CSPC/ PPS Sorting methods
CSPC/ PPS Sorting methodsCSPC/ PPS Sorting methods
CSPC/ PPS Sorting methods
 
Basic data-structures-v.1.1
Basic data-structures-v.1.1Basic data-structures-v.1.1
Basic data-structures-v.1.1
 
Stack and queue
Stack and queueStack and queue
Stack and queue
 
Data Structures 2
Data Structures 2Data Structures 2
Data Structures 2
 
Quick sort Algorithm Discussion And Analysis
Quick sort Algorithm Discussion And AnalysisQuick sort Algorithm Discussion And Analysis
Quick sort Algorithm Discussion And Analysis
 
Week2-stacks-queues.pptx
Week2-stacks-queues.pptxWeek2-stacks-queues.pptx
Week2-stacks-queues.pptx
 
LEC4-DS ALGO.pdf
LEC4-DS  ALGO.pdfLEC4-DS  ALGO.pdf
LEC4-DS ALGO.pdf
 
Lect11 Sorting
Lect11 SortingLect11 Sorting
Lect11 Sorting
 
standard template library(STL) in C++
standard template library(STL) in C++standard template library(STL) in C++
standard template library(STL) in C++
 
16-sorting.ppt
16-sorting.ppt16-sorting.ppt
16-sorting.ppt
 
Queue
QueueQueue
Queue
 
Queue data structure
Queue data structureQueue data structure
Queue data structure
 

More from 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 & wireframeprototyping
Convergent divergent thinking & wireframeprototypingConvergent divergent thinking & wireframeprototyping
Convergent divergent thinking & 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
 
Heaps & Adaptable priority Queues
Heaps & Adaptable priority QueuesHeaps & Adaptable priority Queues
Heaps & Adaptable priority QueuesPriyanka 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
 

More from 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 & wireframeprototyping
Convergent divergent thinking & wireframeprototypingConvergent divergent thinking & wireframeprototyping
Convergent divergent thinking & 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
 
Heaps & Adaptable priority Queues
Heaps & Adaptable priority QueuesHeaps & Adaptable priority Queues
Heaps & Adaptable 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
 

Recently uploaded

"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii SoldatenkoFwdays
 
Connect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationConnect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationSlibray Presentation
 
Dev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebDev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebUiPathCommunity
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationRidwan Fadjar
 
Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Scott Keck-Warren
 
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...shyamraj55
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsMark Billinghurst
 
SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024Scott Keck-Warren
 
Gen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfGen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfAddepto
 
Pigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping ElbowsPigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping ElbowsPigging Solutions
 
Unleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubUnleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubKalema Edgar
 
Key Features Of Token Development (1).pptx
Key  Features Of Token  Development (1).pptxKey  Features Of Token  Development (1).pptx
Key Features Of Token Development (1).pptxLBM Solutions
 
Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024BookNet Canada
 
SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024Lorenzo Miniero
 
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticscarlostorres15106
 
Benefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other FrameworksBenefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other FrameworksSoftradix Technologies
 
Artificial intelligence in the post-deep learning era
Artificial intelligence in the post-deep learning eraArtificial intelligence in the post-deep learning era
Artificial intelligence in the post-deep learning eraDeakin University
 
New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024BookNet Canada
 
APIForce Zurich 5 April Automation LPDG
APIForce Zurich 5 April  Automation LPDGAPIForce Zurich 5 April  Automation LPDG
APIForce Zurich 5 April Automation LPDGMarianaLemus7
 

Recently uploaded (20)

"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko
 
Connect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationConnect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck Presentation
 
Dev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebDev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio Web
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 Presentation
 
Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024
 
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR Systems
 
SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024
 
Gen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfGen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdf
 
Vulnerability_Management_GRC_by Sohang Sengupta.pptx
Vulnerability_Management_GRC_by Sohang Sengupta.pptxVulnerability_Management_GRC_by Sohang Sengupta.pptx
Vulnerability_Management_GRC_by Sohang Sengupta.pptx
 
Pigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping ElbowsPigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping Elbows
 
Unleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubUnleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding Club
 
Key Features Of Token Development (1).pptx
Key  Features Of Token  Development (1).pptxKey  Features Of Token  Development (1).pptx
Key Features Of Token Development (1).pptx
 
Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
 
SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024
 
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
 
Benefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other FrameworksBenefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other Frameworks
 
Artificial intelligence in the post-deep learning era
Artificial intelligence in the post-deep learning eraArtificial intelligence in the post-deep learning era
Artificial intelligence in the post-deep learning era
 
New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
 
APIForce Zurich 5 April Automation LPDG
APIForce Zurich 5 April  Automation LPDGAPIForce Zurich 5 April  Automation LPDG
APIForce Zurich 5 April Automation LPDG
 

Priority queues

  • 1. Priority Queues (Algorithms and Data Structures) By Priyanka Rana By – Priyanka Rana
  • 2. Priority Queues Which assets (stock, company, real-estate) the company should purchase for investment.
  • 3. Priority Queues Many factors determine the future value of an asset. Let’s call this “Utility”. Utility(asset) = priority or importance that Utility() return the minimum value. Use Utility() as key or order of assets in the list.
  • 4. Definition Priority Queue Is an Abstract Data Type. Storing collection of prioritized elements. Supports arbitrary element insertion. removal of elements in order of priority.
  • 5. Priority Queue ADT Each entry is a pair (key, value). Key: used to identify or weigh that element. Two distinct entries in a priority queue can have the same key. Mathematical concept of total order relation, must satisfy: Reflexive property: k ≤ k. Antisymmetric property: if k1 ≤ k2 and k2 ≤ k1 , then k1 = k2. Transitive property: if k1 ≤ k2 and k2 ≤ k3, then k1 ≤ k3.
  • 7. Main methods of Priority Queue ADT size(): Return the number of entries in the queue. isEmpty(): Test whether the queue is empty. min(): Return (do not remove) an entry with smallest key. insert(k, x): Insert an entry with key k and value x. removeMin(): Remove and return the entry with smallest key.
  • 8. Sorting with a Priority Queue Sorting a collection S of n comparable elements with a priority queue Q, called PriorityQueueSort. Hint: Using insert() and removeMin()
  • 9. Exercise Operation Output Insert(5,A) nil Insert(4,B) nil Insert(7,I) nil removeMin() (4,B) insert(3,J) Nil removeMin() (3,J) insert(6,L) nil removeMin() (5,A)
  • 10. Implementing a Priority Queue • With an unsorted list : • insert() – adds entry at last -> takes O(1) • removeMin() – requires complete scan -> takes O(n) • “fast insertion and slow removal” 4 5 2 3 1
  • 11.  With a Sorted List:  Insert() – requires complete scan -> takes O(n)  removeMin() – always access the first element -> takes O(1)  fast removal and slow insertions. 1 2 3 4 5
  • 12. Method Unsorted List Sorted List size, isEmpty O(1) O(1) insert O(1) O(n) Min, removeMin O(n) O(1)
  • 13. Sorting using priority Queues • Selection – Sort • Insertion - Sort
  • 14. Selection Sort – 7 4 8 2 5 3 9 7 4 8 2 5 3 9 2 4 8 7 5 3 9 2 3 8 7 5 4 9 2 3 4 7 5 8 9 2 3 4 5 7 8 9 2 3 4 5 7 8 9 2 3 4 5 7 8 9
  • 15. Selection- Sorting using priority Queues Sequence S Priority Queue P Input (7,4,8,2,5,3,9) () Phase 1 (a) (b) . . . (g) (4,8,2,5,3,9) (8,2,5,3,9) . . . () (7) (7,4) . . . (7,4,8,2,5,3,9) Phase 2 (a) (b) (c) (d) (e) (f) (g) (2) (2,3) (2,3,4) (2,3,4,5) (2,3,4,5,7) (2,3,4,5,7,8) (2,3,4,5,7,8,9) (7,4,8,2,5,3,9) (7,4,8,5,9) (7,8,5,9) (7,8,9) (8,9) (9) ()
  • 16. Running Time • First removeMin takes O(n) time • Second removeMin takes O(n-1) time) and so on • Till last n operation takes O(1) time = O(n2)O(n
  • 17. Insertion Sort – 7 4 8 2 5 3 9 7 4 7 4 7 8 2 4 7 8 2 3 4 5 7 8 9 2 3 4 5 7 8 2 4 5 7 8
  • 18. Insertion -Sorting using priority Queues Sequence S Priority Queue P Input (7,4,8,2,5,3,9) () Phase 1 (a) (b) (c) (d) (e) (f) (g) (4,8,2,5,3,9) (8,2,5,3,9) (2,5,3,9) (5,3,9) (3,9) (9) () (7) (4,7) (4,7,8) (2,4,7,8) (2,4,5,7,8) (2,3,4,5,7,8) (2,3,4,5,7,8,9) Phase 2 (a) (b) ● . (2) (2,3) ● . ● . ● . (2,3,4,5,7,8,9) (3,4,5,7,8,9) (4,5,7,8,9) . . . ()
  • 19. Running Time • First element -no comparison • Second element – 1 comparison • Third element – 2 comparisons and so on. = O(n2)O(n

Editor's Notes

  1. <number>
  2. If you have been asked which assets the company should purchase for investment. There are many assets like stock, , real-estate which you can choose from a big list.// What would be your criteria for choosing any asset that you would like to buy? <number>
  3. <number>
  4. <number>
  5. <number>
  6. <number>
  7. <number>
  8. An important application of a priority queue is sorting. The algorithm for sorting a collection S of n comparable elements with a priority queue Q, called PriorityQueueSort, consists of two phases: <number>
  9. Let it be an exercise for you, if you have understood the priority queue methods in previous slide this exercise wont be very challenging,// here hint has also been given Note that the running time of this sorting method depends on the priority queue implementation. <number>
  10. In this session we'll learn how to implement a priority queue by storing its entries in a list S , where the list can be sorted or unsorted. for unsorted list, //simple way of performing insert operation is to add the element at the end of list , by executing method addLast()./ / This implementation of method insert takes O(1) time as inserting entries at the end of list does not take into account the ordering of the keys. To perform removeMin method , we have to inspect all the elements of list to find an entry with minimum k. So it taken O(n) time, where n is the number of entries, or data size.// thats make implementation with an unsorted list is also called as "fast insertion and slow removal". <number>
  11. An alternative implementation of a priority queue is in a sorted list. ,// here insertion requires a complete scan of the list to find the appropriate position to insert the new entry so it takes O(n) time, while// removeMin method just needs to access the first element of the list as that will be the one which is least in the sorted list.and so it takes O(1) times.// it is also called as fast removal and slow insertions, <number>
  12. <number>
  13. Ttwo most popular techniques for sorting, selection-sort and insertion-sort. <number>
  14. In selection -sort Consider we have to sort n numbers stored in array A by first finding the smallest element of A and exchanging it with the element in A[0], the first element. Then finding the second smallest element of A, and exchanging it with A[1], the second element. Continue in this manner for the first n−1 elements of A. This method of sorting is known as selection sort. <number>
  15. // Selection sort occurs in two phases, in first phase we insert all the elements in priority queue P using simple insert() method,// we ll pick first item// from the sequence insert into P,then second element// , third and so on, this way we have complete data in prority queue now, now in Phase 2 //we repeatedly remove the elements from P using the removeMin() method. we ll find the least element in the qyueue, and put it back in sequence S, // //, we ll do this till the last element in priority queue , when all the elements of queue are moved to sequence S in order. In this whole process bottleneck is in phase 2, where we repeatedly remove an entry with smallest key from Queue P. We start with priority queue size equals n and incrementally decreases with each removeMin until it becomes 0. <number>
  16. //Thus, the first removeMin operation takes time O(n) as there are n entries, then the second one// takes time O(n − 1) as one entry has already been moved to sequence S and so number of elements are (n-1) now and so on, //until the last (nth) operation takes time O(1). Therefore, //the total time needed for the second phase is // <number>
  17. // Insertion of the second entry requires one comparison with the first entry, in order to find its proper position. //Insertion of the third entry requires two comparisons, and // fourth entry requires three comparisons and so on. This method is known as insertion sort, where in each iteration, one entry is added to the sorted list. Inserting after comparison <number>
  18. In Phase 1, we remove the// first element of list and insert it into Priority queue P,, then second element// is removed from the sequence and we scan the data in priority queue , until we find the correct place for this element. , same step is repeated for //third element, it is compared with existing two elements in priority queue and placed at right position, and this is repeated for the whole data set.// // // In Phase 2, we call removeMin method on Priority queue repeatedly,// here because of phase 1 every time it returns the first element of the list and// // we add that element at the end of Sequence S, and this way we get sorted sequence S. <number>
  19. Analyzing the running time of this whole process, we see that phase 1 is the bottleneck of this sorting ,//as for placing first element , there is no comparison , //while for second element , there is 1 comparison and so on. and like selection sort, //insertion sort also runs in O(n2) time <number>