CS 401 Master Theorem / Closest Points Xiaorui Sun 1
Master Theorem
Master Theorem % & + (" ) for all " > +. Then, Suppose ! " = $ ! c: absolute constant • If $ < + ) then ! " = Θ " ) • If $ = + ) then ! " = Θ " ) log " • If $ > + ) then ! " = Θ " 234 5 6 Works even if it is % & instead of % & . We also need $ ≥ 1, + > 1 , : ≥ 0 and ! " = <(1) for " ≤ +.
Quiz Consider the following recurrence. Which case of the master theorem? Θ 1 if " = 1 ! " = 4 3!( "/2 ) + Θ(") if " > 1 ? + @" A for > Suppose ! " = = ! all " > B. • A. ! " = Θ " %&' ( ) = *(" ,../. ) if = < B A 1 ! " = Θ " A 4 • B. ! " = Θ "log " if = = B A log ? " " %&' D E if = > B A • C. ! " = Θ " • D. Master theorem not applicable
Quiz Consider the following recurrence. Which case of the master theorem? 0 if " ≤ 1 ! " = " + ! " − 3 " + 11 ! 5 " if " > 1 5 10 • A. ! " = Θ " • B. ! " = Θ "log " • C. ! " = Θ " ( Akra–Bazzi theorem • D. Master theorem not applicable Wiki!
How to use master theorem? For a divide and conquer algorithm • a: number of subproblems • b: ratio of problem size / subproblem size c ⋅ n k : running time of divide and combine step • & ' + )# * for all # > ,. We have recurrence " # = % " Example: Mergesort have two subproblems of half size of the original problem, and the cost of divide and combine step is O(n), so T(n) = 2 T(n / 2) + c n, which implies T(n) = Θ (n log n)
Quiz In a divide and conquer algorithm, every problem of size n is divided into 20 subproblems of size n / 10, the cost of divide and combine steps is ! " , what is the running time of the algorithm? 7 8 + :! ; for all ! > =. Suppose 4 ! = 6 4 A. Θ(! " ) if 6 < = ; 1 B. Θ(! " log !) if 6 = = ; log 8 ! 4 ! = Θ ! ; 6 -./ B 7 C. Θ(! -./ 01 23 ) if 6 > = ; = ; D. None of above
Proving Master Theorem Problem size ! " = $!("/') + *" + # probs cost " *" + $ 1 . = log 2 " $ "/' $ ⋅ * "/' + $ , ⋅ * "/' , + "/' , $ 2 ' $ + ⋅ * "/' + + 1 $ . 97:;< = > " + $ 6 * ! " = 5 ' 6 678
Master Theorem % & + (" ) for all " > +. Then, Suppose ! " = $ ! # of problems increases slower • If $ < + ) then ! " = Θ " ) than the decreases of cost. First term dominates. • If $ = + ) then ! " = Θ " ) log " # of problems increases faster • If $ > + ) then ! " = Θ " 234 5 6 than the decreases of cost Last term dominates. ;9234 5 % " ) $ 8 ( ! " = 7 + 8 89:
A Useful Identity Theorem: 1 + # + # $ + ⋯ + # & = ( )*+ ,- (,- Proof: Let . = 1 + # + # $ + ⋯ + # & Then, #. = # + # $ + ⋯ + # &/- So, #. − . = # &/- − 1 i.e., . # − 1 = # &/- − 1 Therefore, . = ( )*+ ,- 2 ( means the hidden (,- constant depends on # Corollary: 2 ( 1 if # < 1 1 + # + # $ + ⋯ + # & = 1 6 + 1 if # = 1 2 ( # &/- if # > 1
% & + (" ) Solve: ! " = $! Corollary: Θ 1 1 if + < 1 1 + + + + , + ⋯ + + . = / Θ 5 if + = 1 Θ 1 + .67 if + > 1 Going back, we have .;=>? @ % .;=>? @ % " ) $ : = (" ) 9 $ : ( ! " = 9 A : A ) :;< :;< Hence, we have if $ < A ) 1 if $ = A ) log & " ! " = Θ " ) $ =>? @ % if $ > A ) A )
% & + (" ) Solve: ! " = $! if $ < / ) 1 if $ = / ) log & " ! " = Θ " ) $ 345 6 % if $ > / ) / ) For $ < / ) , we simply have ! " = Θ " ) . For $ = / ) , we have ! " = Θ " ) log & " = Θ(" ) log ") . 345 6 % : For $ > / ) , we have ! " = Θ " ) = Θ(" 345 6 : ) . & ; $ 345 6 % / ) 345 6 % = (/ 345 6 : ) 345 6 % = / 345 6 % ) = (/ 345 6 % ) 345 6 : = " ) = " 345 6 :
Finding the Closest Pair of Points
Recap: Closest Pair of Points (1-dim) Given ! points, find the closest pair. Brute force: Check all " # pairwise distances 1-dim case: 11, 2, 4, 19, 4.8, 7, 8.2, 16, 11.5, 13, 1 find the closest pair 1 2 4 4.8 7 11 11.5 13 8.2 16 19 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
Recap: Closest Pair of Points (2-dim) Given ! points in the plane, find a pair with smallest Euclidean distance between them. Idea: make use of 1-dim algorithm (but not in a simple way) No single direction along which one can sort points to guarantee success!
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 How ? Return best solutions L 8 21 12
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. L 7 6 5 21 4 12 3 2 1 d =12
Almost 1D Problem Partition each side of ! into " # × " # squares Claim: No two points lie in the same " # × " # box. j 49 Proof: Such points would be within > ' # # " " ( + = ' # ≈ 0.7' < ' # # 31 30 ½ d Let . / have the 0 12 smallest 3 -coordinate 29 29 among points in the 2' -width-strip. ½ d 28 i 27 Claim: If 0 − 6 > 11 , then the distance 26 between . / and . 9 is > ' . 25 Proof: only 11 boxes within ' of 3(. / ) . d d
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 d from separation line L Sort remaining points p[1]…p[m] by y-coordinate. for , = ", $, ⋯ , . for k = ", $, ⋯ , "" if , + 0 ≤ . d = min( d , distance(p[i], p[i+k])); return d . }
Closest Pair Analysis Let !(#) be the number of pairwise distance calculations in the Closest-Pair Algorithm
Closest Pair (2 dimension) Closest-Pair( ! " , ! $ , ⋯ , ! & ) { if( & ≤ $ ) return distance( ! " , ! $ ) 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 d from separation line L Sort remaining points p[1]…p[m] by y-coordinate. for + = ", $, ⋯ , - for k = ", $, ⋯ , "" 111 ≤ 112 if + + / ≤ - d = min( d , distance(p[i], p[i+k])); distance calculations return d . }
Closest Pair Analysis Let !(#) be the number of pairwise distance calculations in the Closest-Pair Algorithm 1 if # ≤ 2 2! # ! # ≤ & o. w. ⇒ ! # = O(#log #) 2 + 11 # BUT, that’s only the number of distance calculations What if we counted running time?
Closest Pair (2 dimension) Closest-Pair( ! " , ! $ , ⋯ , ! & ) { if( & ≤ $ ) return distance( ! " , ! $ ) Compute separation line ) such that half the points 0(2 log 2) are on one side and half on the other side. * " = Closest-Pair(left half) * $ = Closest-Pair(right half) 0(1) * = min( * " , * $ ) Delete all points further than d from separation line L 0(2) 0(2 log 2) Sort remaining points p[1]…p[m] by y-coordinate. for + = ", $, ⋯ , - for k = ", $, ⋯ , "" 0(2) if + + / ≤ - d = min( d , distance(p[i], p[i+k])); return d . }
Closest Pair Analysis Let !(#) be the number of pairwise distance calculations in the Closest-Pair Algorithm 1 if # ≤ 2 2! # ! # ≤ & o. w. ⇒ ! # = O(#log #) 2 + 11 # BUT, that’s only the number of distance calculations What if we counted running time? 1 if # ≤ 2 o. w. ⇒ 4 # = O(#log 6 #) 24 # 4 # ≤ & 2 + 5(# log #) Can we do better?
Closest Pair (2 dimension) Improved Closest-Pair( ! " , ! $ , ⋯ , ! & ) { Assume: input sorted by x-coordinate if( & ≤ $ ) return distance( ! " , ! $ ) (O(n log n) overhead initially) Compute separation line ) such that half the points ?(1) are on one side and half on the other side. ( * " , + " ) = Closest-Pair(left half) ( * $ , + $ ) = Closest-Pair(right half) ?(1) * = min( * " , * $ ) ?(9) + ,-./01 = merge( + " , + $ ) (merge sort it by y-coordinate) ?(9) Let 2 be points (ordered as + ,-./01 ) that is * from line L. for 3 = ", $, ⋯ , 5 for k = ", $, ⋯ , "" ?(9) if 3 + 7 ≤ 5 d = min( d , distance(S[i], S[i+k])); 1 if 9 = 1 return d and + ,-./01 . 28 9 8 9 ≤ : } 2 + ? 9 o. w. Input sorted by y-coordinate ⇒ 8 9 = ?(9 log 9)
Recommend
More recommend