0% found this document useful (0 votes)
17 views3 pages

AAD Lec04

Algorithm Lecture 04

Uploaded by

tamjid
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views3 pages

AAD Lec04

Algorithm Lecture 04

Uploaded by

tamjid
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Algorithm analysis and design

Lecture-04

DIVIDE AND CONQUER ALGORITHM


In this approach, we solve a problem recursively by applying 3 steps

1. DIVIDE-break the problem into several sub problems of smaller size.


2. CONQUER-solve the problem recursively.
3. COMBINE-combine these solutions to create a solution to the original problem.

Computing time of DAndC is described by the recurrence relation

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.

Let a recurrence relation is expressed as


………………….(1)

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.

(10ma + b)(10mc + d) = 102mac + 10m(bc + ad) + bd


This recurrence immediately suggests the following divide-and-conquer algorithm to
multiply two n-digit numbers x and y. Each of the four sub-products ac, bc, ad, and bd is
computed recursively, but the multiplications in the last line are not recursive, because we
can multiply by a power of ten by shifting the digits to the left and filling in the correct
number of zeros, all in O(n) time.

Algorithm:

Correctness of this algorithm follows easily by induction. The running time for
this algorithm follows the recurrence

T(n) = 4T[n/2] + O(n)


The parameters are a = 4, b = 2, d = 1, so a > bd, hence T(n) = O(nlog 4) = O(n2)
2

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:

The running time of Karatsuba’s FastMultiply algorithm follows the recurrence


T(n) ≤ 3T [n/2] + O(n)
The parameters are a = 3, b = 2, d = 1, so a > bd, hence T(n) = O(nlog 3) = O(n1.59).
2

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

11 * 15 can in turn be multiplied using Karatsuba Algorithm

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy