SlideShare una empresa de Scribd logo
1 de 28
Analysis of Algorithms II The Recursive Case Except as otherwise noted, the content of this presentation is licensed under the Creative Commons Attribution 2.5 License.
Key Topics * Recurrence Relations * Solving Recurrence Relations * The Towers of Hanoi * Analyzing Recursive Subprograms
Recurrence Relations ,[object Object],[object Object],[object Object],A  recurrence relation  for a sequence a 1 , a 2 , a 3 , ...  is a formula that relates each term a  k  to certain of its predecessors a k-1 , a k-2 , ..., a  k-i , where  i  is a fixed integer and  k  is any integer greater than or equal to  i .  The initial conditions for such a recurrence relation specify the values of a 1 , a 2 , a 3 , ..., a i-1 .
Example 1 ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],Set of all bit strings of form 0 ----- K-1 bits without 11 Set of all bit strings of form 10 --- k-2 bits without 11
  A Recurrence Relation: 1) s 0  = 1   s 1  = 2 2) s  k  = s k-1  + s k-2 If we calculate this recurrence to s 10 , we get 144
Example 2 ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Proof: Let P(n) denote the following: if a 1 , a 2 , a 3 , ..., a n  is the sequence defined by:  A  0  = 1 A  k  = a k-1   + 2 then, a n  = 1 + 2n  for all n >= 1 Base Case : prove that P(1) is true. Using the definition of the recurrence relation, we have: a 1  = a 0  + 2 = 1 + 2 = 3. Now, we show that we get the same answer using the formula: a n  = 1 + 2n for n = 1. a n  = 1 + 2n = 1 + 2 = 3.  Therefore, the base case holds. Inductive Case:   The induction hypothesis is to assume P(k): a k  = 1 + 2k.  We then need to show that P(k+1) = a k+1  = 1 + 2(k + 1). a k+1  = a ((k+1)–1)  + 2   from the recurrence relation (this is a "given") a k+1  = a k  + 2 algebra a k+1  = 1 + 2k + 2   substitution from inductive hypothesis (a k  = 1 + 2k) a k+1  = 1 + 2(k + 1)   algebra
Example 3 Enumerating the first few values of this sequence yields: K2 = K1 + 1 = 0 + 1 = 1 K3 = K2 + 2 = 1 + 2 = 3 K4 = K3 + 3 = 3 + 3 = 6 K5 = K4 + 4 = 6 + 4 = 10 From the recurrence relation, we can see that this sequence may be given by the formula  (for all n >= 1): n-1 K n  = 0 + 1 + 2 + ... + (n – 1) =    i =  n(n – 1)/2 i=0
Example 4 The object of the  Towers of Hanoi  is to move all the disks from the first peg to the third peg while always following these rules: 1. Only one disk may be moved at a time (specifically, the top disk on any peg). 2. At no time can a larger disk be placed on a smaller one. Tower of Hanoi
The n – 1 disks on the auxiliary peg are moved to the ending peg, requiring an additional H n-1  moves.  This gives us the recurrence relation :  H n  = 2H n-1  + 1 The base case for this relations is H  0  = 0, since if there are no disks, there are no moves required to have “all” the disks on the third peg.  To determine a closed form formula where there is no H  n  on the right side of the equation, we will try repeated substitution: H n   = 2H n-1  + 1 = 2(2H n-2  + 1) + 1 = 2 2  * H n-2  + 2 + 1 = 2 2  (2H n-3  + 1) + 2 + 1 = 2 3  * H n-3  + 2 2   + 2 + 1 The pattern to this recurrence relation is: H n = (2 i  * H n-i ) + 2 i-1  + 2 i-2  + ... + 2 2   + 2 1  + 2 0 We can set n = i (this will require an inductive proof): H n = (2 n   * H 0 ) + 2 n-1  + 2 n-2  + ... + 2 2   + 2 1  + 1 Substituting H0 = 0, yields the formula: H n = 2 n-1  + 2 n-2  + ... + 2 2  + 2 1  + 1
Example 5 Find a closed form solution for the following recurrence relations.  For brevity, we omit the inductive proofs. a) Recurrence relation : a 0  = 2 a n  = 3a n-1 Solution :  a n  = 3a n-1 a n  = 3 (3a n-2 ) = 3 2  a n-2 a n  = 3 2  (3a n-3 ) = 3 3  a n-3 a n  = 3 i-1  (3a n-i ) = 3 i  a n-I Set i = n, yielding: a n  = 3 n  a 0 a n  = 2 * 3 n
b) Recurrence relation : a 0  = 1 a n  = 2a n-1  – 1 Solution : a n  = 2(2a n-2  – 1) – 1 = 2 2  a n-2  – 2 – 1 a n  = 2 2  (2a n-3  – 1) – 2 – 1 = 2 3  a n-3  – 2 2  – 2 – 1 a n  = 2 i  (a n-i ) – 2 i-1  – 2 i-2  – … – 2 0 Set i = n, yielding: a n  = 2 n  (a 0 ) – 2 n-1  – 2 n-2  – … – 2 0  = 2 n  – 2 n-1  – 2 n-2  – … – 2 0 a n  = 2 n  – (2 n  – 1)  (Note: 2 n-1  – 2 n-2  – … – 1 = 2 n  – 1) a n  = 1 c) Recurrence relation: a 0  = 5 a n  = na n-1 Solution : a n  = n(n-1)a n-2 a n  = n(n-1)(n-2)a n-3 a n  = n(n-1)(n-2)…(n-i+1)a n-I Set i = n, yielding: a n  = n(n-1)(n-2)…(1) a 0  = a 0  n!  a n  = 5n!
A  recurrence relation  expresses the running time of a recursive algorithm.  This includes how many recursive calls are generated at each level of the recursion, how much of the problem is solved by each recursive call, and how much work is done at each level.
Example 6 Consider the following function to compute factorials: int factorial(int n) { 1) if (n <= 1) 2)   return(1); else 3) return(n * factorial(n-)); } Recurrence relation: base case: T(1) = a induction: T(n) = b + T(n-1), for n > 1 Closed form formula: T(1) = a T(2) = b + T(1) = b + a T(3) = b + T(2) = b + (b + a) = a + 2b T(4) = b + T(3) = b + (a + 2b) = a + 3b T(n) = a + (n-1)b for all n >= 1
Proof: Let P(n) denote that the running time of  factorial  is T(n) = a + (n-1)b.  And, recall that the recurrence relation definition give us: T(1) = a, and T(n) = b + T(n-1) for n > 1. Base case : prove that P(1) is true T(1) = a + ((1 – 1) * b) = a + (0 * b) = a.  This equation holds since the base case of our inductive definition states that T(1) = a. Inductive case : inductive hypothesis is to assume P(k): T(k) = a + (k-1)b, and show P(k+1): T(k+1) = a + kb. We know from the recurrence relation definition that T(k+1) = b + T(k).  We use the inductive hypothesis to substitute for T(k).  This gives us: T(k+1) = b + T(k) = b + a + (k-1)b = b + a + kb - b = a + kb P(k+1) holds when P(k) holds, and P(1) holds, therefore P(n) is true for all n >= 1.
Example 7 Consider the following recursive selection sort algorithm: void SelectionSort(int A[], int i, int n) {   int j, small, temp; 1)  if (i < n) { 2)  small = i; 3)  for (j = i + 1, j <= n, j++) { 4)   if (A[j] < A[small])  small = j;  } 6)  temp = A[small]; 7)  A[small] = A[i]; 8)  A[i] = temp; 9)  SelectionSort(A, i + 1, n);   } }
The inductive definition of recursive SelectionSort is:   T(1) = a T(m) = T(m-1) + O(m)   To solve this recurrence relation, we first get rid of the big-Oh expression by substituting the definition of big-Oh: (f(n) = O(g(n)) if f(n) <= C * g(n)), so we can substitute C*m for O(m):   T(1) = a T(m) = T(m-1) + C*m   Now, we can either try repeated substitutions or just enumerate a few cases to look for a pattern.  Let’s try repeated substitution:  T(m) = T(m-1) + C*m = T(m-2) + 2Cm - C because T(m-1) = T(m-2) + C(m-1) = T(m-3) + 3Cm - 3C  because T(m-2) = T(m-3) + C(m-2) = T(m-4) + 4Cm - 6C  because T(m-3) = T(m-4) + C(m-3) = T(m-5) + 5Cm - 10C   because T(m-4) = T(m-5) + C(m-4) ... = T(m-j) + jCm - (j(j-1)/2)C
To get a closed form formula we let j = m – 1 T(m)  = T(1) + (m-1)Cm - ((m-1)(m-2)/2)C = a + m 2 C - Cm - (m 2 C - 3Cm + 2C)/2 = a + (2m 2 C - 2Cm - m 2 C + 3Cm - 2C)/2 = a + (m 2 C + Cm - 2C)/2 Closed form formula  T(m) = a + (m 2 C + Cm - 2C)/2
Example 8 MergeSort(L) 1)  if (length of L > 1) { 2) Split list into first half and second half 3) MergeSort(first half) 4) MergeSort(second half) 5) Merge first half and second half into sorted list }
To analyze the complexity of  MergeSort , we need to define a recurrence relation.  The base case is when we have a list of 1 element and only line 1 of the function is executed.  Thus, the base case is constant time, O(1). If the test of line 1 fails, we must execute lines 2-5.  The time spent in this function when the length of the list > 1 is the sum of the following: 1) O(1) for the test on line 1 2) O(n) for the split function call on line 2 3) T(n/2) for recursive call on line 3 4) T(n/2) for recursive call on line 4 5) O(n) for the merge function call on line 5 If we drop the O(1)'s and apply the summation rule, we get  2T(n/2) + O(n).   If we substitute constants in place of the big-Oh notation we obtain: T(1) = a T(n) = 2T(n/2) + bn To solve this recurrence relation, we will enumerate a few values.  We will stick to n's that are powers of two so things divide evenly: T(2)  = 2T(1) + 2b =  2a + 2b T(4) = 2T(2) + 4b = 2(2a + 2b) + 4b = 4a + 8b T(8) = 2T(4) + 8b = 2(4a + 8b) + 8b = 8a + 24b T(16)  = 2T(8) + 16b  = 2(8a + 24b) + 16b = 16a + 64b
There is obviously a pattern but it is not as easy to represent as the others have been.  Note the following relationships: value of n: 2 4 8 16 coefficient of b: 2 8 24 64 ratio: 1 2 3 4 So, it appears that the coefficient of b is n times another factor that grows by 1 each time n doubles.  The ratio is log 2  n because  log 2  2 = 1, log 2  4 = 2, log 2  8 = 3 , etc.  Our &quot;guess&quot; for the solution of this recurrence relation is  T(n) = an + bn log 2  n .  We could have used repeated substitution in which case we would have the following formula: T(n) = 2 i  T(n/2 i ) + ibn Now, if we let i = log 2  n, we end up with: n*T(1) + bn log 2  n  = an + bn log 2  n  (because 2 log2n  = n).
Theorem 1 (Master Theorem) :  Let  f  be an increasing function that satisfies the recurrence relation: f (n) = a  f (n/b) + cn d whenever n = b k , where k is a positive integer, a >= 1, b is an integer greater than 1, c is a positive real number, and d is a non-negative real number.  Then: O(n d ) if a < b d f (n)  =  O(n d  log n)  if a = b d O(n  log a )  if a > b d
Example 9 Problem : Use Theorem 1 above to find the big-Oh running time of MergeSort (from Example 9). Solution : In Example 9, we are given the recurrence relation for MergeSort as: T(n) = 2T(n/2) + xn where we have simply replaced the positive constant b in the original recurrence relation with the constant x, so that we do not confuse variable names below. Applying Theorem 1, we choose these constants:  a = 2, b = 2, c = x, and d = 1  and it becomes: f (n) = 2  f (n/2) + xn 1
Consider the three cases of the  Master Theorem , which are based on the relationship between a and bd.  Here are some examples to consider: 1.  a <  b d : f(n) = 2 f(n/2) + xn 2 .  In this example, 2 < 2 2 , and thus f(n) = O(n 2 ).  The xn 2  term is growing faster than the 2 f(n/2) and thus dominates.  2.  a =  b d : f(n) = 2 f(n/2) + xn 1 .  In this example, 2 = 2, and thus f(n) = O(n log n).  The two terms grow together. 3.  a >  b d : f(n) = 8 f(n/2) + xn 2 .  In this example, 8 < 2 2 , and thus f(n) = O(n log 8 ) = O(n 3 ).  The 8 f(n/2)  term is growing faster than the xn 2  and thus dominates.
Example 10 ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
We use these two points to perform the recurrence relation analysis below: From the base case, we have: T 0  = a From the recursive case, we have: T n  = 4T n-2 + b We apply repeated substitution: T  n  = 4T n-2  + b T  n  = 4(4T n-4  + b) + b = 4 2  T n-4  + 4b + b T  n  = 4 2  (4T n-6  + b) + 4b + b = 4 3  T n-6  + 4 2  b + 4b + b T  n  = 4 i  T n-2i  + 4 i-1  b + 4 i-2  b + … + 4 0  b Note that 4 k  = 2 2k , yielding: T n  = 2 2i  T n-2i  + 2 2 ( i-1 ) b + 2  2(i-2)  b + … + 2 0  b Set i = n/2, yielding: T n  = 2 n  T 0  + 2 n-2  b + 2 n-4  b + … + 2 0  b = 2 n  a + (2 n-2  + 2 n-4  + … + 2 0 ) b
We need to compute the closed form for  (2 n-2  + 2 n-4  + … + 2 0 ) = (2 n  – 1)/3 . Here is a brief digression on how we compute this closed form formula: Let  x = (2 n-2  + 2 n-4  + … + 2 0 ) So,  2 2  x = 4x = 2 2  (2 n-2  + 2 n-4  + …  + 2 0 ) = (2 n  + 2 n-2  + … + 2 2 ) 2 2  x – x = 3x = (2 n  + 2 n-2  + … + 2 2 ) – (2 n-2  + 2 n-4  + … + 2 0 ) 3x = 2 n  – 2 0  = 2 n  – 1 x = (2 n  – 1)/3 = (2 n-2  + 2 n-4  + … + 2 0 )
Base case:  Show P(0).T 0  =  2 0  a + ((2 0  – 1)/3) b = a + ((1 – 1)/3) b = a.   This is the initial condition. Inductive case:  Assume inductive hypothesis  P(n): T n  = 2 n  a + ((2 n  – 1)/3) b Show  P(n+2): T n+2  = 2 n+2  a + ((2 n+2  – 1)/3)   b   Note: only consider  even  values of n T n+2  = 4T n  + b by definition of the recurrence T n+2  = 4(2 n  a + ((2 n  – 1)/3) b) + b substituting T  n  with the inductive hypothesis T n+2  = 2 2  (2 n  a + ((2 n  – 1)/3) b) + b   algebra: 4 = 2 2   T n+2  = 2 n+2  a + ((2 n+2  – 2 2 )/3) b + b   algebra: multiply in the 2 2 T n+2  = 2 n+2  a + ((2 n+2  – 3 – 1)/3) b + b  algebra: -2 2  = -3 - 1 T n+2  = 2 n+2  a + ((2 n+2  – 1)/3) b – b + b algebra: (-3/3)b = -b T n+2  = 2 n+2  a + ((2 n+2  – 1)/3) b this is the desired result QED. We calculate the big-Oh complexity for the function, as follows: From the recurrence relation  T n  = 2 n  a + ((2 n  – 1)/3) b , we see that the algorithm has time complexity:  O(2 n  a) + O(2 n  b) = O(2 n ) + O(2 n ) = O(2 n ).

Más contenido relacionado

La actualidad más candente

A Proof of the Riemann Hypothesis
A Proof of the Riemann  HypothesisA Proof of the Riemann  Hypothesis
A Proof of the Riemann Hypothesisnikos mantzakouras
 
Recurrences
RecurrencesRecurrences
RecurrencesDEVTYPE
 
Solving recurrences
Solving recurrencesSolving recurrences
Solving recurrencesWaqas Akram
 
Recurrence relationclass 5
Recurrence relationclass 5Recurrence relationclass 5
Recurrence relationclass 5Kumar
 
Physical Chemistry Assignment Help
Physical Chemistry Assignment HelpPhysical Chemistry Assignment Help
Physical Chemistry Assignment HelpEdu Assignment Help
 
recurrence relations
 recurrence relations recurrence relations
recurrence relationsAnurag Cheela
 
recurence solutions
recurence solutionsrecurence solutions
recurence solutionssoumya8396
 
Local Volatility 1
Local Volatility 1Local Volatility 1
Local Volatility 1Ilya Gikhman
 
International Journal of Mathematics and Statistics Invention (IJMSI)
International Journal of Mathematics and Statistics Invention (IJMSI) International Journal of Mathematics and Statistics Invention (IJMSI)
International Journal of Mathematics and Statistics Invention (IJMSI) inventionjournals
 
Recurrence Relation
Recurrence RelationRecurrence Relation
Recurrence RelationPapu Kumar
 
Modeling with Recurrence Relations
Modeling with Recurrence RelationsModeling with Recurrence Relations
Modeling with Recurrence RelationsDevanshu Taneja
 
CMSC 56 | Lecture 8: Growth of Functions
CMSC 56 | Lecture 8: Growth of FunctionsCMSC 56 | Lecture 8: Growth of Functions
CMSC 56 | Lecture 8: Growth of Functionsallyn joy calcaben
 
11X1 T14 09 mathematical induction 2 (2010)
11X1 T14 09 mathematical induction 2 (2010)11X1 T14 09 mathematical induction 2 (2010)
11X1 T14 09 mathematical induction 2 (2010)Nigel Simmons
 
Mathematical Induction
Mathematical InductionMathematical Induction
Mathematical InductionEdelyn Cagas
 
RECURRENCE EQUATIONS & ANALYZING THEM
RECURRENCE EQUATIONS & ANALYZING THEMRECURRENCE EQUATIONS & ANALYZING THEM
RECURRENCE EQUATIONS & ANALYZING THEMAlpana Ingale
 
Dinive conquer algorithm
Dinive conquer algorithmDinive conquer algorithm
Dinive conquer algorithmMohd Arif
 

La actualidad más candente (20)

A Proof of the Riemann Hypothesis
A Proof of the Riemann  HypothesisA Proof of the Riemann  Hypothesis
A Proof of the Riemann Hypothesis
 
Recurrences
RecurrencesRecurrences
Recurrences
 
Recurrences
RecurrencesRecurrences
Recurrences
 
Solving recurrences
Solving recurrencesSolving recurrences
Solving recurrences
 
Recurrence relationclass 5
Recurrence relationclass 5Recurrence relationclass 5
Recurrence relationclass 5
 
Physical Chemistry Assignment Help
Physical Chemistry Assignment HelpPhysical Chemistry Assignment Help
Physical Chemistry Assignment Help
 
recurrence relations
 recurrence relations recurrence relations
recurrence relations
 
recurence solutions
recurence solutionsrecurence solutions
recurence solutions
 
Local Volatility 1
Local Volatility 1Local Volatility 1
Local Volatility 1
 
International Journal of Mathematics and Statistics Invention (IJMSI)
International Journal of Mathematics and Statistics Invention (IJMSI) International Journal of Mathematics and Statistics Invention (IJMSI)
International Journal of Mathematics and Statistics Invention (IJMSI)
 
Time complexity
Time complexityTime complexity
Time complexity
 
Recurrence Relation
Recurrence RelationRecurrence Relation
Recurrence Relation
 
Recurrences
RecurrencesRecurrences
Recurrences
 
Modeling with Recurrence Relations
Modeling with Recurrence RelationsModeling with Recurrence Relations
Modeling with Recurrence Relations
 
CMSC 56 | Lecture 8: Growth of Functions
CMSC 56 | Lecture 8: Growth of FunctionsCMSC 56 | Lecture 8: Growth of Functions
CMSC 56 | Lecture 8: Growth of Functions
 
11X1 T14 09 mathematical induction 2 (2010)
11X1 T14 09 mathematical induction 2 (2010)11X1 T14 09 mathematical induction 2 (2010)
11X1 T14 09 mathematical induction 2 (2010)
 
Mathematical Induction
Mathematical InductionMathematical Induction
Mathematical Induction
 
RECURRENCE EQUATIONS & ANALYZING THEM
RECURRENCE EQUATIONS & ANALYZING THEMRECURRENCE EQUATIONS & ANALYZING THEM
RECURRENCE EQUATIONS & ANALYZING THEM
 
G03201034038
G03201034038G03201034038
G03201034038
 
Dinive conquer algorithm
Dinive conquer algorithmDinive conquer algorithm
Dinive conquer algorithm
 

Destacado

03 mathematical anaylsis
03 mathematical anaylsis03 mathematical anaylsis
03 mathematical anaylsisHira Gul
 
Lec2 Algorth
Lec2 AlgorthLec2 Algorth
Lec2 Algorthhumanist3
 
Parallel Prefix Adders Presentation
Parallel Prefix Adders PresentationParallel Prefix Adders Presentation
Parallel Prefix Adders PresentationPeeyush Pashine
 
Design and Analysis of Algorithms
Design and Analysis of AlgorithmsDesign and Analysis of Algorithms
Design and Analysis of AlgorithmsSwapnil Agrawal
 
DESIGN AND ANALYSIS OF ALGORITHMS
DESIGN AND ANALYSIS OF ALGORITHMSDESIGN AND ANALYSIS OF ALGORITHMS
DESIGN AND ANALYSIS OF ALGORITHMSGayathri Gaayu
 
Design and Analysis of Algorithms
Design and Analysis of AlgorithmsDesign and Analysis of Algorithms
Design and Analysis of AlgorithmsArvind Krishnaa
 

Destacado (7)

SENZOS CV
SENZOS CVSENZOS CV
SENZOS CV
 
03 mathematical anaylsis
03 mathematical anaylsis03 mathematical anaylsis
03 mathematical anaylsis
 
Lec2 Algorth
Lec2 AlgorthLec2 Algorth
Lec2 Algorth
 
Parallel Prefix Adders Presentation
Parallel Prefix Adders PresentationParallel Prefix Adders Presentation
Parallel Prefix Adders Presentation
 
Design and Analysis of Algorithms
Design and Analysis of AlgorithmsDesign and Analysis of Algorithms
Design and Analysis of Algorithms
 
DESIGN AND ANALYSIS OF ALGORITHMS
DESIGN AND ANALYSIS OF ALGORITHMSDESIGN AND ANALYSIS OF ALGORITHMS
DESIGN AND ANALYSIS OF ALGORITHMS
 
Design and Analysis of Algorithms
Design and Analysis of AlgorithmsDesign and Analysis of Algorithms
Design and Analysis of Algorithms
 

Similar a Analysis Of Algorithms Ii

SMB_2012_HR_VAN_ST-last version
SMB_2012_HR_VAN_ST-last versionSMB_2012_HR_VAN_ST-last version
SMB_2012_HR_VAN_ST-last versionLilyana Vankova
 
T2311 - Ch 4_Part1.pptx
T2311 - Ch 4_Part1.pptxT2311 - Ch 4_Part1.pptx
T2311 - Ch 4_Part1.pptxGadaFarhan
 
Binomial Theorem, Recursion ,Tower of Honai, relations
Binomial Theorem, Recursion ,Tower of Honai, relationsBinomial Theorem, Recursion ,Tower of Honai, relations
Binomial Theorem, Recursion ,Tower of Honai, relationsAqeel Rafique
 
8.-DAA-LECTURE-8-RECURRENCES-AND-ITERATION-METHOD.pdf
8.-DAA-LECTURE-8-RECURRENCES-AND-ITERATION-METHOD.pdf8.-DAA-LECTURE-8-RECURRENCES-AND-ITERATION-METHOD.pdf
8.-DAA-LECTURE-8-RECURRENCES-AND-ITERATION-METHOD.pdfRishikeshJha33
 
Stochastic Processes Homework Help
Stochastic Processes Homework HelpStochastic Processes Homework Help
Stochastic Processes Homework HelpExcel Homework Help
 
Rosser's theorem
Rosser's theoremRosser's theorem
Rosser's theoremWathna
 
Solving recurrences
Solving recurrencesSolving recurrences
Solving recurrencesWaqas Akram
 
Matlab lab manual
Matlab lab manualMatlab lab manual
Matlab lab manualnmahi96
 
Copy of y16 02-2119divide-and-conquer
Copy of y16 02-2119divide-and-conquerCopy of y16 02-2119divide-and-conquer
Copy of y16 02-2119divide-and-conquerJoepang2015
 
Conjugate Gradient Methods
Conjugate Gradient MethodsConjugate Gradient Methods
Conjugate Gradient MethodsMTiti1
 
lecture 3
lecture 3lecture 3
lecture 3sajinsc
 
LINEAR RECURRENCE RELATIONS WITH CONSTANT COEFFICIENTS
 LINEAR RECURRENCE RELATIONS WITH CONSTANT COEFFICIENTS LINEAR RECURRENCE RELATIONS WITH CONSTANT COEFFICIENTS
LINEAR RECURRENCE RELATIONS WITH CONSTANT COEFFICIENTSAartiMajumdar1
 
Week 8 [compatibility mode]
Week 8 [compatibility mode]Week 8 [compatibility mode]
Week 8 [compatibility mode]Hazrul156
 

Similar a Analysis Of Algorithms Ii (20)

SMB_2012_HR_VAN_ST-last version
SMB_2012_HR_VAN_ST-last versionSMB_2012_HR_VAN_ST-last version
SMB_2012_HR_VAN_ST-last version
 
ch3.ppt
ch3.pptch3.ppt
ch3.ppt
 
3.pdf
3.pdf3.pdf
3.pdf
 
T2311 - Ch 4_Part1.pptx
T2311 - Ch 4_Part1.pptxT2311 - Ch 4_Part1.pptx
T2311 - Ch 4_Part1.pptx
 
Binomial Theorem, Recursion ,Tower of Honai, relations
Binomial Theorem, Recursion ,Tower of Honai, relationsBinomial Theorem, Recursion ,Tower of Honai, relations
Binomial Theorem, Recursion ,Tower of Honai, relations
 
8.-DAA-LECTURE-8-RECURRENCES-AND-ITERATION-METHOD.pdf
8.-DAA-LECTURE-8-RECURRENCES-AND-ITERATION-METHOD.pdf8.-DAA-LECTURE-8-RECURRENCES-AND-ITERATION-METHOD.pdf
8.-DAA-LECTURE-8-RECURRENCES-AND-ITERATION-METHOD.pdf
 
Stochastic Processes Homework Help
Stochastic Processes Homework HelpStochastic Processes Homework Help
Stochastic Processes Homework Help
 
Rosser's theorem
Rosser's theoremRosser's theorem
Rosser's theorem
 
Solving recurrences
Solving recurrencesSolving recurrences
Solving recurrences
 
Imc2017 day1-solutions
Imc2017 day1-solutionsImc2017 day1-solutions
Imc2017 day1-solutions
 
05_AJMS_332_21.pdf
05_AJMS_332_21.pdf05_AJMS_332_21.pdf
05_AJMS_332_21.pdf
 
kactl.pdf
kactl.pdfkactl.pdf
kactl.pdf
 
Matlab lab manual
Matlab lab manualMatlab lab manual
Matlab lab manual
 
Copy of y16 02-2119divide-and-conquer
Copy of y16 02-2119divide-and-conquerCopy of y16 02-2119divide-and-conquer
Copy of y16 02-2119divide-and-conquer
 
Computer science-formulas
Computer science-formulasComputer science-formulas
Computer science-formulas
 
Maths
MathsMaths
Maths
 
Conjugate Gradient Methods
Conjugate Gradient MethodsConjugate Gradient Methods
Conjugate Gradient Methods
 
lecture 3
lecture 3lecture 3
lecture 3
 
LINEAR RECURRENCE RELATIONS WITH CONSTANT COEFFICIENTS
 LINEAR RECURRENCE RELATIONS WITH CONSTANT COEFFICIENTS LINEAR RECURRENCE RELATIONS WITH CONSTANT COEFFICIENTS
LINEAR RECURRENCE RELATIONS WITH CONSTANT COEFFICIENTS
 
Week 8 [compatibility mode]
Week 8 [compatibility mode]Week 8 [compatibility mode]
Week 8 [compatibility mode]
 

Más de Sri Prasanna

Más de Sri Prasanna (20)

Qr codes para tech radar
Qr codes para tech radarQr codes para tech radar
Qr codes para tech radar
 
Qr codes para tech radar 2
Qr codes para tech radar 2Qr codes para tech radar 2
Qr codes para tech radar 2
 
Test
TestTest
Test
 
Test
TestTest
Test
 
assds
assdsassds
assds
 
assds
assdsassds
assds
 
asdsa
asdsaasdsa
asdsa
 
dsd
dsddsd
dsd
 
About stacks
About stacksAbout stacks
About stacks
 
About Stacks
About  StacksAbout  Stacks
About Stacks
 
About Stacks
About  StacksAbout  Stacks
About Stacks
 
About Stacks
About  StacksAbout  Stacks
About Stacks
 
About Stacks
About  StacksAbout  Stacks
About Stacks
 
About Stacks
About  StacksAbout  Stacks
About Stacks
 
About Stacks
About StacksAbout Stacks
About Stacks
 
About Stacks
About StacksAbout Stacks
About Stacks
 
Network and distributed systems
Network and distributed systemsNetwork and distributed systems
Network and distributed systems
 
Introduction & Parellelization on large scale clusters
Introduction & Parellelization on large scale clustersIntroduction & Parellelization on large scale clusters
Introduction & Parellelization on large scale clusters
 
Mapreduce: Theory and implementation
Mapreduce: Theory and implementationMapreduce: Theory and implementation
Mapreduce: Theory and implementation
 
Other distributed systems
Other distributed systemsOther distributed systems
Other distributed systems
 

Último

Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibitjbellavia9
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsMebane Rash
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsTechSoup
 
Unit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxUnit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxVishalSingh1417
 
Food safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfFood safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfSherif Taha
 
Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Jisc
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfAdmir Softic
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...Nguyen Thanh Tu Collection
 
SOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning PresentationSOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning Presentationcamerronhm
 
Dyslexia AI Workshop for Slideshare.pptx
Dyslexia AI Workshop for Slideshare.pptxDyslexia AI Workshop for Slideshare.pptx
Dyslexia AI Workshop for Slideshare.pptxcallscotland1987
 
Single or Multiple melodic lines structure
Single or Multiple melodic lines structureSingle or Multiple melodic lines structure
Single or Multiple melodic lines structuredhanjurrannsibayan2
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxheathfieldcps1
 
How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17Celine George
 
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfUGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfNirmal Dwivedi
 
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...pradhanghanshyam7136
 
Google Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptxGoogle Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptxDr. Sarita Anand
 
Vishram Singh - Textbook of Anatomy Upper Limb and Thorax.. Volume 1 (1).pdf
Vishram Singh - Textbook of Anatomy  Upper Limb and Thorax.. Volume 1 (1).pdfVishram Singh - Textbook of Anatomy  Upper Limb and Thorax.. Volume 1 (1).pdf
Vishram Singh - Textbook of Anatomy Upper Limb and Thorax.. Volume 1 (1).pdfssuserdda66b
 
FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024Elizabeth Walsh
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfagholdier
 

Último (20)

Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibit
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan Fellows
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The Basics
 
Unit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxUnit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptx
 
Food safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfFood safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdf
 
Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdf
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
 
Spatium Project Simulation student brief
Spatium Project Simulation student briefSpatium Project Simulation student brief
Spatium Project Simulation student brief
 
SOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning PresentationSOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning Presentation
 
Dyslexia AI Workshop for Slideshare.pptx
Dyslexia AI Workshop for Slideshare.pptxDyslexia AI Workshop for Slideshare.pptx
Dyslexia AI Workshop for Slideshare.pptx
 
Single or Multiple melodic lines structure
Single or Multiple melodic lines structureSingle or Multiple melodic lines structure
Single or Multiple melodic lines structure
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
 
How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17
 
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfUGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
 
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
 
Google Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptxGoogle Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptx
 
Vishram Singh - Textbook of Anatomy Upper Limb and Thorax.. Volume 1 (1).pdf
Vishram Singh - Textbook of Anatomy  Upper Limb and Thorax.. Volume 1 (1).pdfVishram Singh - Textbook of Anatomy  Upper Limb and Thorax.. Volume 1 (1).pdf
Vishram Singh - Textbook of Anatomy Upper Limb and Thorax.. Volume 1 (1).pdf
 
FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 

Analysis Of Algorithms Ii

  • 1. Analysis of Algorithms II The Recursive Case Except as otherwise noted, the content of this presentation is licensed under the Creative Commons Attribution 2.5 License.
  • 2. Key Topics * Recurrence Relations * Solving Recurrence Relations * The Towers of Hanoi * Analyzing Recursive Subprograms
  • 3.
  • 4.
  • 5. A Recurrence Relation: 1) s 0 = 1 s 1 = 2 2) s k = s k-1 + s k-2 If we calculate this recurrence to s 10 , we get 144
  • 6.
  • 7. Proof: Let P(n) denote the following: if a 1 , a 2 , a 3 , ..., a n is the sequence defined by: A 0 = 1 A k = a k-1 + 2 then, a n = 1 + 2n for all n >= 1 Base Case : prove that P(1) is true. Using the definition of the recurrence relation, we have: a 1 = a 0 + 2 = 1 + 2 = 3. Now, we show that we get the same answer using the formula: a n = 1 + 2n for n = 1. a n = 1 + 2n = 1 + 2 = 3. Therefore, the base case holds. Inductive Case: The induction hypothesis is to assume P(k): a k = 1 + 2k. We then need to show that P(k+1) = a k+1 = 1 + 2(k + 1). a k+1 = a ((k+1)–1) + 2 from the recurrence relation (this is a &quot;given&quot;) a k+1 = a k + 2 algebra a k+1 = 1 + 2k + 2 substitution from inductive hypothesis (a k = 1 + 2k) a k+1 = 1 + 2(k + 1) algebra
  • 8. Example 3 Enumerating the first few values of this sequence yields: K2 = K1 + 1 = 0 + 1 = 1 K3 = K2 + 2 = 1 + 2 = 3 K4 = K3 + 3 = 3 + 3 = 6 K5 = K4 + 4 = 6 + 4 = 10 From the recurrence relation, we can see that this sequence may be given by the formula (for all n >= 1): n-1 K n = 0 + 1 + 2 + ... + (n – 1) =  i = n(n – 1)/2 i=0
  • 9. Example 4 The object of the Towers of Hanoi is to move all the disks from the first peg to the third peg while always following these rules: 1. Only one disk may be moved at a time (specifically, the top disk on any peg). 2. At no time can a larger disk be placed on a smaller one. Tower of Hanoi
  • 10. The n – 1 disks on the auxiliary peg are moved to the ending peg, requiring an additional H n-1 moves. This gives us the recurrence relation : H n = 2H n-1 + 1 The base case for this relations is H 0 = 0, since if there are no disks, there are no moves required to have “all” the disks on the third peg. To determine a closed form formula where there is no H n on the right side of the equation, we will try repeated substitution: H n = 2H n-1 + 1 = 2(2H n-2 + 1) + 1 = 2 2 * H n-2 + 2 + 1 = 2 2 (2H n-3 + 1) + 2 + 1 = 2 3 * H n-3 + 2 2 + 2 + 1 The pattern to this recurrence relation is: H n = (2 i * H n-i ) + 2 i-1 + 2 i-2 + ... + 2 2 + 2 1 + 2 0 We can set n = i (this will require an inductive proof): H n = (2 n * H 0 ) + 2 n-1 + 2 n-2 + ... + 2 2 + 2 1 + 1 Substituting H0 = 0, yields the formula: H n = 2 n-1 + 2 n-2 + ... + 2 2 + 2 1 + 1
  • 11. Example 5 Find a closed form solution for the following recurrence relations. For brevity, we omit the inductive proofs. a) Recurrence relation : a 0 = 2 a n = 3a n-1 Solution : a n = 3a n-1 a n = 3 (3a n-2 ) = 3 2 a n-2 a n = 3 2 (3a n-3 ) = 3 3 a n-3 a n = 3 i-1 (3a n-i ) = 3 i a n-I Set i = n, yielding: a n = 3 n a 0 a n = 2 * 3 n
  • 12. b) Recurrence relation : a 0 = 1 a n = 2a n-1 – 1 Solution : a n = 2(2a n-2 – 1) – 1 = 2 2 a n-2 – 2 – 1 a n = 2 2 (2a n-3 – 1) – 2 – 1 = 2 3 a n-3 – 2 2 – 2 – 1 a n = 2 i (a n-i ) – 2 i-1 – 2 i-2 – … – 2 0 Set i = n, yielding: a n = 2 n (a 0 ) – 2 n-1 – 2 n-2 – … – 2 0 = 2 n – 2 n-1 – 2 n-2 – … – 2 0 a n = 2 n – (2 n – 1) (Note: 2 n-1 – 2 n-2 – … – 1 = 2 n – 1) a n = 1 c) Recurrence relation: a 0 = 5 a n = na n-1 Solution : a n = n(n-1)a n-2 a n = n(n-1)(n-2)a n-3 a n = n(n-1)(n-2)…(n-i+1)a n-I Set i = n, yielding: a n = n(n-1)(n-2)…(1) a 0 = a 0 n! a n = 5n!
  • 13. A recurrence relation expresses the running time of a recursive algorithm. This includes how many recursive calls are generated at each level of the recursion, how much of the problem is solved by each recursive call, and how much work is done at each level.
  • 14. Example 6 Consider the following function to compute factorials: int factorial(int n) { 1) if (n <= 1) 2) return(1); else 3) return(n * factorial(n-)); } Recurrence relation: base case: T(1) = a induction: T(n) = b + T(n-1), for n > 1 Closed form formula: T(1) = a T(2) = b + T(1) = b + a T(3) = b + T(2) = b + (b + a) = a + 2b T(4) = b + T(3) = b + (a + 2b) = a + 3b T(n) = a + (n-1)b for all n >= 1
  • 15. Proof: Let P(n) denote that the running time of factorial is T(n) = a + (n-1)b. And, recall that the recurrence relation definition give us: T(1) = a, and T(n) = b + T(n-1) for n > 1. Base case : prove that P(1) is true T(1) = a + ((1 – 1) * b) = a + (0 * b) = a. This equation holds since the base case of our inductive definition states that T(1) = a. Inductive case : inductive hypothesis is to assume P(k): T(k) = a + (k-1)b, and show P(k+1): T(k+1) = a + kb. We know from the recurrence relation definition that T(k+1) = b + T(k). We use the inductive hypothesis to substitute for T(k). This gives us: T(k+1) = b + T(k) = b + a + (k-1)b = b + a + kb - b = a + kb P(k+1) holds when P(k) holds, and P(1) holds, therefore P(n) is true for all n >= 1.
  • 16. Example 7 Consider the following recursive selection sort algorithm: void SelectionSort(int A[], int i, int n) { int j, small, temp; 1) if (i < n) { 2) small = i; 3) for (j = i + 1, j <= n, j++) { 4) if (A[j] < A[small]) small = j; } 6) temp = A[small]; 7) A[small] = A[i]; 8) A[i] = temp; 9) SelectionSort(A, i + 1, n); } }
  • 17. The inductive definition of recursive SelectionSort is: T(1) = a T(m) = T(m-1) + O(m) To solve this recurrence relation, we first get rid of the big-Oh expression by substituting the definition of big-Oh: (f(n) = O(g(n)) if f(n) <= C * g(n)), so we can substitute C*m for O(m): T(1) = a T(m) = T(m-1) + C*m Now, we can either try repeated substitutions or just enumerate a few cases to look for a pattern. Let’s try repeated substitution: T(m) = T(m-1) + C*m = T(m-2) + 2Cm - C because T(m-1) = T(m-2) + C(m-1) = T(m-3) + 3Cm - 3C because T(m-2) = T(m-3) + C(m-2) = T(m-4) + 4Cm - 6C because T(m-3) = T(m-4) + C(m-3) = T(m-5) + 5Cm - 10C because T(m-4) = T(m-5) + C(m-4) ... = T(m-j) + jCm - (j(j-1)/2)C
  • 18. To get a closed form formula we let j = m – 1 T(m) = T(1) + (m-1)Cm - ((m-1)(m-2)/2)C = a + m 2 C - Cm - (m 2 C - 3Cm + 2C)/2 = a + (2m 2 C - 2Cm - m 2 C + 3Cm - 2C)/2 = a + (m 2 C + Cm - 2C)/2 Closed form formula T(m) = a + (m 2 C + Cm - 2C)/2
  • 19. Example 8 MergeSort(L) 1) if (length of L > 1) { 2) Split list into first half and second half 3) MergeSort(first half) 4) MergeSort(second half) 5) Merge first half and second half into sorted list }
  • 20. To analyze the complexity of MergeSort , we need to define a recurrence relation. The base case is when we have a list of 1 element and only line 1 of the function is executed. Thus, the base case is constant time, O(1). If the test of line 1 fails, we must execute lines 2-5. The time spent in this function when the length of the list > 1 is the sum of the following: 1) O(1) for the test on line 1 2) O(n) for the split function call on line 2 3) T(n/2) for recursive call on line 3 4) T(n/2) for recursive call on line 4 5) O(n) for the merge function call on line 5 If we drop the O(1)'s and apply the summation rule, we get 2T(n/2) + O(n). If we substitute constants in place of the big-Oh notation we obtain: T(1) = a T(n) = 2T(n/2) + bn To solve this recurrence relation, we will enumerate a few values. We will stick to n's that are powers of two so things divide evenly: T(2) = 2T(1) + 2b = 2a + 2b T(4) = 2T(2) + 4b = 2(2a + 2b) + 4b = 4a + 8b T(8) = 2T(4) + 8b = 2(4a + 8b) + 8b = 8a + 24b T(16) = 2T(8) + 16b = 2(8a + 24b) + 16b = 16a + 64b
  • 21. There is obviously a pattern but it is not as easy to represent as the others have been. Note the following relationships: value of n: 2 4 8 16 coefficient of b: 2 8 24 64 ratio: 1 2 3 4 So, it appears that the coefficient of b is n times another factor that grows by 1 each time n doubles. The ratio is log 2 n because log 2 2 = 1, log 2 4 = 2, log 2 8 = 3 , etc. Our &quot;guess&quot; for the solution of this recurrence relation is T(n) = an + bn log 2 n . We could have used repeated substitution in which case we would have the following formula: T(n) = 2 i T(n/2 i ) + ibn Now, if we let i = log 2 n, we end up with: n*T(1) + bn log 2 n = an + bn log 2 n (because 2 log2n = n).
  • 22. Theorem 1 (Master Theorem) : Let f be an increasing function that satisfies the recurrence relation: f (n) = a f (n/b) + cn d whenever n = b k , where k is a positive integer, a >= 1, b is an integer greater than 1, c is a positive real number, and d is a non-negative real number. Then: O(n d ) if a < b d f (n) = O(n d log n) if a = b d O(n log a ) if a > b d
  • 23. Example 9 Problem : Use Theorem 1 above to find the big-Oh running time of MergeSort (from Example 9). Solution : In Example 9, we are given the recurrence relation for MergeSort as: T(n) = 2T(n/2) + xn where we have simply replaced the positive constant b in the original recurrence relation with the constant x, so that we do not confuse variable names below. Applying Theorem 1, we choose these constants: a = 2, b = 2, c = x, and d = 1 and it becomes: f (n) = 2 f (n/2) + xn 1
  • 24. Consider the three cases of the Master Theorem , which are based on the relationship between a and bd. Here are some examples to consider: 1. a < b d : f(n) = 2 f(n/2) + xn 2 . In this example, 2 < 2 2 , and thus f(n) = O(n 2 ). The xn 2 term is growing faster than the 2 f(n/2) and thus dominates. 2. a = b d : f(n) = 2 f(n/2) + xn 1 . In this example, 2 = 2, and thus f(n) = O(n log n). The two terms grow together. 3. a > b d : f(n) = 8 f(n/2) + xn 2 . In this example, 8 < 2 2 , and thus f(n) = O(n log 8 ) = O(n 3 ). The 8 f(n/2) term is growing faster than the xn 2 and thus dominates.
  • 25.
  • 26. We use these two points to perform the recurrence relation analysis below: From the base case, we have: T 0 = a From the recursive case, we have: T n = 4T n-2 + b We apply repeated substitution: T n = 4T n-2 + b T n = 4(4T n-4 + b) + b = 4 2 T n-4 + 4b + b T n = 4 2 (4T n-6 + b) + 4b + b = 4 3 T n-6 + 4 2 b + 4b + b T n = 4 i T n-2i + 4 i-1 b + 4 i-2 b + … + 4 0 b Note that 4 k = 2 2k , yielding: T n = 2 2i T n-2i + 2 2 ( i-1 ) b + 2 2(i-2) b + … + 2 0 b Set i = n/2, yielding: T n = 2 n T 0 + 2 n-2 b + 2 n-4 b + … + 2 0 b = 2 n a + (2 n-2 + 2 n-4 + … + 2 0 ) b
  • 27. We need to compute the closed form for (2 n-2 + 2 n-4 + … + 2 0 ) = (2 n – 1)/3 . Here is a brief digression on how we compute this closed form formula: Let x = (2 n-2 + 2 n-4 + … + 2 0 ) So, 2 2 x = 4x = 2 2 (2 n-2 + 2 n-4 + … + 2 0 ) = (2 n + 2 n-2 + … + 2 2 ) 2 2 x – x = 3x = (2 n + 2 n-2 + … + 2 2 ) – (2 n-2 + 2 n-4 + … + 2 0 ) 3x = 2 n – 2 0 = 2 n – 1 x = (2 n – 1)/3 = (2 n-2 + 2 n-4 + … + 2 0 )
  • 28. Base case: Show P(0).T 0 = 2 0 a + ((2 0 – 1)/3) b = a + ((1 – 1)/3) b = a. This is the initial condition. Inductive case: Assume inductive hypothesis P(n): T n = 2 n a + ((2 n – 1)/3) b Show P(n+2): T n+2 = 2 n+2 a + ((2 n+2 – 1)/3) b Note: only consider even values of n T n+2 = 4T n + b by definition of the recurrence T n+2 = 4(2 n a + ((2 n – 1)/3) b) + b substituting T n with the inductive hypothesis T n+2 = 2 2 (2 n a + ((2 n – 1)/3) b) + b algebra: 4 = 2 2 T n+2 = 2 n+2 a + ((2 n+2 – 2 2 )/3) b + b algebra: multiply in the 2 2 T n+2 = 2 n+2 a + ((2 n+2 – 3 – 1)/3) b + b algebra: -2 2 = -3 - 1 T n+2 = 2 n+2 a + ((2 n+2 – 1)/3) b – b + b algebra: (-3/3)b = -b T n+2 = 2 n+2 a + ((2 n+2 – 1)/3) b this is the desired result QED. We calculate the big-Oh complexity for the function, as follows: From the recurrence relation T n = 2 n a + ((2 n – 1)/3) b , we see that the algorithm has time complexity: O(2 n a) + O(2 n b) = O(2 n ) + O(2 n ) = O(2 n ).