Randomized Algorithms CS648 Lecture 16 Randomized Incremental Construction (Backward analysis) 1 PROBLEM 1 FIND-MIN PROBLEM 2 Find-Min algorithm A 1 2 Find-Min(A[1..๐]) { ๐๐๐ ๏ A[1]; For ๐ = ๐ to ๐ do { if (A[๐] < ๐๐๐) ๐๐๐ ๏ A[๐] ; } return ๐๐๐; } ๐−๐ … ๐ฟ: no. of times ๐๐๐ is updated. ๐ฟ๐ = ๐ if ๐๐๐ is updated in ๐th iteration ๐ otherwise ๐ฟ=๐+ ๐>๐ ๐ฟ๐ ๏จ๐ ๐ =๐+ =๐+ Probability that “A[๐] is smaller than {A[๐],…, A[๐ − ๐]}” ๐>๐ ๐[๐ฟ๐ ] ๐>๐ ๐(๐ฟ๐ = ๐) ?? 3 Forward analysis for First ๐ − ๐ elements ๐(๐ฟ๐ = ๐) A 1 2 Find-Min(A[1..๐]) { ๐๐๐ ๏ A[1]; For ๐ = ๐ to ๐ do { if (A[๐] < ๐๐๐) ๐๐๐ ๏ A[๐] ; } return ๐๐๐; } … ๐ ๐−๐ Notations: • ๐บ๐−๐ : set of all subsets of A of size ๐ − ๐. • For any ๐ ∈ ๐บ๐−๐ , ๐๐ : first ๐ − ๐ elements of A are some permutation of ๐. Using Partition Theorem, ๐(๐ฟ๐ = ๐) = ๐∈๐บ๐−๐ ๐(๐ฟ๐ = ๐ ๐๐ โ ๐(๐๐ ) 4 Forward analysis for ๐(๐ฟ๐ = ๐) ๐ : a subset of ๐ − ๐ elements. ๐(๐ฟ๐ = ๐ ๐๐ = “Given that first ๐ − ๐ elements of A are some permutation of ๐, what is prob. that ๐ฟ๐ = ๐ ?” For this event to happen, A[๐] must be smaller than every element of ๐. ๏จ ๐(๐ฟ๐ = ๐ ๐๐ depends upon ๐. For example, if the smallest element of ๐ has rank ๐ in A, then ๐(๐ฟ๐ = ๐ ๐๐ = ?? ๐ − ๐ ๐−๐+๐ Dependency on ๐ makes it hard to calculate ๐∈๐บ๐−๐ ๐(๐ฟ๐ = ๐ ๐๐ โ ๐(๐๐ ) 5 Backward analysis for First ๐ elements ๐(๐ฟ๐ = ๐) A 1 2 Find-Min(A[1..๐]) { ๐๐๐ ๏ A[1]; For ๐ = ๐ to ๐ do { if (A[๐] < ๐๐๐) ๐๐๐ ๏ A[๐] ; } return ๐๐๐; } … ๐ ๐−๐ Notations: • ๐บ๐ : set of all subsets of A of size ๐. • For any ๐ ∈ ๐บ๐ , ๐๐ : first ๐ elements of A are some permutation of ๐. Using Partition Theorem, ๐(๐ฟ๐ = ๐) = ๐∈๐บ๐ ๐(๐ฟ๐ = ๐ ๐๐ โ ๐(๐๐ ) 6 Backward analysis for ๐(๐ฟ๐ = ๐) ๐ : a subset of ๐ − ๐ elements. ๐(๐ฟ๐ = ๐ ๐๐ = “Given that first ๐ elements of A are some permutation of ๐, what is prob. that ๐ฟ๐ = ๐ ?” For this event to happen, the smallest element of ๐ must appear at A[๐]. ๏จ ๐(๐ฟ๐ = ๐ ๐๐ = ๐( “the smallest element? ?of ๐ appear at A[๐]” ) Fact: A is permuted randomly uniformly ๏จ Every element of ๐ is equally likely to appear at place A[๐]. ๐ ๏จ ๐(๐ฟ๐ = ๐ ๐๐ = ?? ๐ Same for each ๐ 7 Backward analysis for First ๐ elements ๐(๐ฟ๐ = ๐) A 1 2 Find-Min(A[1..๐]) { ๐๐๐ ๏ A[1]; For ๐ = ๐ to ๐ do { if (A[๐] < ๐๐๐) ๐๐๐ ๏ A[๐] ; } return ๐๐๐; } … ๐ ๐−๐ Notations: • ๐บ๐ : set of all subsets of A of size ๐. • For any ๐ ∈ ๐บ๐ , ๐๐ : first ๐ elements of A are some permutation of ๐. Using Partition Theorem, ๐(๐ฟ๐ = ๐) = ๐∈๐บ๐ ๐(๐ฟ๐ =๐๐ ๐๐ โ ๐(๐๐ ) = ๐ ๐ ๐ ๐ ๐ ๐∈๐บ๐ ๐(๐๐ ) = ⋅๐ 8 PROBLEM 2 CLOSEST PAIR OF POINTS 9 Closest Pair of Points Problem Definition: Given a set ๐ท of ๐ > ๐ points in plane, compute the pair of points with minimum Euclidean distance. Randomized algorithm: • O(๐) : Randomized Incremental Construction based algorithm 10 ๐th iteration Grid structure for first ๐ − ๐ points ๐น๐−๐ ๐น๐−๐ 11 Analysis of ๐th iteration Closest-pair-algorithm(๐ท) Let <๐๐ ,๐๐ ,…,๐๐ > be a uniformly random permutation of ๐ท; ๐น๐ ๏ distance(๐๐ ,๐๐ ); ๐ฎ ๏ Build_Grid(๐น๐ , {๐๐ , ๐๐ }); For ๐ = ๐ to ๐ do { Step 1: locate the cell of the grid ๐ฎ containing ๐๐ ; Step 2: find the point ๐ ∈ {๐๐ , … , ๐๐−๐ } closest to ๐๐ ; let ๐น = distance(๐, ๐๐ ); Step 3: If ๐น > ๐น๐−๐ ๐น๐ ๏ ๐น๐−๐ ; Insert(๐๐ ,๐ฎ); Else ๐น๐ ๏ ๐น; ๐ฎ๏ Build_Grid(๐น, {๐๐ , … , ๐๐ }); } return ๐น๐ ; O(1) O(1) O(1) ๐๐ for constant ๐ 12 running time of ๐th iteration ๐ฟ๐ : running time of ๐th iteration O(1) + ๐๐ โ ๐(๐น๐ < ๐น๐−๐ ) E[๐ฟ๐ ] = ?? Question: What is ๐(๐น๐ < ๐น๐−๐ ) ? 13 Forward analysis for ๐(๐น๐ < ๐น๐−๐ ) Closest-pair-algorithm(๐ท) Let <๐๐ ,๐๐ ,…,๐๐ > be a uniformly random permutation of ๐ท; ๐น๐ ๏ distance(๐๐ ,๐๐ ); ๐ฎ ๏ Build_Grid(๐น๐ , {๐๐ , ๐๐ }); For ๐ = ๐ to ๐ do { Step 1: locate the cell of the grid ๐ฎ containing ๐๐ ; Step 2: find the point ๐ ∈ {๐๐ , … , ๐๐−๐ } closest to ๐๐ ; let ๐น = distance(๐, ๐๐ ); Step 3: If ๐น > ๐น๐−๐ ๐น๐ ๏ ๐น๐−๐ ; Insert(๐๐ ,๐ฎ); Else ๐น๐ ๏ ๐น; ๐ฎ๏ Build_Grid(๐น, {๐๐ , … , ๐๐ }); } return ๐น๐ ; 14 Forward analysis for ๐(๐น๐ < ๐น๐−๐ ) ๐ : a subset of ๐ − ๐ points from ๐ท. ๐๐ : ๐น๐−๐ first ๐ − ๐ points of ๐ท are some permutation of ๐ ๐(๐น๐ < ๐น๐−๐ ๐๐ = ??Depends upon ๐ ๐น๐−๐ Calculating ๐(๐น๐ < ๐น๐−๐ ) : Let ๐บ๐−๐ be the set of all subsets of ๐ท of size ๐ − ๐. ๐(๐น๐ < ๐น๐−๐ )= ๐∈๐บ๐ ๐(๐น๐ < ๐น๐−๐ ๐๐ โ ๐(๐๐ ) Grid structure for first ๐ − ๐ points 15 Backward analysis for ๐(๐น๐ < ๐น๐−๐ ) Closest-pair-algorithm(๐ท) Let <๐๐ ,๐๐ ,…,๐๐ > be a uniformly random permutation of ๐ท; ๐น๐ ๏ distance(๐๐ ,๐๐ ); ๐ฎ ๏ Build_Grid(๐น๐ , {๐๐ , ๐๐ }); For ๐ = ๐ to ๐ do { Step 1: locate the cell of the grid ๐ฎ containing ๐๐ ; Step 2: find the point ๐ ∈ {๐๐ , … , ๐๐−๐ } closest to ๐๐ ; let ๐น = distance(๐, ๐๐ ); Step 3: If ๐น > ๐น๐−๐ ๐น๐ ๏ ๐น๐−๐ ; Insert(๐๐ ,๐ฎ); Else ๐น๐ ๏ ๐น; ๐ฎ๏ Build_Grid(๐น, {๐๐ , … , ๐๐ }); } return ๐น๐ ; 16 Backward analysis for ๐(๐น๐ < ๐น๐−๐ ) ๐ : a subset of ๐ points from ๐ท. ๐๐ : first ๐ points of ๐ท are some permutation of ๐ ๐น๐ ๐ ๐(๐น๐ < ๐น๐−๐ ๐๐ = ?? ๐ Calculating ๐(๐น๐ < ๐น๐−๐ ) : Let ๐บ๐ be the set of all subsets of ๐ท of size ๐. ๐น๐ ๐(๐น๐ < ๐น๐−๐ )= ๐∈๐บ๐ ๐(๐น๐ = ๐∈๐บ๐ ๐ Grid structure for first ๐ points =๐โ ๐∈๐บ๐ ๐ ๐ < ๐น๐−๐ ๐๐ โ ๐(๐๐ ) โ ๐(๐๐ ) ๐(๐๐ ) = ๐ ๐ 17 running time of ๐th iteration ๐ฟ๐ : running time of ๐th iteration E[๐ฟ๐ ] = O(1) + ๐๐ โ ๐(๐น๐ < ๐น๐−๐ ) = O(1) + ๐๐ โ ๐ ๐ = O(1) Expected running time of the algorithm : E[๐ฟ๐ ] ๐≤๐ = O(1) = O(๐) ๐≤๐ Theorem: There exists a linear time Las Vegas algorithm to compute closest pair of points in plane. 18 RANDOMIZED INCREMENTAL CONSTRUCTION 19 Randomized Incremental Construction • Permute the elements of input randomly uniformly. • Build the structure incrementally. • Keep some data structure to perform ๐th iteration efficiently. • Use Backward analysis to analyze the expected running time. 20 Randomized Incremental Construction • Convex Hull of a set of points • Trapezoidal decomposition of a set of segments. • Convex polytope of a set of half-planes • Smallest sphere enclosing a set of points. • Linear programming in finite dimensions. 21 PROBLEM 3 CONVEX HULL OF POINTS 22 Convex hull of Points Problem definition: Given ๐ points in a plane, compute a convex polygon of smallest area that encloses all the points. 23 Convex hull of Points Deterministic algorithm: • O(๐ ๐ฅ๐จ๐ ๐) time algorithm. • Many algorithms exist: Grahams Scan, Jarvis’s march, divide and conquer,… Randomized algorithm: • O(๐ ๐ฅ๐จ๐ ๐) time algorithm. • Based on Randomized Incremental Construction. • Generalizable to higher dimensions. 24 Randomized algorithm for convex hull Convex-hull-algorithm(๐ท) { Let <๐๐ ,๐๐ ,…,๐๐ > be a uniformly random permutation of ๐ท; ๐ฏ๏ triangle(๐๐ ,๐๐ , ๐๐ ); For ๐ = ๐ to ๐ do insert ๐๐ and update ๐ฏ return ๐ฏ; } 25 A simple exercise from geometry Exercise: Given a line L and two points p and q, determine whether the points lie on the same/different sides of L. L p q q ๐ = ๐๐ + ๐ 26 Conflict graph : a powerful data structure ๐๐ ๐ − ๐ points ๐๐ cones ๐๐ ๐๐ ๐๐ ๐๐ ๐๐ ๐๐ ๐๐ ๐๐ ๐๐ ๐๐ ๐๐ ๐๐ ๐๐ ๐๐ 27 Before entering the for loop 28 Before entering the for loop ๐ − ๐ points cones ๐๐ ๐๐ ๐๐ ๐๐ ๐๐ ๐๐ 29 INSERTING ๐TH POINT 30 ๐th iteration ๐๐ ๐๐ ๐๐ ๐๐ ๐๐ ๐๐ ๐๐ ๐๐ 31 ๐th iteration ๐๐ ๐ − ๐ points ๐๐ cones ๐๐ ๐๐ ๐๐ ๐๐ ๐๐ ๐๐ ๐๐ ๐๐ ๐๐ ๐๐ ๐๐ ๐๐ ๐๐ ๐๐ 32 ๐th iteration ๐๐ ๐ − ๐ points ๐๐ cones ๐๐ ๐๐ ๐๐ ๐๐ ๐๐ ๐๐ ๐๐ ๐๐ ๐๐ ๐๐ ๐๐ ๐๐ ๐๐ ๐๐ 33 ๐th iteration ๐๐ ๐ − ๐ points ๐๐ cones ๐๐ ๐๐ ๐๐ ๐๐ ๐๐ ๐๐ ๐๐ ๐๐ ๐๐ ๐๐ ๐๐ ๐๐ ๐๐ ๐๐ 34 ๐th iteration ๐๐ ๐ − ๐ points ๐๐ cones ๐๐ ๐๐ ๐๐ ๐๐ ๐๐ ๐๐ ๐๐ ๐๐ ๐๐ ๐๐ ๐๐ ๐๐ ๐๐ ๐๐ 35 ๐th iteration ๐๐ ๐ − ๐ points ๐๐ cones ๐๐ ๐๐ ๐๐ ๐๐ ๐๐ ๐๐ ๐๐ ๐๐ ๐๐ ๐๐ ๐๐ ๐๐ ๐๐ ๐๐ 36 ๐th iteration ๐๐ ๐ − ๐ points ๐๐ cones ๐๐ ๐๐ ๐๐ ๐๐ ๐๐ ๐๐ ๐๐ ๐๐ ๐๐ ๐๐ ๐๐ ๐๐ ๐๐ ๐๐ 37 ๐th iteration ๐๐ ๐ − ๐ points ๐๐ cones ๐๐ ๐๐ ๐๐ ๐๐ ๐๐ ๐๐ ๐๐ ๐๐ ๐๐ ๐๐ ๐๐ ๐๐ ๐๐ ๐๐ 38 ๐th iteration ๐๐ ๐ − ๐ points ๐๐ cones ๐๐ ๐๐ ๐๐ ๐๐ ๐๐ ๐๐ ๐๐ ๐๐ ๐๐ ๐๐ ๐๐ ๐๐ ๐๐ ๐๐ 39 ๐th iteration ๐ − ๐ points ๐๐ cones ๐๐ ๐๐ ๐′ ๐′ ๐" ๐๐ ๐๐ ๐" ๐๐ ๐๐ ๐๐ 40 Running time of ๐th iteration Running time of ๐th iteration is of the order of • Number of edges destroyed Total time for ๐ iterations = O(๐) • Number of new edges created • Number of points in the two adjacent cones that get created ๐ฟ๐ Question: What is the max. number of new edges created in an iteration ? Answer: 2 ๏จ Number of edges created during the algorithm = O(๐) Since every edge destroyed was once created, so Total number of edges destroyed < Total number of edges decreated 41 Backward analysis of ๐th iteration ๐ : a subset of ๐ points from ๐ท. ๐๐ : first ๐ points of ๐ท are some permutation of ๐ ๐[๐ฟ๐ ๐๐ = ?? 42 Backward analysis of ๐th iteration ๐๐ ๐๐ ๐ : a subset of ๐ points from ๐ท. ๐๐ : first ๐ points of ๐ท are some permutation of ๐ ๐๐ ๐๐ ๐๐ ๐๐ ๐๐ ๐๐ ๐[๐ฟ๐ ๐๐ = ?? ๐๐ ๐๐ ๐๐ ๐๐ ๐๐ ๐๐ ๐๐ ๐๐ 43 Backward analysis of ๐th iteration ๐๐ ๐๐ ๐ : a subset of ๐ points from ๐ท. ๐๐ : first ๐ points of ๐ท are some permutation of ๐ ๐๐ ๐๐ ๐๐ ๐๐ ๐๐ ๐๐ ๐[๐ฟ๐ ๐๐ = ?? ๐๐ ๐๐ ๐๐ ๐๐ ๐๐ ๐๐ ๐๐ ๐๐ 44 Backward analysis of ๐th iteration ๐๐ ๐๐ ๐ : a subset of ๐ points from ๐ท. ๐๐ : first ๐ points of ๐ท are some permutation of ๐ ๐๐ ๐๐ ๐๐ ๐๐ ๐๐ ๐๐ ๐๐ Calculating ๐[๐ฟ๐ ] : ๐๐ ๐๐ ๐๐ ๐๐ ๐๐ ๐๐ ๐[๐ฟ๐ ๐๐ = ??๐(๐ − ๐) ๐ ๐๐ Let ๐บ๐ be the set of all subsets of ๐ท of size ๐. ๐[๐ฟ๐ ] = = = ๐∈๐บ๐ ๐[๐ฟ๐ ๐∈๐บ๐ ๐(๐−๐) ๐ ๐๐ โ ๐(๐๐ ) ๐(๐−๐) โ ๐ ๐(๐๐ ) 45 Running time of the algorithm Expected running time of ith iteration = ๐[๐ฟ๐ ] + O(1) = O( ๐−๐ ) ๐ Expected running time of the algorithm = O(๐ ๐ฅ๐จ๐ ๐ ) Theorem: There is an O(๐ ๐ฅ๐จ๐ ๐ ) time Las Vegas algorithm for computing convex hull. 46 47