Binary Multiplication



Similar documents
CSE373: Data Structures and Algorithms Lecture 3: Math Review; Algorithm Analysis. Linda Shapiro Winter 2015

Analysis of Binary Search algorithm and Selection Sort algorithm

HOMEWORK 5 SOLUTIONS. n!f n (1) lim. ln x n! + xn x. 1 = G n 1 (x). (2) k + 1 n. (n 1)!

Loop Invariants and Binary Search

Generating Elementary Combinatorial Objects

Lecture 11: Tail Recursion; Continuations

Notes on Factoring. MA 206 Kurt Bryan

Factorization in Polynomial Rings

Factoring Algorithms

Modern Optimization Methods for Big Data Problems MATH11146 The University of Edinburgh

CS 103X: Discrete Structures Homework Assignment 3 Solutions

MATH10212 Linear Algebra. Systems of Linear Equations. Definition. An n-dimensional vector is a row or a column of n numbers (or letters): a 1.

Homework until Test #2

Mathematical Induction. Lecture 10-11

Binary search algorithm

Continued Fractions and the Euclidean Algorithm

Regression Verification: Status Report

8 Divisibility and prime numbers

it is easy to see that α = a

Lecture Notes on Linear Search

Section IV.1: Recursive Algorithms and Recursion Trees

3. Mathematical Induction

Divide: Paper & Pencil. Computer Architecture ALU Design : Division and Floating Point. Divide algorithm. DIVIDE HARDWARE Version 1

Regular Languages and Finite Automata

Elementary Number Theory and Methods of Proof. CSE 215, Foundations of Computer Science Stony Brook University

Lecture 1: Course overview, circuits, and formulas

Some Polynomial Theorems. John Kennedy Mathematics Department Santa Monica College 1900 Pico Blvd. Santa Monica, CA

Notes from Week 1: Algorithms for sequential prediction

v w is orthogonal to both v and w. the three vectors v, w and v w form a right-handed set of vectors.

The Tower of Hanoi. Recursion Solution. Recursive Function. Time Complexity. Recursive Thinking. Why Recursion? n! = n* (n-1)!

The Mixed Binary Euclid Algorithm

MATH 22. THE FUNDAMENTAL THEOREM of ARITHMETIC. Lecture R: 10/30/2003

COMP 250 Fall 2012 lecture 2 binary representations Sept. 11, 2012

CS473 - Algorithms I

Mathematical Induction

16. Recursion. COMP 110 Prasun Dewan 1. Developing a Recursive Solution

Chapter R.4 Factoring Polynomials

What Is Recursion? Recursion. Binary search example postponed to end of lecture

The Prime Numbers. Definition. A prime number is a positive integer with exactly two positive divisors.

Sample Induction Proofs

Lecture 8: Binary Multiplication & Division

Continued Fractions. Darren C. Collins

Reminder: Complexity (1) Parallel Complexity Theory. Reminder: Complexity (2) Complexity-new

Data Structures. Algorithm Performance and Big O Analysis

Lecture 13 - Basic Number Theory.

STAT 830 Convergence in Distribution

Automata and Formal Languages

Factoring & Primality

csci 210: Data Structures Recursion

2010/9/19. Binary number system. Binary numbers. Outline. Binary to decimal

TAKE-AWAY GAMES. ALLEN J. SCHWENK California Institute of Technology, Pasadena, California INTRODUCTION

Chapter 4, Arithmetic in F [x] Polynomial arithmetic and the division algorithm.

minimal polyonomial Example

Many algorithms, particularly divide and conquer algorithms, have time complexities which are naturally

Discrete Mathematics and Probability Theory Fall 2009 Satish Rao, David Tse Note 2

Questions and Answers in Computer Science. By Yonas Tesfazghi Weldeselassie weldesel

Positional Numbering System

MATH10040 Chapter 2: Prime and relatively prime numbers

CSC 180 H1F Algorithm Runtime Analysis Lecture Notes Fall 2015

Linear Codes. Chapter Basics

6.2 Permutations continued

SECTION 10-2 Mathematical Induction

Outline BST Operations Worst case Average case Balancing AVL Red-black B-trees. Binary Search Trees. Lecturer: Georgy Gimel farb

~ EQUIVALENT FORMS ~

The finite field with 2 elements The simplest finite field is

Lecture 3: Finding integer solutions to systems of linear equations

2 When is a 2-Digit Number the Sum of the Squares of its Digits?

Lecture 4: AC 0 lower bounds and pseudorandomness

Linear Threshold Units

FACTORING POLYNOMIALS IN THE RING OF FORMAL POWER SERIES OVER Z

MATH 4330/5330, Fourier Analysis Section 11, The Discrete Fourier Transform

Introduction to Algorithms March 10, 2004 Massachusetts Institute of Technology Professors Erik Demaine and Shafi Goldwasser Quiz 1.

The Goldberg Rao Algorithm for the Maximum Flow Problem

Boolean Algebra Part 1

DIFFERENTIABILITY OF COMPLEX FUNCTIONS. Contents

Solutions for Practice problems on proofs

HYPERELLIPTIC CURVE METHOD FOR FACTORING INTEGERS. 1. Thoery and Algorithm

Quotient Rings and Field Extensions

Binary Number System. 16. Binary Numbers. Base 10 digits: Base 2 digits: 0 1

THE DIMENSION OF A VECTOR SPACE

Student Outcomes. Lesson Notes. Classwork. Discussion (10 minutes)

MATHEMATICAL INDUCTION. Mathematical Induction. This is a powerful method to prove properties of positive integers.

Computer Science 210: Data Structures. Searching

Faster deterministic integer factorisation

FOUNDATIONS OF ALGEBRAIC GEOMETRY CLASS 22

Some facts about polynomials modulo m (Full proof of the Fingerprinting Theorem)

Introduction to Learning & Decision Trees

Random Fibonacci-type Sequences in Online Gambling

The last three chapters introduced three major proof techniques: direct,

Section 4.2: The Division Algorithm and Greatest Common Divisors

Lecture 1: Schur s Unitary Triangularization Theorem

Welcome to Math Video Lessons. Stanley Ocken. Department of Mathematics The City College of New York Fall 2013

2.1 Complexity Classes

Influences in low-degree polynomials

Termination Checking: Comparing Structural Recursion and Sized Types by Examples

Kevin James. MTHSC 412 Section 2.4 Prime Factors and Greatest Comm

Randomized algorithms

Course Notes for Math 162: Mathematical Statistics Approximation Methods in Statistics

Transcription:

Binary Multiplication Q: How do we multiply two numbers? eg. 12, 345 6, 789 111105 987600 8641500 + 74070000 83, 810, 205 10111 10101 10111 00000 1011100 0000000 + 101110000 111100011 Pad, multiply and add. 1

Consider the following algorithm to multiply two binary numbers. PR E C O N D I T I O N: x and y are binary bit arrays. PO S T C O N D I T I O N: Returns result a binary bit array equal to the product of x and y. def MU L T I P L Y(x, y): result = [0]; for i in range(len(y)-1, -1, -1): if y[i] == 1: result = B I N A R Y A D D(result, x) x.append(0) #add a 0 to the end of x return result Q: If we measure complexity by the number of bit operations, what is the worst case complexity of MU L T I P L Y? we loop n times, and each loop can require an n-bit addition, for O(n 2 ). Q: Is there a more efficient way to implement the multiplication? yes! 2

Divide and Conquer Multiplication Notice that 010111 = 010000 + 111 = 010 2 n/2 + 111 011101 = 011000 + 101 = 011 2 n/2 + 101 i.e., we can split a binary number into n/2 high bits and n/2 low bits. Q: What is 010111 011101 written in terms of high bits and low bits? 010111 011101 = 010 011 (2 n/2 ) 2 + (010 101 + 111 011) (2 n/2 ) +111 101 Q: What is the complexity of multiplying a number x by 2 n/2? O(n/2), it is simply a shift. In general, x y in terms of low bits {x l, y l } and high bits {x h, y h } is: x y = x h y h 2 n + (x l y h + x h y l ) 2 n/2 + x l y l So we can define a recursive, divide and conquer, multiplication algorithm. 3

PR E C O N D I T I O N: x and y are both binary bit vectors of length n (n a power of 2). PO S T C O N D I T I O N: Returns a binary bit vector equal to the product of x and y. RE C MU L T I P L Y( x, y): if (len(x) == 1): return ([x[0]*y[0]]) xh = x[n/2:n] xl = x[0:n/2] yh = x[n/2:n] yl = x[0:n/2] a = RE C MU L T I P L Y(xh, yh) b = RE C MU L T I P L Y(xh, yl) c = RE C MU L T I P L Y(xl, yh) d = RE C MU L T I P L Y(xl, yl) b = B I N A R Y A D D(b,c) a = S H I F T(a,n) b = S H I F T(b,n/2) return B I N A R Y A D D(a,b,d) end RE C MU L T I P L Y Q: What is the recurrence relation for the complexity of RE C MU L T I P L Y? T (n) = 4T ( n 2 ) + O(n) Q: What is the worst case complexity of RE C MU L T I P L Y? O(n 2 ) This is a bit disappointing... 4

A Better Divide and Conquer Multiplication Algorithm Recall we want to compute: Observation [Gauss] x h y h 2 n + (x l y h + x h y l ) 2 n/2 + x l y l x l y h + x h y l = (x h + x l )(y h + y l ) x h y h x l y l Q: Why is this true? (x h + x l )(y h + y l ) = x l y h + x h y l + x h y h + x l y l Q: How does this help us? In order to compute x h y h 2 n + (x l y h + x h y l ) 2 n/2 + x l y l we only need to do 3 multiplies: 1. x h y h 2. x l y l 3. (x h + x l )(y h + y l ) Therefore, xy = x h y h 2 n + [(x h + x l )(y h + y l ) x h y h x l y l ]2 n 2 + x l y l leading to a new divided and conquer multiplication algorithm: 5

Recursive Multiply Take 2 PR E C O N D I T I O N: x and y are both binary bit arrays of length n, n a power of 2. PO S T C O N D I T I O N: Returns a binary bit array equal to the product of x and y. RE C MU L T I P L Y 2( x, y): if (len(x) == 1): return (x[0]*y[0]) xh = x[n/2:n] xl = x[0:n/2] yh = x[n/2:n] yl = x[0:n/2] p1 = RE C MU L T I P L Y 2(xh, yh) p2 = RE C MU L T I P L Y 2(xh+xl, yh+yl) p3 = RE C MU L T I P L Y 2(xl, yl) p2 = B I N A R Y A D D(p2,-p1, -p3) p2 = S H I F T(p2, n/2) p1 = S H I F T(p1,n) return B I N A R Y A D D(p1,p2,p3) Q: What is the recurrence relation for REC MULTIPLY 2? T (n) = 3T (n/2) + O(n) T (1) = c Q: Is this really any better than T (n) = 4T (n/2) + O(n)? A: See Assignment 1! 6

Program Correctness Chapter 2 Proving program correctness really means proving If some condition P holds at the start of the execution of a program, then the program will terminate some condition Q will hold at the end. Condition P is called a precondition. Condition Q is called a postcondition. Think of this as a contract, if the precondition is satisfied then the progam is required to meet the postcondition. Note: we are not concerned with runtime errors (e.g. overflow, division by zero). They are easier to spot. Two cases we will consider: recursive programs (programs with recursive methods) iterative programs (programs with loops) 7

The Correctness of Recursive Programs Read the book, pages 47 53. In this section, we consider how to prove correct programs that contain recursive methods. We do this by using simple or complete induction over the arguments to the recursive method. How to do the proof To prove a recursive program correct (for a given precondition and a postcondition) we typically 1. prove the recursive method totally correct 2. prove the main program totally correct 8

A first example public class EXP { int EX P O(u,v){ if v == 0 return 1; else if v is even return SQ U A R E(EX P O(u,v DIV 2)); else return u *(SQ U A R E(EX P O(u,v DIV 2))); } int SQ U A R E(x){ return x*x; } void main(){ z = EX P O(x,y); } } *Note DIV truncates the decimals Note: the main program here does nothing but call the method on x and y Lemma: For all m, n N, the method EXPO(m, n) terminates and returns the value m n. Proof: next page Theorem: The program EXP is (totally) correct for precondition x, y N and postcondition z = x y. Proof: immediate from the lemma. 9

Lemma: For all m, n N, the method EXPO(m, n) terminates and returns the value m n. Proof: We prove by complete induction that P(n) holds for all n N, where P(n) is for all m N, EXPO(m, n) terminates and returns m n. Assume that n N, and that P(i) holds for all i N, 0 i < n. So, we have that for all i, m N, i < n: EXPO(m, i) terminates and returns m i. (*) To prove P(n), there are three cases to consider: Case 1: n = 0. For any m, EXPO(m, 0) terminates and returns 1 = m 0. Case 2: n > 0 n is odd. From the code, for any m, when n > 0 and n is odd, EXPO(m, n) works by first calling EXPO(m, n DIV 2), then calling SQUARE on the result, and finally multiplying that result by m. Q.Why is this correct? 10

Case 2 cont. Since n is odd, (n 1) (n 1) n DIV 2 = N and < n. 2 2 So we can apply (*), EXPO(m, n DIV 2) terminates and returns m (n 1)/2. The method SQUARE always terminates, and returns (m (n 1)/2 ) 2 = m (n 1). Therefore, EXPO(m, n) terminates and returns m m (n 1) = m n. Case 3: n > 0 n is even. similar to previous case We conclude that the lemma holds for all n and m. 11

Recall our recursive multiply algorithm: PR E C O N D I T I O N: x and y are both binary bit vectors of length n (n power of 2). PO S T C O N D I T I O N: Returns a binary bit vector equal to the product of x and y. RE C MU L T I P L Y(x, y): if (len(x) == 1): return ([x[0]*y[0]]) xh = x[n/2:n] xl = x[0:n/2] yh = x[n/2:n] yl = x[0:n/2] a = RE C MU L T I P L Y(xh, yh) b = RE C MU L T I P L Y(xh, yl) c = RE C MU L T I P L Y(xl, yh) d = RE C MU L T I P L Y(xl, yl) b = B I N A R Y A D D(b,c) a = S H I F T(a,n) b = S H I F T(b,n/2) return B I N A R Y A D D(a,b,d) 12

Let s prove that RE C MU L T I P L Y 2(x,y) does indeed return the product of x and y. Proof by complete induction. 1. Define P (n): Rec Multiply 2( x, y) meets the postcondition, i.e., terminates and returns the product of x and y where x and y are n bit binary numbers. RTP: P (n) is true for all even n N and n = 1. 2. Base Case: n = 1 Then xy is returned. 3. Inductive Hypothesis: Assume that for arbitrary n N, n even, that P (k) is true for all k < n, k a power of 2. 4. Inductive Step: By the IH, each call to Rec Multiply 2 correctly returns the product of the parameters. The is guaranteed since each parameter is a power of 2 and < n. by Gauss observation, the sum p 1 2 n + (p 2 p 1 p 3 ) 2 n/2 + p 3 = xy. Therefore, Rec Multiply 2 terminates and returns xy meeting the post-condition. 13