0% found this document useful (0 votes)
5 views

Divide and Conquer

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

Divide and Conquer

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

Divide and Conquer /

Dynamic Programming
Presentation by
V. Balasubramanian
SSN College of Engineering
Agenda
• Towers of Hanoi
• Factorial (Recursive / Iterative)
• Fibonacci series
• Divide and Conquer:
– Strassen Multiplication,
– Closest-Pair and Convex-Hull Problems
• Dynamic Programming:
– Computing a Binomial Coefficient
Divide-and-Conquer
• The most-well known algorithm
design strategy:
– Divide instance of problem into two or
more smaller instances
– Solve smaller instances recursively
– Obtain solution to original (larger)
instance by combining these solutions
Master’s Theorem
T(n) = aT(n/b) + f (n) where f(n) 
(nd), d  0

Master Theorem:
If a < bd, T(n)  (nd)
If a = bd, T(n)  (nd log n)
If a > bd, T(n)  (nlog b a )
Integer Multiplication
Consider the problem of multiplying
two (large) n-digit integers
represented by arrays of their digits
such as:

A = 12345678901357986429 B=
87654321284820912836
Example
• High Precision computing
• PC 64 bit computation
• Weight of Neutrino 100 decimal
digits.
• Square root
• PI for million digits
• RSA primes with 1000’s of bit long.
Example
The grade-school algorithm:
a1 a2 … an
b1 b2 … bn
(d10) d11d12 … d1n
(d20) d21d22 … d2n
…………………
(dn0) dn1dn2 … dnn

Efficiency: n2 one-digit multiplications


Contd…
• If you multiply 2 1000 digit number,
then 1000,000 one digit
multiplication needs to be done.
• Cryptography
Analysis
• Conventional Multiplication reuires
n2 digit multiplications
Anatoly Karatsuba
• Russian mathematician
• 1960
• 23 years
• Karatsuba Multiplication
Divide and Conquer
• split an n-digit integer into two
integers of approximately n/2 bits
Example
• 965107x102635
• Normal multiplication = n2 digit
multiplication = 36
• For 1000 digit number = 1000,000
Contd…
U,V large integers
Analysis
Contd…
• U = XY = X *10 n/2 + Y
• V = WZ = W *10 n/2 + Z
• UV = XW 10n + YZ + (XZ+YW) 10n/2
Algorithm
Analysis
Analysis
• 1000 digit number no of operations
= 56000.
Further Faster algorithms
• Fast Fourier Transforms, Borodin and
Munro (1975) developed a
• 0(n(lg n)2) algorithm for multiplying
large integers.
Exercise
• What are the smallest and largest
numbers of digits the product of two
decimal n-digit integers can have?
Solution
• Smallest n digit number = 10(n-1
times) = 10n-1
• 10n-1* 10n-1 = 102n-2 =2n-1digits = 1
followed by 2n-2 zeros
• Largest n digit number = 9999(n
times) = 10n-1
• 10n-1*10n-1 = 102n-1 = 2n digits
a=a1a0, b=b1b0
Exercise
• Compute 2101 * 1130 by applying
the divide-and-conquer algorithm
outlined.
Solution
Contd…
Contd…
Strassen’s Multiplication
Idea
Levitin
Strassen
Example
Contd…
Exercise
Solution
Contd…
Contd…
Contd…
Solution
Analysis
Closest Pair Problem
• Complexity = n2
• For example, an air-traffic controller
might be interested in two closest
planes as the most probable collision
candidates.
• A regional postal service manager -
closest pair problem to find
candidate post-office locations to be
closed.
1D solution
Algorithm
• If n>3, we can divide the points into
two subsets Pl and Pr of n/2 and n/2
points, respectively, by drawing a
vertical line through the median m of
their x coordinates so that n/2 points
lie to the left of or on the line itself,
and n/2 points lie to the right of or
on the line. Then we can solve the
closest-pair problem
Algorithm
The algorithm:
• Input: A set S of n planar points.
• Output: The distance between two
closest points.
Steps
Step 1 Divide the points given into
two subsets S1 and S2 by a vertical
line x = c so that half the points lie
to the left or on the line and half
the points lie to the right or on the
line.
Contd…
Step 2
Step 2: Find recursively the closest
pairs for the left and right
subsets.
Step 3
• Step 3:Set d = min{d1, d2}
We can limit our attention to the points in the
symmetric vertical strip of width 2d as possible
closest pair. Let C1 and C2 be the subsets of points
in the left subset S1 and of the right subset S2,
respectively, that lie in this vertical strip. The points
in C1 and C2 are stored in increasing order of their
y coordinates, which is maintained by merging
during the execution of the next step.
Step 4
• Step 4:
For every point P(x,y) in C1, we
inspect points in C2 that may be closer
to P than d. There can be no more
than 6 such points (because d ≤ d2)!
Worst Case Scenario
Contd…
Step 1: Sort points in S according to
their y-values.
Step 2: If S contains only one point,
return infinity as its distance.
Step 3: Find a median line L
perpendicular to the X-axis to divide
S into SL and SR, with equal sizes.
Contd…
• Step 4: Recursively apply Steps 2
and 3 to solve the closest pair
problems of SL and SR. Let dL(dR)
denote the distance between the
closest pair in SL (SR). Let d =
min(dL, dR).
Contd…
• Step 5: For a point P in the half-slab
bounded by L-d and L, let its y-value
be denoted as yP . For each such P,
find all points in the half-slab
bounded by L and L+d whose y-
value fall within yP+d and yP-d. If
the distance d between P and a point
in the other half-slab is less than d,
let d=d. The final value of d is the
answer.
Contd…

 2T(n/2)+O(n)+O(n) ,n>1
T(n)= 
 1 ,n=1
Analysis
Running time of the algorithm is
described by
T(n) = 2T(n/2) + M(n), where
M(n)  O(n)
By the Master Theorem (with a = 2,
b = 2, d = 1)
T(n)  O(n log n)
Exercise
Solution
• O(n log n)
• Without employing Divide and
Conquer, we can sort it and do it.
O(n log n)
Exercise
Convex Hull
• A shape or set is convex if for any
two points that are part of the shape,
the whole connecting line segment is
also part of the shape.
What is Convex Hull?
• Let S be a set of points in the plane.
• Intuition: Imagine the points of S as
being pegs; the convex hull of S is
the shape of a rubber-band stretched
around the pegs.
• Formal definition: the convex hull
of S is the smallest convex polygon
that contains all the points of S.
Example
Contd…
Contd…
• • A polygon P is said to be convex
if:
• – P is non-intersecting; and
• – for any two points p and q on the
boundary of P, segment pq lies
entirely inside P
Convex

convex
Not Convex
Algorithm
Convex hull: smallest convex set that
includes given points
• Assume points are sorted by x-
coordinate values
• Identify extreme points P1 and P2
(leftmost and rightmost)
Algorithm
• Compute upper hull recursively:
– find point Pmax that is farthest away
from line P1P2
– compute the upper hull of the points to
the left of line P1Pmax
– compute the upper hull of the points to
the left of line PmaxP2
• Compute lower hull in a similar
manner
Pmax

P2

P1
Algorithm Analysis
• Finding point farthest away from line
P1P2 can be done in linear time
• Time efficiency:
– worst case: Θ(n2) (as quicksort)
– average case: Θ(n) (under reasonable
assumptions about distribution of points given)
• If points are not initially sorted by x-
coordinate value, this can be
accomplished in O(n log n) time
Contd…
• Several O(n log n) algorithms for
convex hull are known
Contd…
The divide-and-conquer
strategy
Merge Sequence
1. Select an interior point p.
2. There are 3 sequences of points
which have increasing polar angles
with respect to p.
(1) g, h, i, j, k
(2) a, b, c, d
(3) f, e
Merge Sequence
3. Merge these 3 sequences into 1
sequence:
g, h, a, b, f, c, e, d, i, j, k.
4. Apply Graham scan to examine the
points one by one and eliminate the
points which cause reflexive angles.
Divide-and-conquer for convex hull
• Input : A set S of planar points
• Output : A convex hull for S
Step 1: If S contains no more than five
points, use exhaustive searching to
find the convex hull and return.
Step 2: Find a median line
perpendicular to the X-axis which
divides S into SL and SR, with equal
sizes.
points b and f need to be
deleted
Divide-and-conquer for convex hull
• Step 3: Recursively construct convex
hulls for SL and SR, denoted as
Hull(SL) and Hull(SR), respectively.
• Step 4: Apply the merging procedure
to merge Hull(SL) and Hull(SR)
together to form a convex hull.
• Time complexity:
T(n) = 2T(n/2) + O(n)
= O(n log n)
Dynamic Programming
• Dynamic Programming is a general
algorithm design technique for
solving problems defined by
recurrences with overlapping
subproblems
• Invented by American mathematician
Richard Bellman in the 1950s to
solve optimization problems and
later assimilated by CS
Dynamic Programming
• “Programming” here means
“planning”
• Main idea:
– set up a recurrence relating a solution to a
larger instance to solutions of some smaller
instances
• - solve smaller instances once
– record solutions in a table
– extract solution to the initial instance from that table
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