AAD Lec04
AAD Lec04
Lecture-04
Where T(n) is the time for DAndCon any input of size n and g(n) is the time to compute the
answer directly for small inputs. The function f(n) is the time for dividing P and combining
the solutions to subproblems. For divide and-conquer-based algorithms that produce
subproblems of the same type as the original problem, it is very natural to first describe
such algorithms using recursion.
Where a and b are known constants. We assume that T(l) is known and n is a power of b (i.e.,
n = bk)
One of the methods for solving any such recurrence relation is called the substitution method.
This method repeatedly makes substitution for each occurrence of the function T in the right-
hand side until all such occurrences disappear.
Solve the recurrence relation (1) for the following choices of a, 6, and f(n) (c being a
constant):
(a) a = 1, b = 2, and f(n)= en
(b) a = 5, b = 4, and f(n)= en2
(c) a = 28, b = 3, and f(n) = en3
Fast Multiplication
Problem Given two numbers x and y in binary, we want to compute the product
x × y.
Algorithm:
Correctness of this algorithm follows easily by induction. The running time for
this algorithm follows the recurrence
Karatsuba observed that the middle coefficient bc+ad can be computed from the
other two coefficients ac and bd using only one more recursive multiplication,
via the following algebraic identity:
bc + ad=(a + b)(c + d) − ac − bd
This trick lets us replace the four recursive calls in the previous algorithm with
only three recursive calls, as shown below:
Algorithm:
Example
Consider the following multiplication: 47 x 78
x = 47
x = 4 * 10 + 7
x1 = 4
x2 = 7
y = 78
y = 7 * 10 + 8
y1 = 7
y2 = 8
a = x1 * y1 = 4 * 7 = 28
c = x2 * y2 = 7 * 8 = 56
b = (x1 + x2)(y1 + y2) - a - c = 11 * 15 - 28 - 56