Algorithm Design and Analysis (ADA)
Algorithm Design and Analysis (ADA)
Algorithm Design and Analysis (ADA)
(ADA)
CSE222
Divide and Conquer Algorithms
1. Divide the problem into smaller
subproblems
2. Conquer the subproblems via recursive
calls
𝑃 = { 𝑥1 , 𝑦1 , 𝑥2 , 𝑦2 , ⋯ 𝑥𝑛 , 𝑦𝑛 }
𝑑 𝑎, 𝑏
2 2
= 𝑎𝑥 − 𝑏𝑥 + 𝑎𝑦 − 𝑏𝑦
Euclidean distance in 2D
0
am Sort the points according to X word
a Check every adjacent pair
Node Sorting
reduces number of
do
that we need to
comparisons
Back to 2D
Does sorting by x,y coordinates separately work?
b
b
al
a
Divide and Conquer : Idea
𝑃𝑥 : Sorted by x-coordintes
𝑃𝑦 : Sorted by y-coordintes
Divide Step:
Find the median in the list 𝑃𝑥
Q R
Divide and Conquer : Idea
𝑃𝑥 : Sorted by x-coordintes
𝑃𝑦 : Sorted by y-coordintes
How do you generate the sets
𝑄𝑥 , 𝑄𝑦 , 𝑅𝑥 , 𝑅𝑦 ?
I
Generate by using Pa
Bye
𝑄 𝑅 E
a
Algorithm : High Level
𝑄
for
𝑅
Algorithm : High Level
Tcn 2 TCU 1041
ClosestPair (𝑃𝑥 , 𝑃𝑦 , 𝑛)
If n==2 , return distance
Compute 𝑄𝑥 , 𝑄𝑦 , 𝑅𝑥 , 𝑅𝑦
𝑛
𝛿𝑞 = ClosestPair (𝑄𝑥 , 𝑄𝑦 , )
I
I
2
𝑛
𝛿𝑟 = ClosestPair (𝑅𝑥 , 𝑅𝑦 , )
2
𝛿 = min{𝛿𝑞 , 𝛿𝑟 }
Ig
𝛿𝑠 =
iii doing
ClosestSplitPair (𝑄𝑥 , 𝑄𝑦 , 𝑅𝑥 , 𝑅𝑦 , 𝑛)
𝑄 𝑅 Return
m.me v 8s
Closest Split Pair in Linear Time
An apparently trivial but crucial observation :
𝑛
𝛿𝑞 = ClosestPair (𝑄𝑥 , 𝑄𝑦 , )
2
𝑛
𝛿𝑟 = ClosestPair (𝑅𝑥 , 𝑅𝑦 , )
2
Suffices to compute the closest split pair
correctly if and only if they are closer than
𝛿 = min{𝛿𝑞 , 𝛿𝑟 } !
I do not Leo closest split pair is
care
if 8
Closest Split Pair in Linear Time
Can we reduce our search space ?
Ebs we can
i
1 only focus
on
points
I
l
d 0 that lie in
1 hi 8 strip
l l
l l Ahound the
1 1 median
𝑄 1 I 𝑅
Closest Split Pair in Linear Time
Can we reduce our search space ?
Lemma 1.The distance between closest split pair
(𝑎, 𝑏) is less than 𝛿 implies they both lie in S.
Closest Split Pair in Linear Time
Can we reduce our search space ? Yes, but….
I all points
can still lie
inside this
strip
𝑄 𝑅
How many points to I need to check?
such
Suppose 139 is the closest split pair
dcp.gl coin
T
war
i
Sy F
t
a
F
f
In
fo any fixed point
p
s
Closest Split Pair in Linear Time
𝑆𝑦 : Set S Sorted by
y-coordintes
𝑄 𝑅
𝑆
Closest Split Pair in Linear Time
𝑆𝑦 : Set S Sorted by
y-coordintes
𝑄 𝑅
and they are closer than 𝛿. Let
𝑎 appear before 𝑏 in the list
𝑆𝑦 . Then 𝑏 can be at most 7
I
𝑆 positions away from a in the
list.
Amazing Lemma to Linear Time
ClosestSplitPair (𝑄𝑥 , 𝑄𝑦 , 𝑅𝑥 , 𝑅𝑦 , 𝛿)
Compute 𝑆𝑦 using …
𝛿𝑠 = 𝛿
for 𝑖 = 1,2, ⋯ 𝑆𝑦 −
for 𝑗 = 1,2, ⋯ 7
a7
go.ME
𝛿𝑠 = min{𝛿𝑠 , 𝑑(𝑆𝑦 [𝑖], 𝑆𝑦 [𝑖 + 𝑗])}
𝑄 𝑆 𝑅
Return 𝛿𝑠
Advanced Proof (Optional)
consider
Lem Fix any point qso E S Then
S at the
a ball of radius 558 with
center Then at most 72 points
in
Sf
can lie insider this ball
5pose 772 points home
goes this help
8
V
y coogdinate s sq 9yt f.gg r5S
I
I Advanced Proof (Optional)
roof of Lemma
r
t
have 736
points
IT contradiction
by volume
Advanced Proof (Optional) ay
Advanced Proof (Optional)
Advanced Proof (Optional)
Advanced Proof (Optional)
Advanced Proof (Optional)