Chapter 2 Master Theorem Fall 2017.pdf

# Chapter 2 Master Theorem Fall 2017.pdf - Chapter 2 Master...

• 22

This preview shows pages 1–5. Sign up to view the full content.

Chapter 2 Master Theorem

This preview has intentionally blurred sections. Sign up to view the full version.

Divide and Conquer Recurrence Relations Let T(n) be the number of steps to solve a problem of size n We noticed that T(n) = a T( n/b ) + ࠵? (n d ) when an algorithm: divides a problem of size n into a subproblems of size n/b recursively solves the subproblems combines the answer to the subproblems in to solve the original problem takes ࠵? (n d ) steps to divide the problem and combine the results of the subproblems The running time then is T( n ) = a T( n/b ) + ࠵? (n d ) Example: multiplication of two n -bit numbers: divided the problem into a = 3 subproblems the size of the subproblems was n/b where b = 2 and combining the subproblems took time ࠵? (n d ) for d = 1 T(n) = 3 T( n/2 ) + ࠵? (n 1 )
Binary Search a d f h k l o r t u v z mid Input: An array of sorted numbers a[1…n], and key k Output: i, where a[i] = key if it exists Basic idea: Compare k to middle element of array if k is equal middle element return k if k is less than middle element, recurse on left 1/2 of array if k is greater than the middle element, recurse on right 1/2 of the array Searching for the letter o T ( n ) = T ( n / 2) + O (1) T ( n ) = the number of steps to find an item k in a sorted array of n items master method: a = 1, b = 2, d = 0 T ( n ) = O (log n ) in unit cost model Analysis using the unit cost model

This preview has intentionally blurred sections. Sign up to view the full version.

Merge Sort function mergesort( a[1 . . . n] ) Input: An array of numbers a[1...n] Output: A sorted version of this array if n > 1 : return merge(mergesort( a[1 . . . n/2 ] ), mergesort( a[ n/2 + 1 . . . n] )) else: return a 43 37 25 20 4 15 72 19 4 19 15 72 20 25 43 37 43 37 25 20 4 15 72 19
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### What students are saying

• 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.

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

• 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.

Dana University of Pennsylvania ‘17, Course Hero Intern

• 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.

Jill Tulane University ‘16, Course Hero Intern