{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

note03-1x2 - Divide and Conquer Strategy Algorithm design...

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

View Full Document Right Arrow Icon
Divide and Conquer Strategy Algorithm design is more an art, less so a science. There are a few useful strategies, but no guarantee to succeed. We will discuss: Divide and Conquer, Greedy, Dynamic Programming. For each of them, we will discuss a few examples, and try to identify common schemes. Divide and Conquer Divide the problem into smaller subproblems ( of the same type ). Solve each subproblem ( usually by recursive calls ). Combine the solutions of the subproblems into the solution of the original problem. c Xin He (University at Buffalo) CSE 431/531 Algorithm Analysis and Design 2 / 55 Merge Sort MergeSort Input: an array A [ 1 .. n ] Output: Sort A into increasing order. Use a recursive function MergeSort( A , p , r ) . It sorts A [ p .. r ] . In main program, we call MergeSort( A , 1 , n ) . c Xin He (University at Buffalo) CSE 431/531 Algorithm Analysis and Design 3 / 55
Image of page 1

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

View Full Document Right Arrow Icon
Merge Sort MergeSort( A , p , r ) 1: if ( p < r ) then 2: q = ( p + r ) / 2 3: MergeSort( A , p , q ) 4: MergeSort( A , q + 1 , r ) 5: Merge( A , p , q , r ) 6: else 7: do nothing 8: end if Divide A [ p .. r ] into two sub-arrays of equal size . Sort each sub-array by recursive call. Merge( A , p , q , r ) is a procedure that, assuming A [ p .. q ] and A [ q + 1 .. r ] are sorted, merge them into sorted A [ p .. r ] It can be done in Θ( k ) time where k = r - p is the number of elements to be sorted. c Xin He (University at Buffalo) CSE 431/531 Algorithm Analysis and Design 4 / 55 Analysis of MergeSort Let T ( n ) be the runtime function of MergeSort( A [ 1 .. n ] ). Then: T ( n ) = O ( 1 ) if n = 1 2 T ( n / 2 ) + Θ( n ) if n > 1 If n = 1 , MergeSort does nothing, hence O ( 1 ) time. Otherwise, we make 2 recursive calls. The input size of each is n / 2 . Hence the runtime 2 T ( n / 2 ) . Θ( n ) is the time needed by Merge( A , p , q , r ) and all other processing. c Xin He (University at Buffalo) CSE 431/531 Algorithm Analysis and Design 5 / 55
Image of page 2
Master Theorem For DaC algorithms, the runtime function often satisfies: T ( n ) = O ( 1 ) if n n 0 a T ( n / b ) + Θ( f ( n ) ) if n > n 0 If n n 0 ( n 0 is a small constant ), we solve the problem directly without recursive calls. Since the input size is fixed ( bounded by n 0 ), it takes O ( 1 ) time. We make a recursive calls. The input size of each is n / b . Hence the runtime T ( n / b ) . Θ( f ( n )) is the time needed by all other processing . T ( n ) =? c Xin He (University at Buffalo) CSE 431/531 Algorithm Analysis and Design 7 / 55 Master Theorem Master Theorem (Theorem 4.1, Cormen’s book.) 1 If f ( n ) = O ( n log b a - ) for some constant > 0 , then T ( n ) = Θ( n log b a ) . 2 If f ( n ) = Θ( n log b a ) , then T ( n ) = Θ( n log b a log n ) . 3 If f ( n ) = Ω( n log b a + ) for some constant > 0 , and af ( n / b ) cf ( n ) for some c < 1 for sufficiently large n , then T ( n ) = Θ( f ( n )) . Example: MergeSort We have a = 2 , b = 2 , hence log b a = log 2 2 = 1 . So f ( n ) = Θ( n 1 ) = Θ( n log b a ) . By statement (2), T ( n ) = Θ( n log 2 2 log n ) = Θ( n log n ) . c Xin He (University at Buffalo) CSE 431/531 Algorithm Analysis and Design 8 / 55
Image of page 3

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

View Full Document Right Arrow Icon
Binary Search Binary Search Input: Sorted array A [ 1 .. n ] and a number x Output: Find i such that A [ i ] = x , if no such i exists, output “no”.
Image of page 4
Image of page 5
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

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