SlideShare a Scribd company logo
1 of 6
Download to read offline
CSE332: Data Abstractions
Lecture 4: Priority Queues
Dan Grossman
Spring 2010
A new ADT: Priority Queue
• Textbook Chapter 6
– Will go back to binary search trees
– Nice to see a new and surprising data structure first
• A priority queue holds compare-able data
– Unlike stacks and queues need to compare items
• Given x and y, is x less than, equal to, or greater than y
• What this means can depend on your data
– Much of course will require this: dictionaries, sorting
– Integers are comparable, so will use them in examples
• But the priority queue ADT is much more general
Spring 2010 2
CSE332: Data Abstractions
Priorities
• Assume each item has a “priority”
– The lesser item is the one with the greater priority
– So “priority 1” is more important than “priority 4”
– (Just a convention)
• Operations:
– insert
– deleteMin
– create, is_empty, destroy
• Key property: deleteMin returns and deletes from the queue
the item with greatest priority (lowest priority value)
– Can resolve ties arbitrarily
Spring 2010 3
CSE332: Data Abstractions
insert deleteMin
6 2
15 23
12 18
45 3 7
Focusing on the numbers
• For simplicity in lecture, we’ll often suppose items are just ints
and the int is the priority
– The same concepts without generic usefulness
– So an operation sequence could be
insert 6
insert 5
x = deleteMin
– int priorities are common, but really just need comparable
– Not having “other data” is very rare
• Example: print job is a priority and the file
Spring 2010 4
CSE332: Data Abstractions
Example
insert x1 with priority 5
insert x2 with priority 3
insert x3 with priority 4
a = deleteMin
b = deleteMin
insert x4 with priority 2
insert x5 with priority 6
c = deleteMin
d = deleteMin
• Analogy: insert is like enqueue, deleteMin is like dequeue
– But the whole point is to use priorities instead of FIFO
Spring 2010 5
CSE332: Data Abstractions
Applications
Like all good ADTs, the priority queue arises often
– Sometimes “directly”, sometimes less obvious
• Run multiple programs in the operating system
– “critical” before “interactive” before “compute-intensive”
– Maybe let users set priority level
• Treat hospital patients in order of severity (or triage)
• Select print jobs in order of decreasing length?
• Forward network packets in order of urgency
• Select most frequent symbols for data compression (cf. CSE143)
• Sort: insert all, then repeatedly deleteMin
– Much like Project 1 uses a stack to implement reverse
Spring 2010 6
CSE332: Data Abstractions
More applications
• “Greedy” algorithms
– Will see an example when we study graphs in a few weeks
• Discrete event simulation (system modeling, virtual worlds, …)
– Simulate how state changes when events fire
– Each event e happens at some time t and generates new
events e1, …, en at times t+t1, …, t+tn
– Naïve approach: advance “clock” by 1 unit at a time and
process any events that happen then
– Better:
• Pending events in a priority queue (priority = time happens)
• Repeatedly: deleteMin and then insert new events
• Effectively, “set clock ahead to next event”
Spring 2010 7
CSE332: Data Abstractions
Need a good data structure!
• Will show an efficient, non-obvious data structure for this ADT
– But first let’s analyze some “obvious” ideas
– All times worst-case; assume arrays “have room”
data insert algorithm / time deleteMin algorithm / time
unsorted array
unsorted linked list
sorted circular array
sorted linked list
binary search tree
Spring 2010 8
CSE332: Data Abstractions
Need a good data structure!
• Will show an efficient, non-obvious data structure for this ADT
– But first let’s analyze some “obvious” ideas for n data items
– All times worst-case; assume arrays “have room”
data insert algorithm / time deleteMin algorithm / time
unsorted array add at end O(1) search O(n)
unsorted linked list add at front O(1) search O(n)
sorted circular array search / shift O(n) move front O(1)
sorted linked list remove at front O(1) put in right place O(n)
binary search tree put in right place O(n) leftmost O(n)
Spring 2010 9
CSE332: Data Abstractions
More on possibilities
• If priorities are random, binary search tree will likely do better
– O(log n) insert and O(log n) deleteMin on average
• But we are about to see a data structure called a “binary heap”
– O(log n) insert and O(log n) deleteMin worst-case
– Very good constant factors
– If items arrive in random order, then insert is O(1) on
average
• One more idea: if priorities are 0, 1, …, k can use array of lists
– insert: add to front of list at arr[priority], O(1)
– deleteMin: remove from lowest non-empty list O(k)
Spring 2010 10
CSE332: Data Abstractions
Tree terms (review?)
The binary heap data structure
implementing the priority queue ADT will
be a tree, so worth establishing some
terminology
Spring 2010 11
CSE332: Data Abstractions
A
E
B
D F
C
G
I
H
L
J M
K N
Tree T
root(tree)
children(node)
parent(node)
leaves(tree)
siblings(node)
ancestors(node)
descendents(node)
subtree(node)
depth(node)
height(tree)
degree(node)
branching factor(tree)
Kinds of trees
Certain terms define trees with specific structure
• Binary tree: Each node has at most 2 children
• n-ary tree: Each node as at most n children
• Complete tree: Each row is completely full except maybe the
bottom row, which is filled from left to right
Spring 2010 12
CSE332: Data Abstractions
Teaser: Later we’ll learn a tree is a kind of directed graph with
specific structure
Our data structure
Finally, then, a binary min-heap (or just binary heap or just heap) is:
• A complete tree – the “structure property”
• For every (non-root) node the parent node’s value is less than
the node’s value – the “heap property” (not a binary search tree)
Spring 2010 13
CSE332: Data Abstractions
15
30
80
20
10
99
60
40
80
20
10
50 700
85
not a heap a heap
So:
• Where is the highest-priority item?
• What is the height of a heap with n items?
Operations: basic idea
• findMin: return root.data
• deleteMin:
1. answer = root.data
2. Move right-most node in last
row to root to restore
structure property
3. “Percolate down” to restore
heap property
• insert:
1. Put new node in next position
on bottom row to restore
structure property
2. “Percolate up” to restore
heap property
Spring 2010 14
CSE332: Data Abstractions
99
60
40
80
20
10
50 700
85
15
DeleteMin
3
4
9
8
5
7
10
6
9
11
1. Delete (and return) value at root node
Spring 2010 CSE332: Data Abstractions 16
2. Restore the Structure Property
• We now have a “hole” at the root
– Need to fill the hole with another
value
• When we are done, the tree will have
one less node and must still be complete
3
4
9
8
5
7
10
6
9
11
3
4
9
8
5
7
10
6
9
11
Spring 2010 CSE332: Data Abstractions
17
3. Restore the Heap Property
Percolate down:
• Keep comparing with both children
• Move smaller child up and go down one level
• Done if both children are ≥ item or reached a leaf node
• What is the run time?
3
4
9
8
5
7
10
6
9
11
4
9
8
5
7
10
6
9
11
3
8
4
9
10
5
7
6
9
11
3
?
?
Spring 2010 CSE332: Data Abstractions 18
DeleteMin: Run Time Analysis
• Run time is O(height of heap)
• A heap is a complete binary tree
• Height of a complete binary tree of n nodes?
– height = ⎣ log2(n) ⎦
• Run time of deleteMin is O(log n)
Spring 2010 CSE332: Data Abstractions
19
Insert
• Add a value to the tree
• Structure and heap order properties
must still be correct afterwards
8
4
9
10
5
7
6
9
11
1
2
Spring 2010
CSE332: Data Abstractions
20
Insert: Maintain the Structure Property
• There is only one valid tree shape after
we add one more node
• So put our new data there and then
focus on restoring the heap property
8
4
9
10
5
7
6
9
11
1
2
Spring 2010 CSE332: Data Abstractions
21
Maintain the heap property
2
8
4
9
10
5
7
6
9
11
1
Percolate up:
• Put new data in new location
• If parent larger, swap with parent, and continue
• Done if parent ≤ item or reached root
• Run time?
?
2
5
8
4
9
10
7
6
9
11
1
?
2
5
8
9
10
4
7
6
9
11
1
?
Spring 2010 CSE332: Data Abstractions 22
Insert: Run Time Analysis
• Like deleteMin, worst-case time proportional to tree height
– O(log n)
• But… deleteMin needs the “last used” complete-tree position
and insert needs the “next to use” complete-tree position
– If “keep a reference to there” then insert and deleteMin
have to adjust that reference: O(log n) in worst case
– Could calculate how to find it in O(log n) from the root given
the size of the heap
• But it’s not easy
• And then insert is always O(log n), promised O(1) on
average (assuming random arrival of items)
• There’s a “trick”: don’t represent complete trees with explicit edges!
Spring 2010 CSE332: Data Abstractions

More Related Content

Similar to lecture4.pdf

Ist year Msc,2nd sem module1
Ist year Msc,2nd sem module1Ist year Msc,2nd sem module1
Ist year Msc,2nd sem module1blessyboban92
 
Data structure unitfirst part1
Data structure unitfirst part1Data structure unitfirst part1
Data structure unitfirst part1Amar Rawat
 
Data structure unitfirst part1
Data structure unitfirst part1Data structure unitfirst part1
Data structure unitfirst part1Amar Rawat
 
CS3114_09212011.ppt
CS3114_09212011.pptCS3114_09212011.ppt
CS3114_09212011.pptArumugam90
 
Mathemetics module
Mathemetics moduleMathemetics module
Mathemetics modulemanikanta361
 
Advanced s and s algorithm.ppt
Advanced s and s algorithm.pptAdvanced s and s algorithm.ppt
Advanced s and s algorithm.pptLegesseSamuel
 
10 chapter6 heaps_priority_queues
10 chapter6 heaps_priority_queues10 chapter6 heaps_priority_queues
10 chapter6 heaps_priority_queuesSSE_AndyLi
 
Chapter 8 advanced sorting and hashing for print
Chapter 8 advanced sorting and hashing for printChapter 8 advanced sorting and hashing for print
Chapter 8 advanced sorting and hashing for printAbdii Rashid
 
Introduction to Datamining Concept and Techniques
Introduction to Datamining Concept and TechniquesIntroduction to Datamining Concept and Techniques
Introduction to Datamining Concept and TechniquesSơn Còm Nhom
 
ADS UNIT II -Priority queue.pptx
ADS UNIT II -Priority queue.pptxADS UNIT II -Priority queue.pptx
ADS UNIT II -Priority queue.pptxlaxman1216
 
Unit 1_SLL and DLL.pdf
Unit 1_SLL and DLL.pdfUnit 1_SLL and DLL.pdf
Unit 1_SLL and DLL.pdfKanchanPatil34
 

Similar to lecture4.pdf (20)

Ist year Msc,2nd sem module1
Ist year Msc,2nd sem module1Ist year Msc,2nd sem module1
Ist year Msc,2nd sem module1
 
Data Structures 7
Data Structures 7Data Structures 7
Data Structures 7
 
Data structure unitfirst part1
Data structure unitfirst part1Data structure unitfirst part1
Data structure unitfirst part1
 
Data structure unitfirst part1
Data structure unitfirst part1Data structure unitfirst part1
Data structure unitfirst part1
 
Intro_2.ppt
Intro_2.pptIntro_2.ppt
Intro_2.ppt
 
Intro.ppt
Intro.pptIntro.ppt
Intro.ppt
 
Intro.ppt
Intro.pptIntro.ppt
Intro.ppt
 
CS3114_09212011.ppt
CS3114_09212011.pptCS3114_09212011.ppt
CS3114_09212011.ppt
 
linkedlist.pptx
linkedlist.pptxlinkedlist.pptx
linkedlist.pptx
 
Mathemetics module
Mathemetics moduleMathemetics module
Mathemetics module
 
Advanced s and s algorithm.ppt
Advanced s and s algorithm.pptAdvanced s and s algorithm.ppt
Advanced s and s algorithm.ppt
 
10 chapter6 heaps_priority_queues
10 chapter6 heaps_priority_queues10 chapter6 heaps_priority_queues
10 chapter6 heaps_priority_queues
 
Chapter 15
Chapter 15Chapter 15
Chapter 15
 
Chapter 8 advanced sorting and hashing for print
Chapter 8 advanced sorting and hashing for printChapter 8 advanced sorting and hashing for print
Chapter 8 advanced sorting and hashing for print
 
Introduction to Datamining Concept and Techniques
Introduction to Datamining Concept and TechniquesIntroduction to Datamining Concept and Techniques
Introduction to Datamining Concept and Techniques
 
DA_02_algorithms.pptx
DA_02_algorithms.pptxDA_02_algorithms.pptx
DA_02_algorithms.pptx
 
ADS UNIT II -Priority queue.pptx
ADS UNIT II -Priority queue.pptxADS UNIT II -Priority queue.pptx
ADS UNIT II -Priority queue.pptx
 
Unit 1_SLL and DLL.pdf
Unit 1_SLL and DLL.pdfUnit 1_SLL and DLL.pdf
Unit 1_SLL and DLL.pdf
 
Data Structures 6
Data Structures 6Data Structures 6
Data Structures 6
 
Unit i(dsc++)
Unit i(dsc++)Unit i(dsc++)
Unit i(dsc++)
 

Recently uploaded

Effects of Smartphone Addiction on the Academic Performances of Grades 9 to 1...
Effects of Smartphone Addiction on the Academic Performances of Grades 9 to 1...Effects of Smartphone Addiction on the Academic Performances of Grades 9 to 1...
Effects of Smartphone Addiction on the Academic Performances of Grades 9 to 1...limedy534
 
High Class Call Girls Noida Sector 39 Aarushi 🔝8264348440🔝 Independent Escort...
High Class Call Girls Noida Sector 39 Aarushi 🔝8264348440🔝 Independent Escort...High Class Call Girls Noida Sector 39 Aarushi 🔝8264348440🔝 Independent Escort...
High Class Call Girls Noida Sector 39 Aarushi 🔝8264348440🔝 Independent Escort...soniya singh
 
From idea to production in a day – Leveraging Azure ML and Streamlit to build...
From idea to production in a day – Leveraging Azure ML and Streamlit to build...From idea to production in a day – Leveraging Azure ML and Streamlit to build...
From idea to production in a day – Leveraging Azure ML and Streamlit to build...Florian Roscheck
 
毕业文凭制作#回国入职#diploma#degree澳洲中央昆士兰大学毕业证成绩单pdf电子版制作修改#毕业文凭制作#回国入职#diploma#degree
毕业文凭制作#回国入职#diploma#degree澳洲中央昆士兰大学毕业证成绩单pdf电子版制作修改#毕业文凭制作#回国入职#diploma#degree毕业文凭制作#回国入职#diploma#degree澳洲中央昆士兰大学毕业证成绩单pdf电子版制作修改#毕业文凭制作#回国入职#diploma#degree
毕业文凭制作#回国入职#diploma#degree澳洲中央昆士兰大学毕业证成绩单pdf电子版制作修改#毕业文凭制作#回国入职#diploma#degreeyuu sss
 
B2 Creative Industry Response Evaluation.docx
B2 Creative Industry Response Evaluation.docxB2 Creative Industry Response Evaluation.docx
B2 Creative Industry Response Evaluation.docxStephen266013
 
How we prevented account sharing with MFA
How we prevented account sharing with MFAHow we prevented account sharing with MFA
How we prevented account sharing with MFAAndrei Kaleshka
 
PKS-TGC-1084-630 - Stage 1 Proposal.pptx
PKS-TGC-1084-630 - Stage 1 Proposal.pptxPKS-TGC-1084-630 - Stage 1 Proposal.pptx
PKS-TGC-1084-630 - Stage 1 Proposal.pptxPramod Kumar Srivastava
 
VIP High Class Call Girls Jamshedpur Anushka 8250192130 Independent Escort Se...
VIP High Class Call Girls Jamshedpur Anushka 8250192130 Independent Escort Se...VIP High Class Call Girls Jamshedpur Anushka 8250192130 Independent Escort Se...
VIP High Class Call Girls Jamshedpur Anushka 8250192130 Independent Escort Se...Suhani Kapoor
 
20240419 - Measurecamp Amsterdam - SAM.pdf
20240419 - Measurecamp Amsterdam - SAM.pdf20240419 - Measurecamp Amsterdam - SAM.pdf
20240419 - Measurecamp Amsterdam - SAM.pdfHuman37
 
Call Girls In Mahipalpur O9654467111 Escorts Service
Call Girls In Mahipalpur O9654467111  Escorts ServiceCall Girls In Mahipalpur O9654467111  Escorts Service
Call Girls In Mahipalpur O9654467111 Escorts ServiceSapana Sha
 
EMERCE - 2024 - AMSTERDAM - CROSS-PLATFORM TRACKING WITH GOOGLE ANALYTICS.pptx
EMERCE - 2024 - AMSTERDAM - CROSS-PLATFORM  TRACKING WITH GOOGLE ANALYTICS.pptxEMERCE - 2024 - AMSTERDAM - CROSS-PLATFORM  TRACKING WITH GOOGLE ANALYTICS.pptx
EMERCE - 2024 - AMSTERDAM - CROSS-PLATFORM TRACKING WITH GOOGLE ANALYTICS.pptxthyngster
 
9711147426✨Call In girls Gurgaon Sector 31. SCO 25 escort service
9711147426✨Call In girls Gurgaon Sector 31. SCO 25 escort service9711147426✨Call In girls Gurgaon Sector 31. SCO 25 escort service
9711147426✨Call In girls Gurgaon Sector 31. SCO 25 escort servicejennyeacort
 
办理学位证纽约大学毕业证(NYU毕业证书)原版一比一
办理学位证纽约大学毕业证(NYU毕业证书)原版一比一办理学位证纽约大学毕业证(NYU毕业证书)原版一比一
办理学位证纽约大学毕业证(NYU毕业证书)原版一比一fhwihughh
 
GA4 Without Cookies [Measure Camp AMS]
GA4 Without Cookies [Measure Camp AMS]GA4 Without Cookies [Measure Camp AMS]
GA4 Without Cookies [Measure Camp AMS]📊 Markus Baersch
 
专业一比一美国俄亥俄大学毕业证成绩单pdf电子版制作修改
专业一比一美国俄亥俄大学毕业证成绩单pdf电子版制作修改专业一比一美国俄亥俄大学毕业证成绩单pdf电子版制作修改
专业一比一美国俄亥俄大学毕业证成绩单pdf电子版制作修改yuu sss
 
INTERNSHIP ON PURBASHA COMPOSITE TEX LTD
INTERNSHIP ON PURBASHA COMPOSITE TEX LTDINTERNSHIP ON PURBASHA COMPOSITE TEX LTD
INTERNSHIP ON PURBASHA COMPOSITE TEX LTDRafezzaman
 
Consent & Privacy Signals on Google *Pixels* - MeasureCamp Amsterdam 2024
Consent & Privacy Signals on Google *Pixels* - MeasureCamp Amsterdam 2024Consent & Privacy Signals on Google *Pixels* - MeasureCamp Amsterdam 2024
Consent & Privacy Signals on Google *Pixels* - MeasureCamp Amsterdam 2024thyngster
 
Call Us ➥97111√47426🤳Call Girls in Aerocity (Delhi NCR)
Call Us ➥97111√47426🤳Call Girls in Aerocity (Delhi NCR)Call Us ➥97111√47426🤳Call Girls in Aerocity (Delhi NCR)
Call Us ➥97111√47426🤳Call Girls in Aerocity (Delhi NCR)jennyeacort
 

Recently uploaded (20)

Deep Generative Learning for All - The Gen AI Hype (Spring 2024)
Deep Generative Learning for All - The Gen AI Hype (Spring 2024)Deep Generative Learning for All - The Gen AI Hype (Spring 2024)
Deep Generative Learning for All - The Gen AI Hype (Spring 2024)
 
E-Commerce Order PredictionShraddha Kamble.pptx
E-Commerce Order PredictionShraddha Kamble.pptxE-Commerce Order PredictionShraddha Kamble.pptx
E-Commerce Order PredictionShraddha Kamble.pptx
 
Effects of Smartphone Addiction on the Academic Performances of Grades 9 to 1...
Effects of Smartphone Addiction on the Academic Performances of Grades 9 to 1...Effects of Smartphone Addiction on the Academic Performances of Grades 9 to 1...
Effects of Smartphone Addiction on the Academic Performances of Grades 9 to 1...
 
High Class Call Girls Noida Sector 39 Aarushi 🔝8264348440🔝 Independent Escort...
High Class Call Girls Noida Sector 39 Aarushi 🔝8264348440🔝 Independent Escort...High Class Call Girls Noida Sector 39 Aarushi 🔝8264348440🔝 Independent Escort...
High Class Call Girls Noida Sector 39 Aarushi 🔝8264348440🔝 Independent Escort...
 
From idea to production in a day – Leveraging Azure ML and Streamlit to build...
From idea to production in a day – Leveraging Azure ML and Streamlit to build...From idea to production in a day – Leveraging Azure ML and Streamlit to build...
From idea to production in a day – Leveraging Azure ML and Streamlit to build...
 
毕业文凭制作#回国入职#diploma#degree澳洲中央昆士兰大学毕业证成绩单pdf电子版制作修改#毕业文凭制作#回国入职#diploma#degree
毕业文凭制作#回国入职#diploma#degree澳洲中央昆士兰大学毕业证成绩单pdf电子版制作修改#毕业文凭制作#回国入职#diploma#degree毕业文凭制作#回国入职#diploma#degree澳洲中央昆士兰大学毕业证成绩单pdf电子版制作修改#毕业文凭制作#回国入职#diploma#degree
毕业文凭制作#回国入职#diploma#degree澳洲中央昆士兰大学毕业证成绩单pdf电子版制作修改#毕业文凭制作#回国入职#diploma#degree
 
B2 Creative Industry Response Evaluation.docx
B2 Creative Industry Response Evaluation.docxB2 Creative Industry Response Evaluation.docx
B2 Creative Industry Response Evaluation.docx
 
How we prevented account sharing with MFA
How we prevented account sharing with MFAHow we prevented account sharing with MFA
How we prevented account sharing with MFA
 
PKS-TGC-1084-630 - Stage 1 Proposal.pptx
PKS-TGC-1084-630 - Stage 1 Proposal.pptxPKS-TGC-1084-630 - Stage 1 Proposal.pptx
PKS-TGC-1084-630 - Stage 1 Proposal.pptx
 
VIP High Class Call Girls Jamshedpur Anushka 8250192130 Independent Escort Se...
VIP High Class Call Girls Jamshedpur Anushka 8250192130 Independent Escort Se...VIP High Class Call Girls Jamshedpur Anushka 8250192130 Independent Escort Se...
VIP High Class Call Girls Jamshedpur Anushka 8250192130 Independent Escort Se...
 
20240419 - Measurecamp Amsterdam - SAM.pdf
20240419 - Measurecamp Amsterdam - SAM.pdf20240419 - Measurecamp Amsterdam - SAM.pdf
20240419 - Measurecamp Amsterdam - SAM.pdf
 
Call Girls In Mahipalpur O9654467111 Escorts Service
Call Girls In Mahipalpur O9654467111  Escorts ServiceCall Girls In Mahipalpur O9654467111  Escorts Service
Call Girls In Mahipalpur O9654467111 Escorts Service
 
EMERCE - 2024 - AMSTERDAM - CROSS-PLATFORM TRACKING WITH GOOGLE ANALYTICS.pptx
EMERCE - 2024 - AMSTERDAM - CROSS-PLATFORM  TRACKING WITH GOOGLE ANALYTICS.pptxEMERCE - 2024 - AMSTERDAM - CROSS-PLATFORM  TRACKING WITH GOOGLE ANALYTICS.pptx
EMERCE - 2024 - AMSTERDAM - CROSS-PLATFORM TRACKING WITH GOOGLE ANALYTICS.pptx
 
9711147426✨Call In girls Gurgaon Sector 31. SCO 25 escort service
9711147426✨Call In girls Gurgaon Sector 31. SCO 25 escort service9711147426✨Call In girls Gurgaon Sector 31. SCO 25 escort service
9711147426✨Call In girls Gurgaon Sector 31. SCO 25 escort service
 
办理学位证纽约大学毕业证(NYU毕业证书)原版一比一
办理学位证纽约大学毕业证(NYU毕业证书)原版一比一办理学位证纽约大学毕业证(NYU毕业证书)原版一比一
办理学位证纽约大学毕业证(NYU毕业证书)原版一比一
 
GA4 Without Cookies [Measure Camp AMS]
GA4 Without Cookies [Measure Camp AMS]GA4 Without Cookies [Measure Camp AMS]
GA4 Without Cookies [Measure Camp AMS]
 
专业一比一美国俄亥俄大学毕业证成绩单pdf电子版制作修改
专业一比一美国俄亥俄大学毕业证成绩单pdf电子版制作修改专业一比一美国俄亥俄大学毕业证成绩单pdf电子版制作修改
专业一比一美国俄亥俄大学毕业证成绩单pdf电子版制作修改
 
INTERNSHIP ON PURBASHA COMPOSITE TEX LTD
INTERNSHIP ON PURBASHA COMPOSITE TEX LTDINTERNSHIP ON PURBASHA COMPOSITE TEX LTD
INTERNSHIP ON PURBASHA COMPOSITE TEX LTD
 
Consent & Privacy Signals on Google *Pixels* - MeasureCamp Amsterdam 2024
Consent & Privacy Signals on Google *Pixels* - MeasureCamp Amsterdam 2024Consent & Privacy Signals on Google *Pixels* - MeasureCamp Amsterdam 2024
Consent & Privacy Signals on Google *Pixels* - MeasureCamp Amsterdam 2024
 
Call Us ➥97111√47426🤳Call Girls in Aerocity (Delhi NCR)
Call Us ➥97111√47426🤳Call Girls in Aerocity (Delhi NCR)Call Us ➥97111√47426🤳Call Girls in Aerocity (Delhi NCR)
Call Us ➥97111√47426🤳Call Girls in Aerocity (Delhi NCR)
 

lecture4.pdf

  • 1. CSE332: Data Abstractions Lecture 4: Priority Queues Dan Grossman Spring 2010 A new ADT: Priority Queue • Textbook Chapter 6 – Will go back to binary search trees – Nice to see a new and surprising data structure first • A priority queue holds compare-able data – Unlike stacks and queues need to compare items • Given x and y, is x less than, equal to, or greater than y • What this means can depend on your data – Much of course will require this: dictionaries, sorting – Integers are comparable, so will use them in examples • But the priority queue ADT is much more general Spring 2010 2 CSE332: Data Abstractions Priorities • Assume each item has a “priority” – The lesser item is the one with the greater priority – So “priority 1” is more important than “priority 4” – (Just a convention) • Operations: – insert – deleteMin – create, is_empty, destroy • Key property: deleteMin returns and deletes from the queue the item with greatest priority (lowest priority value) – Can resolve ties arbitrarily Spring 2010 3 CSE332: Data Abstractions insert deleteMin 6 2 15 23 12 18 45 3 7 Focusing on the numbers • For simplicity in lecture, we’ll often suppose items are just ints and the int is the priority – The same concepts without generic usefulness – So an operation sequence could be insert 6 insert 5 x = deleteMin – int priorities are common, but really just need comparable – Not having “other data” is very rare • Example: print job is a priority and the file Spring 2010 4 CSE332: Data Abstractions
  • 2. Example insert x1 with priority 5 insert x2 with priority 3 insert x3 with priority 4 a = deleteMin b = deleteMin insert x4 with priority 2 insert x5 with priority 6 c = deleteMin d = deleteMin • Analogy: insert is like enqueue, deleteMin is like dequeue – But the whole point is to use priorities instead of FIFO Spring 2010 5 CSE332: Data Abstractions Applications Like all good ADTs, the priority queue arises often – Sometimes “directly”, sometimes less obvious • Run multiple programs in the operating system – “critical” before “interactive” before “compute-intensive” – Maybe let users set priority level • Treat hospital patients in order of severity (or triage) • Select print jobs in order of decreasing length? • Forward network packets in order of urgency • Select most frequent symbols for data compression (cf. CSE143) • Sort: insert all, then repeatedly deleteMin – Much like Project 1 uses a stack to implement reverse Spring 2010 6 CSE332: Data Abstractions More applications • “Greedy” algorithms – Will see an example when we study graphs in a few weeks • Discrete event simulation (system modeling, virtual worlds, …) – Simulate how state changes when events fire – Each event e happens at some time t and generates new events e1, …, en at times t+t1, …, t+tn – Naïve approach: advance “clock” by 1 unit at a time and process any events that happen then – Better: • Pending events in a priority queue (priority = time happens) • Repeatedly: deleteMin and then insert new events • Effectively, “set clock ahead to next event” Spring 2010 7 CSE332: Data Abstractions Need a good data structure! • Will show an efficient, non-obvious data structure for this ADT – But first let’s analyze some “obvious” ideas – All times worst-case; assume arrays “have room” data insert algorithm / time deleteMin algorithm / time unsorted array unsorted linked list sorted circular array sorted linked list binary search tree Spring 2010 8 CSE332: Data Abstractions
  • 3. Need a good data structure! • Will show an efficient, non-obvious data structure for this ADT – But first let’s analyze some “obvious” ideas for n data items – All times worst-case; assume arrays “have room” data insert algorithm / time deleteMin algorithm / time unsorted array add at end O(1) search O(n) unsorted linked list add at front O(1) search O(n) sorted circular array search / shift O(n) move front O(1) sorted linked list remove at front O(1) put in right place O(n) binary search tree put in right place O(n) leftmost O(n) Spring 2010 9 CSE332: Data Abstractions More on possibilities • If priorities are random, binary search tree will likely do better – O(log n) insert and O(log n) deleteMin on average • But we are about to see a data structure called a “binary heap” – O(log n) insert and O(log n) deleteMin worst-case – Very good constant factors – If items arrive in random order, then insert is O(1) on average • One more idea: if priorities are 0, 1, …, k can use array of lists – insert: add to front of list at arr[priority], O(1) – deleteMin: remove from lowest non-empty list O(k) Spring 2010 10 CSE332: Data Abstractions Tree terms (review?) The binary heap data structure implementing the priority queue ADT will be a tree, so worth establishing some terminology Spring 2010 11 CSE332: Data Abstractions A E B D F C G I H L J M K N Tree T root(tree) children(node) parent(node) leaves(tree) siblings(node) ancestors(node) descendents(node) subtree(node) depth(node) height(tree) degree(node) branching factor(tree) Kinds of trees Certain terms define trees with specific structure • Binary tree: Each node has at most 2 children • n-ary tree: Each node as at most n children • Complete tree: Each row is completely full except maybe the bottom row, which is filled from left to right Spring 2010 12 CSE332: Data Abstractions Teaser: Later we’ll learn a tree is a kind of directed graph with specific structure
  • 4. Our data structure Finally, then, a binary min-heap (or just binary heap or just heap) is: • A complete tree – the “structure property” • For every (non-root) node the parent node’s value is less than the node’s value – the “heap property” (not a binary search tree) Spring 2010 13 CSE332: Data Abstractions 15 30 80 20 10 99 60 40 80 20 10 50 700 85 not a heap a heap So: • Where is the highest-priority item? • What is the height of a heap with n items? Operations: basic idea • findMin: return root.data • deleteMin: 1. answer = root.data 2. Move right-most node in last row to root to restore structure property 3. “Percolate down” to restore heap property • insert: 1. Put new node in next position on bottom row to restore structure property 2. “Percolate up” to restore heap property Spring 2010 14 CSE332: Data Abstractions 99 60 40 80 20 10 50 700 85 15 DeleteMin 3 4 9 8 5 7 10 6 9 11 1. Delete (and return) value at root node Spring 2010 CSE332: Data Abstractions 16 2. Restore the Structure Property • We now have a “hole” at the root – Need to fill the hole with another value • When we are done, the tree will have one less node and must still be complete 3 4 9 8 5 7 10 6 9 11 3 4 9 8 5 7 10 6 9 11 Spring 2010 CSE332: Data Abstractions
  • 5. 17 3. Restore the Heap Property Percolate down: • Keep comparing with both children • Move smaller child up and go down one level • Done if both children are ≥ item or reached a leaf node • What is the run time? 3 4 9 8 5 7 10 6 9 11 4 9 8 5 7 10 6 9 11 3 8 4 9 10 5 7 6 9 11 3 ? ? Spring 2010 CSE332: Data Abstractions 18 DeleteMin: Run Time Analysis • Run time is O(height of heap) • A heap is a complete binary tree • Height of a complete binary tree of n nodes? – height = ⎣ log2(n) ⎦ • Run time of deleteMin is O(log n) Spring 2010 CSE332: Data Abstractions 19 Insert • Add a value to the tree • Structure and heap order properties must still be correct afterwards 8 4 9 10 5 7 6 9 11 1 2 Spring 2010 CSE332: Data Abstractions 20 Insert: Maintain the Structure Property • There is only one valid tree shape after we add one more node • So put our new data there and then focus on restoring the heap property 8 4 9 10 5 7 6 9 11 1 2 Spring 2010 CSE332: Data Abstractions
  • 6. 21 Maintain the heap property 2 8 4 9 10 5 7 6 9 11 1 Percolate up: • Put new data in new location • If parent larger, swap with parent, and continue • Done if parent ≤ item or reached root • Run time? ? 2 5 8 4 9 10 7 6 9 11 1 ? 2 5 8 9 10 4 7 6 9 11 1 ? Spring 2010 CSE332: Data Abstractions 22 Insert: Run Time Analysis • Like deleteMin, worst-case time proportional to tree height – O(log n) • But… deleteMin needs the “last used” complete-tree position and insert needs the “next to use” complete-tree position – If “keep a reference to there” then insert and deleteMin have to adjust that reference: O(log n) in worst case – Could calculate how to find it in O(log n) from the root given the size of the heap • But it’s not easy • And then insert is always O(log n), promised O(1) on average (assuming random arrival of items) • There’s a “trick”: don’t represent complete trees with explicit edges! Spring 2010 CSE332: Data Abstractions