Divide and Conquer
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
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