MA/CSSE 473 Day 17 Divide-and-conquer Convex Hull

Slides:



Advertisements
Similar presentations
CPSC 411 Design and Analysis of Algorithms Set 3: Divide and Conquer Prof. Jennifer Welch Spring 2011 CPSC 411, Spring 2011: Set 3 1.
Advertisements

MA/CSSE 473 Day 13 Divide and Conquer Closest Points Convex Hull Answer Quiz Question 1.
Divide-and-Conquer The most-well known algorithm design 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
Theory of Algorithms: Divide and Conquer
Introduction to Algorithms Rabie A. Ramadan rabieramadan.org 6 Ack : Carola Wenk nad Dr. Thomas Ottmann tutorials.
DIVIDE AND CONQUER APPROACH. General Method Works on the approach of dividing a given problem into smaller sub problems (ideally of same size).  Divide.
Divide and Conquer. Recall Complexity Analysis – Comparison of algorithm – Big O Simplification From source code – Recursive.
Chapter 4 Divide-and-Conquer Copyright © 2007 Pearson Addison-Wesley. All rights reserved.
Nattee Niparnan. Recall  Complexity Analysis  Comparison of Two Algos  Big O  Simplification  From source code  Recursive.
Chapter 4 Divide-and-Conquer Copyright © 2007 Pearson Addison-Wesley. All rights reserved.
Chapter 4: Divide and Conquer The Design and Analysis of Algorithms.
5 - 1 § 5 The Divide-and-Conquer Strategy e.g. find the maximum of a set S of n numbers.
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.
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.
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.
MA/CSSE 473 Day 17 Permutations by lexicographic order number.
Divide and Conquer Andreas Klappenecker [based on slides by Prof. Welch]
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.
CSC-305 Design and Analysis of AlgorithmsBS(CS) -6 Fall-2014CSC-305 Design and Analysis of AlgorithmsBS(CS) -6 Fall-2014 Design and Analysis of Algorithms.
Divide and Conquer Andreas Klappenecker [based on slides by Prof. Welch]
Divide and Conquer Faculty Name: Ruhi Fatima Topics Covered Divide and Conquer Matrix multiplication Recurrence.
Lecture 6 Sorting II Divide-and-Conquer Algorithms.
MA/CSSE 473 Day 14 Strassen's Algorithm: Matrix Multiplication Decrease and Conquer DFS.
Algorithm Design Techniques, Greedy Method – Knapsack Problem, Job Sequencing, Divide and Conquer Method – Quick Sort, Finding Maximum and Minimum, Dynamic.
Prepared by John Reif, Ph.D.
Some more Decrease-and-conquer Algorithms
Fundamental Data Structures and Algorithms
Lecture 3: Parallel Algorithm Design
Chapter 4 Divide-and-Conquer
Chapter 5 Divide & conquer
MA/CSSE 473 Day 16 Answers to your questions Divide and Conquer
MA/CSSE 473 Day 02 Some Numeric Algorithms and their Analysis
Chapter 6 Transform-and-Conquer
Divide-and-Conquer The most-well known algorithm design strategy:
Chapter 4 Divide-and-Conquer
Convex Hull.
CSCE350 Algorithms and Data Structure
Divide-and-Conquer The most-well known algorithm design strategy:
CSCE 411 Design and Analysis of Algorithms
Quick Sort (11.2) CSE 2011 Winter November 2018.
Divide and Conquer Mergesort Quicksort Binary Search Selection
Algorithm Design Strategy Divide and Conquer Revisit
Divide-and-Conquer The most-well known algorithm design strategy:
Unit-2 Divide and Conquer
Decrease-and-Conquer
Divide-and-Conquer The most-well known algorithm design strategy:
Chapter 4 Divide-and-Conquer
Transform and Conquer Transform and Conquer Transform and Conquer.
Divide-and-Conquer The most-well known algorithm design strategy:
Transform and Conquer Transform and Conquer Transform and Conquer.
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
CSC 380: Design and Analysis of Algorithms
The Selection Problem.
Divide-and-Conquer 7 2  9 4   2   4   7
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
Algorithm Course Algorithms Lecture 3 Sorting Algorithm-1
Divide and Conquer Merge sort and quick sort Binary search
Presentation transcript:

MA/CSSE 473 Day 17 Divide-and-conquer Convex Hull Strassen's Algorithm: Matrix Multiplication (if time, Shell's Sort)

MA/CSSE 473 Day 17 Student Questions Exam 2 specification Levitin 3rd Edition Closest Pairs algorithm Convex Hull (Divide and Conquer) Matrix Multiplication (Strassen) Shell's Sort (a.k.a. shellsort) Pass around attendance sheet.

Levitin 3rd edition Closest Pair Algorithm Sorting by both X and Y coordinates happens once, before the recursive calls are made. When doing the comparisons in the inner loop, we compare all points that are in "y within d" range, not just those on opposite sides of the median line. Simpler but more distances to calculate than in what I presented on Friday. This is here for reference again

A fast algorithm for solving the Convex Hull problem QuickHuLL

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 and 5th terms of the determinant are the same for all points P3, so for each point we only have to do 4 multiplications and 4 additions each time.

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.

Faster Matrix multiplication Strassen's Divide-and-conquer algorithm Faster Matrix multiplication

Ordinary Matrix Multiplication How many additions and multiplications are needed to compute the product of two 2x2 matrices? C00 C01 A00 A01 B00 B01 = * C10 C11 A10 A11 B10 B11 [ ] [ ] [ ]

Strassen’s Matrix Multiplication Strassen observed [1969] that the product of two matrices can be computed as follows: C00 C01 A00 A01 B00 B01 = * C10 C11 A10 A11 B10 B11 M1 + M4 - M5 + M7 M3 + M5 = M2 + M4 M1 + M3 - M2 + M6 [ ] [ ] [ ] [ ] Values of M1, M2, … , M7 are on the next slide

Formulas for Strassen’s Algorithm M1 = (A00 + A11)  (B00 + B11) M2 = (A10 + A11)  B00 M3 = A00  (B01 - B11) M4 = A11  (B10 - B00) M5 = (A00 + A01)  B11 M6 = (A10 - A00)  (B00 + B01) M7 = (A01 - A11)  (B10 + B11) How many additions and multiplications?

The Recursive Algorithm We multiply square matrices whose size is a power of 2 (if not, pad with zeroes) Break up each matrix into four N/2 x N/2 submatrices. Recursively multiply the parts. How many additions and multiplications? If we do "normal matrix multiplication" recursively using divide and conquer? If we use Strassen's formulas? First analysis: T(n) = 8T(n/2) + 1. a = 8, b = 2, k = 0. Third case of master theorem. So T(n) = n^3 Second analysis (is on a later slide)

Analysis of Strassen’s Algorithm If N is not a power of 2, matrices can be padded with zeros. Number of multiplications: M(N) = 7M(N/2) + C, M(1) = 1 Solution: M(N) = (Nlog 27) ≈ N2.807 vs. N3 of brute-force algorithm. What if we also count the additions? Algorithms with better asymptotic efficiency are known but they are even more complex. BEFORE ANIMATION: Analysis M(N) = 7 M(N/2) = 49 M(N/4) …. How do we know that x^(log y) =y^(log x)? A(N) = 7A(N/2) + 18(N/2)2 We are doing 7 multiplications of smaller matrices, and 18 additions of smaller matrices. Use Master Theorem

Shell's sort (a.k.a. ShellSort) This is not a divide-and-conquer algorithm. Today just seemed like a time when we might have a few minutes in which to discuss this interesting sorting technique Insertion Sort on Steroids Shell's sort (a.k.a. ShellSort)

Insertion sort For what kind of arrays is insertion sort reasonably fast? What is the main speed problem with insertion sort in general? Shell's Sort is an attempt to improve that.

Shell's Sort We use the following gaps: 7, then 3, then 1 (last one must always be 1): Next, do the same thing for the next group of 7ths

Shell's sort 2

Shell's sort 3 Why bother if we are going to do a regular insertion sort at the end anyway? Analysis? No one has been able to analyze it yet, thought it works fast in practice if gap sequence is chosen well.

Code from Weiss book