SlideShare a Scribd company logo
Priority Queues
   Scheduling example
   The priority queue ADT
   Implementing a priority queue with a sequence
   Binary Heaps
   Insertion in a Heaps and Heapify




                                                    1
Scheduling
 In a multi-user computer system, multiple users
  submit jobs to run on a single processor.
 We assume that the time required by each job is
  known in advance. Further, jobs can be
  preempted (stopped and resumed later)
 One policy which minimizes the average waiting
  time is SRPT (shortest remaining processing
  time).
 The processor schedules the job with the
  smallest remaining processing time.
 If while a job is running a new job arrives with
  processing time less than the remaining time of
  current job, the current job is preempted.
                                                 2
Data Structure for SRPT
 We   need to maintain the remaining
  processing time of the unfinished jobs at
  any point in time.
 We need to find the job with the shortest
  remaining processing time.
 When a job finishes we should remove it
  from our collection.
 When a new job arrives we need to add it
  to the collection.

                                              3
Priority Queues
 A priority queue is an ADT(abstract data type)
  for maintaining a set S of elements, each with an
  associated value called priority
 A PQ supports the following operations
     Insert(x) insert element x in set S (S S {x})
     Minimum() returns the element of S with smallest
      priority.
     Delete-min() returns and removes the element of S
      with smallest priority.



                                                          4
Priorities and Total Order Relations

 A Priority Queue ranks its elements by priority.
 Every element has a priority. Priorities are not
  necessarily unique and are totally ordered.
 Total Order Relation, denoted by
   Reflexive: k k
   Antisymetric: if k1 k2 and k2 k1, then k1 k2
   Transitive: if k1 k2 and k2 k3, then k1 k3



                                                5
Comparators
 The most general and reusable form of a priority
  queue makes use of comparator objects.
 Comparator objects are external to the keys that
  are to be compared and compare two objects.
 When the priority queue needs to compare two
  keys, it uses the comparator it was given to do the
  comparison.
 Thus a priority queue can be general enough to
  store any object.
 The comparator ADT includes:
    isLessThan(a, b), isLessThanOrEqualTo(a,b),
    isEqualTo(a, b), isGreaterThan(a,b),
    isGreaterThanOrEqualTo(a,b), isComparable(a)
                                                    6
Implem. with Unsorted Sequence
 The items are pairs (priority, element)
 We can implement insert() by using insertLast() on
  the sequence. This takes O(1) time.




   However, because we always insert at the end,
    irrespective of the key value, our sequence is not
    ordered.
                                                         7
Unsorted Sequence (contd.)

   Thus, for methods such as minimum(),delete-min()
    we need to look at all the elements of S. The worst
    case time complexity for these methods is O(n).




                                                     8
Implem. with Sorted Sequence
 Another implementation uses a sequence S,
  sorted by increasing priorities.
 minimum() and delete-min() take O(1) time.




   However, to implement insert(), we must now
    scan through the entire sequence in the worst
    case. Thus insert() runs in O(n) time.




                                                    9
Priority Queues

 Applications:
   jobscheduling shared computing resources
    (Unix)
   Event simulation
   As a building block for other algorithms

 A Heap   can be used to implement a PQ



                                               10
(Binary) Heaps
A binary tree that stores priorities (or priority-
  element) pairs at nodes
Structural  property:
All levels except last                         11
are full. Last level is
left-filled.                    17                        13
Heap property:
Priority of node is at
least as large as that 18                 21         19        17
of its parent.
                     43    23        26    29       31
                                                               11
Examples of non-Heaps
 Heap   property violated

                                   11

                    19                        13


              18              21         19        17


         43    23        26    29       31

                                                        12
Example of non-heap

 Last   level not left-filled
                                              11

                               17                        13


                          18             21         19        17


                     43             26    29       31
                                                              13
Finding the minimum element

 The  element with smallest priority always
  sits at the root of the heap.
 This is because if it was elsewhere, it
  would have a parent with larger priority
  and this would violate the heap property.
 Hence minimum() can be done in O(1)
  time.

                                               14
Height of a heap

 Suppose    a heap of n nodes has height h.
 Recall: complete binary tree of height h
  has 2h+1-1 nodes.
 Hence 2h-1 < n <= 2h+1-1.
 n = log2 h




                                               15
Implementing Heaps
Parent (i)                                     11
  return i/2
                                     17                  13
Left (i)
  return 2i                  18           21        19        17
Right (i)
  return 2i+1            43       23 26

                    1    2       3   4    5     6   7    8        9   10
              A     11 17 13 18 21 19 17 43 23 26
              Level: 0       1             2                  3

Heap property: A[Parent(i)] <= A[i]
                                                                       16
Implementing Heaps (2)

 Notice the implicit tree links; children of
  node i are 2i and 2i+1
 Why is this useful?
   Ina binary representation, a
    multiplication/division by two is left/right shift
   Adding 1 can be done by adding the lowest bit




                                                     17
Insertion in a Heap

 Insert 12
                                  11
 Insert 8
                   17                             13


            18               21         19                 17


       43     23        26    29       31    12        8


                                                                18
Another View of Insertion
   Enlarge heap
   Consider path from root to inserted node
   Find topmost element on this path with higher priority that
    that of inserted element.
   Insert new element at this location by shifting down the
    other elements on the path        11                     12

                               17                    13


                         18              21     19        17


                    43    23        26    29   31
                                                               19
Correctness of Insertion
 The only nodes whose contents change are the
  ones on the path.
 Heap property may be violated only for children
  of these nodes.
 But new contents of these nodes only have
  lower priority.                  11
 So heap property not violated.

                               17                    13
      12
                         18              21     19         17


                    43    23        26    29   31         20
Heapify
i is index into the array A
 Binary trees rooted at Left(i) and Right(i)
  are heaps
 But, A[i] might be smaller than its children,
  thus violating the heap property
 The method Heapify makes binary tree
  rooted at i a heap by moving A[i] down the
  heap.

                                              21
Heapify
 Heap property violated at node with index 1 but
  subtrees rooted at 2, 3 are heaps.
 heapify(1)
                                    17


                     10                             11


               16              21         13             12


          43    23        26    29       31    19

                                                              22
Another View of Heapify
   Heapify(i) traces a path down the tree.
   Last node on path (say j) has both A[left(j)], A[right(j)] are
    larger than A[i]
   All elements on path have lower priority than their siblings.
   All elements on this path are moved up. 17
   A[i] goes to location j.
   This establishes correctness
                                   10                       11


                                16          21      13          12


                           43    23    26    29   31     19
                                                                23
Running time Analysis
 A heap  of n nodes has height O(log n).
 While inserting we might have to move the
  element all the way to the top.
 Hence at most O(log n) steps required.
 In Heapify, the element might be moved all
  the way to the last level.
 Hence Heapify also requires O(log n) time.



                                           24

More Related Content

What's hot (20)

PDF
Secret Sharing Cs416
PPTX
Gnome sort
PPT
L’analyse et Expérimentation de algorithme du Tri par sélection
PDF
Build tic tac toe with javascript (4:11 dc)
PDF
Exact Matrix Completion via Convex Optimization Slide (PPT)
PPT
ADA - Minimum Spanning Tree Prim Kruskal and Dijkstra
PPT
minimum spanning trees Algorithm
PPTX
Unit 6
PPTX
Warshalls and floyds algorithms
DOCX
8 reinas
PDF
Graph theory
PPT
Data Structure and Algorithms Hashing
PDF
Queues
Secret Sharing Cs416
Gnome sort
L’analyse et Expérimentation de algorithme du Tri par sélection
Build tic tac toe with javascript (4:11 dc)
Exact Matrix Completion via Convex Optimization Slide (PPT)
ADA - Minimum Spanning Tree Prim Kruskal and Dijkstra
minimum spanning trees Algorithm
Unit 6
Warshalls and floyds algorithms
8 reinas
Graph theory
Data Structure and Algorithms Hashing
Queues
Ad

Viewers also liked (20)

PPT
Kinect krishna kumar-itkan
PDF
Spectra-projects-Oct-2016
PPS
Akropolis
PPT
Collective action under autocracies
PPT
Elearning rich media_search
PDF
Hieu qua san xuat bap lai tren dat lua dbscl
PDF
Perpres12 1961 tugas belajar
PPTX
Expeditie mont blanc
PPT
Death2
PPS
Top 9 unique structures
PPSX
Greek islands !!
PPS
Gronland
PPT
Borging lokaal klimaatbeleid sos 3 2010 v1.0
PPT
10강 기업교육론 20110504
Kinect krishna kumar-itkan
Spectra-projects-Oct-2016
Akropolis
Collective action under autocracies
Elearning rich media_search
Hieu qua san xuat bap lai tren dat lua dbscl
Perpres12 1961 tugas belajar
Expeditie mont blanc
Death2
Top 9 unique structures
Greek islands !!
Gronland
Borging lokaal klimaatbeleid sos 3 2010 v1.0
10강 기업교육론 20110504
Ad

Similar to Lec20 (20)

PDF
Sienna 7 heaps
PPTX
Heapsort
PPTX
Data Structures and Agorithm: DS 18 Heap.pptx
PDF
Introduction to the logic programing prolog
PPT
Heapsortokkay
PDF
ESINF05-Heaps.pdfESINF05-Heaps.pdfESINF05-Heaps.pdfESINF05-Heaps.pdf
PPTX
Lecture 5_ Sorting and order statistics.pptx
PPT
Complete binary tree and heap
PDF
10 chapter6 heaps_priority_queues
PPTX
Heaps and tries power point this is an educational material
PPT
Heap sort
PDF
Algorithm chapter 6
PDF
LEC 8-DS ALGO(heaps).pdf
PDF
PDF
Heaps
PPT
lecture 5
PPTX
Hash table and heaps
PPT
Priority queues and heap sorting
PDF
Heap Hand note
PPTX
Heap_Sort1.pptx
Sienna 7 heaps
Heapsort
Data Structures and Agorithm: DS 18 Heap.pptx
Introduction to the logic programing prolog
Heapsortokkay
ESINF05-Heaps.pdfESINF05-Heaps.pdfESINF05-Heaps.pdfESINF05-Heaps.pdf
Lecture 5_ Sorting and order statistics.pptx
Complete binary tree and heap
10 chapter6 heaps_priority_queues
Heaps and tries power point this is an educational material
Heap sort
Algorithm chapter 6
LEC 8-DS ALGO(heaps).pdf
Heaps
lecture 5
Hash table and heaps
Priority queues and heap sorting
Heap Hand note
Heap_Sort1.pptx

Lec20

  • 1. Priority Queues  Scheduling example  The priority queue ADT  Implementing a priority queue with a sequence  Binary Heaps  Insertion in a Heaps and Heapify 1
  • 2. Scheduling  In a multi-user computer system, multiple users submit jobs to run on a single processor.  We assume that the time required by each job is known in advance. Further, jobs can be preempted (stopped and resumed later)  One policy which minimizes the average waiting time is SRPT (shortest remaining processing time).  The processor schedules the job with the smallest remaining processing time.  If while a job is running a new job arrives with processing time less than the remaining time of current job, the current job is preempted. 2
  • 3. Data Structure for SRPT  We need to maintain the remaining processing time of the unfinished jobs at any point in time.  We need to find the job with the shortest remaining processing time.  When a job finishes we should remove it from our collection.  When a new job arrives we need to add it to the collection. 3
  • 4. Priority Queues  A priority queue is an ADT(abstract data type) for maintaining a set S of elements, each with an associated value called priority  A PQ supports the following operations  Insert(x) insert element x in set S (S S {x})  Minimum() returns the element of S with smallest priority.  Delete-min() returns and removes the element of S with smallest priority. 4
  • 5. Priorities and Total Order Relations  A Priority Queue ranks its elements by priority.  Every element has a priority. Priorities are not necessarily unique and are totally ordered.  Total Order Relation, denoted by Reflexive: k k Antisymetric: if k1 k2 and k2 k1, then k1 k2 Transitive: if k1 k2 and k2 k3, then k1 k3 5
  • 6. Comparators  The most general and reusable form of a priority queue makes use of comparator objects.  Comparator objects are external to the keys that are to be compared and compare two objects.  When the priority queue needs to compare two keys, it uses the comparator it was given to do the comparison.  Thus a priority queue can be general enough to store any object.  The comparator ADT includes: isLessThan(a, b), isLessThanOrEqualTo(a,b), isEqualTo(a, b), isGreaterThan(a,b), isGreaterThanOrEqualTo(a,b), isComparable(a) 6
  • 7. Implem. with Unsorted Sequence  The items are pairs (priority, element)  We can implement insert() by using insertLast() on the sequence. This takes O(1) time.  However, because we always insert at the end, irrespective of the key value, our sequence is not ordered. 7
  • 8. Unsorted Sequence (contd.)  Thus, for methods such as minimum(),delete-min() we need to look at all the elements of S. The worst case time complexity for these methods is O(n). 8
  • 9. Implem. with Sorted Sequence  Another implementation uses a sequence S, sorted by increasing priorities.  minimum() and delete-min() take O(1) time.  However, to implement insert(), we must now scan through the entire sequence in the worst case. Thus insert() runs in O(n) time. 9
  • 10. Priority Queues  Applications:  jobscheduling shared computing resources (Unix)  Event simulation  As a building block for other algorithms  A Heap can be used to implement a PQ 10
  • 11. (Binary) Heaps A binary tree that stores priorities (or priority- element) pairs at nodes Structural property: All levels except last 11 are full. Last level is left-filled. 17 13 Heap property: Priority of node is at least as large as that 18 21 19 17 of its parent. 43 23 26 29 31 11
  • 12. Examples of non-Heaps  Heap property violated 11 19 13 18 21 19 17 43 23 26 29 31 12
  • 13. Example of non-heap  Last level not left-filled 11 17 13 18 21 19 17 43 26 29 31 13
  • 14. Finding the minimum element  The element with smallest priority always sits at the root of the heap.  This is because if it was elsewhere, it would have a parent with larger priority and this would violate the heap property.  Hence minimum() can be done in O(1) time. 14
  • 15. Height of a heap  Suppose a heap of n nodes has height h.  Recall: complete binary tree of height h has 2h+1-1 nodes.  Hence 2h-1 < n <= 2h+1-1.  n = log2 h 15
  • 16. Implementing Heaps Parent (i) 11 return i/2 17 13 Left (i) return 2i 18 21 19 17 Right (i) return 2i+1 43 23 26 1 2 3 4 5 6 7 8 9 10 A 11 17 13 18 21 19 17 43 23 26 Level: 0 1 2 3 Heap property: A[Parent(i)] <= A[i] 16
  • 17. Implementing Heaps (2)  Notice the implicit tree links; children of node i are 2i and 2i+1  Why is this useful?  Ina binary representation, a multiplication/division by two is left/right shift  Adding 1 can be done by adding the lowest bit 17
  • 18. Insertion in a Heap  Insert 12 11  Insert 8 17 13 18 21 19 17 43 23 26 29 31 12 8 18
  • 19. Another View of Insertion  Enlarge heap  Consider path from root to inserted node  Find topmost element on this path with higher priority that that of inserted element.  Insert new element at this location by shifting down the other elements on the path 11 12 17 13 18 21 19 17 43 23 26 29 31 19
  • 20. Correctness of Insertion  The only nodes whose contents change are the ones on the path.  Heap property may be violated only for children of these nodes.  But new contents of these nodes only have lower priority. 11  So heap property not violated. 17 13 12 18 21 19 17 43 23 26 29 31 20
  • 21. Heapify i is index into the array A  Binary trees rooted at Left(i) and Right(i) are heaps  But, A[i] might be smaller than its children, thus violating the heap property  The method Heapify makes binary tree rooted at i a heap by moving A[i] down the heap. 21
  • 22. Heapify  Heap property violated at node with index 1 but subtrees rooted at 2, 3 are heaps.  heapify(1) 17 10 11 16 21 13 12 43 23 26 29 31 19 22
  • 23. Another View of Heapify  Heapify(i) traces a path down the tree.  Last node on path (say j) has both A[left(j)], A[right(j)] are larger than A[i]  All elements on path have lower priority than their siblings.  All elements on this path are moved up. 17  A[i] goes to location j.  This establishes correctness 10 11 16 21 13 12 43 23 26 29 31 19 23
  • 24. Running time Analysis  A heap of n nodes has height O(log n).  While inserting we might have to move the element all the way to the top.  Hence at most O(log n) steps required.  In Heapify, the element might be moved all the way to the last level.  Hence Heapify also requires O(log n) time. 24