SlideShare une entreprise Scribd logo
1  sur  5
Télécharger pour lire hors ligne
3/7/13                                                                      Heapify


  Chapter 6 - Heapify                                                                 Search C455

                                                  Document last modified: 02/05/2013 02:50:45

  Max-heap property

         The max-heap property:

               for every node i other than the root

                    A[ Parent( i ) ] ≥ A[ i ]

             Note that the root is excluded as it has no parents.

         The max-heap property means that the parent value ≥ child value




  Max-Heapify

         Max-Heapify function, given a tree that is a heap except for node i, arranges node i and it's subtrees to satisfy the heap
         property.

         Max-Heapify maintains max-heap property on an array as data is updated.

         Helper function definitions used by Max-Heapify

               Parent( i ) return ⌊i/2⌋

               Left( i ) return 2i

               Right( i ) return 2i + 1

             For simplicity, assume an array size large enough for
             a full binary tree with empty leaves filled with -∞.



         Pre and post conditions of Max-Heapify in ESCJava




          Max-Heapify (A, i)

          //@ pre    A != null && i >= 1 && i < A.length &&
                     (forall int j;
                         j > i && j <= A.length/2;
                         A[ j ] >= A[ left( j ) ] && A[ j ] >= A[ right( j ) ]);

          //@ post (forall int j;
                       j >= old( i ) && j <= A.length/2;
                      A[ j ] >= A[ left( j ) ] && A[ j ] >= A[ right( j ) ]);

             Precondition says all subtrees of i are max heaps, tree rooted at i not included

             Postcondition says that the i subtree is a max heap.

                    Note that leaves are in array indices greater than A.length/2



homepages.ius.edu/RWISMAN/C455/html/notes/Chapter6/heapify.htm                                                                        1/5
3/7/13                                                                 Heapify
         Question 6.4.1

             Is the tree at right a max-heap?

             Is Max-Heapify( A, 2) precondition met? Explain.




  Max-Heapify (A, i)
  -- alters A, preserves i
  -- pre: i's Left and Right subtrees in A satisfy heap property
  -- post: the tree rooted at location i in A satisfy heap property
  1      l ← Left (i)
  2      r ← Right (i)
  3      if l ≤ A.heap-size and A[l] > A[i]
  4        then largest ← l
  5        else largest ← i
  6      if r ≤ A.heap-size and A[r] > A[largest]
  7        then largest ← r
  8      if largest ≠ i then
  9        exchange A[i] ↔ A[largest]
  10       Max-Heapify (A, largest)



  Running Heapify

         MAX-HEAPIFY operation:

             Find location of largest value of:

                  A[ i ], A[ Left( i )] and A[ Right( i ) ]

             If not A[ i ], max-heap property does not hold.

                  Exchange A[ i ] with the larger of the two children to preserve max-heap property.

             Continue this process of compare/exchange down the heap until subtree rooted at i is a max-heap.

                  At a leaf, the subtree rooted at the leaf is trivially a max-heap.

                                                                      Max-Heapify (A, 2)




homepages.ius.edu/RWISMAN/C455/html/notes/Chapter6/heapify.htm                                                  2/5
3/7/13                                                                      Heapify
       1     2   3    4    5 6 7 8 9 10                               1    2      3   4   5 6 7 8 9      10

       16    4   10   14   7 9 3 2 8 1                                16 14 10        4   7 9 3 2    8   1

             i        l    r                                                          i         l    r

            Exchange i with largest child                            Exchange i with largest child


   Max-Heapify (A, i)
   1       l ← Left (i)

   2       r ← Right (i)
   3       if l ≤ A.heap-size and A[l] > A[i]
   4         then largest ← l
   5         else largest ← i
   6       if r ≤ A.heap-size and A[r] > A[largest]
   7         then largest ← r
   8       if largest ≠ i then
   9         exchange A[i] ↔ A[largest]
   10        Max-Heapify (A, largest)




             1   2    3    4 5 6 7 8 9          10

             16 14 10 8 7 9 3 2             4   1

                                            i

            Terminate when index i = largest

   Question 6.4.2

            Is the tree at (c) a max-heap?

            Is Max-Heapify(A, 2) postcondition met? Explain.

            //@ post (forall int j;
                         j >= old( i ) && j <= A.length/2;
                        A[ j ] >= A[ left( j ) ] && A[ j ] >= A[ right( j ) ]);

            On the tree at right, execute Max-Heapify( A, 2 ).



   Example: Max-Heapify( A, 2 )

            (a) Node 2 violates the max-heap property.

                           for every node k other than the root
                                A[ Parent( k ) ] ≥ A[ k ]

            (b) Compare node 2 with its children, and then exchange with the larger of the two children.

            (c) Continue down the tree, exchanging until the value is properly placed at the root of a subtree that is a max-heap; in this case, at




homepages.ius.edu/RWISMAN/C455/html/notes/Chapter6/heapify.htm                                                                            3/5
3/7/13                                                                    Heapify


  Max-Heapify (A, i)
  1      l ← Left (i)
  2      r ← Right (i)
  3      if l ≤ A.heap-size and A[l] > A[i]
  4        then largest ← l
  5        else largest ← i
  6      if r ≤ A.heap-size and A[r] > A[largest]
  7        then largest ← r
  8      if largest ≠ i then
  9        exchange A[i] ↔ A[largest]
  10       Max-Heapify (A, largest)



  Analysis of Heapify Running Time

                   | O(1)                        if n = 1
           T(n) = |
                   | 2T(n/2) + D(n) + C(n)     if n > 1

         The standard Divide-and-Conquer algorithm usually works as follows:

           1. spend some time handling the base case

           2. if not at the base case, divide the problem up into smaller subproblems: D(n)

           3. make recursive calls to handle all the subproblems created on step 2: T(n/b)

           4. combine results created by recursive calls on step 3: C(n)

         Max-Heapify is a degenerate (meaning not standard) version of a Divide-and-Conquer algorithm

               Multiple base cases, appearing in line 3, 6 and 8, constant time for any base case is Θ(1):
                    3    if l > A.heap-size
                                and
                    6    if r > A.heap-size

                               then Max-Heapify has reached the base case, i.e., Max-Heapify has reached a node in the
                               tree with empty left and right subtrees.

                    8    if largest = i

                               then Max-Heapify has reached the base case, i.e., Max-Heapify has reached a node in the
                               tree with empty left and right subtrees or the parent is larger than either child.

               There is no combining part to this algorithm, i.e., no work is done after the recursive call returns since solution
               occurs in place.

               Applying recurrence analysis:

                    T(n) = a * T(n/b) + f(n)

                         a = number of subproblems
                         n/b = size of the subproblems
                         f(n) = D(n) + C(n)

                    For Max-Heapify
                         a = 1, i.e., there is only one recursive call made


                         n/b = (2/3) * n, is the worst case size of the one
                         subproblem

                               The worst case is when the last level of the tree is
                               half full.

                               That's when the left subtree has one entire full level
                               as compared to the right subtree.

                               In this worst case, Max-Heapify has to "float down" through this left subtree.

                               The size of the left subtree is (2/3) * n, see table for numeric analysis.

homepages.ius.edu/RWISMAN/C455/html/notes/Chapter6/heapify.htm                                                                       4/5
3/7/13                                                                  Heapify



                         f(n) = Θ(1), all the work done in lines 1 - 9 takes a constant time

                         T(n) = T(2n/3) + Θ(1)

                    Applying Master Method

                         a=1
                         b = 3/2
                         f(n) = 1
                         nlogba = nlog3/21 = n0 = 1

                         Case 2 applies since f(n) = Θ(nlogba) = Θ(1)

                         T(n) = Θ(lg n)

                    Since the height h = ⌊lg n⌋, we can also say that Max-Heapify is Θ(h)

   Max-Heapify (A, i)
   1     l ← Left (i)
   2     r ← Right (i)
   3     if l ≤ A.heap-size and A[l] > A[i]
   4       then largest ← l

   5       else largest ← i
   6     if r ≤ A.heap-size and A[r] > A[largest]
   7       then largest ← r
   8     if largest ≠ i then
   9       exchange A[i] ↔ A[largest]
   10      Max-Heapify (A, largest)

  Question 6.4.3 - Give an algorithm to determine if an array is a heap.

         What is the recurrence equation?

         Determine the run time?

         Does this make sense?




homepages.ius.edu/RWISMAN/C455/html/notes/Chapter6/heapify.htm                                 5/5

Contenu connexe

Tendances

Heap Sort in Design and Analysis of algorithms
Heap Sort in Design and Analysis of algorithmsHeap Sort in Design and Analysis of algorithms
Heap Sort in Design and Analysis of algorithmssamairaakram
 
05 Analysis of Algorithms: Heap and Quick Sort - Corrected
05 Analysis of Algorithms: Heap and Quick Sort - Corrected05 Analysis of Algorithms: Heap and Quick Sort - Corrected
05 Analysis of Algorithms: Heap and Quick Sort - CorrectedAndres Mendez-Vazquez
 
Heapsort
HeapsortHeapsort
Heapsortmmoylan
 
Presentation on Heap Sort
Presentation on Heap Sort Presentation on Heap Sort
Presentation on Heap Sort Amit Kundu
 
Data visualization using case study
Data visualization using case studyData visualization using case study
Data visualization using case studyRupak Roy
 
Grouping & Summarizing Data in R
Grouping & Summarizing Data in RGrouping & Summarizing Data in R
Grouping & Summarizing Data in RJeffrey Breen
 

Tendances (12)

Heapsort ppt
Heapsort pptHeapsort ppt
Heapsort ppt
 
Heap and heapsort
Heap and heapsortHeap and heapsort
Heap and heapsort
 
3.7 heap sort
3.7 heap sort3.7 heap sort
3.7 heap sort
 
Heaptree
HeaptreeHeaptree
Heaptree
 
Heapsort
HeapsortHeapsort
Heapsort
 
Heap Sort in Design and Analysis of algorithms
Heap Sort in Design and Analysis of algorithmsHeap Sort in Design and Analysis of algorithms
Heap Sort in Design and Analysis of algorithms
 
05 Analysis of Algorithms: Heap and Quick Sort - Corrected
05 Analysis of Algorithms: Heap and Quick Sort - Corrected05 Analysis of Algorithms: Heap and Quick Sort - Corrected
05 Analysis of Algorithms: Heap and Quick Sort - Corrected
 
Heapsort
HeapsortHeapsort
Heapsort
 
Presentation on Heap Sort
Presentation on Heap Sort Presentation on Heap Sort
Presentation on Heap Sort
 
Data visualization using case study
Data visualization using case studyData visualization using case study
Data visualization using case study
 
Day 8b examples
Day 8b examplesDay 8b examples
Day 8b examples
 
Grouping & Summarizing Data in R
Grouping & Summarizing Data in RGrouping & Summarizing Data in R
Grouping & Summarizing Data in R
 

Similaire à Heapify

Algorithm chapter 6
Algorithm chapter 6Algorithm chapter 6
Algorithm chapter 6chidabdu
 
CS-102 BT_24_3_14 Binary Tree Lectures.pdf
CS-102 BT_24_3_14 Binary Tree Lectures.pdfCS-102 BT_24_3_14 Binary Tree Lectures.pdf
CS-102 BT_24_3_14 Binary Tree Lectures.pdfssuser034ce1
 
lecture 5
lecture 5lecture 5
lecture 5sajinsc
 
Lecture 07 - HeapSort.pptx
Lecture 07 - HeapSort.pptxLecture 07 - HeapSort.pptx
Lecture 07 - HeapSort.pptxIsrar63
 
Heap Sort (project).ppt
Heap Sort (project).pptHeap Sort (project).ppt
Heap Sort (project).pptSudhaPatel11
 
Heap Sort (project).ppt
Heap Sort (project).pptHeap Sort (project).ppt
Heap Sort (project).pptAmitShou
 
Algorithm Design and Complexity - Course 4 - Heaps and Dynamic Progamming
Algorithm Design and Complexity - Course 4 - Heaps and Dynamic ProgammingAlgorithm Design and Complexity - Course 4 - Heaps and Dynamic Progamming
Algorithm Design and Complexity - Course 4 - Heaps and Dynamic ProgammingTraian Rebedea
 
heap sort in the design anad analysis of algorithms
heap sort in the design anad analysis of algorithmsheap sort in the design anad analysis of algorithms
heap sort in the design anad analysis of algorithmsssuser7319f8
 
Sienna 7 heaps
Sienna 7 heapsSienna 7 heaps
Sienna 7 heapschidabdu
 

Similaire à Heapify (20)

Algorithm chapter 6
Algorithm chapter 6Algorithm chapter 6
Algorithm chapter 6
 
Heaps
HeapsHeaps
Heaps
 
CS-102 BT_24_3_14 Binary Tree Lectures.pdf
CS-102 BT_24_3_14 Binary Tree Lectures.pdfCS-102 BT_24_3_14 Binary Tree Lectures.pdf
CS-102 BT_24_3_14 Binary Tree Lectures.pdf
 
lecture 5
lecture 5lecture 5
lecture 5
 
Chapter 10 ds
Chapter 10 dsChapter 10 ds
Chapter 10 ds
 
Cis435 week05
Cis435 week05Cis435 week05
Cis435 week05
 
Unit III Heaps.ppt
Unit III Heaps.pptUnit III Heaps.ppt
Unit III Heaps.ppt
 
Lecture 07 - HeapSort.pptx
Lecture 07 - HeapSort.pptxLecture 07 - HeapSort.pptx
Lecture 07 - HeapSort.pptx
 
Heapsort quick sort
Heapsort quick sortHeapsort quick sort
Heapsort quick sort
 
Heap Sort (project).ppt
Heap Sort (project).pptHeap Sort (project).ppt
Heap Sort (project).ppt
 
Heap Sort (project).ppt
Heap Sort (project).pptHeap Sort (project).ppt
Heap Sort (project).ppt
 
Heap Sort (project).ppt
Heap Sort (project).pptHeap Sort (project).ppt
Heap Sort (project).ppt
 
Algorithms - "heap sort"
Algorithms - "heap sort"Algorithms - "heap sort"
Algorithms - "heap sort"
 
Heap_Sort1.pptx
Heap_Sort1.pptxHeap_Sort1.pptx
Heap_Sort1.pptx
 
Algorithm Design and Complexity - Course 4 - Heaps and Dynamic Progamming
Algorithm Design and Complexity - Course 4 - Heaps and Dynamic ProgammingAlgorithm Design and Complexity - Course 4 - Heaps and Dynamic Progamming
Algorithm Design and Complexity - Course 4 - Heaps and Dynamic Progamming
 
Heap tree
Heap treeHeap tree
Heap tree
 
heap sort in the design anad analysis of algorithms
heap sort in the design anad analysis of algorithmsheap sort in the design anad analysis of algorithms
heap sort in the design anad analysis of algorithms
 
Leftlist Heap-1.pdf
Leftlist Heap-1.pdfLeftlist Heap-1.pdf
Leftlist Heap-1.pdf
 
Sienna 7 heaps
Sienna 7 heapsSienna 7 heaps
Sienna 7 heaps
 
heapsort_bydinesh
heapsort_bydineshheapsort_bydinesh
heapsort_bydinesh
 

Dernier

USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...Postal Advocate Inc.
 
Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)Mark Reed
 
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️9953056974 Low Rate Call Girls In Saket, Delhi NCR
 
Earth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatEarth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatYousafMalik24
 
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptxMULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptxAnupkumar Sharma
 
ANG SEKTOR NG agrikultura.pptx QUARTER 4
ANG SEKTOR NG agrikultura.pptx QUARTER 4ANG SEKTOR NG agrikultura.pptx QUARTER 4
ANG SEKTOR NG agrikultura.pptx QUARTER 4MiaBumagat1
 
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdfAMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdfphamnguyenenglishnb
 
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
 
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTiammrhaywood
 
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17Celine George
 
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxiammrhaywood
 
How to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERPHow to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERPCeline George
 
ENGLISH6-Q4-W3.pptxqurter our high choom
ENGLISH6-Q4-W3.pptxqurter our high choomENGLISH6-Q4-W3.pptxqurter our high choom
ENGLISH6-Q4-W3.pptxqurter our high choomnelietumpap1
 
Roles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in PharmacovigilanceRoles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in PharmacovigilanceSamikshaHamane
 
Full Stack Web Development Course for Beginners
Full Stack Web Development Course  for BeginnersFull Stack Web Development Course  for Beginners
Full Stack Web Development Course for BeginnersSabitha Banu
 
Q4 English4 Week3 PPT Melcnmg-based.pptx
Q4 English4 Week3 PPT Melcnmg-based.pptxQ4 English4 Week3 PPT Melcnmg-based.pptx
Q4 English4 Week3 PPT Melcnmg-based.pptxnelietumpap1
 
Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17Celine George
 

Dernier (20)

USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
 
Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)
 
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
 
Earth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatEarth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice great
 
LEFT_ON_C'N_ PRELIMS_EL_DORADO_2024.pptx
LEFT_ON_C'N_ PRELIMS_EL_DORADO_2024.pptxLEFT_ON_C'N_ PRELIMS_EL_DORADO_2024.pptx
LEFT_ON_C'N_ PRELIMS_EL_DORADO_2024.pptx
 
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptxMULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
 
YOUVE GOT EMAIL_FINALS_EL_DORADO_2024.pptx
YOUVE GOT EMAIL_FINALS_EL_DORADO_2024.pptxYOUVE GOT EMAIL_FINALS_EL_DORADO_2024.pptx
YOUVE GOT EMAIL_FINALS_EL_DORADO_2024.pptx
 
YOUVE_GOT_EMAIL_PRELIMS_EL_DORADO_2024.pptx
YOUVE_GOT_EMAIL_PRELIMS_EL_DORADO_2024.pptxYOUVE_GOT_EMAIL_PRELIMS_EL_DORADO_2024.pptx
YOUVE_GOT_EMAIL_PRELIMS_EL_DORADO_2024.pptx
 
ANG SEKTOR NG agrikultura.pptx QUARTER 4
ANG SEKTOR NG agrikultura.pptx QUARTER 4ANG SEKTOR NG agrikultura.pptx QUARTER 4
ANG SEKTOR NG agrikultura.pptx QUARTER 4
 
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdfAMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
 
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
 
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
 
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
 
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
 
How to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERPHow to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERP
 
ENGLISH6-Q4-W3.pptxqurter our high choom
ENGLISH6-Q4-W3.pptxqurter our high choomENGLISH6-Q4-W3.pptxqurter our high choom
ENGLISH6-Q4-W3.pptxqurter our high choom
 
Roles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in PharmacovigilanceRoles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in Pharmacovigilance
 
Full Stack Web Development Course for Beginners
Full Stack Web Development Course  for BeginnersFull Stack Web Development Course  for Beginners
Full Stack Web Development Course for Beginners
 
Q4 English4 Week3 PPT Melcnmg-based.pptx
Q4 English4 Week3 PPT Melcnmg-based.pptxQ4 English4 Week3 PPT Melcnmg-based.pptx
Q4 English4 Week3 PPT Melcnmg-based.pptx
 
Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17
 

Heapify

  • 1. 3/7/13 Heapify Chapter 6 - Heapify Search C455 Document last modified: 02/05/2013 02:50:45 Max-heap property The max-heap property: for every node i other than the root A[ Parent( i ) ] ≥ A[ i ] Note that the root is excluded as it has no parents. The max-heap property means that the parent value ≥ child value Max-Heapify Max-Heapify function, given a tree that is a heap except for node i, arranges node i and it's subtrees to satisfy the heap property. Max-Heapify maintains max-heap property on an array as data is updated. Helper function definitions used by Max-Heapify Parent( i ) return ⌊i/2⌋ Left( i ) return 2i Right( i ) return 2i + 1 For simplicity, assume an array size large enough for a full binary tree with empty leaves filled with -∞. Pre and post conditions of Max-Heapify in ESCJava Max-Heapify (A, i) //@ pre A != null && i >= 1 && i < A.length && (forall int j; j > i && j <= A.length/2; A[ j ] >= A[ left( j ) ] && A[ j ] >= A[ right( j ) ]); //@ post (forall int j; j >= old( i ) && j <= A.length/2; A[ j ] >= A[ left( j ) ] && A[ j ] >= A[ right( j ) ]); Precondition says all subtrees of i are max heaps, tree rooted at i not included Postcondition says that the i subtree is a max heap. Note that leaves are in array indices greater than A.length/2 homepages.ius.edu/RWISMAN/C455/html/notes/Chapter6/heapify.htm 1/5
  • 2. 3/7/13 Heapify Question 6.4.1 Is the tree at right a max-heap? Is Max-Heapify( A, 2) precondition met? Explain. Max-Heapify (A, i) -- alters A, preserves i -- pre: i's Left and Right subtrees in A satisfy heap property -- post: the tree rooted at location i in A satisfy heap property 1 l ← Left (i) 2 r ← Right (i) 3 if l ≤ A.heap-size and A[l] > A[i] 4 then largest ← l 5 else largest ← i 6 if r ≤ A.heap-size and A[r] > A[largest] 7 then largest ← r 8 if largest ≠ i then 9 exchange A[i] ↔ A[largest] 10 Max-Heapify (A, largest) Running Heapify MAX-HEAPIFY operation: Find location of largest value of: A[ i ], A[ Left( i )] and A[ Right( i ) ] If not A[ i ], max-heap property does not hold. Exchange A[ i ] with the larger of the two children to preserve max-heap property. Continue this process of compare/exchange down the heap until subtree rooted at i is a max-heap. At a leaf, the subtree rooted at the leaf is trivially a max-heap. Max-Heapify (A, 2) homepages.ius.edu/RWISMAN/C455/html/notes/Chapter6/heapify.htm 2/5
  • 3. 3/7/13 Heapify 1 2 3 4 5 6 7 8 9 10 1 2 3 4 5 6 7 8 9 10 16 4 10 14 7 9 3 2 8 1 16 14 10 4 7 9 3 2 8 1 i l r i l r Exchange i with largest child Exchange i with largest child Max-Heapify (A, i) 1 l ← Left (i) 2 r ← Right (i) 3 if l ≤ A.heap-size and A[l] > A[i] 4 then largest ← l 5 else largest ← i 6 if r ≤ A.heap-size and A[r] > A[largest] 7 then largest ← r 8 if largest ≠ i then 9 exchange A[i] ↔ A[largest] 10 Max-Heapify (A, largest) 1 2 3 4 5 6 7 8 9 10 16 14 10 8 7 9 3 2 4 1 i Terminate when index i = largest Question 6.4.2 Is the tree at (c) a max-heap? Is Max-Heapify(A, 2) postcondition met? Explain. //@ post (forall int j; j >= old( i ) && j <= A.length/2; A[ j ] >= A[ left( j ) ] && A[ j ] >= A[ right( j ) ]); On the tree at right, execute Max-Heapify( A, 2 ). Example: Max-Heapify( A, 2 ) (a) Node 2 violates the max-heap property. for every node k other than the root A[ Parent( k ) ] ≥ A[ k ] (b) Compare node 2 with its children, and then exchange with the larger of the two children. (c) Continue down the tree, exchanging until the value is properly placed at the root of a subtree that is a max-heap; in this case, at homepages.ius.edu/RWISMAN/C455/html/notes/Chapter6/heapify.htm 3/5
  • 4. 3/7/13 Heapify Max-Heapify (A, i) 1 l ← Left (i) 2 r ← Right (i) 3 if l ≤ A.heap-size and A[l] > A[i] 4 then largest ← l 5 else largest ← i 6 if r ≤ A.heap-size and A[r] > A[largest] 7 then largest ← r 8 if largest ≠ i then 9 exchange A[i] ↔ A[largest] 10 Max-Heapify (A, largest) Analysis of Heapify Running Time | O(1) if n = 1 T(n) = | | 2T(n/2) + D(n) + C(n) if n > 1 The standard Divide-and-Conquer algorithm usually works as follows: 1. spend some time handling the base case 2. if not at the base case, divide the problem up into smaller subproblems: D(n) 3. make recursive calls to handle all the subproblems created on step 2: T(n/b) 4. combine results created by recursive calls on step 3: C(n) Max-Heapify is a degenerate (meaning not standard) version of a Divide-and-Conquer algorithm Multiple base cases, appearing in line 3, 6 and 8, constant time for any base case is Θ(1): 3 if l > A.heap-size and 6 if r > A.heap-size then Max-Heapify has reached the base case, i.e., Max-Heapify has reached a node in the tree with empty left and right subtrees. 8 if largest = i then Max-Heapify has reached the base case, i.e., Max-Heapify has reached a node in the tree with empty left and right subtrees or the parent is larger than either child. There is no combining part to this algorithm, i.e., no work is done after the recursive call returns since solution occurs in place. Applying recurrence analysis: T(n) = a * T(n/b) + f(n) a = number of subproblems n/b = size of the subproblems f(n) = D(n) + C(n) For Max-Heapify a = 1, i.e., there is only one recursive call made n/b = (2/3) * n, is the worst case size of the one subproblem The worst case is when the last level of the tree is half full. That's when the left subtree has one entire full level as compared to the right subtree. In this worst case, Max-Heapify has to "float down" through this left subtree. The size of the left subtree is (2/3) * n, see table for numeric analysis. homepages.ius.edu/RWISMAN/C455/html/notes/Chapter6/heapify.htm 4/5
  • 5. 3/7/13 Heapify f(n) = Θ(1), all the work done in lines 1 - 9 takes a constant time T(n) = T(2n/3) + Θ(1) Applying Master Method a=1 b = 3/2 f(n) = 1 nlogba = nlog3/21 = n0 = 1 Case 2 applies since f(n) = Θ(nlogba) = Θ(1) T(n) = Θ(lg n) Since the height h = ⌊lg n⌋, we can also say that Max-Heapify is Θ(h) Max-Heapify (A, i) 1 l ← Left (i) 2 r ← Right (i) 3 if l ≤ A.heap-size and A[l] > A[i] 4 then largest ← l 5 else largest ← i 6 if r ≤ A.heap-size and A[r] > A[largest] 7 then largest ← r 8 if largest ≠ i then 9 exchange A[i] ↔ A[largest] 10 Max-Heapify (A, largest) Question 6.4.3 - Give an algorithm to determine if an array is a heap. What is the recurrence equation? Determine the run time? Does this make sense? homepages.ius.edu/RWISMAN/C455/html/notes/Chapter6/heapify.htm 5/5