10 Divide&Conquer PartII
10 Divide&Conquer PartII
The study materials/presentations used in this course are solely meant for academic
purposes and collected from different available course materials, slides, books, etc.
4
CSC303 Dept of CSE, NIT Durgapur 2024-25(Odd)
Finding Convex Hull
5
CSC303 Dept of CSE, NIT Durgapur 2024-25(Odd)
Finding Convex Hull
Can we do better?
6
CSC303 Dept of CSE, NIT Durgapur 2024-25(Odd)
Finding Convex Hull
7
CSC303 Dept of CSE, NIT Durgapur 2024-25(Odd)
Finding Convex Hull
How to Merge?
8
CSC303 Dept of CSE, NIT Durgapur 2024-25(Odd)
Finding Convex Hull
How to Merge?
• Find upper tangent (ai, bj). In example, (a4, b2) is Upper Tangent.
• Find lower tangent (ak, bm). In example, (a3, b3) is Lower Tangent.
• Cut and paste in time ϴ(n).
9
CSC303 Dept of CSE, NIT Durgapur 2024-25(Odd)
Finding Convex Hull
How to Merge?
• Find upper tangent (ai, bj). In example, (a4, b2) is Upper Tangent.
• Find lower tangent (ak, bm). In example, (a3, b3) is Lower Tangent.
• Cut and paste in time ϴ(n).
First link ai to bj, go down b list till we see bm and link bm to ak, continue along
the a list until we return to ai. In the example, this gives (a4,b2, b3, a3).
a1, b1 are the right-most and left-most points. We move anti-clockwise from a1,
clockwise from b1. a1,a2,...,ap is a convex hull, as is b1,b2,...,bq. If a is such that
moving from either ai or bj decreases y(i, j) there are no points above the (ai, bj)
line.
Given: 2
1
A list of points
5
Return: 4
6
Distance of the pair of
points that are closest
together
7
(or possibly the pair too)
3
8
18
CSC303 Dept of CSE, NIT Durgapur 2024-25(Odd)
Closest Pair of Points in 2D Space
Given: 2
1
A list of points
5
Pseudo code
for each pt iS 4
for each pt jS and i≠ j 6
{
compute distance of i, j
if distance of i, j < min_dist
min_dist = distance i, j
}
7
return min_dist
3
8
19
CSC303 Dept of CSE, NIT Durgapur 2024-25(Odd)
Closest Pair of Points: Naïve
Given: 2
1
A list of points
Return: 5
4
Distance of the closest 6
pair of points
20
CSC303 Dept of CSE, NIT Durgapur 2024-25(Odd)
Closest Pair of Points: D&C
Divide: How? 2
1
At median x coordinate
5
Conquer: 4
6
Combine: 7
3
8
Divide: 2
1
At median x coordinate
5
Conquer: 4
Recursively find the 6
closest pairs from Left and
closest pair from Right
Combine: 7
3
8
LeftPoints RightPoints
22 CSC303 Dept of CSE, NIT Durgapur 2024-25(Odd)
Closest Pair of Points: D&C
Divide: 2
1
At median x coordinate
5
Conquer: 4
Recursively find the 6
closest pairs from Left and
closest pair from Right
Combine: 7
Return min of Left and
3
Right pairs 8
Problem?
LeftPoints RightPoints
23 CSC303 Dept of CSE, NIT Durgapur 2024-25(Odd)
Closest Pair of Points: D&C
Combine:
2
2 Cases: 1
1. Closest Pair is
completely in Left or 5
Right 4
6
2. Closest Pair Spans
our “Cut”
7
Need to test points 3
?
across the cut 8
LeftPoints RightPoints
24 CSC303 Dept of CSE, NIT Durgapur 2024-25(Odd)
Spanning the Cut
Define “runway” or
“strip” along the cut.
Combine:
2
2. Closest Pair Spanned 1
our “Cut” 𝛿𝐿
Need to test points 5
4
across the cut. 6 𝛿𝑅
Bad approach: Compare
all points within 𝛿 =
min{𝛿𝐿 , 𝛿𝑅 } of the cut. 7
LeftPoints RightPoints
25 CSC303 Dept of CSE, NIT Durgapur 2024-25(Odd)
Spanning the Cut
Define “runway” or
“strip” along the cut.
Combine:
2
2. Closest Pair Spanned 1
our “Cut”
𝛿𝐿
Need to test points 4
across the cut 5
𝛿𝑅
Bad approach: Compare
all points within 𝛿 = 6
7
min{𝛿𝐿 , 𝛿𝑅 } of the cut.
Combine:
2
2. Closest Pair Spanned 1
our “Cut”
𝛿𝐿
Need to test points 4
across the cut 5
𝛿𝑅
We don’t need to test all 6
pairs! 7
LeftPoints RightPoints
27 CSC303 Dept of CSE, NIT Durgapur 2024-25(Odd)
Reducing Search Space
Combine:
Need to test points across the 2⋅𝛿
cut
Claim #1: if two points are
the closest pair that cross the
cut, then you can surround
them in a box that’s 2 ⋅ 𝛿 wide
by 𝛿 tall. 𝛿
3
?
8
LeftPoints RightPoints
𝑇(𝑛)
Combine:
• Process runway points by 𝑦-coordinate and
𝑇(𝑛) = 2𝑇(𝑛/2) + Θ(𝑛) Θ 𝑛 Compare each point in runway to 7 points
above it and save the closest pair
Case 2 of Master’s Theorem • Output closest pair among left, right, and
𝑇 𝑛 = Θ 𝑛 log 𝑛 Θ 1 runway points
𝑇′(𝑛) = 𝑇(𝑛) + Θ(𝑛 log 𝑛)= Θ(𝑛 log 𝑛) + Θ(𝑛 log 𝑛) = Θ(𝑛 log 𝑛)
34 CSC303 Dept of CSE, NIT Durgapur 2024-25(Odd)
Thank You
Questions?