SlideShare une entreprise Scribd logo
1  sur  11
Binary Heaps

 Delete-min
 Building
         a heap in O(n) time
 Heap Sort




                               1
Delete-min
 The  minimum element is the one at the top
  of the heap.
 We can delete this and move one of its
  children up to fill the space.
 Empty location moves down the tree.
 Might end up at any position on last level.
 Resulting tree would not be left filled.



                                            2
Delete-min in a Heap

                                8


                10                             11


          18              21         13              12


     43    23        26    29       31    19    17



               This is not a heap
                                                          3
Delete-min in a Heap (2)
 Replace       top element with last element
  of heap.
 Heapify(1)                     8


                 10                             11


           16              21         13              12


      43    23        26    29       31    19    17


                                                           4
Building a heap
 We start from the bottom and move up
 All leaves are heaps to begin with


                                         23


                           43                            26


                     10             21         13              31


                16    12        8    29       11    19    17
                                                                    5
Building a Heap: Analysis
 Correctness:   induction on i, all trees
  rooted at m > i are heaps
 Running time: n calls to Heapify = n O(lg
  n) = O(n lg n)
 We can provide a better O(n) bound.
   Intuition:
             for most of the time Heapify works
    on smaller than n element heaps



                                                  6
Building a Heap: Analysis (2)

 height of node: length of longest path from
  node to leaf
 height of tree: height of root
 time for Heapify(i) = O(height of subtree
  rooted at i)
 assume n = 2k – 1 (a complete binary
  tree)


                                            7
Building a heap: Analysis (3)
 For the n/2 nodes of height 1, heapify() requires
  at most 1 swap each.
 For the n/4 nodes of height 2, heapify() requires
  at most 2 swaps each.
 For the n/2i nodes of height i, heapify() requires
  at most i swaps each.
 So total number of swaps required is
                   n 1 n 1   n 1
      T ( n)   O           2     3 ... 1 k
                    2   4     8
                          lg n                lg n
                                 i                   i     1/ 2
               O    n 1               since                        2
                                                                       2
                          i 1    2i           i 1    2i   1 1/ 2
               O ( n)
                                                                           8
Building a Heap: Analysis (4)
   How? By using the following "trick"
                  i         1
              x                  if x             1 //differentiate
        i 0               1 x
                      i 1            1
              i x                            2
                                                  //multiply by x
        i 1                      1 x
                      i          x                             1
              i x                        2
                                                 //plug in x
        i 1                 1 x                                2
              i           1/ 2
                                     2
        i 1   2i          1/ 4
   Therefore Build-Heap time is O(n)
                                                                      9
Heap Sort
 Create  a heap.
 Do delete-min
                              8
  repeatedly till
  heap becomes
                       10                    11
  empty.
 To do an in place
                    12    21       13              17
  sort, we move
  deleted element
                  16 23 43 29     26    19    31
  to end of heap.
                                                    10
Running times of heap operations

 Insert:O(log n)
 Heapify: O(log n)
 Find minimum: O(1)
 Delete-min: O(log n)
 Building a heap: O(n)
 Heap Sort: O(nlog n)



                                   11

Contenu connexe

En vedette (20)

Lec22
Lec22Lec22
Lec22
 
Lec2
Lec2Lec2
Lec2
 
Lec19
Lec19Lec19
Lec19
 
Lec23
Lec23Lec23
Lec23
 
Lec1
Lec1Lec1
Lec1
 
I 7
I 7I 7
I 7
 
Lec4
Lec4Lec4
Lec4
 
Lec3
Lec3Lec3
Lec3
 
Marikas blogi
Marikas blogiMarikas blogi
Marikas blogi
 
Neechadi TechConsultants
Neechadi TechConsultantsNeechadi TechConsultants
Neechadi TechConsultants
 
81765 karaagopyan
81765 karaagopyan81765 karaagopyan
81765 karaagopyan
 
賽後心得分享
賽後心得分享賽後心得分享
賽後心得分享
 
one
oneone
one
 
Madrid `in tanitimi
Madrid `in tanitimiMadrid `in tanitimi
Madrid `in tanitimi
 
6강 기업교육론 20110406
6강 기업교육론 201104066강 기업교육론 20110406
6강 기업교육론 20110406
 
Mexico by john2
Mexico by john2  Mexico by john2
Mexico by john2
 
Microsoft Power Point Sig Presentation Final
Microsoft Power Point   Sig Presentation FinalMicrosoft Power Point   Sig Presentation Final
Microsoft Power Point Sig Presentation Final
 
Intro parcmarin amp2010-anglais
Intro parcmarin amp2010-anglaisIntro parcmarin amp2010-anglais
Intro parcmarin amp2010-anglais
 
Pay italie-lombardie
Pay italie-lombardiePay italie-lombardie
Pay italie-lombardie
 
10강 기업교육론 20110504
10강 기업교육론 2011050410강 기업교육론 20110504
10강 기업교육론 20110504
 

Similaire à Lec21

Algorithm chapter 6
Algorithm chapter 6Algorithm chapter 6
Algorithm chapter 6chidabdu
 
Lesson 21: Curve Sketching (Section 4 version)
Lesson 21: Curve Sketching (Section 4 version)Lesson 21: Curve Sketching (Section 4 version)
Lesson 21: Curve Sketching (Section 4 version)Matthew Leingang
 
Lesson 21: Curve Sketching (Section 10 version)
Lesson 21: Curve Sketching (Section 10 version)Lesson 21: Curve Sketching (Section 10 version)
Lesson 21: Curve Sketching (Section 10 version)Matthew Leingang
 
Chapter 12 - Heaps.ppt
Chapter 12 - Heaps.pptChapter 12 - Heaps.ppt
Chapter 12 - Heaps.pptMouDhara1
 

Similaire à Lec21 (7)

Algorithm chapter 6
Algorithm chapter 6Algorithm chapter 6
Algorithm chapter 6
 
Lesson 21: Curve Sketching (Section 4 version)
Lesson 21: Curve Sketching (Section 4 version)Lesson 21: Curve Sketching (Section 4 version)
Lesson 21: Curve Sketching (Section 4 version)
 
Heapsort
HeapsortHeapsort
Heapsort
 
Lesson 21: Curve Sketching (Section 10 version)
Lesson 21: Curve Sketching (Section 10 version)Lesson 21: Curve Sketching (Section 10 version)
Lesson 21: Curve Sketching (Section 10 version)
 
heaps2
heaps2heaps2
heaps2
 
Chapter 12 - Heaps.ppt
Chapter 12 - Heaps.pptChapter 12 - Heaps.ppt
Chapter 12 - Heaps.ppt
 
3.7 heap sort
3.7 heap sort3.7 heap sort
3.7 heap sort
 

Plus de Anjneya Varshney (12)

Lec16
Lec16Lec16
Lec16
 
Lec12
Lec12Lec12
Lec12
 
Lec11
Lec11Lec11
Lec11
 
Lec10
Lec10Lec10
Lec10
 
Lec9
Lec9Lec9
Lec9
 
Lec8
Lec8Lec8
Lec8
 
Lec7
Lec7Lec7
Lec7
 
Lec6
Lec6Lec6
Lec6
 
Lec5
Lec5Lec5
Lec5
 
Lec4
Lec4Lec4
Lec4
 
Lec3
Lec3Lec3
Lec3
 
Lec13
Lec13Lec13
Lec13
 

Lec21

  • 1. Binary Heaps  Delete-min  Building a heap in O(n) time  Heap Sort 1
  • 2. Delete-min  The minimum element is the one at the top of the heap.  We can delete this and move one of its children up to fill the space.  Empty location moves down the tree.  Might end up at any position on last level.  Resulting tree would not be left filled. 2
  • 3. Delete-min in a Heap 8 10 11 18 21 13 12 43 23 26 29 31 19 17 This is not a heap 3
  • 4. Delete-min in a Heap (2)  Replace top element with last element of heap.  Heapify(1) 8 10 11 16 21 13 12 43 23 26 29 31 19 17 4
  • 5. Building a heap  We start from the bottom and move up  All leaves are heaps to begin with 23 43 26 10 21 13 31 16 12 8 29 11 19 17 5
  • 6. Building a Heap: Analysis  Correctness: induction on i, all trees rooted at m > i are heaps  Running time: n calls to Heapify = n O(lg n) = O(n lg n)  We can provide a better O(n) bound.  Intuition: for most of the time Heapify works on smaller than n element heaps 6
  • 7. Building a Heap: Analysis (2)  height of node: length of longest path from node to leaf  height of tree: height of root  time for Heapify(i) = O(height of subtree rooted at i)  assume n = 2k – 1 (a complete binary tree) 7
  • 8. Building a heap: Analysis (3)  For the n/2 nodes of height 1, heapify() requires at most 1 swap each.  For the n/4 nodes of height 2, heapify() requires at most 2 swaps each.  For the n/2i nodes of height i, heapify() requires at most i swaps each.  So total number of swaps required is n 1 n 1 n 1 T ( n) O 2 3 ... 1 k 2 4 8 lg n lg n i i 1/ 2 O n 1 since 2 2 i 1 2i i 1 2i 1 1/ 2 O ( n) 8
  • 9. Building a Heap: Analysis (4)  How? By using the following "trick" i 1 x if x 1 //differentiate i 0 1 x i 1 1 i x 2 //multiply by x i 1 1 x i x 1 i x 2 //plug in x i 1 1 x 2 i 1/ 2 2 i 1 2i 1/ 4  Therefore Build-Heap time is O(n) 9
  • 10. Heap Sort  Create a heap.  Do delete-min 8 repeatedly till heap becomes 10 11 empty.  To do an in place 12 21 13 17 sort, we move deleted element 16 23 43 29 26 19 31 to end of heap. 10
  • 11. Running times of heap operations  Insert:O(log n)  Heapify: O(log n)  Find minimum: O(1)  Delete-min: O(log n)  Building a heap: O(n)  Heap Sort: O(nlog n) 11