MA/CSSE 473 Day 16 Answers to your questions Divide and Conquer

Slides:



Advertisements
Similar presentations
Lecture 3: Parallel Algorithm Design
Advertisements

MA/CSSE 473 Day 13 Divide and Conquer Closest Points Convex Hull Answer Quiz Question 1.
The Divide-and-Conquer Strategy
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 5 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.
1 Divide-and-Conquer The most-well known algorithm design strategy: 1. Divide instance of problem into two or more smaller instances 2. Solve smaller instances.
Divide and Conquer The most well known algorithm design strategy: 1. Divide instance of problem into two or more smaller instances 2. Solve smaller instances.
CS4413 Divide-and-Conquer
Chapter 4: Divide and Conquer Master Theorem, Mergesort, Quicksort, Binary Search, Binary Trees The Design and Analysis of Algorithms.
Theory of Algorithms: Divide and Conquer
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Divide and Conquer.
Introduction to Algorithms Rabie A. Ramadan rabieramadan.org 6 Ack : Carola Wenk nad Dr. Thomas Ottmann tutorials.
Computational Geometry
Chapter 4 Divide-and-Conquer Copyright © 2007 Pearson Addison-Wesley. All rights reserved.
Chapter 4: Divide and Conquer The Design and Analysis of Algorithms.
Prune-and-search Strategy
5 - 1 § 5 The Divide-and-Conquer Strategy e.g. find the maximum of a set S of n numbers.
Data Structures Review Session 1
Design and Analysis of Algorithms - Chapter 41 Divide and Conquer The most well known algorithm design strategy: 1. Divide instance of problem into two.
Divide and Conquer The most well known algorithm design strategy: 1. Divide instance of problem into two or more smaller instances 2. Solve smaller instances.
Design and Analysis of Algorithms - Chapter 41 Divide and Conquer The most well known algorithm design strategy: 1. Divide instance of problem into two.
MA/CSSE 473 Day 03 Asymptotics A Closer Look at Arithmetic With another student, try to write a precise, formal definition of “t(n) is in O(g(n))”
Convex Hull. What is the Convex Hull? Imagine a set of points on a board with a nail hammered into each point. Now stretch a rubber band over all the.
MA/CSSE 473 Day 17 Divide-and-conquer Convex Hull Strassen's Algorithm: Matrix Multiplication.
MA/CSSE 473 Day 02 Some Numeric Algorithms and their Analysis.
Computational Geometry (35/33) Line Segments and cross-product Segment intersection and Sweep Line Convex Hull and Graham’s Scan, Jarvis’s march Divide-and-Conquer.
Theory of Algorithms: Divide and Conquer James Gain and Edwin Blake {jgain | Department of Computer Science University of Cape Town.
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 5 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.
© The McGraw-Hill Companies, Inc., Chapter 6 Prune-and-Search Strategy.
The Selection Problem. 2 Median and Order Statistics In this section, we will study algorithms for finding the i th smallest element in a set of n elements.
1 Prune-and-Search Method 2012/10/30. A simple example: Binary search sorted sequence : (search 9) step 1  step 2  step 3  Binary search.
Divide and Conquer Applications Sanghyun Park Fall 2002 CSE, POSTECH.
MA/CSSE 473 Day 02 Some Numeric Algorithms and their Analysis.
MA/CSSE 473 Day 15 Return Exam Student questions Towers of Hanoi
Advanced Algorithms Analysis and Design
Chapter 2. Divide-and-conquer Algorithms
Lecture 3: Parallel Algorithm Design
Chapter 4 Divide-and-Conquer
MA/CSSE 473 Day 17 Divide-and-conquer Convex Hull
MA/CSSE 473 Day 13 Finish Topological Sort Permutation Generation
Chapter 5 Divide & conquer
MA/CSSE 473 Day 02 Some Numeric Algorithms and their Analysis
Divide-and-Conquer The most-well known algorithm design strategy:
Chapter 4 Divide-and-Conquer
Convex Hull.
CSCE350 Algorithms and Data Structure
Algorithm design techniques Dr. M. Gavrilova
Divide-and-Conquer The most-well known algorithm design strategy:
CSCE 411 Design and Analysis of Algorithms
Chapter 4: Divide and Conquer
Asymptotic Notations Algorithms Lecture 9.
Computational Geometry (35/33)
Divide-and-Conquer The most-well known algorithm design strategy:
Data Structures Review Session
Richard Anderson Lecture 13 Divide and Conquer
Richard Anderson Lecture 11 Minimum Spanning Trees
Decrease-and-Conquer
Divide-and-Conquer The most-well known algorithm design strategy:
Chapter 4 Divide-and-Conquer
CMPS 3130/6130: Computational Geometry Spring 2017
Divide-and-Conquer The most-well known algorithm design strategy:
Trevor Brown CS 341: Algorithms Trevor Brown
CMPS 3120: Computational Geometry Spring 2013
CSC 380: Design and Analysis of Algorithms
CSC 380: Design and Analysis of Algorithms
Introduction to Algorithms
CSC 380: Design and Analysis of Algorithms
The Selection Problem.
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)
Divide and Conquer Merge sort and quick sort Binary search
Presentation transcript:

MA/CSSE 473 Day 16 Answers to your questions Divide and Conquer Closest Points Convex Hull intro

Exercise from last time Which permutation follows each of these in lexicographic order? 183647520 471638520 Try to write an algorithm for generating the next permutation, with only the current permutation as input. If the lexicographic permutations of the numbers [0, 1, 2, 3, 4, 5] are numbered starting with 0, what is the number of the permutation 14032? General form? How to calculate efficiency? In the lexicographic ordering of permutations of [0, 1, 2, 3, 4, 5], which permutation is number 541? How to calculate efficiently? After 183647520: 183650247 After 471638520: 471650238 Algorithm: Scan from right, find first place where p[i] < p[i+1]. find j, position of smallest among numbers in p[i+1]…p[n-1] that are greater than p[i]. exchange p[i] and p[j], then reverse p[i+1] ..p[n-1]

Gray Code and Hamiltonian Cycles A Hamiltonian cycle in an undirected graph is … Hypercubes (picture is from Wikipedia): Binary-reflected Gray Code is a Hamiltonian Cycle of a Hypercube:

Divide and Conquer

Divide-and-conquer algorithms Definition List examples seen in prior courses or so far in this course Have students list some examples of divide-and-conquer algorithms

Divide and Conquer Algorithms Today: Closest Points, Convex Hull Divide and Conquer Algorithms

Divide-and-conquer algorithms Definition Examples seen prior to this course or so far in this course Have students list some examples of divide-and-conquer algorithms Q1

Closest Points problem Given a set, S, of N points in the xy-plane, find the minimum distance between two points in S. Running time for brute force algorithm? Next we examine a divide-and-conquer approach.

Closest Points "divide" phase S is a set of N points in the xy-plane For simplicity, we assume N = 2k for some k. (Otherwise use floor and ceiling functions) Sort the points by x-coordinate If two points have the same x-coordinate, order them by y-coordinate If we use merge sort, the worst case is Ѳ(N log N) Let c be the median x-value of the points Let S1 be {(x, y): x ≤ c}, and S2 be {(x, y): x ≥ c} adjust so we get exactly N/2 points in each subset

Closest Points "conquer" phase Assume that the points of S are sorted by x-coordinate, then by y-coordinate if x's are equal Let d1 be the minimum distance between two points in S1 (the set of "left half" points) Let d2 be the minimum distance between two points in S2 (the set of "right half" points) Let d = min(d1, d2). Is d the minimum distance for S? What else do we have to consider? Suppose we needed to compare every point in S1 to every point in S2. What would the running time be? How can we avoid doing so many comparisons? Next-to-last bullet: T(N) = 2T(N/2) + theta(N2). This is the first case of the Master Theorem. And so T(N) is theta(N2). MASTER THEOREM IS ON NEXT SLIDE

Reference: The Master Theorem The Master Theorem for Divide and Conquer recurrence relations: Consider the recurrence T(n) = aT(n/b) +f(n), T(1)=c, where f(n) = Ѳ(nk) and k≥0 , The solution is Ѳ(nk) if a < bk Ѳ(nk log n) if a = bk Ѳ(nlogba) if a > bk For details, see Levitin pages 483-485 or Weiss section 7.5.3. Grimaldi's Theorem 10.1 is a special case of the Master Theorem. We will use this theorem often. You should review its proof soon (Weiss's proof is a bit easier than Levitin's).

After recursive calls on S1 and S2 d = min(d1, d2). We only need to check points in sets C1 and C2, whose x coordinates are within d of c. And it turns out that for each point in C1, we need to check at most 6 points of C2 (see bottom picture). Assume that the recursive calls have left the two halves of S1 and S2 sorted by Y coordinate. Merge them O(N) so that S1 and S2 are sorted by Y coordinate. Pick out the points that are in C1 and C2. All of this is O(N). Now, we can scan from top to bottom in C1 and C2. For each point in C1, find the (up to 6) points in C2 that are in the rectangle, check the distances. T(N) = 2T(N/2) + Ѳ(N). Solution, according to the Master Theorem, is Ѳ(N log N)

Convex Hull Problem Again, sort by x-coordinate, with tie going to larger y-coordinate. Define the upper hull and lower hull. S1 is set of points above the P1Pn line To construct Upper Hull: If S1 is empty, the upper hull is simply the P1Pn line. Otherwise … Find Pmax, a point in S1 furthest away from the line. If there is a tie, select the point that maximizes the angle Pmax P1Pn. Next slide.

Recursive calculation of Upper Hull S1,1 is the set of points from S1 that are to the left of line P1Pmax. S1,2 is the set of points from S1 that are to the left of line PmaxPn. Note that these sets must be disjoint (or else we did not choose Pmax correctly) Pmax must be in the upper hull. No points inside the triangle can be in the upper hull. Recursively construct upper hulls of P1 U S1,1 U Pmax and of Pmax U S1,2 U Pn.

Simplifying the Calculations We can simplify two things at once: Finding the distance of P from line P1P2, and Determining whether P is "to the left" of P1P2 The area of the triangle through P1=(x1,y1), P2=(x2,y2), and P3=(x3,ye) is ½ of the absolute value of the determinant For a proof of this property, see http://mathforum.org/library/drmath/view/55063.html How do we use this to calculate distance from P to the line? The sign of the determinant is positive if the order of the three points is clockwise, and negative if it is counter-clockwise Clockwise means that P3 is "to the left" of directed line segment P1P2 Speeding up the calculation Speeding up he calculation of the determinant Note that the first two terms of the determinant are the same for all P3, so for each point we only have to do 4 multiplications and 4 additions.

Efficiency of quickhull algorithm What arrangements of points give us worst case behavior? Average case is much better. Why? Worst Case is O(n2). What is the worst case (none of the points are inside the triangle). Average turns out to be O(N), where N = number of points.