SlideShare a Scribd company logo
MAX PRIORITY QUEUE
What is a priority queue?
 A priority queue is a data structure for maintaining a set S of elements, each with
an associated value called a key.
 In a priority queue, an element with high priority is served before an element with
low priority.
Max-Priority Queue
 Here we will implement the priority queue using heap.
 As with heaps, priority queues come in two forms:
max-priority queues and
min-priority queues
 Here we will focus on how to implement max-priority
queues, which are in turn based on max-heaps.
Operations of Max-Priority Queue
 A max-priority queue supports the following operations:
INSERT(S, x):inserts the element x into the set S, which is
equivalent to the operation S = S ∪ 𝒙 .
MAXIMUM(S):returns the element of S with the largest
key.
EXTRACT-MAX(S):removes and returns the element of S
with the largest key.
INCREASE-KEY(S, x, k):increases the value of element
x’s key to the new value k, which is assumed to be atleast
as large as x’s current key value.
Implementation of the operations of max-priority queue
HEAP-MAXIMUM(A)
1 return A[1]
 The procedure HEAP-MAXIMUM implements the MAXIMUM operation in θ(1)
time.
The procedure HEAP-EXTRACT-MAX implements the EXTRACT-MAX operation.
HEAP-EXTRACT-MAX(A)
1 if A.heap-size < 1
2 error “heap underflow”
3 max = A[1]
4 A[1] = A[A.heap-size]
5 A.heap-size = A.heap-size - 1
6 MAX-HEAPIFY(A, 1)
7 return max
Thus the time complexity of HEAP-EXTRACT-MAX is O(lg n).
Implementation of the operations of max-priority queue
From lines 1 to 5 runs is constant
time
From heap we know that the running time of
MAX-HEAPIFY is O(lg n)
Line 7 runs in constant time
67810
100
3050
1
2
3
4 5 6 7
7810
6
3050
1
2
3
4 5 6
MAX-HEAPIFY
7810
50
306
1
2
3
4 5 6
MAX-
HEAPIFY
(a) (b)
(c)
786
50
3010
1
2
3
4 5 6MAX-
HEAPIFY
(d)
The procedure HEAP-INCREASE-KEY implements the INCREASE-KEY operation.
(Parent (i) = )
HEAP-INCREASE-KEY(A, I, key)
1 if key < A[i]
2 error “new key is smaller than current key”
3 A[i] = key
4 while i > 1 and A[PARENT(i) ] < A[i]
5 exchange A[i] with A[PARENT(i)]
6 i = PARENT(i)
The WORST CASE occurs when we have to start INCREASE-KEY procedure from a leaf
node and stop at the root node of the heap.
Thus the time complexity of the HEAP-INCREASE-KEY depends on the number of times
the while loop (lines 4 to 6) executes .
Since the height of the tree is lg n, therefore the time complexity of HEAP-INCREASE-KEY
is O(lg n).
Implementation of the operations of max-priority queue
 2/i
From lines 1 to 3 runs is constant
time
Loop invariant of HEAP-INCREASE-KEY
 At the start of each iteration of the while loop of lines 4–6, the subarray
A[1….A.heap-size] satisfies the max-heap property, except that there
may be one violation: A[i] may be larger than A[PARENT(i)].
 Initialization. A is a heap except that A[i] might be larger
that it's parent, because it has been modified and A[i] is
larger than its children.
 Maintenance. When we exchange A[i] with its parent, the
max-heap property is satisfied except that
now A[PARENT(i)] might be larger than its parent.
 Termination. The loop terminates whenever we reach the
root of the heap or the max-heap property for A[i] and its
parent is satisfied. At the loop termination, A is a max-heap.
Image source (introduction to algorithms )
1
2
3
4 5 6 7
8 =9 10
1
2
3
4 5 6 7
8 =9 10
1
2
3
=4 5 6 7
8 9 10
1
=2 3
4 5 6 7
8 9 10
MAX-HEAP-INSERT(A,key)
1 A.heap-size =A.heap-size +1
2 A[A.heap-size] = -∞
3 HEAP-INCREASE-KEY(A,A.heap-size,key)
 The procedure MAX-HEAP-INSERT implements the INSERT operation. It takes as
an input the key of the new element to be inserted into max-heap A.
 The procedure first expands the max-heap by adding to the tree a new leaf whose
key is -∞.
 Then it calls HEAP-INCREASE-KEY to set the key of this new node to its correct
value and maintain the max-heap property.
 Since the lines 1 and 2 runs in constant time and from we know that complexity
of HEAP-INCREASE-KEY is O(lg n), therefore the complexity of MAX-HEAP-
INSERT is O(lg n).
Implementation of the operations of max-priority queue
The lines 1 and 2 runs in constant
time
67810
100
3050
1
2 3
4 5 6 7
(a) (b)
−∞
67810
100
3050
1
2 3
4 5 6 7
Key=55
(c)
55
67810
100
3050
1
2 3
4 5 6 7
HEAP-INCREASE-
KEY(A,8,55)
8
8
(d)
10
67855
100
3050
1
2 3
4 5 6 7
8
(e)
10
67850
100
3055
1
2 3
4 5 6 7
8
Application of MAX PRIORITY QUEUE
• One of the most popular application Max Priority queue is job
scheduler in a computer.
• The max-priority queue keeps track of the jobs to be performed
and their relative priorities. When a job is finished or interrupted,
the scheduler selects the highest-priority job from among those
pending by calling EXTRACT-MAX. The scheduler can add a new job
to the queue at any time by calling INSERT.
Conclusion
 A heap can support any priority-queue operation on a set of size n in O(lg n)
time.
 Implementation of an efficient Priority Queue is one of the applications of the heap.
 If we want to decrease the value of a key in the heap, after decreasing the value
we have to call the MAX-HEAPIFY procedure on that node.
Bibliography
 Introduction to algorithms – 3rd edition
 Ita.skanev.com

More Related Content

What's hot

Hetro associative memory
Hetro associative memoryHetro associative memory
Hetro associative memory
DEEPENDRA KORI
 
Equivalence of DFAs and NFAs.pptx
Equivalence of DFAs and NFAs.pptxEquivalence of DFAs and NFAs.pptx
Equivalence of DFAs and NFAs.pptx
SamyakJain710491
 
BackTracking Algorithm: Technique and Examples
BackTracking Algorithm: Technique and ExamplesBackTracking Algorithm: Technique and Examples
BackTracking Algorithm: Technique and Examples
Fahim Ferdous
 
Chapter 11 - Sorting and Searching
Chapter 11 - Sorting and SearchingChapter 11 - Sorting and Searching
Chapter 11 - Sorting and Searching
Eduardo Bergavera
 
Deque and its applications
Deque and its applicationsDeque and its applications
Deque and its applications
Jsaddam Hussain
 
Lec 17 heap data structure
Lec 17 heap data structureLec 17 heap data structure
Lec 17 heap data structure
Sajid Marwat
 
sum of subset problem using Backtracking
sum of subset problem using Backtrackingsum of subset problem using Backtracking
sum of subset problem using Backtracking
Abhishek Singh
 
Open addressiing &amp;rehashing,extendiblevhashing
Open addressiing &amp;rehashing,extendiblevhashingOpen addressiing &amp;rehashing,extendiblevhashing
Open addressiing &amp;rehashing,extendiblevhashing
SangeethaSasi1
 
Branch and bound
Branch and boundBranch and bound
Branch and bound
Dr Shashikant Athawale
 
Asymptotic Notation
Asymptotic NotationAsymptotic Notation
Asymptotic Notation
Protap Mondal
 
heap Sort Algorithm
heap  Sort Algorithmheap  Sort Algorithm
heap Sort Algorithm
Lemia Algmri
 
Backtracking
Backtracking  Backtracking
Backtracking
Vikas Sharma
 
Knapsack problem algorithm, greedy algorithm
Knapsack problem algorithm, greedy algorithmKnapsack problem algorithm, greedy algorithm
Knapsack problem algorithm, greedy algorithm
HoneyChintal
 
Greedy method1
Greedy method1Greedy method1
Greedy method1
Rajendran
 
Complexity of Algorithm
Complexity of AlgorithmComplexity of Algorithm
Complexity of Algorithm
Muhammad Muzammal
 
Algorithm analysis in fundamentals of data structure
Algorithm analysis in fundamentals of data structureAlgorithm analysis in fundamentals of data structure
Algorithm analysis in fundamentals of data structure
Vrushali Dhanokar
 
Analysis of algorithms
Analysis of algorithmsAnalysis of algorithms
Analysis of algorithms
Ganesh Solanke
 
Lecture 11 semantic analysis 2
Lecture 11 semantic analysis 2Lecture 11 semantic analysis 2
Lecture 11 semantic analysis 2
Iffat Anjum
 
Knapsack Problem (DP & GREEDY)
Knapsack Problem (DP & GREEDY)Knapsack Problem (DP & GREEDY)
Knapsack Problem (DP & GREEDY)
Ridhima Chowdhury
 
Queue in Data Structure
Queue in Data Structure Queue in Data Structure
Queue in Data Structure
Janki Shah
 

What's hot (20)

Hetro associative memory
Hetro associative memoryHetro associative memory
Hetro associative memory
 
Equivalence of DFAs and NFAs.pptx
Equivalence of DFAs and NFAs.pptxEquivalence of DFAs and NFAs.pptx
Equivalence of DFAs and NFAs.pptx
 
BackTracking Algorithm: Technique and Examples
BackTracking Algorithm: Technique and ExamplesBackTracking Algorithm: Technique and Examples
BackTracking Algorithm: Technique and Examples
 
Chapter 11 - Sorting and Searching
Chapter 11 - Sorting and SearchingChapter 11 - Sorting and Searching
Chapter 11 - Sorting and Searching
 
Deque and its applications
Deque and its applicationsDeque and its applications
Deque and its applications
 
Lec 17 heap data structure
Lec 17 heap data structureLec 17 heap data structure
Lec 17 heap data structure
 
sum of subset problem using Backtracking
sum of subset problem using Backtrackingsum of subset problem using Backtracking
sum of subset problem using Backtracking
 
Open addressiing &amp;rehashing,extendiblevhashing
Open addressiing &amp;rehashing,extendiblevhashingOpen addressiing &amp;rehashing,extendiblevhashing
Open addressiing &amp;rehashing,extendiblevhashing
 
Branch and bound
Branch and boundBranch and bound
Branch and bound
 
Asymptotic Notation
Asymptotic NotationAsymptotic Notation
Asymptotic Notation
 
heap Sort Algorithm
heap  Sort Algorithmheap  Sort Algorithm
heap Sort Algorithm
 
Backtracking
Backtracking  Backtracking
Backtracking
 
Knapsack problem algorithm, greedy algorithm
Knapsack problem algorithm, greedy algorithmKnapsack problem algorithm, greedy algorithm
Knapsack problem algorithm, greedy algorithm
 
Greedy method1
Greedy method1Greedy method1
Greedy method1
 
Complexity of Algorithm
Complexity of AlgorithmComplexity of Algorithm
Complexity of Algorithm
 
Algorithm analysis in fundamentals of data structure
Algorithm analysis in fundamentals of data structureAlgorithm analysis in fundamentals of data structure
Algorithm analysis in fundamentals of data structure
 
Analysis of algorithms
Analysis of algorithmsAnalysis of algorithms
Analysis of algorithms
 
Lecture 11 semantic analysis 2
Lecture 11 semantic analysis 2Lecture 11 semantic analysis 2
Lecture 11 semantic analysis 2
 
Knapsack Problem (DP & GREEDY)
Knapsack Problem (DP & GREEDY)Knapsack Problem (DP & GREEDY)
Knapsack Problem (DP & GREEDY)
 
Queue in Data Structure
Queue in Data Structure Queue in Data Structure
Queue in Data Structure
 

Similar to Max priority queue

Algorithms - "heap sort"
Algorithms - "heap sort"Algorithms - "heap sort"
Algorithms - "heap sort"
Ra'Fat Al-Msie'deen
 
Cis435 week05
Cis435 week05Cis435 week05
Cis435 week05
ashish bansal
 
Heaps
HeapsHeaps
Lecture 5_ Sorting and order statistics.pptx
Lecture 5_ Sorting and order statistics.pptxLecture 5_ Sorting and order statistics.pptx
Lecture 5_ Sorting and order statistics.pptx
JosephKariuki46
 
Heap sort
Heap sortHeap sort
Heap sort
Ayesha Tahir
 
Analysis of Algorithms-Heapsort
Analysis of Algorithms-HeapsortAnalysis of Algorithms-Heapsort
Analysis of Algorithms-Heapsort
Reetesh Gupta
 
week2.v2 dsfjue0owirewoifudsoufsoiuewrew.pptx
week2.v2 dsfjue0owirewoifudsoufsoiuewrew.pptxweek2.v2 dsfjue0owirewoifudsoufsoiuewrew.pptx
week2.v2 dsfjue0owirewoifudsoufsoiuewrew.pptx
hardmarcelia
 
Stack data structure
Stack data structureStack data structure
Stack data structure
rogineojerio020496
 
Heapsort quick sort
Heapsort quick sortHeapsort quick sort
Heapsort quick sort
Dr Sandeep Kumar Poonia
 
Heap
HeapHeap
Computer Network Assignment Help
Computer Network Assignment HelpComputer Network Assignment Help
Computer Network Assignment Help
Computer Network Assignment Help
 
Heap and heapsort
Heap and heapsortHeap and heapsort
Heap and heapsort
Amit Kumar Rathi
 
Heap, quick and merge sort
Heap, quick and merge sortHeap, quick and merge sort
Heap, quick and merge sort
Dr. Mohammad Amir Khusru Akhtar (Ph.D)
 
Algorithm: priority queue
Algorithm: priority queueAlgorithm: priority queue
Algorithm: priority queue
Tareq Hasan
 
Stack
StackStack
Data Structures and Agorithm: DS 06 Stack.pptx
Data Structures and Agorithm: DS 06 Stack.pptxData Structures and Agorithm: DS 06 Stack.pptx
Data Structures and Agorithm: DS 06 Stack.pptx
RashidFaridChishti
 
Priorty queue
Priorty queuePriorty queue
Priorty queue
sidra ali
 
lecture10trsgfchjvxgfzfdchgdchgcgshyjh.ppt
lecture10trsgfchjvxgfzfdchgdchgcgshyjh.pptlecture10trsgfchjvxgfzfdchgdchgcgshyjh.ppt
lecture10trsgfchjvxgfzfdchgdchgcgshyjh.ppt
partho5958
 
Stack linked list
Stack linked listStack linked list
Stack linked list
bhargav0077
 
Stacks in c++
Stacks in c++Stacks in c++
Stacks in c++
Vineeta Garg
 

Similar to Max priority queue (20)

Algorithms - "heap sort"
Algorithms - "heap sort"Algorithms - "heap sort"
Algorithms - "heap sort"
 
Cis435 week05
Cis435 week05Cis435 week05
Cis435 week05
 
Heaps
HeapsHeaps
Heaps
 
Lecture 5_ Sorting and order statistics.pptx
Lecture 5_ Sorting and order statistics.pptxLecture 5_ Sorting and order statistics.pptx
Lecture 5_ Sorting and order statistics.pptx
 
Heap sort
Heap sortHeap sort
Heap sort
 
Analysis of Algorithms-Heapsort
Analysis of Algorithms-HeapsortAnalysis of Algorithms-Heapsort
Analysis of Algorithms-Heapsort
 
week2.v2 dsfjue0owirewoifudsoufsoiuewrew.pptx
week2.v2 dsfjue0owirewoifudsoufsoiuewrew.pptxweek2.v2 dsfjue0owirewoifudsoufsoiuewrew.pptx
week2.v2 dsfjue0owirewoifudsoufsoiuewrew.pptx
 
Stack data structure
Stack data structureStack data structure
Stack data structure
 
Heapsort quick sort
Heapsort quick sortHeapsort quick sort
Heapsort quick sort
 
Heap
HeapHeap
Heap
 
Computer Network Assignment Help
Computer Network Assignment HelpComputer Network Assignment Help
Computer Network Assignment Help
 
Heap and heapsort
Heap and heapsortHeap and heapsort
Heap and heapsort
 
Heap, quick and merge sort
Heap, quick and merge sortHeap, quick and merge sort
Heap, quick and merge sort
 
Algorithm: priority queue
Algorithm: priority queueAlgorithm: priority queue
Algorithm: priority queue
 
Stack
StackStack
Stack
 
Data Structures and Agorithm: DS 06 Stack.pptx
Data Structures and Agorithm: DS 06 Stack.pptxData Structures and Agorithm: DS 06 Stack.pptx
Data Structures and Agorithm: DS 06 Stack.pptx
 
Priorty queue
Priorty queuePriorty queue
Priorty queue
 
lecture10trsgfchjvxgfzfdchgdchgcgshyjh.ppt
lecture10trsgfchjvxgfzfdchgdchgcgshyjh.pptlecture10trsgfchjvxgfzfdchgdchgcgshyjh.ppt
lecture10trsgfchjvxgfzfdchgdchgcgshyjh.ppt
 
Stack linked list
Stack linked listStack linked list
Stack linked list
 
Stacks in c++
Stacks in c++Stacks in c++
Stacks in c++
 

Recently uploaded

Bed Making ( Introduction, Purpose, Types, Articles, Scientific principles, N...
Bed Making ( Introduction, Purpose, Types, Articles, Scientific principles, N...Bed Making ( Introduction, Purpose, Types, Articles, Scientific principles, N...
Bed Making ( Introduction, Purpose, Types, Articles, Scientific principles, N...
Leena Ghag-Sakpal
 
Advanced Java[Extra Concepts, Not Difficult].docx
Advanced Java[Extra Concepts, Not Difficult].docxAdvanced Java[Extra Concepts, Not Difficult].docx
Advanced Java[Extra Concepts, Not Difficult].docx
adhitya5119
 
Liberal Approach to the Study of Indian Politics.pdf
Liberal Approach to the Study of Indian Politics.pdfLiberal Approach to the Study of Indian Politics.pdf
Liberal Approach to the Study of Indian Politics.pdf
WaniBasim
 
Walmart Business+ and Spark Good for Nonprofits.pdf
Walmart Business+ and Spark Good for Nonprofits.pdfWalmart Business+ and Spark Good for Nonprofits.pdf
Walmart Business+ and Spark Good for Nonprofits.pdf
TechSoup
 
คำศัพท์ คำพื้นฐานการอ่าน ภาษาอังกฤษ ระดับชั้น ม.1
คำศัพท์ คำพื้นฐานการอ่าน ภาษาอังกฤษ ระดับชั้น ม.1คำศัพท์ คำพื้นฐานการอ่าน ภาษาอังกฤษ ระดับชั้น ม.1
คำศัพท์ คำพื้นฐานการอ่าน ภาษาอังกฤษ ระดับชั้น ม.1
สมใจ จันสุกสี
 
The basics of sentences session 6pptx.pptx
The basics of sentences session 6pptx.pptxThe basics of sentences session 6pptx.pptx
The basics of sentences session 6pptx.pptx
heathfieldcps1
 
IGCSE Biology Chapter 14- Reproduction in Plants.pdf
IGCSE Biology Chapter 14- Reproduction in Plants.pdfIGCSE Biology Chapter 14- Reproduction in Plants.pdf
IGCSE Biology Chapter 14- Reproduction in Plants.pdf
Amin Marwan
 
MARY JANE WILSON, A “BOA MÃE” .
MARY JANE WILSON, A “BOA MÃE”           .MARY JANE WILSON, A “BOA MÃE”           .
MARY JANE WILSON, A “BOA MÃE” .
Colégio Santa Teresinha
 
B. Ed Syllabus for babasaheb ambedkar education university.pdf
B. Ed Syllabus for babasaheb ambedkar education university.pdfB. Ed Syllabus for babasaheb ambedkar education university.pdf
B. Ed Syllabus for babasaheb ambedkar education university.pdf
BoudhayanBhattachari
 
BÀI TẬP DẠY THÊM TIẾNG ANH LỚP 7 CẢ NĂM FRIENDS PLUS SÁCH CHÂN TRỜI SÁNG TẠO ...
BÀI TẬP DẠY THÊM TIẾNG ANH LỚP 7 CẢ NĂM FRIENDS PLUS SÁCH CHÂN TRỜI SÁNG TẠO ...BÀI TẬP DẠY THÊM TIẾNG ANH LỚP 7 CẢ NĂM FRIENDS PLUS SÁCH CHÂN TRỜI SÁNG TẠO ...
BÀI TẬP DẠY THÊM TIẾNG ANH LỚP 7 CẢ NĂM FRIENDS PLUS SÁCH CHÂN TRỜI SÁNG TẠO ...
Nguyen Thanh Tu Collection
 
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdfবাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
eBook.com.bd (প্রয়োজনীয় বাংলা বই)
 
BBR 2024 Summer Sessions Interview Training
BBR  2024 Summer Sessions Interview TrainingBBR  2024 Summer Sessions Interview Training
BBR 2024 Summer Sessions Interview Training
Katrina Pritchard
 
Pengantar Penggunaan Flutter - Dart programming language1.pptx
Pengantar Penggunaan Flutter - Dart programming language1.pptxPengantar Penggunaan Flutter - Dart programming language1.pptx
Pengantar Penggunaan Flutter - Dart programming language1.pptx
Fajar Baskoro
 
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdfANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
Priyankaranawat4
 
How to deliver Powerpoint Presentations.pptx
How to deliver Powerpoint  Presentations.pptxHow to deliver Powerpoint  Presentations.pptx
How to deliver Powerpoint Presentations.pptx
HajraNaeem15
 
Temple of Asclepius in Thrace. Excavation results
Temple of Asclepius in Thrace. Excavation resultsTemple of Asclepius in Thrace. Excavation results
Temple of Asclepius in Thrace. Excavation results
Krassimira Luka
 
Your Skill Boost Masterclass: Strategies for Effective Upskilling
Your Skill Boost Masterclass: Strategies for Effective UpskillingYour Skill Boost Masterclass: Strategies for Effective Upskilling
Your Skill Boost Masterclass: Strategies for Effective Upskilling
Excellence Foundation for South Sudan
 
LAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UP
LAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UPLAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UP
LAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UP
RAHUL
 
math operations ued in python and all used
math operations ued in python and all usedmath operations ued in python and all used
math operations ued in python and all used
ssuser13ffe4
 
Hindi varnamala | hindi alphabet PPT.pdf
Hindi varnamala | hindi alphabet PPT.pdfHindi varnamala | hindi alphabet PPT.pdf
Hindi varnamala | hindi alphabet PPT.pdf
Dr. Mulla Adam Ali
 

Recently uploaded (20)

Bed Making ( Introduction, Purpose, Types, Articles, Scientific principles, N...
Bed Making ( Introduction, Purpose, Types, Articles, Scientific principles, N...Bed Making ( Introduction, Purpose, Types, Articles, Scientific principles, N...
Bed Making ( Introduction, Purpose, Types, Articles, Scientific principles, N...
 
Advanced Java[Extra Concepts, Not Difficult].docx
Advanced Java[Extra Concepts, Not Difficult].docxAdvanced Java[Extra Concepts, Not Difficult].docx
Advanced Java[Extra Concepts, Not Difficult].docx
 
Liberal Approach to the Study of Indian Politics.pdf
Liberal Approach to the Study of Indian Politics.pdfLiberal Approach to the Study of Indian Politics.pdf
Liberal Approach to the Study of Indian Politics.pdf
 
Walmart Business+ and Spark Good for Nonprofits.pdf
Walmart Business+ and Spark Good for Nonprofits.pdfWalmart Business+ and Spark Good for Nonprofits.pdf
Walmart Business+ and Spark Good for Nonprofits.pdf
 
คำศัพท์ คำพื้นฐานการอ่าน ภาษาอังกฤษ ระดับชั้น ม.1
คำศัพท์ คำพื้นฐานการอ่าน ภาษาอังกฤษ ระดับชั้น ม.1คำศัพท์ คำพื้นฐานการอ่าน ภาษาอังกฤษ ระดับชั้น ม.1
คำศัพท์ คำพื้นฐานการอ่าน ภาษาอังกฤษ ระดับชั้น ม.1
 
The basics of sentences session 6pptx.pptx
The basics of sentences session 6pptx.pptxThe basics of sentences session 6pptx.pptx
The basics of sentences session 6pptx.pptx
 
IGCSE Biology Chapter 14- Reproduction in Plants.pdf
IGCSE Biology Chapter 14- Reproduction in Plants.pdfIGCSE Biology Chapter 14- Reproduction in Plants.pdf
IGCSE Biology Chapter 14- Reproduction in Plants.pdf
 
MARY JANE WILSON, A “BOA MÃE” .
MARY JANE WILSON, A “BOA MÃE”           .MARY JANE WILSON, A “BOA MÃE”           .
MARY JANE WILSON, A “BOA MÃE” .
 
B. Ed Syllabus for babasaheb ambedkar education university.pdf
B. Ed Syllabus for babasaheb ambedkar education university.pdfB. Ed Syllabus for babasaheb ambedkar education university.pdf
B. Ed Syllabus for babasaheb ambedkar education university.pdf
 
BÀI TẬP DẠY THÊM TIẾNG ANH LỚP 7 CẢ NĂM FRIENDS PLUS SÁCH CHÂN TRỜI SÁNG TẠO ...
BÀI TẬP DẠY THÊM TIẾNG ANH LỚP 7 CẢ NĂM FRIENDS PLUS SÁCH CHÂN TRỜI SÁNG TẠO ...BÀI TẬP DẠY THÊM TIẾNG ANH LỚP 7 CẢ NĂM FRIENDS PLUS SÁCH CHÂN TRỜI SÁNG TẠO ...
BÀI TẬP DẠY THÊM TIẾNG ANH LỚP 7 CẢ NĂM FRIENDS PLUS SÁCH CHÂN TRỜI SÁNG TẠO ...
 
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdfবাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
 
BBR 2024 Summer Sessions Interview Training
BBR  2024 Summer Sessions Interview TrainingBBR  2024 Summer Sessions Interview Training
BBR 2024 Summer Sessions Interview Training
 
Pengantar Penggunaan Flutter - Dart programming language1.pptx
Pengantar Penggunaan Flutter - Dart programming language1.pptxPengantar Penggunaan Flutter - Dart programming language1.pptx
Pengantar Penggunaan Flutter - Dart programming language1.pptx
 
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdfANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
 
How to deliver Powerpoint Presentations.pptx
How to deliver Powerpoint  Presentations.pptxHow to deliver Powerpoint  Presentations.pptx
How to deliver Powerpoint Presentations.pptx
 
Temple of Asclepius in Thrace. Excavation results
Temple of Asclepius in Thrace. Excavation resultsTemple of Asclepius in Thrace. Excavation results
Temple of Asclepius in Thrace. Excavation results
 
Your Skill Boost Masterclass: Strategies for Effective Upskilling
Your Skill Boost Masterclass: Strategies for Effective UpskillingYour Skill Boost Masterclass: Strategies for Effective Upskilling
Your Skill Boost Masterclass: Strategies for Effective Upskilling
 
LAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UP
LAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UPLAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UP
LAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UP
 
math operations ued in python and all used
math operations ued in python and all usedmath operations ued in python and all used
math operations ued in python and all used
 
Hindi varnamala | hindi alphabet PPT.pdf
Hindi varnamala | hindi alphabet PPT.pdfHindi varnamala | hindi alphabet PPT.pdf
Hindi varnamala | hindi alphabet PPT.pdf
 

Max priority queue

  • 2. What is a priority queue?  A priority queue is a data structure for maintaining a set S of elements, each with an associated value called a key.  In a priority queue, an element with high priority is served before an element with low priority.
  • 3. Max-Priority Queue  Here we will implement the priority queue using heap.  As with heaps, priority queues come in two forms: max-priority queues and min-priority queues  Here we will focus on how to implement max-priority queues, which are in turn based on max-heaps.
  • 4. Operations of Max-Priority Queue  A max-priority queue supports the following operations: INSERT(S, x):inserts the element x into the set S, which is equivalent to the operation S = S ∪ 𝒙 . MAXIMUM(S):returns the element of S with the largest key. EXTRACT-MAX(S):removes and returns the element of S with the largest key. INCREASE-KEY(S, x, k):increases the value of element x’s key to the new value k, which is assumed to be atleast as large as x’s current key value.
  • 5. Implementation of the operations of max-priority queue HEAP-MAXIMUM(A) 1 return A[1]  The procedure HEAP-MAXIMUM implements the MAXIMUM operation in θ(1) time.
  • 6. The procedure HEAP-EXTRACT-MAX implements the EXTRACT-MAX operation. HEAP-EXTRACT-MAX(A) 1 if A.heap-size < 1 2 error “heap underflow” 3 max = A[1] 4 A[1] = A[A.heap-size] 5 A.heap-size = A.heap-size - 1 6 MAX-HEAPIFY(A, 1) 7 return max Thus the time complexity of HEAP-EXTRACT-MAX is O(lg n). Implementation of the operations of max-priority queue From lines 1 to 5 runs is constant time From heap we know that the running time of MAX-HEAPIFY is O(lg n) Line 7 runs in constant time
  • 7. 67810 100 3050 1 2 3 4 5 6 7 7810 6 3050 1 2 3 4 5 6 MAX-HEAPIFY 7810 50 306 1 2 3 4 5 6 MAX- HEAPIFY (a) (b) (c) 786 50 3010 1 2 3 4 5 6MAX- HEAPIFY (d)
  • 8. The procedure HEAP-INCREASE-KEY implements the INCREASE-KEY operation. (Parent (i) = ) HEAP-INCREASE-KEY(A, I, key) 1 if key < A[i] 2 error “new key is smaller than current key” 3 A[i] = key 4 while i > 1 and A[PARENT(i) ] < A[i] 5 exchange A[i] with A[PARENT(i)] 6 i = PARENT(i) The WORST CASE occurs when we have to start INCREASE-KEY procedure from a leaf node and stop at the root node of the heap. Thus the time complexity of the HEAP-INCREASE-KEY depends on the number of times the while loop (lines 4 to 6) executes . Since the height of the tree is lg n, therefore the time complexity of HEAP-INCREASE-KEY is O(lg n). Implementation of the operations of max-priority queue  2/i From lines 1 to 3 runs is constant time
  • 9. Loop invariant of HEAP-INCREASE-KEY  At the start of each iteration of the while loop of lines 4–6, the subarray A[1….A.heap-size] satisfies the max-heap property, except that there may be one violation: A[i] may be larger than A[PARENT(i)].  Initialization. A is a heap except that A[i] might be larger that it's parent, because it has been modified and A[i] is larger than its children.  Maintenance. When we exchange A[i] with its parent, the max-heap property is satisfied except that now A[PARENT(i)] might be larger than its parent.  Termination. The loop terminates whenever we reach the root of the heap or the max-heap property for A[i] and its parent is satisfied. At the loop termination, A is a max-heap.
  • 10. Image source (introduction to algorithms ) 1 2 3 4 5 6 7 8 =9 10 1 2 3 4 5 6 7 8 =9 10 1 2 3 =4 5 6 7 8 9 10 1 =2 3 4 5 6 7 8 9 10
  • 11. MAX-HEAP-INSERT(A,key) 1 A.heap-size =A.heap-size +1 2 A[A.heap-size] = -∞ 3 HEAP-INCREASE-KEY(A,A.heap-size,key)  The procedure MAX-HEAP-INSERT implements the INSERT operation. It takes as an input the key of the new element to be inserted into max-heap A.  The procedure first expands the max-heap by adding to the tree a new leaf whose key is -∞.  Then it calls HEAP-INCREASE-KEY to set the key of this new node to its correct value and maintain the max-heap property.  Since the lines 1 and 2 runs in constant time and from we know that complexity of HEAP-INCREASE-KEY is O(lg n), therefore the complexity of MAX-HEAP- INSERT is O(lg n). Implementation of the operations of max-priority queue The lines 1 and 2 runs in constant time
  • 12. 67810 100 3050 1 2 3 4 5 6 7 (a) (b) −∞ 67810 100 3050 1 2 3 4 5 6 7 Key=55 (c) 55 67810 100 3050 1 2 3 4 5 6 7 HEAP-INCREASE- KEY(A,8,55) 8 8 (d) 10 67855 100 3050 1 2 3 4 5 6 7 8 (e) 10 67850 100 3055 1 2 3 4 5 6 7 8
  • 13. Application of MAX PRIORITY QUEUE • One of the most popular application Max Priority queue is job scheduler in a computer. • The max-priority queue keeps track of the jobs to be performed and their relative priorities. When a job is finished or interrupted, the scheduler selects the highest-priority job from among those pending by calling EXTRACT-MAX. The scheduler can add a new job to the queue at any time by calling INSERT.
  • 14. Conclusion  A heap can support any priority-queue operation on a set of size n in O(lg n) time.  Implementation of an efficient Priority Queue is one of the applications of the heap.  If we want to decrease the value of a key in the heap, after decreasing the value we have to call the MAX-HEAPIFY procedure on that node.
  • 15. Bibliography  Introduction to algorithms – 3rd edition  Ita.skanev.com