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

Similar documents
CPS 616 DIVIDE-AND-CONQUER 6-1

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

Divide and Conquer algorithms

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

Introduction to Algorithms 6.046J/18.401J/SMA5503

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

Asymptotic Analysis and Recurrences

Fast Convolution; Strassen s Method

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

Chapter 4 Divide-and-Conquer

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

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

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

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

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

Chapter 5 Divide and Conquer

CS483 Design and Analysis of Algorithms

Recurrence Relations

feb abhi shelat Matrix, FFT

MA008/MIIZ01 Design and Analysis of Algorithms Lecture Notes 3

Introduction to Algorithms

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

CMPSCI611: Three Divide-and-Conquer Examples Lecture 2

Divide and conquer. Philip II of Macedon

FFT: Fast Polynomial Multiplications

The Divide-and-Conquer Design Paradigm

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

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

Data Structures and Algorithms Chapter 3

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

Algorithm Design and Analysis

1 Divide and Conquer (September 3)

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

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

CS483 Design and Analysis of Algorithms

Data Structures and Algorithms Chapter 3

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

Matrices: 2.1 Operations with Matrices

Matrix Multiplication

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

Divide and Conquer. Chapter Overview. 1.2 Divide and Conquer

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

CSE 421 Algorithms: Divide and Conquer

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

COMP 382: Reasoning about algorithms

Divide and Conquer. Andreas Klappenecker

Divide and Conquer. Arash Rafiey. 27 October, 2016

CSE 548: Analysis of Algorithms. Lecture 4 ( Divide-and-Conquer Algorithms: Polynomial Multiplication )

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

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

Solving Recurrences. Lecture 23 CS2110 Fall 2011

The Fast Fourier Transform. Andreas Klappenecker

ITEC2620 Introduction to Data Structures

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

Kartsuba s Algorithm and Linear Time Selection

Integer-Valued Polynomials

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

DIVIDE AND CONQUER II

A SUMMARY OF RECURSION SOLVING TECHNIQUES

Solving Quadratic Equations

Analysis of Algorithms - Using Asymptotic Bounds -

CSC373: Algorithm Design, Analysis and Complexity Fall 2017

Recurrence Relations

Design and Analysis of Algorithms

Homework 1 Solutions

Divide & Conquer. CS 320, Fall Dr. Geri Georg, Instructor CS320 Div&Conq 1

CSE548, AMS542: Analysis of Algorithms, Fall 2015 Date: October 12. In-Class Midterm. ( 1:05 PM 2:20 PM : 75 Minutes )

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

Divide and Conquer. Recurrence Relations

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

Lecture 1b: The Maximum Contiguous Subarray Problem

Divide and Conquer Strategy

Algorithm Design and Analysis

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

CSCI 3110 Assignment 6 Solutions

Problem Set 2 Solutions

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

Discrete Mathematics. Kishore Kothapalli

Lecture 20: Discrete Fourier Transform and FFT

CS 4104 Data and Algorithm Analysis. Recurrence Relations. Modeling Recursive Function Cost. Solving Recurrences. Clifford A. Shaffer.

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

Review Of Topics. Review: Induction

Outline. Part 5. Computa0onal Complexity (3) Compound Interest 2/15/12. Recurrence Rela0ons: An Overview. Recurrence Rela0ons: Formal Defini0on

1 Caveats of Parallel Algorithms

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

Design and Analysis of Algorithms

Notes for Recitation 14

CSI2101-W08- Recurrence Relations

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

Data Structures and Algorithms CSE 465

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

Graduate Algorithms CS F-09 Dynamic Programming

1 Substitution method

Lecture 4. Quicksort

Multiplying huge integers using Fourier transforms

Algorithms Chapter 4 Recurrences

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

Divide and Conquer Algorithms

Transcription:

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

Outline Outline: Introduction The polynomial multiplication problem An O(n 2 ) brute force algorithm An O(n 2 ) first divide-and-conquer algorithm An improved divide-and-conquer algorithm Remarks Divide and Conquer: Polynomial Multiplication Version of October 2 / 7, 24201

Divide-and-Conquer Already saw some divide and conquer algorithms Divide Conquer Divide a given problem into a or more subproblems (ideally of approximately equal size n/b) Solve each subproblem (directly if small enough or recursively) a T (n/b) Combine Combine the solutions of the subproblems into a global solution f (n) Cost satisfies T(n) = at(n/b) + f(n). Divide and Conquer: Polynomial Multiplication Version of October 3 / 7, 24201

Divide and Conquer Examples Two major examples so far Maximum Contiguious Subarray Mergesort Both satisfied T (n) = 2T (n/2) + O(n) T (n) = O(n log n) Also saw Quicksort Divide and Conquer, but unequal size problems Divide and Conquer: Polynomial Multiplication Version of October 4 / 7, 24201

Divide and Conquer Analysis Main tool is the Master Theorem for soving recurrences of form where T (n) = at (n/b) + f (n) a 1 and b 1 are constants and f (n) is a (asymptotically) positive function. Note: Initial conditions T (1), T (2),... T (k) for some k. They don t contribute to asymptotic growth n/b could be either n/b or n/b Divide and Conquer: Polynomial Multiplication Version of October 5 / 7, 24201

The Master Theorem T(n) = at(n/b) + f(n), c = log b a If f (n) = Θ(n c ɛ ) for some ɛ > 0 then T (n) = θ(n c ) If T (n) = 4T (n/2) + n then T (n) = Θ(n 2 ) If T (n) = 3T (n/2) + n then T (n) = Θ(n log 2 3 ) = Θ(n 1.58... ) If f (n) = Θ(n c ) then T (n) = Θ(n c log n) If T (n) = 2T (n/2) + n then T (n) = Θ(n log n) If f (n) = Θ(n c+ɛ ) for some ɛ > 0 and if af (n/b) df (n) for some d < 1 and large enough n then T (n) = O(f (n)) If T (n) = T (n/2) + n then T (n) = Θ(n) Divide and Conquer: Polynomial Multiplication Version of October 6 / 7, 24201

More Master Theorem There are many variations of the Master Theorem. Here s one... If T (n) = T (3n/4) + T (n/5) + n then T (n) = Θ(n) More generally, given constants α i > 0 with i α i < 1, if T (n) = n + k i=1 T (α in) then T (n) = Θ(n). Divide and Conquer: Polynomial Multiplication Version of October 7 / 7, 24201

Objective and Outline Outline: Introduction The polynomial multiplication problem An O(n 2 ) brute force algorithm An O(n 2 ) first divide-and-conquer algorithm An improved divide-and-conquer algorithm Remarks Divide and Conquer: Polynomial Multiplication Version of October 8 / 7, 24201

The Polynomial Multiplication Problem Definition (Polynomial Multiplication Problem) Given two polynomials Compute the product A(x)B(x) Example A(x) = a 0 + a 1 x + + a n x n B(x) = b 0 + b 1 x + + b m x m A(x) = 1 + 2x + 3x 2 B(x) = 3 + 2x + 2x 2 A(x)B(x) = 3 + 8x + 15x 2 + 10x 3 + 6x 4 Assume that the coefficients a i and b i are stored in arrays A[0... n] and B[0... m] Cost: number of scalar multiplications and additions Divide and Conquer: Polynomial Multiplication Version of October 9 / 7, 24201

What do we need to compute exactly? Define A(x) = n i=0 a ix i B(x) = m i=0 b ix i C(x) = A(x)B(x) = n+m k=0 c kx k Then c k = a i b j 0 i n, 0 j m, i+j=k for all 0 k m + n Definition The vector (c 0, c 1,..., c m+n ) is the convolution of the vectors (a 0, a 1,..., a n ) and (b 0, b 1,..., b m ) While polynomial multiplication is interesting, real goal is to calculate convolutions. Major subroutine in digital signal processing Divide and Conquer: Polynomial Multiplication Version of October 10 / 7, 24201

Objective and Outline Outline: Introduction The polynomial multiplication problem An O(n 2 ) brute force algorithm An O(n 2 ) first divide-and-conquer algorithm An improved divide-and-conquer algorithm Remarks Divide and Conquer: Polynomial Multiplication Version of October 11 / 7, 24201

Direct (Brute Force) Approach To ease analysis, assume n = m. A(x) = n i=0 a ix i and B(x) = n i=0 b ix i C(x) = A(x)B(x) = 2n k=0 c kx k with c k = a i b j, for all 0 k 2n 0 i,j n,i+j=k Direct approach: Compute all c k s using the formula above Total number of multiplications: Θ(n 2 ) Total number of additions: Θ(n 2 ) Complexity: Θ(n 2 ) Divide and Conquer: Polynomial Multiplication Version of October 12 / 7, 24201

Objective and Outline Outline: Introduction The polynomial multiplication problem An O(n 2 ) brute force algorithm An O(n 2 ) first divide-and-conquer algorithm An improved divide-and-conquer algorithm Remarks Divide and Conquer: Polynomial Multiplication Version of October 13 / 7, 24201

Divide-and-Conquer: Divide Assume n is a power of 2 Define A 0 (x) = a 0 + a 1 x + + a n 2 1 x n 2 1 A 1 (x) = a n n 2 2 +1 x + + a n x n 2 A(x) = A 0 (x) + A 1 (x)x n 2 Similarly, define B 0 (x) and B 1 (x) such that B(x) = B 0 (x) + B 1 (x)x n 2 A(x)B(x) = A 0 (x)b 0 (x)+a 0 (x)b 1 (x)x n 2 +A1 (x)b 0 (x)x n 2 +A1 (x)b 1 (x)x n The original problem (of size n) is divided into 4 problems of input size n/2 Divide and Conquer: Polynomial Multiplication Version of October 14 / 7, 24201

Example A(x) = 2 + 5x + 3x 2 + x 3 x 4 B(x) = 1 + 2x + 2x 2 + 3x 3 + 6x 4 A(x)B(x) = 2 + 9x + 17x 2 + 23x 3 + 34x 4 + 39x 5 +19x 6 + 3x 7 6x 8 A 0 (x) = 2 + 5x, A 1 (x) = 3 + x x 2, A(x) = A 0 (x) + A 1 (x)x 2 B 0 (x) = 1 + 2x, B 1 (x) = 2 + 3x + 6x 2, B(x) = B 0 (x) + B 1 (x)x 2 A 0 (x)b 0 (x) = 2 + 9x + 10x 2 A 1 (x)b 1 (x) = 6 + 11x + 19x 2 + 3x 3 6x 4 A 0 (x)b 1 (x) = 4 + 16x + 27x 2 + 30x 3 A 1 (x)b 0 (x) = 3 + 7x + x 2 2x 3 A 0 (x)b 1 (x) + A 1 (x)b 0 (x) = 7 + 23x + 28x 2 + 28x 3 A 0 (x)b 0 (x) + (A 0 (x)b 1 (x) + A 1 (x)b 0 (x))x 2 + A 1 (x)b 1 (x)x 4 = 2 + 9x + 17x 2 + 23x 3 + 34x 4 + 39x 5 + 19x 6 + 3x 7 6x 8 Divide and Conquer: Polynomial Multiplication Version of October 15 / 7, 24201

Divide-and-Conquer: Conquer Conquer: Solve the four subproblems compute A 0 (x)b 0 (x), A 0 (x)b 1 (x), A 1 (x)b 0 (x), A 1 (x)b 1 (x) by recursively calling the algorithm 4 times Combine adding the following four polynomials A 0 (x)b 0 (x)+a 0 (x)b 1 (x)x n 2 +A1 (x)b 0 (x)x n 2 +A1 (x)b 1 (x)x n takes O(n) operations (Why?) Divide and Conquer: Polynomial Multiplication Version of October 16 / 7, 24201

The First Divide-and-Conquer Algorithm PolyMulti1(A(x), B(x)) begin A 0 (x) = a 0 + a 1 x + + a n 2 1 x n 2 1 ; A 1 (x) = a n + a n 2 2 +1 x + + a n x n 2 ; B 0 (x) = b 0 + b 1 x + + b n 2 1 x n 2 1 ; B 1 (x) = b n + b n 2 2 +1 x + + b n x n 2 ; U(x) = PolyMulti1(A 0 (x), B 0 (x)); V (x) = PolyMulti1(A 0 (x), B 1 (x)); W (x) = PolyMulti1(A 1 (x), B 0 (x)); Z(x) = PolyMulti1(A 1 (x), B 1 (x)); return ( U(x) + [V (x) + W (x)]x n 2 + Z(x)x n) end Divide and Conquer: Polynomial Multiplication Version of October 17 / 7, 24201

Analysis of Running Time Assume that n is a power of 2 { 4T (n/2) + n, if n > 1, T (n) = 1, if n = 1. By the Master Theorem for recurrences T (n) = Θ(n 2 ). Same order as the brute force approach! No improvement! Divide and Conquer: Polynomial Multiplication Version of October 18 / 7, 24201

Objective and Outline Outline: Introduction The polynomial multiplication problem An O(n 2 ) brute force algorithm An O(n 2 ) first divide-and-conquer algorithm An improved divide-and-conquer algorithm Remarks Divide and Conquer: Polynomial Multiplication Version of October 19 / 7, 24201

Two Observations Observation 1: We said that we need the 4 terms: A 0 B 0, A 0 B 1, A 1 B 0, A 1 B. What we really need are the 3 terms: Observation 2: A 0 B 0, A 0 B 1 + A 1 B 0, A 1 B 1! The three terms can be obtained using only 3 multiplications: Y = (A 0 + A 1 )(B 0 + B 1 ) U = A 0 B 0 Z = A 1 B 1 We need U and Z and A 0 B 1 + A 1 B 0 = Y U Z Divide and Conquer: Polynomial Multiplication Version of October 20 / 7, 24201

The Second Divide-and-Conquer Algorithm PolyMulti2(A(x), B(x)) begin A 0 (x) = a 0 + a 1 x + + a n 2 1 x n 2 1 ; A 1 (x) = a n + a n 2 2 +1 x + + a n x n n 2 ; B 0 (x) = b 0 + b 1 x + + b n 2 1 x n 2 1 ; B 1 (x) = b n + b n 2 2 +1 x + + b n x n n 2 ; Y (x) = PolyMulti2(A 0 (x) + A 1 (x), B 0 (x) + B 1 (x)); U(x) = PolyMulti2(A 0 (x), B 0 (x)); Z(x) = PolyMulti2(A 1 (x), B 1 (x)); return ( ) U(x) + [Y (x) U(x) Z(x)]x n 2 + Z(x)x 2 n 2 end Divide and Conquer: Polynomial Multiplication Version of October 21 / 7, 24201

Running Time of the Modified Algorithm T (n) = { 3T (n/2) + n, if n > 1, 1, if n = 1. By the Master Theorem for recurrences T (n) = Θ(n log 2 3 ) = Θ(n 1.58... ). Much better than previous Θ(n 2 ) algorithms! Divide and Conquer: Polynomial Multiplication Version of October 22 / 7, 24201

Objective and Outline Outline: Introduction The polynomial multiplication problem An O(n 2 ) brute force algorithm An O(n 2 ) first divide-and-conquer algorithm An improved divide-and-conquer algorithm Remarks Divide and Conquer: Polynomial Multiplication Version of October 23 / 7, 24201

Remarks This algorithm can also be used for (long) integer multiplication Really designed by Karatsuba (1960, 1962) for that purpose. Response to conjecture by Kolmogorov, founder of modern probability, that this would require Θ(n 2 ). Similar to technique deeloped by Strassen a few years later to multiply 2 n n matrices in O(n log 2 7 ) operations, instead of the Θ(n 3 ) that a straightforward algorithm would use. Takeaway from this lesson is that divide-and-conquer doesn t always give you faster algorithm. Sometimes, you need to be more clever. Coming up. An O(n log n) solution to the polynomial multiplication problem It involves strange recasting of the problem and solution using the Fast Fourier Transform algorithm as a subroutine The FFT is another classic D & C algorithm that we will learn soon. Divide and Conquer: Polynomial Multiplication Version of October 24 / 7, 24201