n = 1 What problems are interesting when n is just 1?

Similar documents
! Addition! Multiplication! Bigger Example - RSA cryptography

7.3 Divide-and-Conquer Algorithm and Recurrence Relations

Design and Analysis of Algorithms Prof. Madhavan Mukund Chennai Mathematical Institute. Week 02 Module 06 Lecture - 14 Merge Sort: Analysis

Primality Testing! 1

CS473 - Algorithms I

Introduction to Algorithms

CSC Design and Analysis of Algorithms

Chapter 4. Divide-and-Conquer. Copyright 2007 Pearson Addison-Wesley. All rights reserved.

CSC Design and Analysis of Algorithms. Lecture 6. Divide and Conquer Algorithm Design Technique. Divide-and-Conquer

DIVIDE & CONQUER. Problem of size n. Solution to sub problem 1

What we still don t know about addition and multiplication

Divide-and-Conquer. The most-well known algorithm design strategy: smaller instances. combining these solutions

Chapter 1: Number and Operations

More Complicated Recursion CMPSC 122

(Refer Slide Time 04:53)

What we still don t know about addition and multiplication

Lecture 7: Divide & Conquer 2. Integer Multiplication. & Matrix Multiplication. CS 341: Algorithms. Tuesday, Jan 29 th 2019

Test 1 Review Questions with Solutions

Lecture 1: Overview

Grade 6 Math Circles November 6 & Relations, Functions, and Morphisms

Parallel Algorithms CSE /22/2015. Outline of this lecture: 1 Implementation of cilk for. 2. Parallel Matrix Multiplication

Algorithm Analysis. (Algorithm Analysis ) Data Structures and Programming Spring / 48

Computer Science 385 Analysis of Algorithms Siena College Spring Topic Notes: Divide and Conquer

CIS 194: Homework 6. Due Monday, February 25. Fibonacci numbers

(Refer Slide Time: 01.26)

UNIT-2 DIVIDE & CONQUER

Skill 1: Multiplying Polynomials

(Refer Slide Time 6:48)

Arithmetic Processing

Module 2: Classical Algorithm Design Techniques

17/05/2018. Outline. Outline. Divide and Conquer. Control Abstraction for Divide &Conquer. Outline. Module 2: Divide and Conquer

Unit #3: Recursion, Induction, and Loop Invariants

COMP 3403 Algorithm Analysis Part 2 Chapters 4 5. Jim Diamond CAR 409 Jodrey School of Computer Science Acadia University

Principles of Algorithm Design

15-451/651: Design & Analysis of Algorithms January 26, 2015 Dynamic Programming I last changed: January 28, 2015

Divisibility Rules and Their Explanations

Unit #2: Recursion, Induction, and Loop Invariants

Algorithms and Data Structures, or

Multiple-choice (35 pt.)

(I m not printing out these notes! Take your own.)

Divide-and-Conquer. Dr. Yingwu Zhu

Attendance (2) Performance (3) Oral (5) Total (10) Dated Sign of Subject Teacher

Chapter 3 Dynamic programming

Solutions to Exam Data structures (X and NV)

CPSC 320 Sample Solution: Programming competitions, CPSC 320 and Divide-and-Conquer

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

Reading 8 : Recursion

Scan and Quicksort. 1 Scan. 1.1 Contraction CSE341T 09/20/2017. Lecture 7

Algorithms and Data Structures

GRADE 7 MATH LEARNING GUIDE

What is a Fraction? Fractions. One Way To Remember Numerator = North / 16. Example. What Fraction is Shaded? 9/16/16. Fraction = Part of a Whole

Writing Parallel Programs; Cost Model.

Section 05: Solutions

CS 3512, Spring Instructor: Doug Dunham. Textbook: James L. Hein, Discrete Structures, Logic, and Computability, 3rd Ed. Jones and Barlett, 2010

Problem solving paradigms

/633 Introduction to Algorithms Lecturer: Michael Dinitz Topic: Sorting lower bound and Linear-time sorting Date: 9/19/17

CPSC 467b: Cryptography and Computer Security

Dense Matrix Algorithms

Advanced Algorithms. Problem solving Techniques. Divide and Conquer הפרד ומשול

Integer Multiplication and Division

Intro. Scheme Basics. scm> 5 5. scm>

CSE373: Data Structure & Algorithms Lecture 21: More Comparison Sorting. Aaron Bauer Winter 2014

AXIOMS FOR THE INTEGERS

"Unpacking the Standards" 5th Grade Student Friendly "I Can" Statements I Can Statements I can explain what the remainder means in a word problem.

Algorithms (III) Yu Yu. Shanghai Jiaotong University

CSE 421 Algorithms: Divide and Conquer

Algorithms (III) Yijia Chen Shanghai Jiaotong University

(Refer Slide Time 3:31)

MA/CSSE 473 Day 05. Integer Primality Testing and Factoring Modular Arithmetic intro Euclid s Algorithm

Presentation for use with the textbook, Algorithm Design and Applications, by M. T. Goodrich and R. Tamassia, Wiley, Merge Sort & Quick Sort

n! = 1 * 2 * 3 * 4 * * (n-1) * n

Dynamic Programming. Lecture Overview Introduction

ELEMENTARY NUMBER THEORY AND METHODS OF PROOF

Algorithms (III) Yijia Chen Shanghai Jiaotong University

Algorithms and Data Structures. Algorithms and Data Structures. Algorithms and Data Structures. Algorithms and Data Structures

Figure 4.1: The evolution of a rooted tree.

COMP 250 Fall recurrences 2 Oct. 13, 2017

1 Counting triangles and cliques

CPSC 320 Sample Solution: Physics, Tug-o-War, and Divide-and-Conquer

Sorting. Riley Porter. CSE373: Data Structures & Algorithms 1

Selection (deterministic & randomized): finding the median in linear time

ELEMENTARY NUMBER THEORY AND METHODS OF PROOF

Algorithmic Analysis. Go go Big O(h)!

Divide and Conquer Algorithms

SUMMER REVIEW PACKET 2 FOR STUDENTS ENTERING ALGEBRA 1

2/26/2016. Divide and Conquer. Chapter 6. The Divide and Conquer Paradigm

Section 05: Solutions

COMP 161 Lecture Notes 16 Analyzing Search and Sort

Two Plus You. Unit. National PASS Center 2013

Week - 01 Lecture - 03 Euclid's Algorithm for gcd. Let us continue with our running example of gcd to explore more issues involved with program.

Computer Science 210 Data Structures Siena College Fall Topic Notes: Recursive Methods

1 Motivation for Improving Matrix Multiplication

HOW TO DIVIDE: MCC6.NS.2 Fluently divide multi-digit numbers using the standard algorithm. WORD DEFINITION IN YOUR WORDS EXAMPLE

More complicated than addition. Let's look at 3 versions based on grade school algorithm (multiplicand) More time and more area

CSC236 Week 4. Larry Zhang

Notes and Answers to Homework Exam 1, Geometric Algorithms, 2017

Data Structures and Algorithms CMPSC 465

JMC 2015 Teacher s notes Recap table

MITOCW watch?v=kvtlwgctwn4

Chapter 18 out of 37 from Discrete Mathematics for Neophytes: Number Theory, Probability, Algorithms, and Other Stuff by J. M. Cargal.

Transcription:

What if n=1???

n = 1 What problems are interesting when n is just 1? Sorting? No Median finding? No Addition? How long does it take to add one pair of numbers? Multiplication? How long does it take to multiply one pair of numbers? Primality? How long does it take to determine with a given number is prime?

It s all about the digits For problems such as these, the runtime is proportional to the number of digits in the numbers. We rephrase our questions as: How long does it take to add one pair of d-digit numbers? How long does it take to multiply one pair of d-digit numbers? How long does it take to determine with a given d-digit number is prime?

Adding d-digit numbers We will count any digit-level pairwise math operation as our unit of work since comparisons don t really make sense here. Grade-school math says we can: - work from right to left adding digits one column at a time (if there is a carry we add it in too) - if there are d-digits, there are d columns, so certainly less than 3d, and looking at it carefully it can be done in 2d, and with custom operators maybe even 1d either way, the runtime is O(d) Can we do this with fewer than d operations? No, so it is Θ(d).

Multiplying d-digit numbers Again, we will count any digit-level pairwise math operation as work since comparisons don t really make sense here. Grade-school math says we can: - for each position in the second number, work from right to left multiplying it against the digits in the first number and then add all of the resulting numbers - if there are d-digits, there are d columns, so the runtime of this is at least d 2 Note: Division has similar issues. Can we do this with fewer than O(d 2 ) operations?

Primality Testing runtime? We could start looping 2 to n-1 and testing whether that value divides our number. We could loop from 2 to sqrt(n) by basic math. We could iterate through a list of all known primes from 2 to sqrt(n) by some slightly more advanced math. Since this requires that list of all known primes up to that point, randomly picking odd numbers is used in some places, using probable primes by others. However, these depend on dividing!

Fermat primality test We won t go into this approach here (I won t call it an algorithm) but relating things back to an earlier topic, there is something called the Fermat primality test which is a faster test but is sometimes wrong. Numbers tested with this approach where a factor is not found are called probably prime numbers.

Solid Math can help too Consider the following for algorithm #2: - The quotient-remainder theorem tells us all integers can be written as 6k+d where k is an integer and d is 0, 1, 2, 3, 4, or 5. - We could easily prove that: 2 divides (6k+0), (6k+2), (6k+4) 3 divides (6k+3) So, with a little bit of CMSC250 cleverness, we know that if our target isn t divisible by 2 or 3, we only need to test it against numbers that can be written as (6k+1) or (6k+5) for a savings of a constant factor of 3.

Faster Multiplication (and Division) There are ways to do division based on how we do multiplication, but we will focus on multiplication for conceptual reasons

Divide&Conquer Multiplying Without loss of generality, let s talk in terms of binary numbers. Let s say we want to multiply a and b. We can write a as a 1 a 2 where a 1 is the first half of n bits and a 2 is the second half of the bits and do the same for b. It turns out that we could try a clever divide and conquer approach: ab = a 1 b 1 2 n +(a 1 b 2 +a 2 b 1 )2 0.5n +a 2 b 2 The question is, does this actually HELP us at all?

Divide&Conquer Multiplying ab = a 1 b 1 2 n +(a 1 b 2 +a 2 b 1 )2 0.5n +a 2 b 2 This equation has four multiplications on (n/2)-bit numbers but also has some other multiplication on larger-bit numbers. However, we can be clever in two ways: avoid having to multiply a 1 b 1 by 2 n but rather add the a 1 b 1 2 n and a 2 b 2 terms by concatenating the a 1 b 1 and a 2 b 2 terms avoid having to multiply by 2 0.5n if we instead shift the sum of the a 1 b 2 +a 2 b 1 value over the appropriate number of bits before adding to the a 1 b 1 a 2 b 2 concatenation result We know addition is linear in the number of bits, so we get M(n) = 4M(n/2)+cn

M(n) = 4M(n/2)+cn What does M(n) = 4M(n/2)+cn work out to for runtime? If you do a quick recursion-tree analysis (work it out for practice) you will find that the runtime is n 2. So, it is perhaps clever but it is no improvement so far

Getting really clever We want to multiply a and b. We wrote a as a 1 a 2 and b as b 1 b 2. We could say: ab = a 1 b 1 2 n +(a 1 b 2 +a 2 b 1 )2 0.5n +a 2 b 2 but we could also get even more clever and say: p 1 =a 1 b 1 p 2 =a 2 b 2 p 3 =(a 1 +a 2 )(b 1 +b 2 ) and make the product be: ab = p 1 2 n +(p 3 -p 1 -p 2 )2 0.5n +p 2 We have now replaced one multiplication with some more addition and some subtraction (which can also be done in linear time like addition).

M(n) = 3M(n/2)+sn What does M(n) = 3M(n/2)+sn work out to for runtime? If you do a quick recursion-tree analysis (work it out for practice) you will find that the runtime is n log2(3). So, some more cleverness which this time leads to a better asymptotic runtime class!

What about division? There are ways to do it in the same runtime as multiplication

How many bits? If you wanted your program to be able to store individual unsigned integers between 1 and N, how many bits would your variable need to have? Answer: log 2 (N)

Time and Space You are monitoring a feed of unique numbers and all numbers between 1 and N will come through the feed EXCEPT ONE OF THEM. You basically get to see it once and then it's gone into the ether. N will be very large and you are not allowed to use a data structure or file to hold all those values. How can you determine the missing number after having seen N-1 numbers pass through the feed. How many bits of extra storage do you need? What if TWO were missing?

Matrix Addition Adding two 2x2 matrices: a a 11 21 a a 12 22 + b b 11 21 b b 12 22 gives: c c 11 21 c c 12 22 Where c ij =a ij +b ij. So, in general, if the matrix is nxn, an n 2 algorithm.

Matrix Multiplication Multiplying two 2x2 matrices: a a 11 21 a a 12 22 b b 11 21 b b 12 22 gives: c c 11 21 c c 12 22 Where c ij = 2 k = 1 ( a ik b kj ) So, in general, if the matrix is nxn, an n 3 algorithm.

Run-Time Improvement? Using the brute force algorithms that we saw earlier, we get runtimes of: MM(n)=Θ(n 3 ) MA(n)=Θ(n 2 ) Do you think there s any way to improve either? If we thought about the lower bounds, what do they feel like they d be?

Start with a small step If we wanted to multiply two 2x2 matrices it could easily be done using 8 multiplication operations. Is there a way to do it using only 7 multiplication operations, and a constant number of additional addition operations? If there were, would this help bring down the asymptotic runtime of full matrix multiplication?

Divide and Conquer It turns out that matrix multiplication can be solved recursively by first imagining any n by n matrix as four smaller matrices, each being n/2 by n/2 and then recursively multiplying those. So, if 2x2 matrix multiplication could be done using 7 multiplication and some number of additions, the overall problem would be MM(2) = constant amount of work MM(n) = 7 MM(n/2) + Θ(n 2 ) Is this better than O(n 3 )? Work out the recurrence tree

Strassen s algorithm Since MATH240 isn t a prerequisite for this class we won t dive into the very clever Strassen algorithm for doing the 2x2 problem using only 7 multiplication operations, but it s easy to find online if you are curious