Download presentation
Presentation is loading. Please wait.
Published byJakob Engen Modified over 5 years ago
1
Given a list of n 8 integers, what is the runtime bound on the optimal algorithm that sorts the first eight? O(1) O(log n) O(n) O(n log n) O(n2)
2
Given a list of n 8 integers, what is the runtime bound on the optimal algorithm that sorts the first eight? O(1) O(log n) O(n) O(n log n) O(n2)
3
Closest Point Problem
4
Problem Statement Input: A set of points on the plane (given in the form of x and y coordinates) Output: One pair of the input points Goal: Return the closest pair
5
Example
6
What is the runtime for the naive Closest Point algorithm?
O(log n) O(n) O(n log n) O(n2) O(n3)
7
What is the runtime for the naive Closest Point algorithm?
O(log n) O(n) O(n log n) O(n2) O(n3)
8
Divide and Conquer: Attempt 1
Step 1: Split list in two based on median x value Step 2: Recursively apply algorithm to each half Step 3: Return the closer of the two pairs
9
Sample Execution
10
The algorithm always returns the optimal answer.
True False
11
The algorithm always returns the optimal answer.
True False
12
Sample Execution (Counter Example)
13
Divide and Conquer: Attempt 2
Step 1: Split in two based on median x value Step 2: Recursively apply algorithm to each half Step 3: Check every node on the left against every node on the right
14
What is the runtime of this formula?
T(n) = 2T(n/2) + (n) T(n) = 2T(n/2) + (n2) T(n) = T(n/2) + (n) T(n) = T(n/2) + (n2)
15
What is the runtime of this formula?
T(n) = 2T(n/2) + (n) T(n) = 2T(n/2) + (n2) T(n) = T(n/2) + (n) T(n) = T(n/2) + (n2)
16
Analysis T(n) = 2T(n/2) + (n2 ) a = 2, b = 2, logba = 1 f(n) = (n2)
f(n) = (n1+ ) (and regularity holds) If we want T(n) = (n log n), we can only spend linear time in the split/merge T(n) = (n2)
17
L R = min(L, R )
18
Divide and Conquer: Attempt 3
Step 1: Split in two based on median x value Step 2: Recursively apply algorithm to each partition Let L and R be the minimum distance on each side Let = min(L, R) Step 3: Check only points within of the dividing line against each other
19
Worst Case 2 Still requires O(n2 ) time
20
Don’t need to look more than below this point
We only need to compare this point against those in the rectangle L How many points can be in the rectangle? R = min(L, R )
21
Point Limit Limit: 8 points in the rectangle
Region now blocked out – no points can fall in the blue 2 2 Could have two points here (one per partition) Assume this is in the left partition
22
Checking Points To process the “strip”:
Sort the points by y coordinate For each point: If this point is the upper left-hand (or right-hand) corner of a box, the box can only contain seven more points Those points would have to be the next seven in the sorted order We need only compare this point against the next seven in the list (O(1) time)
23
Algorithm Sort points by x coordinate ((n log n))
Calculate partition point ((1)) Recursively find L, R and ( 2T(n/2)) Remove elements outside of “strip” ((n)) Sort by y coordinate ((n log n)) For each point in strip: Check against the next seven points ((1)) T(n) = 2T(n/2) + (n log n)
24
Analysis T(n) = 2T(n/2) + (n log n) Master Theorem won’t work – this is the “gap” between case two and three Turns out: T(n) = (n log n) – not what we wanted We can speed this up pre-processing
25
Pre-processing Pre-processing: “Setup work” done before calling the algorithm In this case, before starting the algorithm: Sort all points by x coordinate Sort all points by y coordinates (Actually, sort a list of references into the point list – this gets tricky) If you maintain your data correctly, you will not need to sort again
26
Algorithm Sort points by x coordinate ((n log n))
Calculate partition point ((1)) Recursively find L, R and (2T(n/2)) Remove elements outside of “strip” ((n)) Sort by y coordinate ((n log n)) For each point in strip: Check against the next seven points ((1)) T(n) = 2T(n/2) + (n log n) (n )
27
Analysis T(n) = 2T(n/2) + (n)
28
What is the runtime of this algorithm (not counting pre-processing)
O(log n) O(n) O(n log n) O(n2)
29
What is the runtime of this algorithm (not counting pre-processing)
O(log n) O(n) O(n log n) O(n2)
30
T(n) = 2T(n/2) + (n) Same as merge sort, so: T(n) = (n log n)
Analysis T(n) = 2T(n/2) + (n) Same as merge sort, so: T(n) = (n log n) With preprocessing: n log n + T(n) – still (n log n)
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.