MA/CSSE 473 Day 04 Multiplication runtime Multiplication based on Gauss formula Mathematical induction review.

Slides:



Advertisements
Similar presentations
CS Divide and Conquer/Recurrence Relations1 Divide and Conquer.
Advertisements

A simple example finding the maximum of a set S of n numbers.
Recursive Definitions and Structural Induction
MA/CSSE 473/474 How (not) to do an induction proof.
MA/CSSE 473 Day 13 Divide and Conquer Closest Points Convex Hull Answer Quiz Question 1.
Algorithms Recurrences. Definition – a recurrence is an equation or inequality that describes a function in terms of its value on smaller inputs Example.
Induction and Recursion. Odd Powers Are Odd Fact: If m is odd and n is odd, then nm is odd. Proposition: for an odd number m, m k is odd for all non-negative.
Induction and recursion
Divide-and-Conquer1 7 2  9 4   2  2 79  4   72  29  94  4.
Data Structures, Spring 2004 © L. Joskowicz 1 Data Structures – LECTURE 3 Recurrence equations Formulating recurrence equations Solving recurrence equations.
Data Structures, Spring 2006 © L. Joskowicz 1 Data Structures – LECTURE 3 Recurrence equations Formulating recurrence equations Solving recurrence equations.
CSE115/ENGR160 Discrete Mathematics 03/31/11
Tirgul 6 B-Trees – Another kind of balanced trees Problem set 1 - some solutions.
Recurrences Part 3. Recursive Algorithms Recurrences are useful for analyzing recursive algorithms Recurrence – an equation or inequality that describes.
CSS342: Induction1 Professor: Munehiro Fukuda. CSS342: Induction2 Today’s Topics Review of sequence and summations Mathematical induction Strong form.
Module #1 - Logic 1 Based on Rosen, Discrete Mathematics & Its Applications. Prepared by (c) , Michael P. Frank. Modified By Mingwu Chen Induction.
1 Section 3.3 Mathematical Induction. 2 Technique used extensively to prove results about large variety of discrete objects Can only be used to prove.
Induction and recursion
Copyright © Cengage Learning. All rights reserved. CHAPTER 11 ANALYSIS OF ALGORITHM EFFICIENCY ANALYSIS OF ALGORITHM EFFICIENCY.
What it is? Why is it a legitimate proof method? How to use it?
MA/CSSE 473 Day 03 Asymptotics A Closer Look at Arithmetic With another student, try to write a precise, formal definition of “t(n) is in O(g(n))”
Divide-and-Conquer 7 2  9 4   2   4   7
Induction and recursion
Analysis of Algorithms
MA/CSSE 473 Day 17 Divide-and-conquer Convex Hull Strassen's Algorithm: Matrix Multiplication.
MA/CSSE 473 Day 02 Some Numeric Algorithms and their Analysis.
Data Structures and Algorithms A. G. Malamos
Project 2 due … Project 2 due … Project 2 Project 2.
Divide and Conquer Andreas Klappenecker [based on slides by Prof. Welch]
Design of Algorithms by Induction Part 1 Algorithm Design and Analysis Week 3 Bibliography: [Manber]- Chap.
Induction Proof. Well-ordering A set S is well ordered if every subset has a least element. [0, 1] is not well ordered since (0,1] has no least element.
MA/CSSE 473 Day 02 Some Numeric Algorithms and their Analysis.
ICS 253: Discrete Structures I Induction and Recursion King Fahd University of Petroleum & Minerals Information & Computer Science Department.
CompSci 102 Discrete Math for Computer Science March 1, 2012 Prof. Rodger Slides modified from Rosen.
CS 103 Discrete Structures Lecture 13 Induction and Recursion (1)
4/3/2003CSE More Math CSE Algorithms Euclidean Algorithm Divide and Conquer.
Divide and Conquer Andreas Klappenecker [based on slides by Prof. Welch]
COMPSCI 102 Discrete Mathematics for Computer Science.
1 INFO 2950 Prof. Carla Gomes Module Induction Rosen, Chapter 4.
Lecture 5 Today, how to solve recurrences We learned “guess and proved by induction” We also learned “substitution” method Today, we learn the “master.
Mathematical Induction Section 5.1. Climbing an Infinite Ladder Suppose we have an infinite ladder: 1.We can reach the first rung of the ladder. 2.If.
Mathematical Induction
Divide and Conquer Faculty Name: Ruhi Fatima Topics Covered Divide and Conquer Matrix multiplication Recurrence.
CompSci 102 Discrete Math for Computer Science March 13, 2012 Prof. Rodger Slides modified from Rosen.
Chapter 5. Section 5.1 Climbing an Infinite Ladder Suppose we have an infinite ladder: 1.We can reach the first rung of the ladder. 2.If we can reach.
MA/CSSE 473 Day 30 Optimal BSTs. MA/CSSE 473 Day 30 Student Questions Optimal Linked Lists Expected Lookup time in a Binary Tree Optimal Binary Tree (intro)
MA/CSSE 473 Days Optimal linked lists Optimal BSTs.
MA/CSSE 473 Day 09 Modular Division Revisited Fermat's Little Theorem Primality Testing.
MA/CSSE 473 Day 06 Mathematical Induction Modular Arithmetic Do question 1 on today's quiz (work with another person)
MA/CSSE 473 Day 06 DivisionPrimes Modular Arithmetic.
MA/CSSE 473 Day 05 More induction Factors and Primes Recursive division algorithm.
Chapter 5 With Question/Answer Animations 1. Chapter Summary Mathematical Induction - Sec 5.1 Strong Induction and Well-Ordering - Sec 5.2 Lecture 18.
Section Recursion 2  Recursion – defining an object (or function, algorithm, etc.) in terms of itself.  Recursion can be used to define sequences.
Mathematical Induction EECS 203: Discrete Mathematics Lecture 11 Spring
Mathematical Induction What it is? Why is it a legitimate proof method? How to use it?
MA/CSSE 473 Day 05 Factors and Primes Recursive division algorithm.
Chapter 5 1. Chapter Summary  Mathematical Induction  Strong Induction  Recursive Definitions  Structural Induction  Recursive Algorithms.
(Proof By) Induction Recursion
MA/CSSE 473 Day 04 Multiplication runtime
MA/CSSE 473 Day 05 Factors and Primes Recursive division algorithm.
MA/CSSE 473 Day 16 Answers to your questions Divide and Conquer
CS2210:0001Discrete Structures Induction and Recursion
MA/CSSE 473 Day 02 Some Numeric Algorithms and their Analysis
Induction and recursion
Great Theoretical Ideas in Computer Science
Chapter 5 Induction and Recursion
CSCE 411 Design and Analysis of Algorithms
Induction and recursion
Divide-and-Conquer 7 2  9 4   2   4   7
Induction and recursion
Presentation transcript:

MA/CSSE 473 Day 04 Multiplication runtime Multiplication based on Gauss formula Mathematical induction review

MA/CSSE 473 Day 04 Divide and Conquer Multiplication à la Gauss Mathematical Induction review Tiling with Trominoes (if there is time) – 8by8/ 8by8/ What questions do you have?

EFFICIENT INTEGER MULTIPLICATION CONTINUED

For reference: The Master Theorem The Master Theorem for Divide and Conquer recurrence relations: Consider the recurrence T(n) = aT(n/b) +f(n), T(1)=c, where f(n) = Ѳ(n k ) and k≥0, The solution is –Ѳ(n k )if a < b k –Ѳ(n k log n)if a = b k –Ѳ(n log b a )if a > b k For details, see Levitin pages or Weiss section Grimaldi's Theorem 10.1 is a special case of the Master Theorem. We will use this theorem often. You should review its proof soon (Weiss's proof is a bit easier than Levitin's).

Recap: "Ordinary" Multiplication Example: multiply 13 by x (1101 times 1) (1101 times 1, shifted once) (1101 times 1, shifted twice) (1101 times 1, shifted thrice) (binary 143) There are n rows of 2 n bits to add, so we do an O( n ) operation n times, thus the whole multiplication is O( ) ? Can we do better? Q 1

This approach was known to Al Khwarizimi Still used today in some European countries –According to Dasgupta, et al –Repeat until 1 st number is 1, keeping all results: Divide 1 st number by 2 (rounding down) double 2 nd number Example Correct? Analysis Multiplication by an Ancient Method Then strike out any rows whose first number is even, and add up the remaining numbers in the second column. Q 2

Recursive code for this algorithm

New Multiplication Approach Divide and Conquer To multiply two n-bit integers x and y: –Split each into its left and right halves so that x = 2 n/2 x L + x R, and y = 2 n/2 y L + y R –The straightforward calculation of xy would be (2 n/2 x L + x R )(2 n/2 y L + y R ) = 2 n x L y L + 2 n/2 (x L y R + x R y L ) + x R y R –Code on next slide –Thus T(n) =. Solution?

Divide and Conquer multiplication Recurrence relation: solution: Q 3

Can we do better than O(n 2 )? Is there an algorithm for multiplying two n-bit numbers in time that is less than O(n 2 )? Basis: A discovery of Carl Gauss ( ) –Multiplying complex numbers: –(a + bi)(c+di) = ac – bd + (bc + ad)i

Gauss's Algorithm (a + bi)(c+di) = ac – bd + (bc + ad)i –Needs four real-number multiplications and three additions But bc + ad = (a+b)(c+d) – ac –bd –And we have already computed ac and bd when we computed the real part of the product! Thus we can do the complex product with three multiplications and five additions Additions are so much faster than multiplications that we can essentially ignore them. A little savings, but not a big deal until applied recursively! Q 4

Code for Gauss-based Algorithm Recurrence relation: solution: Q 5

Is this really a lot faster? Standard multiplication: Ѳ(n 2 ) Divide and conquer with Gauss trick: Ѳ(n 1.59 ) But there is a lot of additional overhead with Gauss, so standard multiplication is faster for small values of n. In reality we would not let the recursion go down to the single bit level, but only down to the number of bits that our machine can multiply in hardware without overflow.

QUICK REVIEW OF MATHEMATICAL INDUCTION Back to the "review thread"

Induction Review To show that property* P(n) is true for all integers n≥n 0, it suffices to show: –Ordinary Induction P(n 0 ) is true For all k≥n 0, if P (k) is true, then P(k+1) is also true. or –Strong Induction P(n 0 ) is true (sometimes you need multiple base cases) For all k>n 0, if P(j) is true for all j with n 0 ≤ j < k, then P(k) is also true. * In this context, a property is a function whose domain is a subset of the non-negative integers and whose range is {true, false} Q 6

Induction examples For all N≥0, –This is formula 7 on P 470, and is useful in one of the HW2 problems Show that any postage amount of 24 cents or more can be achieved using only 5-cent stamps and 7-cent stamps Q 7-8

Proof by Induction On Liquor Production by David M. Smith A friend who's in liquor production Owns a still of astounding construction. The alcohol boils Through old magnetic coils… She says that it's "proof by induction." Disclaimer: The presentation of this multiple pun should not be taken as an implied endorsement on the part of the instructor of the production and/or consumption of liquor. For example, according to the National Institutes of Health ( ), 40% of traffic deaths involve alcohol. NIH studies revealed that young people who began drinking before age 15 are four times more likely to develop alcohol dependence during their lifetime than those who began drinking at age 21 or later. Those that drank before age 15 are also seven times more likely to report having been in a traffic crash because of drinking both during adolescence and adulthood. Alcohol also plays a significant role in risky sexual behavior and increases the risk of physical and sexual assault. Among college students under age 21, 50,000 experience alcohol-related date rape, and 43,000 are injured by another student who has been drinking. Each year, approximately 5,000 persons under the age of 21 die from causes related to underage drinking. These deaths include about 1,600 homicides and 300 suicides.

Another Induction Example Tiling with Trominoes We saw that a 2 n  2 n checkerboard can be tiled with dominoes. What about trominoes? Clearly, we can't tile an entire board! Definition: A deficient rectangular grid of squares is one that has one square missing. It's easy to see that we can tile a 2  2 deficient rectangle! (We can rotate the tromino) Note: HW 4 will mainly be about tiling with trominoes.

Trominoes Continued What about a 4 x 4 deficient rectangle? Can we tile this? Fun with Tromino tiling :

Trominoes Continued Prove by induction that we can tile any 2 n  2 n deficient rectangle with trominoes Base case: n=1 Done Assume that we can do it for n=k Show that we can do it for n=k+1 Assume WLOG that the missing square is in the lower right quadrant of the rectangle –If it is somewhere else, we could simply rotate the board. –Can we place one tromino in a way that allows us to use the induction assumption?

Another Induction Example Extended Binary Tree (EBT) An Extended Binary tree is either –an external node, or –an ( internal ) root node and two EBTs T L and T R. We draw internal nodes as circles and external nodes as squares. –Generic picture and detailed picture. This is simply an alternative way of viewing binary trees, in which we view the null pointers as “places” where a search can end or an element can be inserted.

Property P(N): For any N>=0, any EBT with N internal nodes has _______ external nodes. Proof by strong induction, based on the recursive definition. –A notation for this problem: IN(T), EN(T) –Note that, like some other simple examples, this one can be done without induction. –But the purpose of this exercise is practice with strong induction, especially on binary trees. What is the crux of any induction proof? –Finding a way to relate the properties for larger values (in this case larger trees) to the property for smaller values (smaller trees). Do the proof now. A property of EBTs Q9-12