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)

Slides:



Advertisements
Similar presentations
2/9/06CS 3343 Analysis of Algorithms1 Convex Hull  Given a set of pins on a pinboard  And a rubber band around them  How does the rubber band look when.
Advertisements

Lecture 3: Parallel Algorithm Design
Theory of Computing Lecture 3 MAS 714 Hartmut Klauck.
Order Statistics(Selection Problem) A more interesting problem is selection:  finding the i th smallest element of a set We will show: –A practical randomized.
MA/CSSE 473 Day 13 Divide and Conquer Closest Points Convex Hull Answer Quiz Question 1.
CS 3343: Analysis of Algorithms Lecture 14: Order Statistics.
Prune-and-Search Method
Closest Pair Given a set S = {p1, p2,..., pn} of n points in the plane find the two points of S whose distance is the smallest. Images in this presentation.
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.
Lectures on Recursive Algorithms1 COMP 523: Advanced Algorithmic Techniques Lecturer: Dariusz Kowalski.
Divide and Conquer. Recall Complexity Analysis – Comparison of algorithm – Big O Simplification From source code – Recursive.
Spring 2015 Lecture 5: QuickSort & Selection
8/29/06CS 6463: AT Computational Geometry1 CS 6463: AT Computational Geometry Spring 2006 Convex Hulls Carola Wenk.
Nattee Niparnan. Recall  Complexity Analysis  Comparison of Two Algos  Big O  Simplification  From source code  Recursive.
CS38 Introduction to Algorithms Lecture 7 April 22, 2014.
Chapter 4 Divide-and-Conquer Copyright © 2007 Pearson Addison-Wesley. All rights reserved.
Chapter 4: Divide and Conquer The Design and Analysis of Algorithms.
Lecture 4: Divide and Conquer III: Other Applications and Examples Shang-Hua Teng.
Median, order statistics. Problem Find the i-th smallest of n elements.  i=1: minimum  i=n: maximum  i= or i= : median Sol: sort and index the i-th.
Closest Pair of Points Computational Geometry, WS 2006/07 Lecture 9, Part II Prof. Dr. Thomas Ottmann Khaireel A. Mohamed Algorithmen & Datenstrukturen,
Lecture 6 Divide and Conquer for Nearest Neighbor Problem Shang-Hua Teng.
CSE 421 Algorithms Richard Anderson Lecture 13 Divide and Conquer.
Nattee Niparnan. Recall  Complexity Analysis  Comparison of Two Algos  Big O  Simplification  From source code  Recursive.
1 Closest Pair of Points (from “Algorithm Design” by J.Kleinberg and E.Tardos) Closest pair. Given n points in the plane, find a pair with smallest Euclidean.
Order Statistics ● The ith order statistic in a set of n elements is the ith smallest element ● The minimum is thus the 1st order statistic ● The maximum.
Divide and Conquer Applications Sanghyun Park Fall 2002 CSE, POSTECH.
Order Statistics(Selection Problem)
Divide-and-Conquer. Outline Introduction Merge Sort Quick Sort Closest pair of points Large integer multiplication.
CSE 421 Algorithms Lecture 15 Closest Pair, Multiplication.
Lecture 5 Today, how to solve recurrences We learned “guess and proved by induction” We also learned “substitution” method Today, we learn the “master.
BINARY SEARCH CS16: Introduction to Data Structures & Algorithms Thursday February 12,
Advanced Algorithms Analysis and Design
Chapter 2. Divide-and-conquer Algorithms
Lecture 3: Parallel Algorithm Design
Rank Rank of an element is its position in ascending key order.
Chapter 4 Divide-and-Conquer
MA/CSSE 473 Day 17 Divide-and-conquer Convex Hull
Divide-and-Conquer 6/30/2018 9:16 AM
CS 3343: Analysis of Algorithms
MA/CSSE 473 Day 16 Answers to your questions Divide and Conquer
Randomized Algorithms
CSCE350 Algorithms and Data Structure
Algorithm design techniques Dr. M. Gavrilova
Jordi Cortadella and Jordi Petit Department of Computer Science
More on Merge Sort CS 244 This presentation is not given in class
Order Statistics(Selection Problem)
CS 3343: Analysis of Algorithms
Rank Rank of an element is its position in ascending key order.
Divide and Conquer / Closest Pair of Points Yin Tat Lee
Chapter 5 Divide and Conquer
Ch 7: Quicksort Ming-Te Chi
Randomized Algorithms
Punya Biswas Lecture 15 Closest Pair, Multiplication
Divide-and-Conquer 7 2  9 4   2   4   7
Medians and Order Statistics
Richard Anderson Lecture 13 Divide and Conquer
CS 3343: Analysis of Algorithms
Divide and Conquer / Closest Pair of Points Yin Tat Lee
Divide-and-Conquer The most-well known algorithm design strategy:
Order Statistics Def: Let A be an ordered set containing n elements. The i-th order statistic is the i-th smallest element. Minimum: 1st order statistic.
Divide-and-Conquer 7 2  9 4   2   4   7
Lecture 15, Winter 2019 Closest Pair, Multiplication
Divide-and-Conquer 7 2  9 4   2   4   7
CSE 332: Sorting II Spring 2016.
Lecture 15 Closest Pair, Multiplication
Divide and Conquer Merge sort and quick sort Binary search
Richard Anderson Lecture 13 Divide and Conquer
Divide-and-Conquer 7 2  9 4   2   4   7
Presentation transcript:

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)

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)

Closest Point Problem

Problem Statement Input: A set of points on the plane (given in the form of x and y coordinates) Output: One pair of the input points Goal: Return the closest pair

Example

What is the runtime for the naive Closest Point algorithm? O(log n) O(n) O(n log n) O(n2) O(n3)

What is the runtime for the naive Closest Point algorithm? O(log n) O(n) O(n log n) O(n2) O(n3)

Divide and Conquer: Attempt 1 Step 1: Split list in two based on median x value Step 2: Recursively apply algorithm to each half Step 3: Return the closer of the two pairs

Sample Execution

The algorithm always returns the optimal answer. True False

The algorithm always returns the optimal answer. True False

Sample Execution (Counter Example)

Divide and Conquer: Attempt 2 Step 1: Split in two based on median x value Step 2: Recursively apply algorithm to each half Step 3: Check every node on the left against every node on the right

What is the runtime of this formula? T(n) = 2T(n/2) + (n) T(n) = 2T(n/2) + (n2) T(n) = T(n/2) + (n) T(n) = T(n/2) + (n2)

What is the runtime of this formula? T(n) = 2T(n/2) + (n) T(n) = 2T(n/2) + (n2) T(n) = T(n/2) + (n) T(n) = T(n/2) + (n2)

Analysis T(n) = 2T(n/2) + (n2 ) a = 2, b = 2, logba = 1 f(n) = (n2) f(n) = (n1+ ) (and regularity holds) If we want T(n) = (n log n), we can only spend linear time in the split/merge  T(n) = (n2)

  L R  = min(L, R )

Divide and Conquer: Attempt 3 Step 1: Split in two based on median x value Step 2: Recursively apply algorithm to each partition Let L and R be the minimum distance on each side Let  = min(L, R) Step 3: Check only points within  of the dividing line against each other

Worst Case 2 Still requires O(n2 ) time 

Don’t need to look more than  below this point We only need to compare this point against those in the rectangle  L How many points can be in the rectangle? R  = min(L, R )

Point Limit Limit: 8 points in the rectangle    Region now blocked out – no points can fall in the blue   2  2 Could have two points here (one per partition) Assume this is in the left partition

Checking Points To process the “strip”: Sort the points by y coordinate For each point: If this point is the upper left-hand (or right-hand) corner of a box, the box can only contain seven more points Those points would have to be the next seven in the sorted order We need only compare this point against the next seven in the list (O(1) time)

Algorithm Sort points by x coordinate ((n log n)) Calculate partition point ((1)) Recursively find L, R and  ( 2T(n/2)) Remove elements outside of “strip” ((n)) Sort by y coordinate ((n log n)) For each point in strip: Check against the next seven points ((1)) T(n) = 2T(n/2) + (n log n)

Analysis T(n) = 2T(n/2) + (n log n) Master Theorem won’t work – this is the “gap” between case two and three Turns out: T(n) = (n log n) – not what we wanted We can speed this up pre-processing

Pre-processing Pre-processing: “Setup work” done before calling the algorithm In this case, before starting the algorithm: Sort all points by x coordinate Sort all points by y coordinates (Actually, sort a list of references into the point list – this gets tricky) If you maintain your data correctly, you will not need to sort again

Algorithm Sort points by x coordinate ((n log n)) Calculate partition point ((1)) Recursively find L, R and  (2T(n/2)) Remove elements outside of “strip” ((n)) Sort by y coordinate ((n log n)) For each point in strip: Check against the next seven points ((1)) T(n) = 2T(n/2) + (n log n) (n )

Analysis T(n) = 2T(n/2) + (n)

What is the runtime of this algorithm (not counting pre-processing) O(log n) O(n) O(n log n) O(n2)

What is the runtime of this algorithm (not counting pre-processing) O(log n) O(n) O(n log n) O(n2)

T(n) = 2T(n/2) + (n) Same as merge sort, so: T(n) = (n log n) Analysis T(n) = 2T(n/2) + (n) Same as merge sort, so: T(n) = (n log n) With preprocessing: n log n + T(n) – still (n log n)