Algorithm Design and Analysis

Similar documents
5.4 Closest Pair of Points

Divide and Conquer 1

Ch5. Divide-and-Conquer

CSE 421 Closest Pair of Points, Master Theorem, Integer Multiplication

Closest Pair of Points. Cormen et.al 33.4

Chapter 5. Divide and Conquer. Slides by Kevin Wayne. Copyright 2005 Pearson-Addison Wesley. All rights reserved.

Divide-and-Conquer. Combine solutions to sub-problems into overall solution. Break up problem of size n into two equal parts of size!n.

Divide-Conquer-Glue Algorithms

5. DIVIDE AND CONQUER I

Closest Pair of Points in the Plane. Closest pair of points. Closest Pair of Points. Closest Pair of Points

Plan for Today. Finish recurrences. Inversion Counting. Closest Pair of Points

CSE 202 Divide-and-conquer algorithms. Fan Chung Graham UC San Diego

CSE 202 Divide-and-conquer algorithms. Fan Chung Graham UC San Diego

CSE 421 Algorithms: Divide and Conquer

Lecture 4 CS781 February 3, 2011

CS473 - Algorithms I

Divide and conquer algorithms. March 12, 2018 CSCI211 - Sprenkle. What is a recurrence rela&on? How can you compute D&C running &mes?

CSC Design and Analysis of Algorithms

CSE 202: Design and Analysis of Algorithms Lecture 5

CSC Design and Analysis of Algorithms. Lecture 6. Divide and Conquer Algorithm Design Technique. Divide-and-Conquer

CSE 421 Greedy Alg: Union Find/Dijkstra s Alg

Divide-and-Conquer. The most-well known algorithm design strategy: smaller instances. combining these solutions

Advanced Algorithms. Problem solving Techniques. Divide and Conquer הפרד ומשול

Chapter 4. Divide-and-Conquer. Copyright 2007 Pearson Addison-Wesley. All rights reserved.

Divide-and-Conquer Algorithms

Randomized algorithms. Inge Li Gørtz

CSE 202: Design and Analysis of Algorithms Lecture 4

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

Data Structures and Algorithms CMPSC 465

Introduction to Algorithms

Divide and Conquer. Algorithm Fall Semester

1 Closest Pair Problem

Algorithms: Lecture 7. Chalmers University of Technology

Divide-and-Conquer. Dr. Yingwu Zhu

Computer Science 385 Analysis of Algorithms Siena College Spring Topic Notes: Divide and Conquer

Data Structures and Algorithms CSE 465

Parallel Models RAM. Parallel RAM aka PRAM. Variants of CRCW PRAM. Advanced Algorithms

CS 372: Computational Geometry Lecture 3 Line Segment Intersection

CS 6463: AT Computational Geometry Spring 2006 Convex Hulls Carola Wenk

In what follows, we will focus on Voronoi diagrams in Euclidean space. Later, we will generalize to other distance spaces.

CSCE 411 Design and Analysis of Algorithms

Union Find. Data Structures and Algorithms Andrei Bulatov

Design and Analysis of Algorithms Prof. Madhavan Mukund Chennai Mathematical Institute

Chapter 1 Divide and Conquer Algorithm Theory WS 2015/16 Fabian Kuhn

Jana Kosecka. Linear Time Sorting, Median, Order Statistics. Many slides here are based on E. Demaine, D. Luebke slides

Computational Geometry

Computer Science Approach to problem solving

Lecture 2: Divide and Conquer

CS583 Lecture 01. Jana Kosecka. some materials here are based on Profs. E. Demaine, D. Luebke A.Shehu, J-M. Lien and Prof. Wang s past lecture notes

CS Divide and Conquer

Chapter 1 Divide and Conquer Algorithm Theory WS 2013/14 Fabian Kuhn

Algorithm Design and Analysis

CS483 Analysis of Algorithms Lecture 03 Divide-n-Conquer

Triangulation and Convex Hull. 8th November 2018

Theory and Algorithms Introduction: insertion sort, merge sort

Geometric Computation: Introduction. Piotr Indyk

Chapter 1 Divide and Conquer Algorithm Theory WS 2013/14 Fabian Kuhn

Parallel Algorithms for (PRAM) Computers & Some Parallel Algorithms. Reference : Horowitz, Sahni and Rajasekaran, Computer Algorithms

CSC Design and Analysis of Algorithms. Lecture 6. Divide and Conquer Algorithm Design Technique. Divide-and-Conquer

Midterm solutions. n f 3 (n) = 3

EECS 2011M: Fundamentals of Data Structures

17/05/2018. Outline. Outline. Divide and Conquer. Control Abstraction for Divide &Conquer. Outline. Module 2: Divide and Conquer

A 0 A 1... A i 1 A i,..., A min,..., A n 1 in their final positions the last n i elements After n 1 passes, the list is sorted.

CS Divide and Conquer

DIVIDE & CONQUER. Problem of size n. Solution to sub problem 1

UML CS Algorithms Qualifying Exam Fall, 2003 ALGORITHMS QUALIFYING EXAM

Algorithm Design and Analysis

Cilk, Matrix Multiplication, and Sorting

Lecture 4: Geometric Algorithms (Convex Hull, Voronoi diagrams)

Graduate Algorithms CS F-19 Computational Geometry

CMPSCI 311: Introduction to Algorithms Practice Final Exam

Union Find 11/2/2009. Union Find. Union Find via Linked Lists. Union Find via Linked Lists (cntd) Weighted-Union Heuristic. Weighted-Union Heuristic

CSE 3101: Introduction to the Design and Analysis of Algorithms. Office hours: Wed 4-6 pm (CSEB 3043), or by appointment.

Divide and Conquer. Divide and Conquer

CS 410/584, Algorithm Design & Analysis, Lecture Notes 8!

Parallel Algorithms CSE /22/2015. Outline of this lecture: 1 Implementation of cilk for. 2. Parallel Matrix Multiplication

I/O Model. Cache-Oblivious Algorithms : Algorithms in the Real World. Advantages of Cache-Oblivious Algorithms 4/9/13

Algorithms and Data Structures, or

Introduction to Algorithms 6.046J/18.401J

Problem. Input: An array A = (A[1],..., A[n]) with length n. Output: a permutation A of A, that is sorted: A [i] A [j] for all. 1 i j n.

Chapter 3:- Divide and Conquer. Compiled By:- Sanjay Patel Assistant Professor, SVBIT.

Master Theorem, Introduction to Graphs

Algorithms and Data Structures. Algorithms and Data Structures. Algorithms and Data Structures. Algorithms and Data Structures

Second Midterm Exam, CMPSC 465, Spring 2009 Practice problems

Rank. Selection Problem

Selection Problem. Rank. Divide-And-Conquer Selection. Selection By Sorting

Computational Geometry Overview from Cormen, et al.

Geometric Primitives. primitive operations convex hull closest pair voronoi diagram. primitive operations convex hull closest pair voronoi diagram

Lecture 18. What we ve done and what s to come

Chapter 2: Divide and Conquer

Simple Sorting Algorithms

08 A: Sorting III. CS1102S: Data Structures and Algorithms. Martin Henz. March 10, Generated on Tuesday 9 th March, 2010, 09:58

(Master Course) Mohammad Farshi Department of Computer Science, Yazd University. Yazd Univ. Computational Geometry.

CS 360 Exam 1 Fall 2014 Name. 1. Answer the following questions about each code fragment below. [8 points]

Chapter 1 Divide and Conquer Algorithm Theory WS 2014/15 Fabian Kuhn

n = 1 What problems are interesting when n is just 1?

CS61B Lectures # Purposes of Sorting. Some Definitions. Classifications. Sorting supports searching Binary search standard example

Multithreaded Programming in. Cilk LECTURE 2. Charles E. Leiserson

Introduction to Algorithms 6.046J/18.401J/SMA5503

Convex Hull Algorithms

Transcription:

Algorithm Design and Analysis LECTURE 13 Divide and Conquer Closest Pair of Points Convex Hull Strassen Matrix Mult. Adam Smith 9/24/2008 A. Smith; based on slides by E. Demaine, C. Leiserson, S. Raskhodnikova, K. Wayne

Midterm Exam #1 Willard Building Room 76 Tuesday night, 8:15pm You may bring: one (1) double-sided, hand-written 8.5 x 11 sheet of notes on colored paper Hint: use its preparation as a study aid 9/24/2008 A. Smith; based on slides by E. Demaine, C. Leiserson, S. Raskhodnikova, K. Wayne

Review questions Find the solution to the recurrence using MT: T(n)=8T(n/2)+cn. (Answer: log b (a)=3>1, solution is Θ(n 3 ).) Draw the recursion tree for this recurrence. a. What is its height? b. What is the number of leaves in the tree? (Answer: h=log n.) (Answer: 8 h = 8 log n = n log 8 = n 3.) 9/24/2008 A. Smith; based on slides by E. Demaine, C. Leiserson, S. Raskhodnikova, K. Wayne

Review questions: recursion tree Solve T(n)=8T(n/2)+cn: c(n/2) cn c(n/4) c(n/4) c(n/4) (n/4) 8 c(n/2) 8 8 cn 4cn 16cn 9/24/2008 Θ(1) Total = cn(1+4+4 2 +4 3 + +n 2 ) = Θ(n 3 ) geometric series A. Smith; based on slides by E. Demaine, C. Leiserson, S. Raskhodnikova, K. Wayne

Appendix: geometric series for x 1 for x < 1 Return to last slide viewed. 9/24/2008 A. Smith; based on slides by E. Demaine, C. Leiserson, S. Raskhodnikova, K. Wayne

Closest Pair of Points Closest pair. Given n points in the plane, find a pair with smallest Euclidean distance between them. Fundamental geometric primitive. Graphics, computer vision, geographic information systems, molecular modeling, air traffic control. Special case of nearest neighbor, Euclidean MST, Voronoi. fast closest pair inspired fast algorithms for these problems Brute force. Check all pairs of points p and q with Θ(n 2 ) comparisons. 1-D version (points on a line): O(n log n) easy via sorting Assumption. No two points have same x coordinate. to make presentation cleaner

Closest Pair of Points: First Attempt Divide. Sub-divide region into 4 quadrants. L

Closest Pair of Points: First Attempt Divide. Sub-divide region into 4 quadrants. Obstacle. Impossible to ensure n/4 points in each piece. L

Closest Pair of Points Algorithm. Divide: draw vertical line L so that roughly ½n points on each side. L

Closest Pair of Points Algorithm. Divide: draw vertical line L so that roughly ½n points on each side. Conquer: find closest pair in each side recursively. L 21 16

Closest Pair of Points Algorithm. Divide: draw vertical line L so that roughly ½n points on each side. Conquer: find closest pair in each side recursively. Combine: find closest pair with one point in each side. seems like Θ(n 2 ) Return best of 3 solutions. L 8 21 16

Closest Pair of Points Find closest pair with one point in each side, assuming that distance < δ. L 21 16 δ = min(16, 21)

Closest Pair of Points Find closest pair with one point in each side, assuming that distance < δ. Observation: only need to consider points within δ of line L. L 21 16 δ = min(16, 21) δ

Closest Pair of Points Find closest pair with one point in each side, assuming that distance < δ. Observation: only need to consider points within δ of line L. Sort points in 2δ-strip by their y coordinate. 7 L 6 4 5 21 16 3 δ = min(16, 21) 2 1 δ

Closest Pair of Points Find closest pair with one point in each side, assuming that distance < δ. Observation: only need to consider points within δ of line L. Sort points in 2δ-strip by their y coordinate. Theorem: Only need to check distances of those within 11 positions in sorted list! 7 L 6 4 5 21 16 3 δ = min(16, 21) 2 1 δ

Closest Pair of Points Def. Let s i be the point in the 2δ-strip, with the i th smallest y-coordinate. Claim. If i j 12, then the distance between s i and s j is at least δ. 31 39 j Proof. No two points lie in same ½δ-by-½δ box. Two points at least 2 rows apart have distance 2(½δ). 2 rows 29 30 ½δ ½δ i 27 28 ½δ Fact. Still true if we replace 12 with 7. 26 25 δ δ

Closest Pair Algorithm Closest-Pair(p 1,, p n ) { Compute separation line L such that half the points are on one side and half on the other side. δ 1 = Closest-Pair(left half) δ 2 = Closest-Pair(right half) δ = min(δ 1, δ 2 ) Delete all points further than δ from separation line L Sort remaining points by y-coordinate. Scan points in y-order and compare distance between each point and next 11 neighbors. If any of these distances is less than δ, update δ. O(n log n) 2T(n / 2) O(n) O(n log n) O(n) } return δ.

Closest Pair of Points: Analysis Running time. Q. Can we achieve O(n log n)? A. Yes. Don't sort points in strip from scratch each time. Sort entire point set by x-coordinate only once Each recursive call takes as input a set of points sorted by x coordinates and returns the same points sorted by y coordinate (together with the closest pair) Create new y-sorted list by merging two output from recursive calls Totaltime(n) = O(n log(n)) + T(n) T(n) 2T( n /2) + O(n) T(n) = O(n log n)

Divide and Conquer in low-dimensional geometry Powerful technique for low-dimensional geometric problems Intuition: points in different parts of the plane don t interfere too much Example: convex hull in O(n log (n)) time a la MergeSort 1. Convex-Hull(left-half) 2. Convex-Hull(right-half) 3. Merge (see Cormen et al., Chap 33) T(n/2) T(n/2) Θ(n)

Matrix multiplication Input: A = [a ij ], B = [b ij ]. Output: C = [c ij ] = A B. i, j = 1, 2,, n. 9/24/2008 A. Smith; based on slides by E. Demaine, C. Leiserson, S. Raskhodnikova, K. Wayne

Standard algorithm for i 1 to n do for j 1 to n do c ij 0 for k 1 to n do c ij c ij + a ik b kj Running time = Θ(n 3 ) 9/24/2008 A. Smith; based on slides by E. Demaine, C. Leiserson, S. Raskhodnikova, K. Wayne

Divide-and-conquer algorithm IDEA: n n matrix = 2 2 matrix of (n/2) (n/2) submatrices: r = ae + bg s = af + bh t = ce + dh u = cf + dg C = A B recursive 8 mults of (n/2) (n/2) submatrices ^ 4 adds of (n/2) (n/2) submatrices 9/24/2008 A. Smith; based on slides by E. Demaine, C. Leiserson, S. Raskhodnikova, K. Wayne

Analysis of D&C algorithm T(n) = 8 T(n/2) + Θ(n 2 ) # submatrices submatrix size work adding submatrices n log ba = n log 28 = n 3 CASE 1 T(n) = Θ(n 3 ). No better than the ordinary algorithm. 9/24/2008 A. Smith; based on slides by E. Demaine, C. Leiserson, S. Raskhodnikova, K. Wayne

9/24/2008 Strassen s idea Multiply 2 2 matrices with only 7 recursive mults. P 1 = a ( f h) P 2 = (a + b) h P 3 = (c + d) e P 4 = d (g e) P 5 = (a + d) (e + h) P 6 = (b d) (g + h) P 7 = (a c) (e + f ) r = P 5 + P 4 P 2 + P 6 s = P 1 + P 2 t = P 3 + P 4 u = P 5 + P 1 P 3 P 7 7 mults, 18 adds/subs. Note: No reliance on commutativity of multiplication! A. Smith; based on slides by E. Demaine, C. Leiserson, S. Raskhodnikova, K. Wayne

Strassen s idea Multiply 2 2 matrices with only 7 recursive mults. P 1 = a ( f h) P 2 = (a + b) h P 3 = (c + d) e P 4 = d (g e) P 5 = (a + d) (e + h) P 6 = (b d) (g + h) P 7 = (a c) (e + f ) r = P 5 + P 4 P 2 + P 6 = (a + d) (e + h) + d (g e) (a + b) h + (b d) (g + h) = ae + ah + de + dh + dg de ah bh + bg + bh dg dh = ae + bg 9/24/2008 A. Smith; based on slides by E. Demaine, C. Leiserson, S. Raskhodnikova, K. Wayne

Strassen s algorithm 1.Divide: Partition A and B into (n/2) x (n/2) submatrices. Form terms to be multiplied using + and. 2.Conquer: Perform 7 multiplications of (n/2) x (n/2) submatrices recursively. 3.Combine: Form product matrix C using + and on (n/2) x (n/2) submatrices. T(n) = 7 T(n/2) + Θ(n 2 ) 9/24/2008 A. Smith; based on slides by E. Demaine, C. Leiserson, S. Raskhodnikova, K. Wayne

Analysis of Strassen T(n) = 7 T(n/2) + Θ(n 2 ) n log ba = n log 27 n 2.81 CASE 1 T(n) = Θ(n lg 7 ). Number 2.81 may not seem much smaller than 3. But the difference is in the exponent. The impact on running time is significant. Strassen s algorithm beats the ordinary algorithm on today s machines for n 32 or so. Best to date (of theoretical interest only): Θ(n 2.376 ). 9/24/2008 A. Smith; based on slides by E. Demaine, C. Leiserson, S. Raskhodnikova, K. Wayne