Welcome to COMP 157!

1.
A problem is divided into several (often 2)
sub-problems of same type, ideally of about
equal size.
2.
The sub-problems are solved (often
recursively).
3.
Combine solutions to sub-problems into
solution to overall problem.
Convex hull: smallest convex set that includes
given points
 Sort points by x-coordinate values
 Identify extreme points P1 and P2 (leftmost
and rightmost)
 Splits points into 2 sets S1 and S2.
P2
P1

Compute upper (and lower) hull recursively:
 find point Pmax that is farthest away from line P1P2
 compute the upper hull of the points outside line
P1Pmax
 compute the upper hull of the points outside line
PmaxP2
Pmax
P1
P2
Discard points inside
P1 - P2- Pmax triangle.
 compute the upper hull of the points outside line
P1Pmax
 compute the upper hull of the points outside line
PmaxP2 (no points in set)
Pmax
P1
P2

Sorting points in O(n log n) time

Finding point farthest away from line P1P2 can
be done in linear time using determinant.

Overall Time efficiency:
 worst case: Θ(n2) (as quicksort)
 average case: Θ(n) (under reasonable assumptions about
distribution of points given)

Assume that your n points are sorted by xcoordinate in one list and by y-coordinate in
second list.
 If not already, can sort in Θ(n log2n)

If 2 ≤ n ≤ 3, use brute force.


If n > 3, draw line through
median of x-values and divide
points into 2 subsets, PL & PR
Apply recursively to PL & PR.
 d = min(dL,dR)
 Check pairs
within distance d
of median.

d = min(dL,dR)
 For points within distance d in x-
direction of median…
 …check adjacent pairs in ysorted list within distance d
of each other…
 …update d if necessary
 At most 6 such points



Dividing Problem in halves: linear time
Combining Solutions: linear time
𝑛
𝑇 𝑛 = 2𝑇
+ 𝑓 𝑛1
2
Applying Master Theorem: Θ(n log2n)
 Has been proven that not possible to solve closest
pair in fewer than Θ(n log2n) operations.

Integers too large to store in regular variables
 Used in RSA

Naïve algorithm: n2 digit multiplications
 Divide and Conquer can do better!
Consider:
23 = 2 ∙ 101 + 3 ∙ 100
14 = 1 ∙ 101 + 4 ∙ 100

23 ∗ 14
= 2 ∙ 101 + 3 ∙ 100 ∗ 1 ∙ 101 + 4 ∙ 100
= 2 ∗ 1 102 + 2 ∗ 4 + 3 ∗ 1 101 + 3 ∗ 4 100
= 322
4 multiplications, like naïve
but can we do better
2 ∗ 1 102 + 2 ∗ 4 + 3 ∗ 1 101 + 3 ∗ 4 100
2∗4+3∗1
= 2+3 ∗ 1+4 −2∗1−3∗4
For any 2-digit numbers 𝑎 = 𝑎1 𝑎0 and 𝑏 =
𝑏1 𝑏0 product 𝑐 is similarly defined.
For any 2-digit numbers 𝑎 = 𝑎1 𝑎0 and 𝑏 =
𝑏1 𝑏0 product 𝑐 is:
𝑐 = 𝑐2 ∙ 102 + 𝑐1 ∙ 101 + 𝑐0
 𝑐2 = 𝑎1 ∗ 𝑏1
 𝑐0 = 𝑎0 ∗ 𝑏0
 𝑐1 = 𝑎1 + 𝑎0 ∙ 𝑏1 + 𝑏0 − 𝑐2 + 𝑐0
Your Turn: 35 * 42
For any 2-digit numbers 𝑎 = 𝑎1 𝑎0 and 𝑏 =
𝑏1 𝑏0 product 𝑐 is:
𝑐 = 𝑐2 ∙ 102 + 𝑐1 ∙ 101 + 𝑐0
 𝑐2 = 𝑎1 ∗ 𝑏1
 𝑐0 = 𝑎0 ∗ 𝑏0
 𝑐1 = 𝑎1 + 𝑎0 ∙ 𝑏1 + 𝑏0 − 𝑐2 + 𝑐0
Where’s the divide-and-conquer???
For any n-digit numbers, where n is even, split
the digits in half 𝑎 = 𝑎1 𝑎0 and 𝑏 = 𝑏1 𝑏0 :
𝑐 = 𝑐2 ∙ 10𝑛 + 𝑐1 ∙ 10𝑛 2 + 𝑐0
 𝑐2 = 𝑎1 ∗ 𝑏1
 𝑐0 = 𝑎0 ∗ 𝑏0
 𝑐1 = 𝑎1 + 𝑎0 ∙ 𝑏1 + 𝑏0 − 𝑐2 + 𝑐0
Can apply recursively until n = 1.
Number of Multiplications M(n):
𝑛
𝑀 𝑛 = 3𝑀
+ 𝑐 𝑓𝑜𝑟 𝑛 > 1,
2

𝑀 1 =1
According to Master Theorem, what is θ(g(n))?
𝜃 𝑛𝑑
𝑖𝑓 𝑎 < 𝑏𝑑
𝑇 𝑛 ∈
𝜃 𝑛𝑑 ∙ log 𝑛
𝑖𝑓 𝑎 = 𝑏𝑑
𝜃 𝑛log𝑏 𝑎
𝑖𝑓 𝑎 > 𝑏𝑑
Number of Multiplications M(n):
𝑛
𝑀 𝑛 = 3𝑀
𝑓𝑜𝑟 𝑛 > 1,
𝑀 1 =1
2

𝑀 𝑛 = 𝑛log2 3 ≈ 𝑛1.585
Number of Multiplications M(n):
𝑛
𝑀 𝑛 = 3𝑀
𝑓𝑜𝑟 𝑛 > 1,
𝑀 1 =1
2

But what about the extra additions and
subtractions?
𝑛
𝐴 𝑛 = 3𝐴
+ 𝑐𝑛 𝑓𝑜𝑟 𝑛 > 1,
𝐴 1 =1
2
≈ 𝑛1.585

Depending on system, outperforms
conventional method at 8 digits.
 Switch to conventional at crossover point.

More than twice as fast on 300-digit nums.

Object-Oriented Languages have built-in
classes.

Until 1960, assumed Θ(n2) was limit

Strassen’s Algorithm: reduce multiplication of
two 2x2 matrices from 8 multiplications to 7:
𝑐00
𝑐10
𝑐01
𝑎00
𝑐11 = 𝑎10
𝑎01
𝑏00
𝑎11 ∗ 𝑏10
𝑚1 + 𝑚4 − 𝑚5 + 𝑚7
=
𝑚2 + 𝑚4
𝑏01
𝑏11
𝑚3 + 𝑚5
𝑚1 + 𝑚3 − 𝑚2 + 𝑚6
 𝑚1 = 𝑎00 + 𝑎11 ∗ (𝑏00 + 𝑏11 )
 𝑚2 = 𝑎10 + 𝑎11 ∗ 𝑏00
 𝑚3 = 𝑎00 ∗ (𝑏01 − 𝑏11 )
 𝑚4 = 𝑎11 ∗ (𝑏10 − 𝑏00 )
 𝑚5 = 𝑎00 + 𝑎01 ∗ 𝑏11
 𝑚6 = 𝑎10 − 𝑎00 ∗ (𝑏00 + 𝑏01 )
 𝑚7 = 𝑎01 − 𝑎11 ∗ 𝑏10 + 𝑏11
▪ 7 multiplications and 18 additions
Usefulness again comes from applying recursively:
𝑛
𝑀 𝑛 = 7𝑀
𝑀 1 =1
2
≈ 𝑛2.807

𝑛
𝑛
𝐴 𝑛 = 7𝐴
+ 18
2
2
≈ 𝑛2.807
 Brute Force: Θ(n3)
2
𝐴 1 =0
34
Right Subtree
Left Subtree
19
1
50
21
46
87

Pre-Order

In-Order

Post-Order
34
19
1
50
21
46
87
Height(T)
if T is empty
return 0
else
return max(Height(Tleft), Height(Tright))+1
Which operation is executed most?
Height(T)
if T is empty
return 0
else
return max(Height(Tleft), Height(Tright))+1
Which operation is executed most?
Comparisons:
Additions:
C(n) = 2n+1
A(n) = n

Divide-and-Conquer: anything that requires a
full traversal.

Variable-Size-Decrease: anything that
requires just a single path from root to leaf.
 Insert, Search