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

Similar documents
The Divide-and-Conquer Design Paradigm

Introduction to Algorithms 6.046J/18.401J/SMA5503

CS60020: Foundations of Algorithm Design and Machine Learning. Sourangshu Bhattacharya

Divide-and-conquer algorithm

Chapter 2. Recurrence Relations. Divide and Conquer. Divide and Conquer Strategy. Another Example: Merge Sort. Merge Sort Example. Merge Sort Example

Algorithm Design and Analysis

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

Chapter 4 Divide-and-Conquer

What we have learned What is algorithm Why study algorithm The time and space efficiency of algorithm The analysis framework of time efficiency Asympt

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

CPS 616 DIVIDE-AND-CONQUER 6-1

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

Design and Analysis of Algorithms

Data Structures and Algorithms CSE 465

Introduction to Algorithms 6.046J/18.401J LECTURE 3 Divide and conquer Binary search Powering a number Fibonacci numbers Matrix multiplication

Data Structures and Algorithms Chapter 3

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

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

The maximum-subarray problem. Given an array of integers, find a contiguous subarray with the maximum sum. Very naïve algorithm:

A design paradigm. Divide and conquer: (When) does decomposing a problem into smaller parts help? 09/09/ EECS 3101

Analysis of Algorithms - Using Asymptotic Bounds -

1 Substitution method

Divide and Conquer Algorithms

Asymptotic Analysis and Recurrences

Divide-and-Conquer. Reading: CLRS Sections 2.3, 4.1, 4.2, 4.3, 28.2, CSE 6331 Algorithms Steve Lai

Divide-and-Conquer Algorithms and Recurrence Relations. Niloufar Shafiei

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

Data Structures and Algorithms CMPSC 465

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

Divide and Conquer. Andreas Klappenecker

Algorithms Design & Analysis. Divide & Conquer

V. Adamchik 1. Recurrences. Victor Adamchik Fall of 2005

CS 5321: Advanced Algorithms - Recurrence. Acknowledgement. Outline. Ali Ebnenasir Department of Computer Science Michigan Technological University

Recurrence Relations

Algorithms Chapter 4 Recurrences

CS 5321: Advanced Algorithms Analysis Using Recurrence. Acknowledgement. Outline

Data Structures and Algorithms Chapter 3

Design and Analysis of Algorithms Recurrence. Prof. Chuhua Xian School of Computer Science and Engineering

ITEC2620 Introduction to Data Structures

Divide and Conquer. Recurrence Relations

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

Lecture 4. Quicksort

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

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

COMP 382: Reasoning about algorithms

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

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

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

Design and Analysis of Algorithms

Fast Convolution; Strassen s Method

Data Structures and Algorithms CSE 465

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

MA008/MIIZ01 Design and Analysis of Algorithms Lecture Notes 3

Divide and Conquer algorithms

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

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

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

Chapter 2. Divide-and-conquer. 2.1 Strassen s algorithm

Objective. - mathematical induction, recursive definitions - arithmetic manipulations, series, products

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

Recurrence Relations

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

Solving Recurrences. Lecture 23 CS2110 Fall 2011

Chapter. Divide-and-Conquer. Contents

A SUMMARY OF RECURSION SOLVING TECHNIQUES

COMP Analysis of Algorithms & Data Structures

COMP Analysis of Algorithms & Data Structures

CIS 121. Analysis of Algorithms & Computational Complexity. Slides based on materials provided by Mary Wootters (Stanford University)

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

Divide and Conquer. Arash Rafiey. 27 October, 2016

Divide and Conquer Algorithms

Algorithms Design & Analysis. Analysis of Algorithm

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

Analysis of Algorithms CMPSC 565

Powering a number. More Divide & Conquer

CSE 421 Algorithms: Divide and Conquer

CS173 Running Time and Big-O. Tandy Warnow

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

Divide and Conquer Algorithms

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

Review Of Topics. Review: Induction

Algorithms. Adnan YAZICI Dept. of Computer Engineering Middle East Technical Univ. Ankara - TURKEY. Algorihms, A.Yazici, Fall 2007 CEng 315

Matrix Multiplication

Answer the following questions: Q1: ( 15 points) : A) Choose the correct answer of the following questions: نموذج اإلجابة

Algorithm efficiency analysis

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

CMPT 307 : Divide-and-Conqer (Study Guide) Should be read in conjunction with the text June 2, 2015

Mergesort and Recurrences (CLRS 2.3, 4.4)

The divide-and-conquer strategy solves a problem by: 1. Breaking it into subproblems that are themselves smaller instances of the same type of problem

Analysis of Algorithms

Methods for solving recurrences

1 Caveats of Parallel Algorithms

CSE 417: Algorithms and Computational Complexity

Asymptotic Algorithm Analysis & Sorting

Introduction to Algorithms 6.046J/18.401J

CS 310 Advanced Data Structures and Algorithms

CSCI 3110 Assignment 6 Solutions

Analysis of Algorithm Efficiency. Dr. Yingwu Zhu

Analysis of Multithreaded Algorithms

Lecture 1: Asymptotics, Recurrences, Elementary Sorting

Transcription:

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

The divide-and-conquer design paradigm 1. Divide the problem (instance) into subproblems of sizes that are fractions of the original problem size. 2. Conquer the subproblems by solving them recursively. 3. Combine subproblem solutions. 2

Binary search Find an element in a sorted array: 1. Divide: Check middle element. 2. Conquer: Recursively search 1 subarray. 3. Combine: Trivial. Example: Find 9 3 5 7 8 9 12 15 3

Binary search Find an element in a sorted array: 1. Divide: Check middle element. 2. Conquer: Recursively search 1 subarray. 3. Combine: Trivial. Example: Find 9 3 5 7 8 9 12 15 4

Binary search Find an element in a sorted array: 1. Divide: Check middle element. 2. Conquer: Recursively search 1 subarray. 3. Combine: Trivial. Example: Find 9 3 5 7 8 9 12 15 5

Binary search Find an element in a sorted array: 1. Divide: Check middle element. 2. Conquer: Recursively search 1 subarray. 3. Combine: Trivial. Example: Find 9 3 5 7 8 9 12 15 6

Binary search Find an element in a sorted array: 1. Divide: Check middle element. 2. Conquer: Recursively search 1 subarray. 3. Combine: Trivial. Example: Find 9 3 5 7 8 9 12 15 7

Binary search Find an element in a sorted array: 1. Divide: Check middle element. 2. Conquer: Recursively search 1 subarray. 3. Combine: Trivial. Example: Find 9 3 5 7 8 9 12 15 8

Merge sort 1. Divide: Trivial. 2. Conquer: Recursively sort 2 subarrays of size n/2 3. Combine: Linear-time key subroutine MERGE MERGE-SORT (A[0.. n-1]) 1. If n = 1, done. 2. MERGE-SORT (A[ 0.. n/2-1]) 3. MERGE-SORT (A[ n/2.. n-1 ]) 4. Merge the 2 sorted lists. 9

Merging two sorted arrays 20 12 20 12 20 12 20 12 20 12 20 12 13 11 13 11 13 11 13 11 13 11 13 7 9 7 9 7 9 9 2 1 2 1 2 7 9 11 12 Time dn (n) to merge a total of n elements (linear time). 10

Analyzing merge sort T(n) d 0 T(n/2) T(n/2) dn MERGE-SORT (A[0.. n-1]) 1. If n = 1, done. 2. MERGE-SORT (A[ 0.. n/2+1]) 3. MERGE-SORT (A[ n/2.. n-1 ]) 4. Merge the 2 sorted lists. Sloppiness: Should be T( n/2 ) + T( n/2 ), but it turns out not to matter asymptotically. 11

Recurrence for merge sort T(n) = d 0 if n = 1; 2T(n/2) + dn if n > 1. But what does T(n) solve to? I.e., is it O(n) or O(n 2 ) or O(n 3 ) or? 12

Recursion tree Solve T(n) = 2T(n/2) + dn, where d > 0 is constant. T(n) 13

Recursion tree Solve T(n) = 2T(n/2) + dn, where d > 0 is constant. dn T(n/2) T(n/2) 14

Recursion tree Solve T(n) = 2T(n/2) + dn, where d > 0 is constant. dn dn/2 dn/2 T(n/4) T(n/4) T(n/4) T(n/4) 15

Recursion tree Solve T(n) = 2T(n/2) + dn, where d > 0 is constant. dn dn dn/2 dn/2 dn h = log n dn/4 dn/4 dn/4 dn/4 dn d 0 #leaves = n d 0 n Total dn log n + d 0 n 16

Mergesort Conclusions Merge sort runs in (n log n) time. (n log n) grows more slowly than (n 2 ). Therefore, merge sort asymptotically beats insertion sort in the worst case. In practice, merge sort beats insertion sort for n > 30 or so. (Why not earlier?) 17

Recursion-tree method A recursion tree models the costs (time) of a recursive execution of an algorithm. The recursion-tree method can be unreliable, just like any method that uses ellipses ( ). It is good for generating guesses of what the runtime could be. But: Need to verify that the guess is correct. Induction (substitution method) 18

Substitution method The most general method to solve a recurrence (prove O and separately): 1. Guess the form of the solution: (e.g. using recursion trees, or expansion) 2. Verify by induction (inductive step). 3. Solve for O-constants n 0 and c (base case of induction) 19

Convex Hull Problem Given a set of pins on a pinboard and a rubber band around them. How does the rubber band look when it snaps tight? The convex hull of a point set is one of the simplest shape approximations for a set of points. 20

Convex Hull: Divide & Conquer Preprocessing: sort the points by x- coordinate Divide the set of points into two sets A and B: A contains the left n/2 points, B contains the right n/2 points Recursively compute the convex hull of A Recursively compute the convex hull of B Merge the two convex hulls A B 21

Merging Find upper and lower tangent With those tangents the convex hull of AB can be computed from the convex hulls of A and the convex hull of B in O(n) linear time A B 22

Finding the lower tangent a = rightmost point of A 3 b = leftmost point of B 4 4=b 2 while T=ab not lower tangent to both convex hulls of A and B do{ 3 5 } while T not lower tangent to convex hull of A do{ a=a-1 } while T not lower tangent to convex hull of B do{ b=b+1 } left turn check with orientation test 5 a=2 1 0 A 6 1 7 0 B right turn 23

Convex Hull: Runtime Preprocessing: sort the points by x- coordinate Divide the set of points into two sets A and B: A contains the left n/2 points, B contains the right n/2 points Recursively compute the convex hull of A Recursively compute the convex hull of B Merge the two convex hulls O(n log n) just once O(1) T(n/2) T(n/2) O(n) 24

Convex Hull: Runtime Runtime Recurrence: T(n) = 2 T(n/2) + cn Solves to T(n) = (n log n) 25

Powering a number Problem: Compute a n, where n N. Naive algorithm: (n). Divide-and-conquer algorithm: (recursive squaring) a n = a n/2 a n/2 a (n 1)/2 a (n 1)/2 a if n is even; if n is odd. T(n) = T(n/2) + (1) T(n) = (log n). 26

The divide-and-conquer design paradigm 1. Divide the problem (instance) into subproblems of sizes that are fractions of the original problem size. 2. Conquer the subproblems by solving them recursively. 3. Combine subproblem solutions. Runtime recurrences 27

The master method The master method applies to recurrences of the form T(n) = at(n/b) + f (n), where a 1, b > 1, and f is asymptotically positive. 28

Example: merge sort 1. Divide: Trivial. 2. Conquer: Recursively sort a=2 subarrays of size n/2=n/b 3. Combine: Linear-time merge, runtime f(n)o(n) T(n) = 2 T(n/2) + O(n) # subproblems subproblem size T(n) = at(n/b) + f(n) work dividing and combining 29

Master Theorem T(n) = at(n/b) + f (n) CASE 1: f (n) = O(n log ba ) T(n) = (n log ba ) for some >0 CASE 2: f (n) = (n log ba log k n) T(n) = (n log ba log k+1 n) for some k 0 CASE 3: (i) f (n) = (n log ba + ) for some >0 and (ii) a f (n/b) cf(n) for some c < 1 T(n) = ( f (n)) 30

How to apply the theorem Compare f (n) with n log ba : 1. f (n) = O(n log ba ) for some constant > 0. f (n) grows polynomially slower than n log ba (by an n factor). Solution: T(n) = (n log ba ). 2. f (n) = (n log ba log k n) for some constant k 0. f (n) and n log ba grow at similar rates. Solution: T(n) = (n log ba log k+1 n). 31

How to apply the theorem Compare f (n) with n log ba : 3. f (n) = (n log ba + ) for some constant > 0. f (n) grows polynomially faster than n log ba (by an n factor), and f (n) satisfies the regularity condition that af(n/b) cf(n) for some constant c < 1. Solution: T(n) = ( f (n)). 32

Example: merge sort 1. Divide: Trivial. 2. Conquer: Recursively sort 2 subarrays. 3. Combine: Linear-time merge. T(n) = 2 T(n/2) + O(n) # subproblems subproblem size work dividing and combining n log ba = n log 22 = n 1 = n CASE 2 (k = 0) T(n) = (n log n). 33

Example: binary search T(n) = 1 T(n/2) + (1) # subproblems subproblem size work dividing and combining n log ba = n log 21 = n 0 = 1 CASE 2 (k = 0) T(n) = (log n). 34

Master theorem: Examples Ex. T(n) = 4T(n/2) + sqrt(n) a = 4, b = 2 n log ba = n 2 ; f (n) = sqrt(n). CASE 1: f(n) = O(n 2 ) for = 1.5. T(n) = (n 2 ). Ex. T(n) = 4T(n/2) + n 2 a = 4, b = 2 n log ba = n 2 ; f (n) = n 2. CASE 2: f (n) = (n 2 log 0 n), that is, k = 0. T(n) = (n 2 log n). 35

Master theorem: Examples Ex. T(n) = 4T(n/2) + n 3 a = 4, b = 2 n log ba = n 2 ; f (n) = n 3. CASE 3: f (n) = (n 2+ ) for = 1 and 4(n/2) 3 cn 3 (reg. cond.) for c = 1/2. T(n) = (n 3 ). Ex. T(n) = 4T(n/2) + n 2 /logn a = 4, b = 2 n log ba = n 2 ; f (n) = n 2 /logn. Master method does not apply. In particular, for every constant > 0, we have log n o(n ). 36

9/22/17 37 Matrix multiplication nn n n n n nn n n n n nn n n n n b b b b b b b b b a a a a a a a a a c c c c c c c c c 2 1 2 22 21 1 12 11 2 1 2 22 21 1 12 11 2 1 2 22 21 1 12 11 n k kj ik ij b a c 1 Input: A = [a ij ], B = [b ij ]. Output: C = [c ij ] = A B. i, j = 1, 2,, n. CMPS 2200 Introduction to Algorithms

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 ) 38

Divide-and-conquer algorithm IDEA: nn matrix = 22 matrix of (n/2)n/2) submatrices: r = a e+b g s = a f+ b h t = c e+d g u=c f +d h r t s u a c e g 39 b d C = A B 8 recursive mults of (n/2)n/2) submatrices 4 adds of (n/2)n/2) submatrices f h

Matrix multiplication: 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 matrix multiplication algorithm. 40

Strassen s idea Multiply 22 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 mult! 41

Strassen s idea Multiply 22 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 42

Strassen s algorithm 1. Divide: Partition A and B into (n/2)(n/2) submatrices. Form P-terms to be multiplied using + and. 2. Conquer: Perform 7 multiplications of (n/2)(n/2) submatrices recursively. 3. Combine: Form C using + and on (n/2)(n/2) submatrices. T(n) = 7 T(n/2) + (n 2 ) n log ba = n log 27 n 2.81 CASE 1 T(n) = (n log 7 ) 43

Analysis of Strassen T(n) = 7 T(n/2) + (n 2 ) Solves to T(n) = (n log 7 ) The number 2.81 may not seem much smaller than 3, but because the difference is in the exponent, the impact on running time is significant. In fact, Strassen s algorithm beats the ordinary algorithm on today s machines for n 30 or so. Best to date (of theoretical interest only): (n 2.376 ). 44

Conclusion Divide and conquer is just one of several powerful techniques for algorithm design. Divide-and-conquer algorithms can be analyzed using recurrences and the master method. Can lead to more efficient algorithms 45