Note03-1x2 - Divide and Conquer Strategy Algorithm design is more an art less so a science There are a few useful strategies but no guarantee to

Info iconThis preview shows pages 1–4. Sign up to view the full content.

View Full Document Right Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: 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 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 Master Theorem For DaC algorithms, the runtime function often satisfies: T ( n ) = O ( 1 ) if n ≤ n a T ( n / b ) + Θ( f ( n ) ) if n > n If n ≤ n ( n is a small constant ), we solve the problem directly without recursive calls. Since the input size is fixed ( bounded by n ), 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 > , then T ( n ) = Θ( n log b a ) . 2 If f ( n ) = Θ( n log b a ) , then T ( n ) = Θ( n log b a log n ) ....
View Full Document

This document was uploaded on 10/05/2010.

Page1 / 24

Note03-1x2 - Divide and Conquer Strategy Algorithm design is more an art less so a science There are a few useful strategies but no guarantee to

This preview shows document pages 1 - 4. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online