Download Divide and Conquer: Iteration vs Recursion, Towers of Hanoi, and Recurrence Equations and more Slides Aeronautical Engineering in PDF only on Docsity! Recap • Iteration versus Recursion • Towers of Hanoi • Computed time taken to solve towers of Hanoi docsity.com Divide and Conquer • It is an algorithmic design paradigm that contains the following steps – Divide: Break the problem into smaller sub-problems – Recur: Solve each of the sub-problems recursively – Conquer: Combine the solutions of each of the sub-problems to form the solution of the problem Represent the solution using a recurrence equation docsity.com Towers of Hanoi T(n) = 2 T( n-1 ) +1 1 Given: T(1) = 1 N No.Moves 1 2 3 3 7 4 15 5 31 docsity.com Using Iteration T(n) = 2 T( n-1 ) +1 T(n) = 2 [ 2 T(n-2) + 1 ] +1 T(n) = 2 [ 2 [ 2 T(n-3) + 1 ]+ 1 ] +1 T(n) = 2 [ 2 [ 2 [ 2 T( n-4 ) + 1 ] + 1 ]+ 1 ] +1 T(n) = 24 T ( n-4 ) + 15 … T(n) = 2k T ( n-k ) + 2k - 1 Since n is finite, k → n. Therefore, lim T(n) k → n = 2n - 1 docsity.com Greatest Common Divisor Given two natural numbers a, b - If b = 0, then GCD := a - If b /= 0, then - c := a MOD b - a := b - b := c - GCD(a,b) docsity.com Exercise • Write 6 as an integer combination of 10 and 38 –Find GCD (38,10) –Express the GCD as a linear combination of 38 and 10 –Multiply that expression by (6/GCD) 6 = 3 (4*10 – 1 *38 ) = 12 * 10 – 3 * 38 docsity.com Multiplication • Standard method for multiplying long numbers: (1000a+b)x(1000c+d) = 1,000,000 ac + 1000 (ad + bc) + bd • Instead use: (1000a+b)x(1000c+d) = 1,000,000 ac + 1000 ((a+b)(c+d) – ac - bd) + bd One length-k multiply = 3 length-k/2 multiplies and a bunch of additions and shifting docsity.com [Logarithms – logb(x)] • A logarithm of base b for value y is the power to which b is raised to get y. – logby = x ↔ bx = y ↔ blogby = y – logb 1 = 0, logbb = 1 for all values of b docsity.com Recurrence Tree 1 node at depth-0 3 nodes at depth-1 9 nodes at depth-2 3lg n nodes at depth-lg n ... ... ... ... ... Recurrence Equation : T(n) < 3T(n/2) + c n c n c n/2 c n/2 c n/2 c n/4 c n/4 c n/4 c n/4 c n/4... c c c c c c c c c docsity.com Solving using Recurrence Tree T(n) < cn ( 1 + 3(1/2) + 9(1/4) + ... + 3lg n(1/ 2lg n)) < cn ( 1 + 3/2 + (3/2)2 + ... + (3/2)lg n ). < cn ( (3/2)(lg n + 1) – 1) / ((3/2)-1) < cn ( (3/2)lg n (3/2) – 1) / (1/2) < ((c n (3/2)lg n (3/2))/(1/2) )–2cn < c n (3/2)lg n –2cn . T(n) < 3 c n (3/2)lg n --approximation 3cn (nlg(3/2)) = 3c n1+lg(3/2) docsity.com Important Theorems Arithmetic Series For n≥ 1, 1 + 2 + … + n = n(n+1)/2 Geometric Series For a ≥ 1, ak + ak-1 + ... 1 = (ak+1 – 1) / (a-1) Logarithmic Behavior a lg b = b lg a docsity.com