Download presentation
Presentation is loading. Please wait.
Published byAnastasia Hall Modified over 5 years ago
1
Divide and Conquer / Closest Pair of Points Yin Tat Lee
CSE 421 Divide and Conquer / Closest Pair of Points Yin Tat Lee
2
Where is the mistake? Let π π ="1+1+β―(π π‘ππππ )β―+1=π(1)β Base case (π=1): 1=π(1) is true. Induction Hypothesis: Suppose π(π) holds. Induction Step: (Goal: show π(π+1) holds) By induction hypothesis, 1+1+β― π+1 π‘ππππ β―+1=π 1 +1=π 1 . Hence, π π+1 holds. This finishes the induction. Now, we know that π=π(1) ο Problem: Hidden constant in the π(β―) is increasing in the induction. Almost all students give a similar wrong proof in HW3 Q1. For midterm/final, we wonβt catch you on such small details. But it is the key point for this problem. So, we canβt forgive this.
3
Homework comments Avoid writing code in the homework.
It is often harder to understand. You need to write much more detail. We are unfair: We deduct point for bug in code. We do not deduct point for grammar mistake in pseudocode. See how pseudocode is done in the slide.
4
Midterm Date: Next Mon (Oct 29) Time: 1:30-2:20. Location: Same. Admin said there is no room left with large table ο. Coverage: All materials up to this Friday. Open book, open notes, open printout, hard copies only. Half of the score are MC/Fill in the blank/simple questions. This Friday (Oct 19): midterm review. Go over some sample questions that is relevant to the midterm.
5
Divide and Conquer Approach
6
Divide and Conquer We reduce a problem to several subproblems.
Typically, each sub-problem is at most a constant fraction of the size of the original problem Recursively solve each subproblem Merge the solutions Examples: Mergesort, Binary Search, Strassenβs Algorithm, n Log n levels n/2 n/2 n/4
7
A Classical Example: Merge Sort
recursively Split to π/2 merge
8
Why Balanced Partitioning?
An alternative "divide & conquer" algorithm: Split into n-1 and 1 Sort each sub problem Merge them Runtime π π =π πβ1 +π 1 +π Solution: π π =π+π πβ1 +π 1 =π+πβ1+ π πβ2 =π+πβ1+πβ2+π πβ3 =π+πβ1+πβ2+β¦+1=π( π 2 )
9
Reinventing Mergesort
Suppose we've already invented Bubble-Sort, and we know it takes π 2 Try just one level of divide & conquer: Bubble-Sort (first n/2 elements) Bubble-Sort (last n/2 elements) Merge results Time: 2 π(π/2) + π = π2/2 + π βͺ π2 Almost twice as fast! D&C in a nutshell
10
Reinventing Mergesort
βthe more dividing and conquering, the betterβ Two levels of D&C would be almost 4 times faster, 3 levels almost 8, etc., even though overhead is growing. Best is usually full recursion down to a small constant size (balancing "work" vs "overhead"). In the limit: youβve just rediscovered mergesort! Even unbalanced partitioning is good, but less good Bubble-sort improved with a 0.1/0.9 split: .1π π π = .82π2 + π The 18% savings compounds significantly if you carry recursion to more levels, actually giving π(π log π ), but with a bigger constant. This is why Quicksort with random splitter is good β badly unbalanced splits are rare, and not instantly fatal.
11
Finding the Root of a Function
12
Finding the Root of a Function
Given a continuous function f and two points π<π such that π π β€0 π π β₯0 Goal: Find a point π where π π is close to 0. π has a root in [π,π] by intermediate value theorem Note that roots of π may be irrational, So, we want to approximate the root with an arbitrary precision! π(π₯)= sin π₯ β π₯ + π₯ 4 a b
13
A Naive Approach Suppose we want π approximation to a root. Divide [π,π] into π= πβπ π intervals. For each interval check π π₯ β€0,π π₯+π β₯0 This runs in time π π =π( πβπ π ) Can we do faster? a b
14
Divide & Conquer (Binary Search)
Bisection (π,π,π) if πβπ <π then return π; else πβ(π+π)/2; if π π β€0 then return Bisection(π,π,π); return Bisection(π,π,π); a c b
15
Time Analysis Let π= πβπ π be the # of intervals and π=(π+π)/2 Always half of the intervals lie to the left and half lie to the right of c So, π π =π π 2 +π(1) i.e., π π =π( log π)=π( log ( πβπ π ) ) a c b n/2 n/2 For π dimension , βBinary searchβ can be used to minimize convex functions. The current best algorithms take π( π 3 log π 1 (π/π) ). Unfortunately, the algorithm is unusably complicated.
16
Fast Exponentiation
17
Fast Exponentiation Power(π,π) Obvious algorithm Observation:
Input: integer πβ₯π and number π Output: π π Obvious algorithm πβπ multiplications Observation: if π is even, then π π = π π/2 β
π π/2 .
18
Divide & Conquer (Repeated Squaring)
Power(π,π) { if (π=π) return π else if (π is even) return Power(π,π/π)ο· Power(π,π/2) else return Power(π,(πβπ)/π)ο· Power(π,(πβπ)/2) ο· π } Is there any problem in the program? π=Power(π,π/2); return πο·π; π=Power(π,(πβ1)/2); return πο·πο·π; Time (# of multiplications): π π β€π( π/2 )+2 for πβ₯1 π 0 =0 Solving it, we have π π β€π( π/2 )+2β€π( π/4 )+2+2 β€β―β€π 1 +2+β―+2β€2 log 2 π . log π (π) copies
19
Finding the Closest Pair of Points
20
Closest Pair of Points (general metric)
Given π points and arbitrary distances between them, find the closest pair. Must look at all π 2 pairwise distances, else any one you didnβt check might be the shortest. i.e., you have to read the whole input. (β¦ and all the rest of the π 2 edgesβ¦)
21
Closest Pair of Points (1-dimension)
Given π points on the real line, find the closest pair, e.g., given 11, 2, 4, 19, 4.8, 7, 8.2, 16, 11.5, 13, 1 find the closest pair Fact: Closest pair is adjacent in ordered list So, first sort, then scan adjacent pairs. Time π(π log π) to sort, if needed, Plus π(π) to scan adjacent pairs Key point: do not need to calculate distances between all pairs: exploit geometry + ordering 1 2 4 4.8 7 8.2 11 11.5 13 16 19
22
Closest Pair of Points (2-dimensions)
Given π points in the plane, find a pair with smallest Euclidean distance between them. Fundamental geometric primitive. Graphics, computer vision, geographic information systems, molecular modeling, air traffic control. Special case of nearest neighbor, Euclidean MST, Voronoi. Brute force: Check all pairs of points in Ξ( π 2 ) time. Assumption: No two points have same π₯ coordinate.
23
Closest Pair of Points (2-dimensions)
No single direction along which one can sort points to guarantee success!
24
Divide & Conquer Divide: draw vertical line πΏ with β π/2 points on each side. Conquer: find closest pair on each side, recursively. Combine to find closest pair overall Return best solutions How ? L 8 21 12
25
Why the strip problem is easier?
Key Observation Suppose πΏ is the minimum distance of all pairs in left/right of πΏ. πΏ= min 12,21 =12. Key Observation: suffices to consider points within πΏ of line πΏ. Almost the one-D problem again: Sort points in 2πΏ-strip by their π¦ coordinate. ο€=12 L 7 1 2 3 4 5 6 21 12
26
Almost 1D Problem Partition each side of πΏ into πΏ 2 Γ πΏ 2 squares Claim: No two points lie in the same πΏ 2 Γ πΏ 2 box. Proof: Such points would be within πΏ πΏ 2 2 =πΏ 1 2 β0.7πΏ<πΏ Let π π have the π π‘β smallest π¦-coordinate among points in the 2πΏ-width-strip. Claim: If πβπ >11, then the distance between π π and π π is >πΏ. Proof: only 11 boxes within πΏ of π¦( π π ). j 49 >πΏ 31 30 Β½ο€ 29 29 Β½ο€ 28 i 27 26 25 ο€ ο€
27
Closest Pair (2 dimension)
Where is the bottleneck? Closest Pair (2 dimension) Closest-Pair( π π , π π ,β―, π π ) { if(πβ€π) return | π π β π π | Compute separation line π³ such that half the points are on one side and half on the other side. πΉ π = Closest-Pair(left half) πΉ π = Closest-Pair(right half) πΉ = min( πΉ π , πΉ π ) Delete all points further than ο€ from separation line L Sort remaining points p[1]β¦p[m] by y-coordinate. for π=π,π,β―,π for k = π,π,β―,ππ if π+πβ€π ο€ = min(ο€, distance(p[i], p[i+k])); return ο€. }
28
Closest Pair Analysis Let π·(π) be the number of pairwise distance calculations in the Closest-Pair Algorithm π· π β€ if π=1 2π· π π o.w. βπ· π =O(πlog π) BUT, thatβs only the number of distance calculations What if we counted running time? π π β€ if π=1 2π π 2 +π(π log π) o.w. βπ· π =O(π log 2 π)
29
Closest Pair (2 dimension) Improved
if(πβ€π) return | π π β π π | Compute separation line π³ such that half the points are on one side and half on the other side. ( πΉ π , π π ) = Closest-Pair(left half) ( πΉ π , π π ) = Closest-Pair(right half) πΉ = min( πΉ π , πΉ π ) π ππππππ
= merge( π π , π π ) (merge sort it by y-coordinate) Let π be points (ordered as π ππππππ
) that is πΉ from line L. for π=π,π,β―,π for k = π,π,β―,ππ if π+πβ€π ο€ = min(ο€, distance(q[i], q[i+k])); return ο€ and π ππππππ
. } π π β€ if π=1 2π π 2 +π π o.w. βπ· π =π(π log π)
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.