Docsity

Divide and Conquer: Iteration vs Recursion, Towers of Hanoi, and Recurrence Equations, Slides of Aeronautical Engineering

Partial preview of the text

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 ofHanoi

Divide and Conquer • It is an algorithmic design paradigm that contains the following steps – Divide : Break the problem into smallersub-problems – Recur : Solve each of the sub-problemsrecursively – Conquer : Combine the solutions of each ofthe sub-problems to form the solution ofthe problem Represent the solution using a recurrence equation

Solving Recurrence Equations • Iteration • Recurrence Trees • Substitution • Master Method

Towers of Hanoi T(n) = 2 T( n-1 ) +^

Given: T(1) = 1^

N^ No.Moves^12 33 74 155 31 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)

[The MOD function] • Notation:^ m^ mod^ n^ = x • x = integer remainder when

m^ is divided by^ n = m^ -^ ⎣ m / n ⎦ n • Examples: – 8 mod 3^ = 2 – 42 mod 6^ = 0 – 5 mod 7^ = 5

Exercise • Write 6 as an integer combination of

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

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 abunch of additions and shifting

PRS - 1^2 • Given n, n log n,^ n,^ n(log n)

2 , for large n: 1. n^ has the largest value 2. n log n^ has the largest value^2 3. n has the largest value^2 4. n(log n) has the largest value

Relative size of^2 n, n log n,^ n,^ n(log n)

2

-^ ( n log n )/ n^ = log

n^ Æ^ ∞ n is more efficient than log n (^2) • n (log n )/^ n log n = log^ n^ Æ^ ∞ n log n is more efficient than^ n (log n

(^2 2) • n (log n )/^ n = (log (^2) n )/^ n^ Æ^0 (^2) n (log n )is more efficient than n

2

-^ Order of efficiency is^ n, n log n, n

(^2 2) (log n ) , n

Solving using Recurrence Tree T(n) < cn ( 1 + 3(1/2) + 9(1/4) + ... + 3

lg nlg n (1/ 2)) < cn ( 1 + 3/2 + (3/2)

2 lg n^ + ... + (3/2)). (lg n + 1)^ < cn ( (3/2) –

  1. / ((3/2)-1) lg n^ < cn ( (3/2)(3/2) – 1) / (1/2) lg n^ < ((c n (3/2)(3/2))/(1/2) )–2cn lg n^ < c n (3/2)–2cn. lg n^ T(n) < 3 c n (3/2)--approximationlg(3/2)1+lg(3/2) 3cn (n) = 3c n

Important Theorems Arithmetic SeriesFor n≥ 1, 1 + 2 + … + n = n(n+1)/2 Geometric Series k^ k-1^ For a ≥ 1, a+ a+ ... 1 = (a

k+1^ – 1) / (a-1) Logarithmic Behaviorlg b^ lg a a^ = b^