SlideShare une entreprise Scribd logo
1  sur  24
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

Contenu connexe

Tendances

ICPC国内予選F解説
ICPC国内予選F解説ICPC国内予選F解説
ICPC国内予選F解説tmaehara
 
小學三年級語文知識工作紙:標點符號(十)
小學三年級語文知識工作紙:標點符號(十)小學三年級語文知識工作紙:標點符號(十)
小學三年級語文知識工作紙:標點符號(十)miss wong
 
小學三年級語文知識工作紙:排句成段(十一)
小學三年級語文知識工作紙:排句成段(十一)小學三年級語文知識工作紙:排句成段(十一)
小學三年級語文知識工作紙:排句成段(十一)miss wong
 
Chapitre iv algorithmes de tri
Chapitre iv algorithmes de triChapitre iv algorithmes de tri
Chapitre iv algorithmes de triSana Aroussi
 
Binary trees
Binary treesBinary trees
Binary treesAmit Vats
 
直前合宿 講義スライド
直前合宿 講義スライド直前合宿 講義スライド
直前合宿 講義スライドtozan gezan
 
Béton. 1 composant du béton
Béton. 1 composant du bétonBéton. 1 composant du béton
Béton. 1 composant du bétonSami Sahli
 
Algorythmes et programmation
Algorythmes et programmationAlgorythmes et programmation
Algorythmes et programmationMohamed Latifi
 
Fonctions formules excel
Fonctions formules excelFonctions formules excel
Fonctions formules excelCarlitza
 
AtCoder Regular Contest 033 解説
AtCoder Regular Contest 033 解説AtCoder Regular Contest 033 解説
AtCoder Regular Contest 033 解説AtCoder Inc.
 

Tendances (20)

Lec7
Lec7Lec7
Lec7
 
Lec5
Lec5Lec5
Lec5
 
Lec3
Lec3Lec3
Lec3
 
Lec1
Lec1Lec1
Lec1
 
Lec2
Lec2Lec2
Lec2
 
Lec3
Lec3Lec3
Lec3
 
ICPC国内予選F解説
ICPC国内予選F解説ICPC国内予選F解説
ICPC国内予選F解説
 
Red Black Trees
Red Black TreesRed Black Trees
Red Black Trees
 
小學三年級語文知識工作紙:標點符號(十)
小學三年級語文知識工作紙:標點符號(十)小學三年級語文知識工作紙:標點符號(十)
小學三年級語文知識工作紙:標點符號(十)
 
小學三年級語文知識工作紙:排句成段(十一)
小學三年級語文知識工作紙:排句成段(十一)小學三年級語文知識工作紙:排句成段(十一)
小學三年級語文知識工作紙:排句成段(十一)
 
Chapitre iv algorithmes de tri
Chapitre iv algorithmes de triChapitre iv algorithmes de tri
Chapitre iv algorithmes de tri
 
Binary trees
Binary treesBinary trees
Binary trees
 
Binomial Heaps
Binomial HeapsBinomial Heaps
Binomial Heaps
 
Lec2
Lec2Lec2
Lec2
 
直前合宿 講義スライド
直前合宿 講義スライド直前合宿 講義スライド
直前合宿 講義スライド
 
Béton. 1 composant du béton
Béton. 1 composant du bétonBéton. 1 composant du béton
Béton. 1 composant du béton
 
Algorythmes et programmation
Algorythmes et programmationAlgorythmes et programmation
Algorythmes et programmation
 
Fonctions formules excel
Fonctions formules excelFonctions formules excel
Fonctions formules excel
 
Nazoki
NazokiNazoki
Nazoki
 
AtCoder Regular Contest 033 解説
AtCoder Regular Contest 033 解説AtCoder Regular Contest 033 解説
AtCoder Regular Contest 033 解説
 

En vedette (20)

Lec22
Lec22Lec22
Lec22
 
Lec21
Lec21Lec21
Lec21
 
Lec19
Lec19Lec19
Lec19
 
Lec23
Lec23Lec23
Lec23
 
I 7
I 7I 7
I 7
 
Lec4
Lec4Lec4
Lec4
 
Kinect krishna kumar-itkan
Kinect krishna kumar-itkanKinect krishna kumar-itkan
Kinect krishna kumar-itkan
 
Spectra-projects-Oct-2016
Spectra-projects-Oct-2016Spectra-projects-Oct-2016
Spectra-projects-Oct-2016
 
Akropolis
AkropolisAkropolis
Akropolis
 
Collective action under autocracies
Collective action under autocraciesCollective action under autocracies
Collective action under autocracies
 
Elearning rich media_search
Elearning rich media_searchElearning rich media_search
Elearning rich media_search
 
Hieu qua san xuat bap lai tren dat lua dbscl
Hieu qua san  xuat bap lai tren dat lua dbsclHieu qua san  xuat bap lai tren dat lua dbscl
Hieu qua san xuat bap lai tren dat lua dbscl
 
Perpres12 1961 tugas belajar
Perpres12 1961 tugas belajarPerpres12 1961 tugas belajar
Perpres12 1961 tugas belajar
 
Expeditie mont blanc
Expeditie mont blancExpeditie mont blanc
Expeditie mont blanc
 
Death2
Death2Death2
Death2
 
Top 9 unique structures
Top 9 unique structuresTop 9 unique structures
Top 9 unique structures
 
Greek islands !!
Greek islands !!Greek islands !!
Greek islands !!
 
Gronland
GronlandGronland
Gronland
 
Borging lokaal klimaatbeleid sos 3 2010 v1.0
Borging lokaal klimaatbeleid sos 3 2010 v1.0Borging lokaal klimaatbeleid sos 3 2010 v1.0
Borging lokaal klimaatbeleid sos 3 2010 v1.0
 
10강 기업교육론 20110504
10강 기업교육론 2011050410강 기업교육론 20110504
10강 기업교육론 20110504
 

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