lecture3.ppt - Introduction to Algorithms Lecture 3 Recap • • • • • Master Theorem Unbalanced recursion trees Divide-and-conquer paradigm

lecture3.ppt - Introduction to Algorithms Lecture 3 Recap...

This preview shows page 1 - 17 out of 62 pages.

Introduction to Algorithms Lecture 3
Image of page 1
2 Recap Master Theorem Unbalanced recursion trees Divide-and-conquer paradigm Strassen’s algorithm Polynomial multiplication
Image of page 2
3 Today Heaps and Heapsort Priority Queues Quicksort Probability Randomized Algorithms
Image of page 3
4 Heapsort Properties: O( n lg n ) – like merge sort, but unlike insertion Sorts in place – like insertion, but unlike merge Heapsort combines better attributes of these sorting algorithms
Image of page 4
5 Heapsort Data Structure: binary heap A Simple array Viewed as a nearly complete binary tree heap property : A [parent( x )] A [ x ] This defines a max-heap There are also min-heaps and k -ary heaps
Image of page 5
6 Heaps: Example Notice the implicit tree links: Children of node i are 2 i and 2 i +1 Why is this useful? Multiplication by 2 is a left shift in binary or add to self - fast 16 14 10 8 7 2 4 1 9 3 1 2 3 4 5 6 7 8 9 10
Image of page 6
7 Heaps: Extract-Max Heap-Extract-Max( A ) 1. //Removes and returns largest element of A 2. max A [1] 3. A [1] A [ n ] 4. n n 1 5. Heapify( A ,1 ) //Remakes heap 6. return max Running time? (1) + Heapify time.
Image of page 7
8 Heaps: Heapify Heapify( A , i ): i is index into array A Binary trees rooted at Left( i ) and Right( i ) are heaps But, A [ i ] may be smaller than its children, thus violating the heap property. Heapify makes A a heap once more. How? Move A [ i ] down in heap until heap property is satisfied.
Image of page 8
9 Heaps: Heapify Heapify( A , i ) // n is total number of elements 1. //Left & Right subtrees of i are heaps 2. //Makes subtree rooted at i a heap 3. l Left( i ) // l =2 i 4. r Right( i ) // r =2 i +1 5. if l n and A [ l ] A [ i ] 6. then largest l 7. else largest i 8. if r n and A [ r ] A [ largest ] 9. then largest r 10. if largest i 11. then exchange A [ i ] A [ largest ] 12. Heapify( A , largest )
Image of page 9
10 Heapsort – comments Correctness : induction on the height of i Analysis : time proportional to height of i = O (lg n ) For faster code, use a loop instead of recursion.
Image of page 10
11 Heaps: Heapify Example 1. Call Heapify( A , 2 ) 16 4 10 14 7 2 8 1 9 3 1 2 3 4 5 6 7 8 9 10 i
Image of page 11
12 Heaps: Heapify Example (cont.) 2. Exchange A [2] with A [4] and recursively call Heapify( A , 4 ) 16 14 10 4 7 2 8 1 9 3 1 2 3 4 5 6 7 8 9 10 i
Image of page 12
13 Heaps: Heapify Example (cont.) 3. Exchange A [4] with A [9] and recursively call Heapify( A , 9 ) 16 14 10 8 7 2 4 1 9 3 1 2 3 4 5 6 7 8 9 10 i
Image of page 13
14 Heaps: Heapify Example (cont.) 4. Node 9 has no children, so we are done. 16 14 10 8 7 2 4 1 9 3 1 2 3 4 5 6 7 8 9 10
Image of page 14
15 Heaps: Heapsort Heapsort( A ) Analysis 1. Build-Heap( A ) ?? 2. for i n downto 2 n times 3. do exchange A [1] A [ i ] O (1) 4. n n –1 O (1) 5. Heapify( A ,1 ) O (lg n ) Total Running time : O ( n lg n ) + Build-Heap( A ) time
Image of page 15
16 Heaps: Building a heap Convert an array A [1.. n ] , where n = length[ A ] , into a heap. Notice that the elements in the subarray A [( n /2+1).. n ] are already 1 -element heaps to begin with.
Image of page 16
Image of page 17

You've reached the end of your free preview.

Want to read all 62 pages?

  • Fall '05
  • RudolfFleischer

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern

Stuck? We have tutors online 24/7 who can help you get unstuck.
A+ icon
Ask Expert Tutors You can ask You can ask ( soon) You can ask (will expire )
Answers in as fast as 15 minutes
A+ icon
Ask Expert Tutors