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

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

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

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

Analysis of Algorithms - Using Asymptotic Bounds -

Practical Session #3 - Recursions

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

Asymptotic Analysis and Recurrences

Divide and Conquer. Andreas Klappenecker

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

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

Data Structures and Algorithms Chapter 3

CPS 616 DIVIDE-AND-CONQUER 6-1

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

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

Data Structures and Algorithms Chapter 3

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

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

Introduction to Algorithms

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

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

CMPSCI611: Three Divide-and-Conquer Examples Lecture 2

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

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

The Divide-and-Conquer Design Paradigm

Algorithm Design and Analysis

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

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

Kartsuba s Algorithm and Linear Time Selection

Divide and Conquer algorithms

COMP 382: Reasoning about algorithms

Divide and Conquer. Chapter Overview. 1.2 Divide and Conquer

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

Recurrence Relations

Introduction to Algorithms 6.046J/18.401J/SMA5503

Pre-lecture R: Solving Recurrences

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

Solving Recurrences. Lecture 23 CS2110 Fall 2011

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

CS483 Design and Analysis of Algorithms

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

CSE 421 Algorithms: Divide and Conquer

Chapter 4 Divide-and-Conquer

1 Divide and Conquer (September 3)

CSI2101-W08- Recurrence Relations

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

Reductions, Recursion and Divide and Conquer

Methods for solving recurrences

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

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

Divide and conquer. Philip II of Macedon

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

An analogy from Calculus: limits

Notes for Recitation 14

Divide and Conquer. Recurrence Relations

Chapter 4. Recurrences

Chapter. Divide-and-Conquer. Contents

Growth of Functions (CLRS 2.3,3)

Design Patterns for Data Structures. Chapter 3. Recursive 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

Data Structures and Algorithms CMPSC 465

Algorithms Chapter 4 Recurrences

Asymptotic Algorithm Analysis & Sorting

DIVIDE AND CONQUER II

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

Midterm 1 for CS 170

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

A SUMMARY OF RECURSION SOLVING TECHNIQUES

CS483 Design and Analysis of Algorithms

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

Divide and Conquer. Arash Rafiey. 27 October, 2016

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

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

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

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

Homework 1 Solutions

More Asymptotic Analysis Spring 2018 Discussion 8: March 6, 2018

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

Introduction to Divide and Conquer

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

Solving Recurrences. 1. Express the running time (or use of some other resource) as a recurrence.

Asymptotic Analysis 1

Divide-and-conquer algorithm

1 Substitution method

Divide-and-Conquer and Recurrence Relations: Notes on Chapter 2, Dasgupta et.al. Howard A. Blair

Computational Complexity - Pseudocode and Recursions

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

COMPUTER ALGORITHMS. Athasit Surarerks.

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

Cost/Performance Tradeoffs:

CS 4349 Lecture August 30th, 2017

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

Review Of Topics. Review: Induction

Divide and Conquer Strategy

Algorithm Design and Analysis

Data Structures and Algorithms

COMP 355 Advanced Algorithms

Design and Analysis of Algorithms

Math 2602 Finite and Linear Math Fall 14. Homework 8: Core solutions

Copyright 2000, Kevin Wayne 1

CS 310 Advanced Data Structures and Algorithms

Transcription:

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

Integer multiplication Say we have 5 baskets with 8 apples in each How do we determine how many apples we have? Count them all? That would take awhile Since we know we have 8 in each basket, and 5 baskets, lets simply add 8 + 8 + 8 + 8 + 8 = 40! This is essentially multiplication! 8 * 5 = 8 + 8 + 8 + 8 + 8 2

What about bigger numbers? Like 1284 * 1583, I mean! That would take way longer than counting the 40 apples! Let s think of it like this: 1284 * 1583 = 1284*3 + 1284*80 + 1284*500 + 1284*1000 1284 x 1583 3852 + 102720 + 642000 + 1284000 = 2032572 3

OK, I m guessing we all knew that... and learned it quite some time ago So why bring it up now? What is there to cover about multiplication What is the runtime of this multiplication algorithm? For 2 n-digit numbers: n 2 Space? 4

Yeah, but the processor has a MUL instruction Assuming x86 Given two 32-bit integers, MUL will produce a 64-bit integer in a few cycles What about when we need to multiply large ints? VERY large ints? RSA keys should be at least 2048 bits Back to grade school 5

Gradeschool algorithm on binary numbers 10100000100 x 11000101111 10100000100 101000001000 1010000010000 10100000100000 000000000000000 1010000010000000 00000000000000000 000000000000000000 0000000000000000000 10100000100000000000 101000001000000000000 111110000001110111100 6

How can we improve our runtime? Let s try to divide and conquer: Break our n-bit integers in half: x = 1001011011001000, n = 16 Let the high-order bits be x H = 10010110 Let the low-order bits be x L = 11001000 x = 2 n/2 x H + x L Do the same for y x * y = (2 n/2 x H + x L ) * (2 n/2 y H + y L ) x * y = 2 n x H y H + 2 n/2 (x H y L + x L y H ) + x L y L 7

So what does this mean? 4 multiplications of n/2 bit integers 3 additions of n-bit integers 2 n x H y H + 2 n/2 (x H y L + x L y H ) + x L y L A couple shifts of up to n positions Actually 16 multiplications of n/4 bit integers Actually 64 multiplications of n/8 bit integers (plus additions/shifts) (plus additions/shifts)... 8

So what s the runtime??? Recursion really complicates our analysis We ll use a recurrence relation to analyze the recursive runtime Goal is to determine: How much work is done in the current recursive call? How much work is passed on to future recursive calls? All in terms of input size 9

Recurrence relation for divide and conquer multiplication Assuming we cut integers exactly in half at each call i.e., input bit lengths are a power of 2 Work in the current call: Shifts and additions are Θ(n) Work left to future calls: 4 more multiplications on half of the input size T(n) = 4T(n/2) + Θ(n) 10

Soooo what s the runtime? Need to solve the recurrence relation Remove the recursive component and express it purely in terms of n A cookbook approach to solving recurrence relations: The master theorem 11

The master theorem Usable on recurrence relations of the following form: T(n) = at(n/b) + f(n) Where: a is a constant >= 1 b is a constant > 1 and f(n) is an asymptotically positive function 12

Applying the master theorem T(n) = at(n/b) + f(n) If f(n) is O(n log_b(a) - ε ): T(n) is Θ(n log_b(a) ) If f(n) is Θ(n log_b(a) ) T(n) is Θ(n log_b(a) lg n) If f(n) is Ω(n log_b(a) + ε ) and (a * f(n/b) <= c * f(n)) for some c < 1: T(n) is Θ(f(n)) 13

Mergesort master theorem analysis Recurrence relation for mergesort? T(n) = 2T(n/2) + Θ(n) a = 2 b = 2 f(n) is Θ(n) So... n log_b(a) = n lg 2 = n If f(n) is O(n log_b(a) - ε ): T(n) is Θ(n log_b(a) ) If f(n) is Θ(n log_b(a) ) T(n) is Θ(n log_b(a) lg n) If f(n) is Ω(n log_b(a) + ε ) and (a * f(n/b) <= c * f(n)) for some c < 1: T(n) is Θ(f(n)) Being Θ(n) means f(n) is Θ(n log_b(a) ) T(n) = Θ(n log_b(a) lg n) = Θ(n lg 2 lg n) = Θ(n lg n) 14

For our divide and conquer multiplication approach T(n) = 4T(n/2) + Θ(n) a = 4 b = 2 f(n) is Θ(n) So... n log_b(a) = n lg 4 = n 2 If f(n) is O(n log_b(a) - ε ): T(n) is Θ(n log_b(a) ) If f(n) is Θ(n log_b(a) ) T(n) is Θ(n log_b(a) lg n) If f(n) is Ω(n log_b(a) + ε ) and (a * f(n/b) <= c * f(n)) for some c < 1: T(n) is Θ(f(n)) Being Θ(n) means f(n) is polynomially smaller than n 2 T(n) = Θ(n log_b(a) ) = Θ(n lg 4 ) = Θ(n 2 ) 15

@#$%^&* Leaves us back where we started with the grade school algorithm Actually, the overhead of doing all of the dividing and conquering will make it slower than grade school 16

SO WHY EVEN BOTHER? Let s look for a smarter way to divide and conquer Look at the recurrence relation again to see where we can improve our runtime: T(n) = 4T(n/2) + Θ(n) Can we reduce the number of subproblems? Can we reduce the amount of work done by the current call? Can we reduce the subproblem size? 17

Karatsuba s algorithm By reducing the number of recursive calls (subproblems), we can improve the runtime x * y = 2 n x H y H + 2 n/2 (x H y L + x L y H ) + x L y L M1 M2 M3 M4 We don t actually need to do both M2 and M3 We just need the sum of M2 and M3 If we can find this sum using only 1 multiplication, we decrease the number of recursive calls and hence improve our runtime 18

Karatsuba craziness M1 = x h y h ; M2 = x h y l ; M3 = x l y h ; M4 = x l y l ; The sum of all of them can be expressed as a single mult: M1 + M2 + M3 + M4 = x h y h + x h y l + x l y h + x l y l = (x h + x l ) * (y h + y l ) Lets call this single multiplication M5: M5 = (x h + x l ) * (y h + y l ) = M1 + M2 + M3 + M4 Hence, M5 - M1 - M4 = M2 + M3 So: x * y = 2 n M1 + 2 n/2 (M5 - M1 - M4) + M4 Only 3 multiplications required! At the cost of 2 more additions, and 2 subtractions 19

Karatsuba runtime To get M5, we have to multiply (at most) n/2 + 1 bit ints Asymptotically the same as our other recursive calls Requires extra additions and subtractions But these are all Θ(n) So, the recurrence relation for Karatsuba s algorithm is: T(n) = 3T(n/2) + Θ(n) Which solves to be Θ(n lg 3 ) Asymptotic improvement over grade school algorithm! For large n, this will translate into practical improvement 20

Large integer multiplication in practice Can use a hybrid algorithm of grade school for large operands, Karatsuba s algorithm for VERY large operands Why are we still bothering with grade school at all? 21

Is this the best we can do? The Schönhage Strassen algorithm Uses Fast Fourier transforms to achieve better asymptotic runtime O(n*log(n)*log(log(n))) Fastest asymptotic runtime known from 1971-2007 Required n to be astronomical to achieve practical improvements to runtime Numbers beyond 2 2^15 to 2 2^17 Fürer was able to achieve even better asymptotic runtime in 2007 n*log(n)*2o(log* n) No practical difference for realistic values of n 22