CMPSCI611: Three Divide-and-Conquer Examples Lecture 2

Similar documents
Class Note #14. In this class, we studied an algorithm for integer multiplication, which. 2 ) to θ(n

Divide and Conquer. Andreas Klappenecker. [based on slides by Prof. Welch]

Introduction to Algorithms

Algorithms and Data Structures Strassen s Algorithm. ADS (2017/18) Lecture 4 slide 1

CPS 616 DIVIDE-AND-CONQUER 6-1

Tutorials. Algorithms and Data Structures Strassen s Algorithm. The Master Theorem for solving recurrences. The Master Theorem (cont d)

b + O(n d ) where a 1, b > 1, then O(n d log n) if a = b d d ) if a < b d O(n log b a ) if a > b d

The Master Theorem for solving recurrences. Algorithms and Data Structures Strassen s Algorithm. Tutorials. The Master Theorem (cont d)

Grade 11/12 Math Circles Fall Nov. 5 Recurrences, Part 2

CS 577 Introduction to Algorithms: Strassen s Algorithm and the Master Theorem

Divide and Conquer. Maximum/minimum. Median finding. CS125 Lecture 4 Fall 2016

Divide and Conquer Strategy

Divide and Conquer Algorithms. CSE 101: Design and Analysis of Algorithms Lecture 14

1 Divide and Conquer (September 3)

CS/COE 1501 cs.pitt.edu/~bill/1501/ Integer Multiplication

Divide and Conquer Algorithms

Fast Convolution; Strassen s Method

/633 Introduction to Algorithms Lecturer: Michael Dinitz Topic: Asymptotic Analysis, recurrences Date: 9/7/17

Divide and Conquer: Polynomial Multiplication Version of October 1 / 7, 24201

1 Closest Pair of Points on the Plane

Computational Complexity. This lecture. Notes. Lecture 02 - Basic Complexity Analysis. Tom Kelsey & Susmit Sarkar. Notes

CS483 Design and Analysis of Algorithms

Divide and Conquer. CSE21 Winter 2017, Day 9 (B00), Day 6 (A00) January 30,

Introduction to Algorithms 6.046J/18.401J/SMA5503

Divide and Conquer. Chapter Overview. 1.2 Divide and Conquer

Mergesort and Recurrences (CLRS 2.3, 4.4)

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

Divide and conquer. Philip II of Macedon

Divide and Conquer. Arash Rafiey. 27 October, 2016

An analogy from Calculus: limits

(x 1 +x 2 )(x 1 x 2 )+(x 2 +x 3 )(x 2 x 3 )+(x 3 +x 1 )(x 3 x 1 ).

Lecture 1: Asymptotics, Recurrences, Elementary Sorting

Divide-and-Conquer. a technique for designing algorithms

Dynamic Programming: Matrix chain multiplication (CLRS 15.2)

Dynamic Programming (CLRS )

Growth of Functions (CLRS 2.3,3)

Matrix Multiplication

Asymptotic Analysis and Recurrences

Module 1: Analyzing the Efficiency of Algorithms

Bucket-Sort. Have seen lower bound of Ω(nlog n) for comparisonbased. Some cheating algorithms achieve O(n), given certain assumptions re input

1 Caveats of Parallel Algorithms

CSE332: Data Structures & Parallelism Lecture 2: Algorithm Analysis. Ruth Anderson Winter 2019

Inf 2B: Sorting, MergeSort and Divide-and-Conquer

Quicksort algorithm Average case analysis

CSE548, AMS542: Analysis of Algorithms, Fall 2017 Date: October 11. In-Class Midterm. ( 7:05 PM 8:20 PM : 75 Minutes )

Outline. 1 Introduction. Merging and MergeSort. 3 Analysis. 4 Reference

CSCI 3110 Assignment 6 Solutions

Lecture 17: Trees and Merge Sort 10:00 AM, Oct 15, 2018

Lecture 3. Big-O notation, more recurrences!!

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

CSE101: Design and Analysis of Algorithms. Ragesh Jaiswal, CSE, UCSD

When we use asymptotic notation within an expression, the asymptotic notation is shorthand for an unspecified function satisfying the relation:

University of the Virgin Islands, St. Thomas January 14, 2015 Algorithms and Programming for High Schoolers. Lecture 5

Partition and Select

Analysis of Algorithms [Reading: CLRS 2.2, 3] Laura Toma, csci2200, Bowdoin College

Chapter 5 Divide and Conquer

CS173 Running Time and Big-O. Tandy Warnow

Solving recurrences. Frequently showing up when analysing divide&conquer algorithms or, more generally, recursive algorithms.

CS 231: Algorithmic Problem Solving

! Break up problem into several parts. ! Solve each part recursively. ! Combine solutions to sub-problems into overall solution.

CS 4407 Algorithms Lecture 2: Iterative and Divide and Conquer Algorithms

1.1 Administrative Stuff

Divide and Conquer. Recurrence Relations

Copyright 2000, Kevin Wayne 1

Divide and Conquer. Andreas Klappenecker

CS483 Design and Analysis of Algorithms

MATRIX MULTIPLICATION AND INVERSION

When we use asymptotic notation within an expression, the asymptotic notation is shorthand for an unspecified function satisfying the relation:

Computational complexity and some Graph Theory

Data Structures and Algorithms

COMP 382: Reasoning about algorithms

Divide-and-conquer: Order Statistics. Curs: Fall 2017

Divide-and-conquer. Curs 2015

CSE332: Data Structures & Parallelism Lecture 2: Algorithm Analysis. Ruth Anderson Winter 2018

Divide and Conquer CPE 349. Theresa Migler-VonDollen

data structures and algorithms lecture 2

CSE332: Data Structures & Parallelism Lecture 2: Algorithm Analysis. Ruth Anderson Winter 2018

What Every Programmer Should Know About Floating-Point Arithmetic DRAFT. Last updated: November 3, Abstract

Methods for solving recurrences

Design and Analysis of Algorithms

The Divide-and-Conquer Design Paradigm

Introduction to Divide and Conquer

Fundamental Algorithms

Fundamental Algorithms

Introduction to Computer Science and Programming for Astronomers

Analysis of Algorithms - Using Asymptotic Bounds -

Analysis of Algorithms I: Asymptotic Notation, Induction, and MergeSort

Admin NP-COMPLETE PROBLEMS. Run-time analysis. Tractable vs. intractable problems 5/2/13. What is a tractable problem?

Solving Recurrences. Lecture 23 CS2110 Fall 2011

N/4 + N/2 + N = 2N 2.

Dot Products, Transposes, and Orthogonal Projections

Kartsuba s Algorithm and Linear Time Selection

Lecture 3: Big-O and Big-Θ

Divide and Conquer algorithms

Lecture 8 HASHING!!!!!

Linear Time Selection

CMPS 2200 Fall Divide-and-Conquer. Carola Wenk. Slides courtesy of Charles Leiserson with changes and additions by Carola Wenk

P, NP, NP-Complete, and NPhard

CS 470/570 Divide-and-Conquer. Format of Divide-and-Conquer algorithms: Master Recurrence Theorem (simpler version)

Algorithms, Design and Analysis. Order of growth. Table 2.1. Big-oh. Asymptotic growth rate. Types of formulas for basic operation count

Transcription:

CMPSCI611: Three Divide-and-Conquer Examples Lecture 2 Last lecture we presented and analyzed Mergesort, a simple divide-and-conquer algorithm. We then stated and proved the Master Theorem, which gives the big-o running time for divide-and-conquer algorithms with recurrences of a particular form: If T (n) satisfies T (n) = at (n/b) + Θ(n α ) and T (n) = O(1) for n c, we let β = log b a and then: If α > β, T (n) = Θ(n α ) If α = β, T (n) = Θ(n α log n) If α < β, T (n) = Θ(n β ) 1

Mergesort had a recurrence of T (n) = 2T (n/2) + Θ(n), and thus a solution of T (n) = Θ(n log n). Today we will see examples of all three cases of the Master Theorem. Note that when we have a recurrence of this form, with a Θ bound on the time to integrate the solutions, the result is a Θ bound on the worst-case time taken by this algorithm. It provides only a big-o bound on the worst-case time needed to solve the problem, since there might be a different, faster algorithm. Sometimes our analysis gives us only a big-o bound on the time to integrate the solutions, but the Master Theorem then still gives us a big-o result. To get a Θ bound on the time for the problem, we need a lower bound argument. For the sorting problem, with the right assumptions, we have a matching lower bound, but usually we don t have one. 2

Matrix Multiplication: If A and B are each n by n matrices over any ring, their product C = AB is defined by the rule: C i,j = k A i,k B k,j There is an algorithm to compute C that should be obvious from this definition (too obvious to require code). For each of the n 2 entries of C, for each k, we multiply A i,k by B k,j, requiring n scalar multiplications. Then we add up n products for each entry of C. All in all we use exactly n 3 multiplications and Θ(n 3 ) (actually n 3 n 2 ) additions, for Θ(n 3 ) total operations. We can t hope for fewer than 2n 2 operations because each entry of A or B might affect the answer and must be looked at in the worst case. Surprisingly, though, we can do better than the standard algorithm! 3

Before we present subcubic matrix multiplication, we will warm up with a related but simpler problem, Multiplying Integers: Our input is two n-bit integers a = i a i 2 i and b = i b i 2 i, given as sequences of bits. Our output is the integer c = ab, given in the same format. Note that c has at most 2n bits. Note: Most of the integers we see in daily life may be multiplied in a single step. But the algorithm we are about to present can be adapted to multiply integers stored in n words of fixed size, in a format like that used for the BigNum class in Java. For the time being we will count how many bit operations we use to multiply if we have words of O(1) bits each a word operation is only an abbreviation for O(1) bit operations anyway. Adding integers is easy in O(n) bit operations. The standard algorithm computes c = i,j a i b j 2 i+j by taking the AND of each a i with each b j, shifting the result into position, and adding the results in Θ(n 2 ) total time. 4

Divide and Conquer? If we write a = a 1 2 n/2 + a 2 and b = b 1 2 n/2 + b 2, then we can express ab in terms of these n/2-bit numbers as a 1 b 1 2 n + (a 1 b 2 + a 2 b 1 )2 n/2 + a 2 b 2. We can thus multiply the n-bit numbers by carrying out four multiplications of n/2-bit numbers, plus a constant number of additions. Our recurrence is T (n) = 4T (n/2)+ Θ(n), fitting the Master Theorem with a = 4 and b = 2. But since β = log 2 4 = 2, this algorithm also takes Θ(n 2 ) time, the same as the standard algorithm. 5

A Trick: There are three coefficients we need to compute, and we originally thought of four binary products we could use, along with additions, to get them. But these four binary products are not linearly independent. It turns out that if we compute just three binary products: P 1 = a 1 b 1 P 2 = a 2 b 2 P 3 = (a 1 + a 2 )(b 1 + b 2 ) then the three coefficients that we want are P 1, P 3 P 1 P 2, and P 2. These three multiplications of n/2-bit numbers, together with a constant number of additions (a larger constant number, note), allow us to multiply the n-bit numbers. The new recurrence is: T (n) = 3T (n/2) + Θ(n) which has solution T (n) = Θ(n log 2 3 ) = Θ(n 1.59... ). There is a way to use the Fast Fourier Transform algorithm from next lecture to multiply two n-bit numbers even faster, in Θ(n log n log log n) time. The obvious lower bound on the time needed is only Ω(n), the time needed to read all the input. 6

Back To Matrix Mulitiplication: Just as we could write an n-bit integer as a shifted sum of two n/2-bit integers, we can write an n by n matrix as a 2 by 2 matrix whose entries are n/2 by n/2 matrices. A = B = AB = A 11 A 12 A 21 A 22 B 11 B 12 B 21 B 22 A 11 B 11 + A 12 B 21 A 11 B 12 + A 12 B 22 A 21 B 11 + A 22 B 21 A 21 B 12 + A 22 B 22 Simple Divide-and-Conquer: There are eight products of n/2 by n/2 matrices, plus a constant number of additions of such matrices. (Note that two n by n matrices may be added in Θ(n 2 ) time.) The recurrence here is T (n) = 8T (n/2) + Θ(n 2 ), which has solution T (n) = Θ(n 3 ) because β = log 2 8 = 3. So this is no better than the standard algorithm. 7

Strassen s Trick: We want four particular sums of products of the eight n/2 by n/2 matrices. There are a variety of ways we might multiply one sum of matrices by another, beyond the eight simple combinations we just used. Strassen was able to find a set of seven products of sums that could be used to form all four necessary coefficients: P 1 = (A 11 + A 22 )(B 11 + B 22 ) P 2 = (A 21 + A 22 )(B 11 ) P 3 = (A 11 )(B 12 B 22 ) P 4 = (A 22 )( B 11 + B 21 ) P 5 = (A 11 + A 12 )(B 22 ) P 6 = ( A 11 + A 21 )(B 11 + B 12 ) P 7 = (A 12 A 22 )(B 21 + B 22 ) The product matrix is expressed as follows: AB = P 1 + P 4 P 5 + P 7 P 3 + P 5 P 2 + P 4 P 1 P 2 + P 3 + P 6 8

The new recurrence is T (n) = 7T (n/2)+θ(n 2 ). The new β is log 2 7 = 2.81..., so the solution of T (n 2.81... ) is indeed subcubic. There are more recent algorithms that are (at least theoretically) even faster than Strassen the current champion is by Coppersmith and Winograd (1990) and takes time O(n 2.376... ). CLRS, Chapter 28, has more on how Strassen may have gone about finding his solution. 9

Is Strassen s algorithm practical? At some value of n, it ought to be, because a function that is o(n 3 ) will eventually overtaken by one that is Θ(n 3 ) as n increases. When this happens depends, of course, on the exact constants and lower-order terms in the two time functions. Further complications come from details of the implementation. For example, copies from one location to another within the RAM of the computer are always much faster than those that involve the external memory. Once matrices get too big to fit in RAM, things slow down dramatically. As the notes say, different sources quote quite different numbers for this tradeoff point, ranging from n = 8 to n = 100. Of course, when we use Strassen we will actually use a hybrid algorithm that uses Strassen s trick to subdivide the matrices only until they are small enough that the standard algorithm is faster. Similarly, a Mergesort algorithm might use another general sorting algorithm or even special-purpose code on sufficiently small lists. 10

The Closest Pair Problem: Our final example of a divide-and-conquer algorithm comes from computational geometry. Suppose that we are given n points in a plane, each given by a pair of real (actually floating-point) numbers. We want to find which of the ( ) n 2 pairs of points has the shortest distance between them. We can do this in Θ(n 2 ) time by computing the distance for each of the pairs and taking the minimum of all these distances. There is a clear Ω(n) lower bound on the time needed, because if we never look at one of the points it might turn out to be very close to another point. We ll use a recursive algorithm to solve this problem in time O(n log n). The base case of n 2 is easy, as we have at most one pair to check. 11

Dividing the Problem in Half: The basic idea will be to draw a vertical line that has n/2 points on each side of it. Once we do this, we can apply the algorithm recursively to find the closest pair on each side. But there are a number of issues left after we have this idea: 1. How do we find the correct vertical line, and how long does it take? 2. What if the closest pair involves one point on each side of the line? We don t want to check all the pairs that cross the line, because there are n 2 /4 of them! The first problem is not so bad we sort the points by x-coordinate, taking Θ(n log n) time, and pick the median coordinate as the position of our line. Any points exactly on this line can be arbitrarily assigned to one side or the other to make the sides have exactly n/2 points each (again we assume that n is a power of two). 12

Checking Pairs that Cross the Line: The key insight is that we only care about a pair that crosses the line if its distance is smaller than the smallest distance that doesn t cross the line. Let δ L be the smallest distance found by the recursive call to the left, and δ R the smallest distance found on the right. Let δ be the minimum of these two. The only points that could be part of interesting line-crossing pairs are those within δ of the line. We can find this set of points, called P M, easily (in O(n) time) from the list of points sorted by x-coordinate. Of course, there is no reason why most or all of the n points might not be in P M, so we need a better idea than just checking all the pairs of points in P M. 13

But a point in P M can be close to another point in P M only if they are close in their y-coordinate as well as in their x-coordinate. If we sort P M by y-coordinate, we only have to check the distance of each point to the next few points in the y list. This is because points in P M on the same side of the vertical line are guaranteed to be at least δ apart, so only so many can fit into the 2δ by δ box in P M just above any given point x. (We only check above x because we will cover the pairs where x is the higher point when we check the box above the other point.) There s a diagram on page 16 of the printed notes that shows (or would show, if the captions were readable) that there might be as many as seven points in this box to check for distance to x. This means that even if there are Θ(n) points in P M, once we sort them we have to check only O(1) pairs for each, so we can do this in O(n) time. 14

What s the recurrence resulting from this? To find the closest pair, we: 1. Sorted the points by x-coordinate O(n log n) time 2. Found δ L and δ R 2T (n/2) time 3. Found P M O(n) time 4. Sorted P M by y-coordinate O(n log n) time, and 5. Checked each point in P M against its close neighbors O(n) time. This gives us T (n) = 2T (n/2) + O(n log n), which is not a case of the Master Theorem but (as you ll show on HW#1) has a solution of T (n) = O(n log 2 n). This is much better than O(n 2 ), but we can actually do a little better. 15

If we sort the entire set of points once by y-coordinate at the beginning of the algorithm, this costs us O(n log n) time up front. But then we can sort any subset of the points in O(n) time, by moving down the sorted list and picking out the points we want. This gives us: T (n) = 2T (n/2) + Θ(n) which we know has solution T (n) = Θ(n log n). Adding in the time for the presort keeps us at Θ(n log n). This is actually the best possible running time for this problem the lower bound is the sort of thing discussed in the computational geometry course taught by Profs. Brock and Streinu. 16