SlideShare a Scribd company logo
1 of 15
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

Priority Queue in Data Structure
Priority Queue in Data StructurePriority Queue in Data Structure
Priority Queue in Data StructureMeghaj Mallick
 
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 structureVrushali Dhanokar
 
Lec 17 heap data structure
Lec 17 heap data structureLec 17 heap data structure
Lec 17 heap data structureSajid Marwat
 
Quick sort-Data Structure
Quick sort-Data StructureQuick sort-Data Structure
Quick sort-Data StructureJeanie Arnoco
 
Longest Common Subsequence
Longest Common SubsequenceLongest Common Subsequence
Longest Common SubsequenceKrishma Parekh
 
Longest common subsequence
Longest common subsequenceLongest common subsequence
Longest common subsequenceKiran K
 
data structures- back tracking
data structures- back trackingdata structures- back tracking
data structures- back trackingAbinaya B
 
Queue - Data Structure - Notes
Queue - Data Structure - NotesQueue - Data Structure - Notes
Queue - Data Structure - NotesOmprakash Chauhan
 
Daa:Dynamic Programing
Daa:Dynamic ProgramingDaa:Dynamic Programing
Daa:Dynamic Programingrupali_2bonde
 
Divide and conquer - Quick sort
Divide and conquer - Quick sortDivide and conquer - Quick sort
Divide and conquer - Quick sortMadhu Bala
 
Bubble Sort
Bubble SortBubble Sort
Bubble Sortgeeortiz
 
Greedy Algorithm - Knapsack Problem
Greedy Algorithm - Knapsack ProblemGreedy Algorithm - Knapsack Problem
Greedy Algorithm - Knapsack ProblemMadhu Bala
 

What's hot (20)

Priority Queue in Data Structure
Priority Queue in Data StructurePriority Queue in Data Structure
Priority Queue in Data Structure
 
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
 
Dynamic pgmming
Dynamic pgmmingDynamic pgmming
Dynamic pgmming
 
Quick sort
Quick sortQuick sort
Quick sort
 
Lec 17 heap data structure
Lec 17 heap data structureLec 17 heap data structure
Lec 17 heap data structure
 
Merge sort
Merge sortMerge sort
Merge sort
 
Operator precedence
Operator precedenceOperator precedence
Operator precedence
 
Quick sort-Data Structure
Quick sort-Data StructureQuick sort-Data Structure
Quick sort-Data Structure
 
Hash table
Hash tableHash table
Hash table
 
Longest Common Subsequence
Longest Common SubsequenceLongest Common Subsequence
Longest Common Subsequence
 
Merge sort algorithm power point presentation
Merge sort algorithm power point presentationMerge sort algorithm power point presentation
Merge sort algorithm power point presentation
 
Longest common subsequence
Longest common subsequenceLongest common subsequence
Longest common subsequence
 
Data structure stack&queue basics
Data structure stack&queue   basicsData structure stack&queue   basics
Data structure stack&queue basics
 
Ppt bubble sort
Ppt bubble sortPpt bubble sort
Ppt bubble sort
 
data structures- back tracking
data structures- back trackingdata structures- back tracking
data structures- back tracking
 
Queue - Data Structure - Notes
Queue - Data Structure - NotesQueue - Data Structure - Notes
Queue - Data Structure - Notes
 
Daa:Dynamic Programing
Daa:Dynamic ProgramingDaa:Dynamic Programing
Daa:Dynamic Programing
 
Divide and conquer - Quick sort
Divide and conquer - Quick sortDivide and conquer - Quick sort
Divide and conquer - Quick sort
 
Bubble Sort
Bubble SortBubble Sort
Bubble Sort
 
Greedy Algorithm - Knapsack Problem
Greedy Algorithm - Knapsack ProblemGreedy Algorithm - Knapsack Problem
Greedy Algorithm - Knapsack Problem
 

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
 
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
 
Priorty queue
Priorty queuePriorty queue
Priorty queue
 
Stack linked list
Stack linked listStack linked list
Stack linked list
 
Stacks in c++
Stacks in c++Stacks in c++
Stacks in c++
 
Data structure lab manual
Data structure lab manualData structure lab manual
Data structure lab manual
 
Stack.pptx
Stack.pptxStack.pptx
Stack.pptx
 
The Ring programming language version 1.9 book - Part 88 of 210
The Ring programming language version 1.9 book - Part 88 of 210The Ring programming language version 1.9 book - Part 88 of 210
The Ring programming language version 1.9 book - Part 88 of 210
 

Recently uploaded

CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxGaneshChakor2
 
Hybridoma Technology ( Production , Purification , and Application )
Hybridoma Technology  ( Production , Purification , and Application  ) Hybridoma Technology  ( Production , Purification , and Application  )
Hybridoma Technology ( Production , Purification , and Application ) Sakshi Ghasle
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionSafetyChain Software
 
Biting mechanism of poisonous snakes.pdf
Biting mechanism of poisonous snakes.pdfBiting mechanism of poisonous snakes.pdf
Biting mechanism of poisonous snakes.pdfadityarao40181
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxmanuelaromero2013
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Krashi Coaching
 
Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxthorishapillay1
 
Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Celine George
 
Painted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of IndiaPainted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of IndiaVirag Sontakke
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsanshu789521
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdfSoniaTolstoy
 
History Class XII Ch. 3 Kinship, Caste and Class (1).pptx
History Class XII Ch. 3 Kinship, Caste and Class (1).pptxHistory Class XII Ch. 3 Kinship, Caste and Class (1).pptx
History Class XII Ch. 3 Kinship, Caste and Class (1).pptxsocialsciencegdgrohi
 
ENGLISH5 QUARTER4 MODULE1 WEEK1-3 How Visual and Multimedia Elements.pptx
ENGLISH5 QUARTER4 MODULE1 WEEK1-3 How Visual and Multimedia Elements.pptxENGLISH5 QUARTER4 MODULE1 WEEK1-3 How Visual and Multimedia Elements.pptx
ENGLISH5 QUARTER4 MODULE1 WEEK1-3 How Visual and Multimedia Elements.pptxAnaBeatriceAblay2
 
Class 11 Legal Studies Ch-1 Concept of State .pdf
Class 11 Legal Studies Ch-1 Concept of State .pdfClass 11 Legal Studies Ch-1 Concept of State .pdf
Class 11 Legal Studies Ch-1 Concept of State .pdfakmcokerachita
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...Marc Dusseiller Dusjagr
 
How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17Celine George
 

Recently uploaded (20)

CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptx
 
Hybridoma Technology ( Production , Purification , and Application )
Hybridoma Technology  ( Production , Purification , and Application  ) Hybridoma Technology  ( Production , Purification , and Application  )
Hybridoma Technology ( Production , Purification , and Application )
 
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
 
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory Inspection
 
9953330565 Low Rate Call Girls In Rohini Delhi NCR
9953330565 Low Rate Call Girls In Rohini  Delhi NCR9953330565 Low Rate Call Girls In Rohini  Delhi NCR
9953330565 Low Rate Call Girls In Rohini Delhi NCR
 
Biting mechanism of poisonous snakes.pdf
Biting mechanism of poisonous snakes.pdfBiting mechanism of poisonous snakes.pdf
Biting mechanism of poisonous snakes.pdf
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptx
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
 
Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptx
 
Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17
 
Painted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of IndiaPainted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of India
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha elections
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
 
History Class XII Ch. 3 Kinship, Caste and Class (1).pptx
History Class XII Ch. 3 Kinship, Caste and Class (1).pptxHistory Class XII Ch. 3 Kinship, Caste and Class (1).pptx
History Class XII Ch. 3 Kinship, Caste and Class (1).pptx
 
ENGLISH5 QUARTER4 MODULE1 WEEK1-3 How Visual and Multimedia Elements.pptx
ENGLISH5 QUARTER4 MODULE1 WEEK1-3 How Visual and Multimedia Elements.pptxENGLISH5 QUARTER4 MODULE1 WEEK1-3 How Visual and Multimedia Elements.pptx
ENGLISH5 QUARTER4 MODULE1 WEEK1-3 How Visual and Multimedia Elements.pptx
 
Class 11 Legal Studies Ch-1 Concept of State .pdf
Class 11 Legal Studies Ch-1 Concept of State .pdfClass 11 Legal Studies Ch-1 Concept of State .pdf
Class 11 Legal Studies Ch-1 Concept of State .pdf
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
 
How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17
 
Staff of Color (SOC) Retention Efforts DDSD
Staff of Color (SOC) Retention Efforts DDSDStaff of Color (SOC) Retention Efforts DDSD
Staff of Color (SOC) Retention Efforts DDSD
 

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