CSC303: Algorithm Design and Analysis I
Audience: B. Tech. (CSE), Semester III
Instructor: Dr. Mamata Dalui
Assistant Professor
Department of Computer Science and Engineering
National Institute of Technology Durgapur
The study materials/presentations used in this course are solely meant for academic
purposes and collected from different available course materials, slides, books, etc.
CSC303 Dept of CSE, NIT Durgapur 2024-25(Odd) 1
Disclaimer
• The study materials/presentations used in this course are solely
meant for academic purposes
• The study materials presented/ distributed can be reused,
reproduced, modified, and distributed by others for academic
purposes only with proper acknowledgements
CSC303 Dept of CSE, NIT Durgapur 2024-25(Odd) 2
Divide and Conquer
Approach
CSC303 Dept of CSE, NIT Durgapur 2024-25(Odd) 3
Finding Convex Hull
Given n points in plane
S = {(xi, yi)|i = 1, 2,...,n}
assume no two points have same x coordinate, no two have same y
coordinate, and no three points are in a line for convenience.
Convex Hull (CH(S) ): smallest polygon containing all points in S.
4
CSC303 Dept of CSE, NIT Durgapur 2024-25(Odd)
Finding Convex Hull
CH(S) represented by the sequence of points on the boundary in
order clockwise as doubly linked list.
5
CSC303 Dept of CSE, NIT Durgapur 2024-25(Odd)
Finding Convex Hull
Brute force for Convex Hull
Test each line segment to see if it makes up an edge of the convex hull
• If the rest of the points are on one side of the segment, the segment
is on the convex hull.
• else the segment is not.
O(n2) edges, O(n) tests ⇒ O(n3) complexity
Can we do better?
6
CSC303 Dept of CSE, NIT Durgapur 2024-25(Odd)
Finding Convex Hull
Divide and Conquer Convex Hull
Sort points by x coordinates (once and for all, O(n log n))
For input set S of points:
• Divide into left half A and right half B by x coordinates
• Compute CH(A) and CH(B)
• Combine CH’s of two halves (merge step)
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).
CSC303 Dept of CSE, NIT Durgapur 2024-25(Odd)
10
Finding Convex Hull
Finding Tangents
CSC303 Dept of CSE, NIT Durgapur 2024-25(Odd)
11
Finding Convex Hull
Finding Tangents
1. i=1
2. j=1
3. while (y(i, j +1) > y(i, j) or y(i - 1, j) > y(i, j))
4. if (y(i, j +1) > y(i, j)) [move right finger clockwise
5. j = j +1(mod q)
6. else
7. i = i - 1( mod p) [move left finger anti-clockwise
8. return (ai, bj) as upper tangent
Similarly for lower tangent.
T(n)=2T(n/2) + ϴ(n) = T(n log n)
CSC303 Dept of CSE, NIT Durgapur 2024-25(Odd)
12
Finding Convex Hull
Intuition for why Merge works
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.
CSC303 Dept of CSE, NIT Durgapur 2024-25(Odd)
13
Finding Convex Hull
a3, b1 is upper tangent. a4 > a3, b2 > b1 in terms of Y coordinates.
a1, b3 is lower tangent, a2 <a1, b4 < b3 in terms of Y coordinates.
ai, bj is an upper tangent. Does not mean that ai or bj is the highest
point.
Similarly, for lower tangent.
CSC303 Dept of CSE, NIT Durgapur 2024-25(Odd)
14
Suggested reference: Divide & Conquer: Convex Hull, Median
Finding, MIT 6.046J Design and Analysis of Algorithms, Spring
2015
Instructor: Srinivas Devadas
CSC303 Dept of CSE, NIT Durgapur 2024-25(Odd)
15
Closest Pair of Points in 2D Space
CSC303 Dept of CSE, NIT Durgapur 2024-25(Odd)
16
Closest Pair of Points in 2D Space
Consider the problem of finding the closest pair of points
in a set Q of n ≥ 2 points.
“Closest” refers to the usual Euclidean distance: the
distance between points p1 = (x1, y1) and p2 = (x2, y2)
is (𝑥1 − 𝑥2)2 +(𝑦1 − 𝑦2)2
Two points in set Q may be coincident, in which case the
distance between them is zero.
This problem has applications in, for example, traffic-
control systems. A system for controlling air or sea traffic
might need to identify the two closest vehicles in order to
detect potential collisions.
CSC303 Dept of CSE, NIT Durgapur 2024-25(Odd)
Closest Pair of Points in 2D Space
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
Naive Algorithm: 𝑂(𝑛2 )
Test every pair of points, 7
return the closest.
3
We can do better! 8
Θ(𝑛 log 𝑛)
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
21 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
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
How many are there? 3
2𝛿 8
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.
How many are there? 3 8
𝒏 𝒏 𝟐 2𝛿
𝑻 𝒏 = 𝟐𝑻 +
𝟐 𝟐
= 𝚯 𝒏𝟐 LeftPoints RightPoints
26 CSC303 Dept of CSE, NIT Durgapur 2024-25(Odd)
Spanning 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
Don’t need to test any
points that are > 𝛿 from 3 8
one another 2𝛿
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. 𝛿
Let’s draw some examples.
28 CSC303 Dept of CSE, NIT Durgapur 2024-25(Odd)
Claim #1: if two points are the
closest pair that cross the cut, then
Reducing Search Space you can surround them in a box
that’s 2 ⋅ 𝛿 wide by 𝛿 tall.
Assume you’re checking in
increasing y-order, and you’ve 2⋅𝛿
reached the first point of the
closest pair.
Do you have to look at all
points above it to be guaranteed
to find the other point and the
minimum distance? 𝛿
No!
• Imagine you drew a box with its
bottom at point’s y-coordinate.
• See Claim #1.
• Claim #2: only 8 points can be in
the box.
29 CSC303 Dept of CSE, NIT Durgapur 2024-25(Odd)
Claim #1: if two points are the
closest pair that cross the cut, then
you can surround them in a box
Reducing Search Space that’s 2 ⋅ 𝛿 wide by 𝛿 tall.
Assume you’re checking in
increasing y-order, and you’ve 2⋅𝛿
reached the first point of the
closest pair.
Do you have to look at all
points above it to be guaranteed
to find the other point and the
minimum distance? 𝛿
No!
• Imagine you drew a box with its
bottom at point’s y-coordinate.
• See Claim #1.
• Claim #2: only 8 points can be in
the box.
30 CSC303 Dept of CSE, NIT Durgapur 2024-25(Odd)
Spanning the Cut
Combine: 2
1
2. Closest Pair Spanned our “Cut”
𝛿𝐿
Consider points in strip in 4
increasing y-order. 5
𝛿𝑅 Only
check
For a given point p, we can 6 next 7
prove the 8th point and beyond 7
is more than 𝛿 from p.
3 8
So for each point in strip, 2𝛿
check next 7 points in y-order.
LeftPoints RightPoints
𝚯 𝒏 𝑩𝒆𝒕𝒕𝒆𝒓!
31 CSC303 Dept of CSE, NIT Durgapur 2024-25(Odd)
Closest Pair of Points: Divide and Conquer
Initialization: Sort points by 𝑥-coordinate
(Later we’ll also need to process points by y- 2
1
coordinate, too.)
Divide: Partition points into two lists of points 5
based on 𝑥-coordinate (split at the median 𝑥) 4
6
Conquer: Recursively compute the closest pair
of points in each list
Base case?
3
?
8
LeftPoints RightPoints
32 CSC303 Dept of CSE, NIT Durgapur 2024-25(Odd)
Closest Pair of Points: Divide and Conquer
Initialization: Sort points by 𝑥-coordinate
(Later we’ll also need to process points by y- 2
1
coordinate, too.)
Divide: Partition points into two lists of points 5
based on 𝑥-coordinate (split at the median 𝑥) 4
6
Conquer: Recursively compute the closest pair of
points in each list
Base case: If there are two points, so only one pair
and directly compute their distance.
If there are three points, compute the distances of 7
each of the three pairs and take the minimum one. ?
Combine: 3
• Consider only points in the runway 8
(𝑥-coordinate within distance 𝛿 of median)
• Process runway points by 𝑦-coordinate
LeftPoints RightPoints
• Compare each point in runway to 7 points
above it and save the closest pair
• Output closest pair among left, right, and
runway points
33 CSC303 Dept of CSE, NIT Durgapur 2024-25(Odd)
Closest Pair of Points: Divide and Conquer
What is the running time? Θ 𝑛 log 𝑛 Initialization: Sort points by 𝑥-coordinate
Θ(𝑛 log 𝑛) Θ 1 Divide: Partition points into two lists of points
based on 𝑥-coordinate (split at the median 𝑥)
Conquer: Recursively compute the closest pair
2𝑇(𝑛/2) of points in each list
𝑇(𝑛)
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?
CSC303 Dept of CSE, NIT Durgapur 2024-25(Odd) 36