Introduction to Algorithms 6.046J/18.401J/SMA5503

Similar documents
Divide-and-conquer algorithm

The Divide-and-Conquer Design Paradigm

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

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

Algorithms Design & Analysis. Divide & Conquer

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

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

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

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

Chapter 4 Divide-and-Conquer

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

Algorithm Design and Analysis

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

Fast Convolution; Strassen s Method

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

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

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

Divide and Conquer Algorithms

Matrix Multiplication

Data Structures and Algorithms CMPSC 465

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

Lecture 8 - Algebraic Methods for Matching 1

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

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

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

Design and Analysis of Algorithms

MA008/MIIZ01 Design and Analysis of Algorithms Lecture Notes 3

Analysis of Multithreaded Algorithms

Problem Set 1 Solutions

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

Divide and Conquer algorithms

CPS 616 DIVIDE-AND-CONQUER 6-1

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

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

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

COMP 382: Reasoning about algorithms

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

Chapter 1 Divide and Conquer Algorithm Theory WS 2016/17 Fabian Kuhn

Data Structures and Algorithms Chapter 3

Introduction to Algorithms 6.046J/18.401J

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

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

Introduction to Algorithms 6.046J/18.401J

CMPSCI611: Three Divide-and-Conquer Examples Lecture 2

Data Structures and Algorithms CSE 465

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

Algorithm efficiency analysis

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

Design and Analysis of Algorithms

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

Review Of Topics. Review: Induction

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

A SUMMARY OF RECURSION SOLVING TECHNIQUES

Computational Complexity - Pseudocode and Recursions

8/13/16. Data analysis and modeling: the tools of the trade. Ø Set of numbers. Ø Binary representation of numbers. Ø Floating points.

Lecture 7: Dynamic Programming I: Optimal BSTs

CSE 421 Algorithms. T(n) = at(n/b) + n c. Closest Pair Problem. Divide and Conquer Algorithms. What you really need to know about recurrences

Problem Set 2 Solutions

shelat divide&conquer closest points matrixmult median

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

Solving Recurrences. Lecture 23 CS2110 Fall 2011

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

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

Asymptotic Analysis and Recurrences

CSCI Honor seminar in algorithms Homework 2 Solution

The divide-and-conquer strategy solves a problem by: Chapter 2. Divide-and-conquer algorithms. Multiplication

Divide and Conquer. Andreas Klappenecker

CS483 Design and Analysis of Algorithms

Recurrence Relations

COMP 9024, Class notes, 11s2, Class 1

shelat 16f-4800 sep Matrix Mult, Median, FFT

Taking Stock. IE170: Algorithms in Systems Engineering: Lecture 3. Θ Notation. Comparing Algorithms

Data Structures and Algorithms Chapter 3

Algorithm Analysis Recurrence Relation. Chung-Ang University, Jaesung Lee

1 Substitution method

A matrix over a field F is a rectangular array of elements from F. The symbol

Asymptotic Algorithm Analysis & Sorting

feb abhi shelat Matrix, FFT

Chapter 5 Divide and Conquer

Divide and Conquer. Arash Rafiey. 27 October, 2016

Chapter. Divide-and-Conquer. Contents

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

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.

Algorithms Design & Analysis. Analysis of Algorithm

Recurrence Relations

Copyright 2000, Kevin Wayne 1

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

CSE 613: Parallel Programming. Lecture 8 ( Analyzing Divide-and-Conquer Algorithms )

CSE 613: Parallel Programming. Lectures ( Analyzing Divide-and-Conquer Algorithms )

Divide and Conquer Strategy

Divide and Conquer. Chapter Overview. 1.2 Divide and Conquer

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

Discrete Mathematics U. Waterloo ECE 103, Spring 2010 Ashwin Nayak May 17, 2010 Recursion

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

CS483 Design and Analysis of Algorithms

Chapter 4. Recurrences

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

Introduction to Algorithms

COMP Analysis of Algorithms & Data Structures

Transcription:

Introduction to Algorithms 6.046J/8.40J/SMA5503 Lecture 3 Prof. Piotr Indyk

The divide-and-conquer design paradigm. Divide the problem (instance) into subproblems. 2. Conquer the subproblems by solving them recursively. 3. Combine subproblem solutions. February, 2003 (c) Charles Leiserson and Piotr Indyk L3.2

Example: merge sort. 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 February, 2003 (c) Charles Leiserson and Piotr Indyk L3.3

Binary search Find an element in a sorted array:. Divide: Check middle element. 2. Conquer: Recursively search subarray. 3. Combine: Trivial. Example: Find 9 3 5 7 8 9 2 5 February, 2003 (c) Charles Leiserson and Piotr Indyk L3.4

Binary search Find an element in a sorted array:. Divide: Check middle element. 2. Conquer: Recursively search subarray. 3. Combine: Trivial. Example: Find 9 3 5 7 8 9 2 5 February, 2003 (c) Charles Leiserson and Piotr Indyk L3.5

Binary search Find an element in a sorted array:. Divide: Check middle element. 2. Conquer: Recursively search subarray. 3. Combine: Trivial. Example: Find 9 3 5 7 8 9 2 5 February, 2003 (c) Charles Leiserson and Piotr Indyk L3.6

Binary search Find an element in a sorted array:. Divide: Check middle element. 2. Conquer: Recursively search subarray. 3. Combine: Trivial. Example: Find 9 3 5 7 8 9 2 5 February, 2003 (c) Charles Leiserson and Piotr Indyk L3.7

Binary search Find an element in a sorted array:. Divide: Check middle element. 2. Conquer: Recursively search subarray. 3. Combine: Trivial. Example: Find 9 3 5 7 8 9 2 5 February, 2003 (c) Charles Leiserson and Piotr Indyk L3.8

Binary search Find an element in a sorted array:. Divide: Check middle element. 2. Conquer: Recursively search subarray. 3. Combine: Trivial. Example: Find 9 3 5 7 8 9 2 5 February, 2003 (c) Charles Leiserson and Piotr Indyk L3.9

Recurrence for binary search T(n) = T(n/2) + Θ() # subproblems subproblem size work dividing and combining n log ba = n log 2 = n 0 = CASE 2 (k = 0) T(n) = Θ(lg n). February, 2003 (c) Charles Leiserson and Piotr Indyk L3.0

Powering a number Problem: Compute a n, where n N. Naive algorithm: Θ(n). Divide-and-conquer algorithm: a n = a n/2 a n/2 a (n )/2 a (n )/2 a if n is even; if n is odd. T(n) = T(n/2) + Θ() T(n) = Θ(lg n). February, 2003 (c) Charles Leiserson and Piotr Indyk L3.

Polynomial multiplication Input: a(x)=a 0 +a x+ +a n x n, b(x)=b 0 +b x+ +b n x n, Output: c(x)= a(x)*b(x) =c 0 +c x+ +c 2n x 2n c i =a 0 b i +a b i- + + a i- b +a i b 0 Example: (a 0 +a x) * (b 0 +b x) = a 0 b 0 + (a 0 b +a b 0 )x + a b x 2 = c 0 + c x + c 2 x 2 February, 2003 (c) Charles Leiserson and Piotr Indyk L3.2

Motivation (more in recitations) Essentially equivalent to multiplying large integers: 6046*600 = (6*0 0 + 4*0 + 0*0 2 + 6*0 3 ) * (*0 0 + 0 * 0 + 0*0 2 + 6*0 3 ) = a(0) * b(0) = c(0), where c(x)=a(x)*b(x) c(0) = c 0 0 0 + c 0 + + c 6 0 6 The coefficients of c form the digits of the product c(0) February, 2003 (c) Charles Leiserson and Piotr Indyk L3.3

How to multiply two polynomials From the definition: Θ(n 2 ) time Faster? Use divide and conquer Divide: a(x) = a 0 +a x+ +a n x n = (a 0 + + a n/2 x n/2 ) + x n/2 (a n/2 x 0 + + a n x n/2 ) = p(x) + x n/2 q(x) = p + x n/2 q In the same way: b(x) = s+x n/2 t February, 2003 (c) Charles Leiserson and Piotr Indyk L3.4

Conquer Observe that: a*b = (p+x n/2 q) * (s+x n/2 t) = p*s + x n/2 (p*t+q*s) + x n q*t But p,q,s,t have degree n/2 can compute the products recursively! (and then perform Θ(n) additions) February, 2003 (c) Charles Leiserson and Piotr Indyk L3.5

The great moment T(n) = 4 T(n/2) + Θ(n) # subproblems subproblem size work dividing and combining n log ba = n log 2 4 = n 2 CASE T(n) = Θ(n 2 ). No better than the ordinary algorithm??? February, 2003 (c) Charles Leiserson and Piotr Indyk L3.6

Need to be more clever Compute: p*s q*t (p+q) * (s+t) = p*s + (p*t + q*s) + q*t (all polynomials have degree n/2 ) Can extract (p*t + q*s) without any additional multiplications! February, 2003 (c) Charles Leiserson and Piotr Indyk L3.7

The truly great moment T(n) = 3T(n/2) + Θ(n) # subproblems subproblem size work adding and subtracting n log ba = n log 2 3 = n.58496 Much better than Θ(n 2 )! February, 2003 (c) Charles Leiserson and Piotr Indyk L3.8

Matrix multiplication Input: A = [a ij ], B = [b ij ]. Output: C = [c ij ] = A B. i, j =, 2,, n. c c 2 c c 2 22 c c n 2n = a a 2 a a 2 22 a a n 2n b b 2 b b 2 22 b b n 2n c n c n2 c nn a n a n2 a nn b n b n2 b nn c ij = n k= a ik b kj February, 2003 (c) Charles Leiserson and Piotr Indyk L3.9

Standard algorithm for i to n do for j to n do c ij 0 for k to n do c ij c ij + a ik b kj Running time = Θ(n 3 ) February, 2003 (c) Charles Leiserson and Piotr Indyk L3.20

Divide-and-conquer algorithm IDEA: n n matrix = 2 2 matrix of (n/2) (n/2) submatrices: r t s u = a c b d e g f h r = ae + bg s = af + bh t = ce + dg u = cf + dh C = A B 8 mults of (n/2) (n/2) submatrices 4 adds of (n/2) (n/2) submatrices February, 2003 (c) Charles Leiserson and Piotr Indyk L3.2

Analysis of D&C algorithm T(n) = 8 T(n/2) + Θ(n 2 ) # submatrices submatrix size work adding submatrices n log ba = n log 2 8 = n 3 CASE T(n) = Θ(n 3 ). No better than the ordinary algorithm. February, 2003 (c) Charles Leiserson and Piotr Indyk L3.22

Strassen s idea (969) Multiply 2 2 matrices with only 7 recursive mults. P = 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 + P 2 t = P 3 + P 4 u = P 5 + P P 3 P 7 7 mults, 8 adds/subs. Note: No reliance on commutativity of mult! February, 2003 (c) Charles Leiserson and Piotr Indyk L3.23

Strassen s idea Multiply 2 2 matrices with only 7 recursive mults. P = 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 February, 2003 (c) Charles Leiserson and Piotr Indyk L3.24

Strassen s algorithm. Divide: Partition A and B into (n/2) (n/2) submatrices. Form 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 ) February, 2003 (c) Charles Leiserson and Piotr Indyk L3.25

Analysis of Strassen T(n) = 7 T(n/2) + Θ(n 2 ) n log ba = n log 2 7 n 2.8 CASE T(n) = Θ(n lg 7 ). The number 2.8 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 ). February, 2003 (c) Charles Leiserson and Piotr Indyk L3.26

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 (so practice this math). Can lead to more efficient algorithms February, 2003 (c) Charles Leiserson and Piotr Indyk L3.27

VLSI layout Problem: Embed a complete binary tree with n leaves in a grid using minimal area. W(n) H(n) H(n) = H(n/2) + Θ() W(n) = 2W(n/2) + Θ() = Θ(lg n) = Θ(n) Area = Θ(n lg n) February, 2003 (c) Charles Leiserson and Piotr Indyk L3.28

H-tree embedding L(n) L(n) L(n) = 2L(n/4) + Θ() = Θ( n) Area = Θ(n) L(n/4) Θ() L(n/4) February, 2003 (c) Charles Leiserson and Piotr Indyk L3.29

Master theorem (reprise) T(n) = a T(n/b) + f (n) CASE : f(n) = O(n log ba ε ) T(n) = Θ(n log ba ). CASE 2: f (n) = Θ(n log ba lg k n) T(n) = Θ(n log ba lg k+ n). CASE 3: f (n) = Ω(n log ba + ε ) and a f (n/b) c f (n) T(n) = Θ( f (n)). Merge sort: a = 2, b = 2 n log ba = n CASE 2 (k = 0) T(n) = Θ(n lg n). February, 2003 (c) Charles Leiserson and Piotr Indyk L3.30

Fibonacci numbers Recursive definition: 0 if n = 0; F n = if n = ; F n + F n 2 if n 2. 0 2 3 5 8 3 2 34 Naive recursive algorithm: Ω(φ n ) (exponential time), where φ = ( + is the golden ratio. 5)/2 February, 2003 (c) Charles Leiserson and Piotr Indyk L3.3

Computing Fibonacci numbers Naive recursive squaring: F n = φ n / 5 rounded to the nearest integer. Recursive squaring: Θ(lg n) time. This method is unreliable, since floating-point arithmetic is prone to round-off errors. Bottom-up: Compute F 0, F, F 2,, F n in order, forming each number by summing the two previous. Running time: Θ(n). February, 2003 (c) Charles Leiserson and Piotr Indyk L3.32

February, 2003 (c) Charles Leiserson and Piotr Indyk L3.33 Recursive squaring n F F F F n n n n = + 0 Theorem:. Proof of theorem. (Induction on n.) Base (n = ):. 0 0 2 = F F F F Algorithm: Recursive squaring. Time = Θ(lg n).

February, 2003 (c) Charles Leiserson and Piotr Indyk L3.34 Recursive squaring.. Inductive step (n 2): n n F F F F F F F F n n n n n n n n = = = + 0 0 0 0 2