CS/COE 1501 cs.pitt.edu/~bill/1501/ More Math

Similar documents
Applied Cryptography and Network Security

UCT Algorithm Circle: Number Theory

! Addition! Multiplication! Bigger Example - RSA cryptography

COP 4516: Math for Programming Contest Notes

Lecture Notes, CSE 232, Fall 2014 Semester

Recursion. CSCI 112: Programming in C

Algorithms with numbers (1) CISC4080, Computer Algorithms CIS, Fordham Univ.! Instructor: X. Zhang Spring 2017

Solutions to the Second Midterm Exam

Creating a new data type

CS 310 Advanced Data Structures and Algorithms

Conditionals !

Data Dependences and Parallelization

1 Elementary number theory

Euclid's Algorithm. MA/CSSE 473 Day 06. Student Questions Odd Pie Fight Euclid's algorithm (if there is time) extended Euclid's algorithm

Mathematics. Jaehyun Park. CS 97SI Stanford University. June 29, 2015

Programming Techniques in Computer Algebra

CS473 - Algorithms I

Math Introduction to Advanced Mathematics

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

CS 97SI: INTRODUCTION TO PROGRAMMING CONTESTS. Jaehyun Park

Department of Computer Applications. MCA 312: Design and Analysis of Algorithms. [Part I : Medium Answer Type Questions] UNIT I

CSCE 411 Design and Analysis of Algorithms

Recursive Functions. Biostatistics 615 Lecture 5

BEng (Hons) Telecommunications. Examinations for / Semester 2

COE428 Lecture Notes Week 1 (Week of January 9, 2017)

CSC Design and Analysis of Algorithms. Lecture 5. Decrease and Conquer Algorithm Design Technique. Decrease-and-Conquer

Module 2: Classical Algorithm Design Techniques

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

Queens College, CUNY, Department of Computer Science Numerical Methods CSCI 361 / 761 Spring 2018 Instructor: Dr. Sateesh Mane.

Two Approaches to Algorithms An Example (1) Iteration (2) Recursion

Data Structures and Algorithms CMPSC 465

COMPSCI 230 Discrete Math Prime Numbers January 24, / 15

Data Structures and Algorithms CSE 465

Lecture 10: Recursion vs Iteration

CSC 421: Algorithm Design & Analysis. Spring 2015

CSC 222: Object-Oriented Programming. Spring 2012

CS583 Lecture 01. Jana Kosecka. some materials here are based on Profs. E. Demaine, D. Luebke A.Shehu, J-M. Lien and Prof. Wang s past lecture notes

Jana Kosecka. Linear Time Sorting, Median, Order Statistics. Many slides here are based on E. Demaine, D. Luebke slides

2.3 Recursion. Overview. Mathematical Induction. What is recursion? When one function calls itself directly or indirectly.

Overview. What is recursion? When one function calls itself directly or indirectly.

Multiple-choice (35 pt.)

Run Times. Efficiency Issues. Run Times cont d. More on O( ) notation

Standard Version of Starting Out with C++, 4th Edition. Chapter 19 Recursion. Copyright 2003 Scott/Jones Publishing

Recursion Chapter 3.5

Mergesort again. 1. Split the list into two equal parts

Algorithmic number theory Cryptographic hardness assumptions. Table of contents

Comp 182 Data Structures Sample Midterm Examination

ELEMENTARY NUMBER THEORY AND METHODS OF PROOF

Divide-and-Conquer. Dr. Yingwu Zhu

Reading 8 : Recursion

CSE373: Data Structures and Algorithms Lecture 4: Asymptotic Analysis. Aaron Bauer Winter 2014

PRAM Divide and Conquer Algorithms

Recursion Chapter 8. What is recursion? How can a function call itself? How can a function call itself?

CSC Design and Analysis of Algorithms

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

Parallel Algorithms for (PRAM) Computers & Some Parallel Algorithms. Reference : Horowitz, Sahni and Rajasekaran, Computer Algorithms

Plotting run-time graphically. Plotting run-time graphically. CS241 Algorithmics - week 1 review. Prefix Averages - Algorithm #1

Programming Iterative Loops. for while

1 Probabilistic analysis and randomized algorithms

Test 1 Review Questions with Solutions

Introduction to Programming in C Department of Computer Science and Engineering\ Lecture No. #02 Introduction: GCD

Problem Solving & C M. Tech. (CS) 1st year, 2014

Algorithm Design and Analysis

Loops / Repetition Statements

Types of recursion. Structural vs. general recursion. Pure structural recursion. Readings: none. In this module: learn to use accumulative recursion

What is an Algorithm?

CS116 - Module 5 - Accumulative Recursion

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

PROGRAM EFFICIENCY & COMPLEXITY ANALYSIS

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

Lab Manual. Program Design and File Structures (P): IT-219

Recursion Chapter 8. What is recursion? How can a function call itself? How can a function call itself? contains a reference to itself.

CS302 Topic: Algorithm Analysis #2. Thursday, Sept. 21, 2006

Lecture 3: Sorting 1

Types of recursion. Readings: none. In this module: a glimpse of non-structural recursion. CS 135 Winter : Types of recursion 1

CS669 Network Security

Analysis of Algorithms. Unit 4 - Analysis of well known Algorithms

Taking Stock. IE170: Algorithms in Systems Engineering: Lecture 5. The Towers of Hanoi. Divide and Conquer

Public Key Algorithms

CSC236 Week 5. Larry Zhang

Divide & Conquer. 2. Conquer the sub-problems by solving them recursively. 1. Divide the problem into number of sub-problems

1 Closest Pair Problem

Lecture 3 Algorithms with numbers (cont.)

UNIVERSITY REGULATIONS

Assertions & Verification & Example Loop Invariants Example Exam Questions

Lecture T5: Analysis of Algorithm

CSC Design and Analysis of Algorithms. Lecture 5. Decrease and Conquer Algorithm Design Technique. Decrease-and-Conquer

Taking Stock. IE170: Algorithms in Systems Engineering: Lecture 6. The Master Theorem. Some More Examples...

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

RSA System setup and test

Quick Sort. CSE Data Structures May 15, 2002

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

Intro to Computational Programming in C Engineering For Kids!

ECE 2574: Data Structures and Algorithms - Recursion Part I. C. L. Wyatt

Today: Finish up hashing Sorted Dictionary ADT: Binary search, divide-and-conquer Recursive function and recurrence relation

Assertions & Verification Example Exam Questions

LECTURE NOTES OF ALGORITHMS: DESIGN TECHNIQUES AND ANALYSIS

Solving Linear Recurrence Relations (8.2)

10/5/2016. Comparing Algorithms. Analyzing Code ( worst case ) Example. Analyzing Code. Binary Search. Linear Search

We can use a max-heap to sort data.

Transcription:

CS/COE 1501 cs.pitt.edu/~bill/1501/ More Math

Exponentiation x y Can easily compute with a simple algorithm: Runtime? ans = 1 i = y while i > 0: ans = ans * x i-- 2

Just like with multiplication, let s consider large integers... Runtime = # of iterations * cost to multiply Cost to multiply was covered in the last lecture So how many iterations? Single loop from 1 to y, so linear, right? What is the size of our input? n The bitlength of y So, linear in the value of y But, increasing n by 1 doubles the number of iterations Θ(2 n ) Exponential in the bitlength of y 3

This is RIDICULOUSLY BAD Assuming 512 bit operands, 2 512 : 134078079299425970995740249982058461274793658205923 933777235614437217640300735469768018742981669034276 900318581864860508537538828119465699464336490060840 96 Assume we can do multiplications in 1 cycle Which we can t as we discussed last lecture And further that these operations are completely parallelizable 16 cores at 4 GHz = 64,000,000,000 cycles/second (2 512 / 64000000000) / (60 * 60 * 24 * 365) = 6.64 * 10 135 years to compute 4

This is way too long to do exponentiations! So how do we do better? Let s try divide and conquer! x y = (x (y/2) ) 2 when y is even; (x (y/2) ) 2 * x when y is odd Analyzing a recursive approach: Base case? When y is 1, x y is x; when y is 0, x y is 1 Runtime? 5

Building another recurrence relation x y = (x (y/2) ) 2 = x (y/2) * x (y/2) Similarly, (x (y/2) ) 2 * x = x (y/2) * x (y/2) * x So, our recurrence relation is: T(n) = T(n-1) +? How much work is done per call? 1 (or 2) multiplication(s) Examined runtime of multiplication last lecture But how big are the operands in this case? 6

Determining work done per call Base case returns x n bits Base case results are multiplied: x * x n bit operands Result size? 2n These results are then multiplied: x 2 * x 2 2n bit operands Result size? 4n bits x (y/2) * x (y/2)? (y / 2) * n bit operands = 2 (n-1) * n bit operands Result size? y * n bits = 2 n * n bits 7

Multiplication input size increases throughout Our recurrence relation looks like: T(n) = T(n-1) + Θ((2 (n-1) * n) 2 ) multiplication input size square: assuming we use the gradeschool algorithm 8

Runtime analysis Can we use the master theorem? Nope, we don t have a b > 1 OK, then How many times can y be divided by 2 until a base case? lg(y) Further, we know the max value of y Relative to n, that is: 2 n So, we have, at most lg(y) = lg(2 n ) = n recursions 9

But we need to do expensive mult in each call We need to do Θ((2 (n-1) * n) 2 ) work in just the root call! Our runtime is dominated by multiplication time Exponentiation quickly generates HUGE numbers Time to multiply them quickly becomes impractical 10

Can we do better? We go top-down in the recursive approach Start with y Halve y until we reach the base case Square base case result Continue combining until we arrive at the solution What about a bottom-up approach? Start with our base case Operate on it until we reach a solution 11

A bottom-up approach To calculate x y ans = 1 foreach bit in y: ans = ans 2 if bit == 1: ans = ans * x From most to least significant 12

Bottom-up exponentiation example Consider x y where y is 43 (computing x 43 ) Iterate through the bits of y (43 in binary: 101011) ans = 1 ans = 1 2 = 1 ans = 1 * x = x ans = x 2 = x 2 ans = (x 2 ) 2 = x 4 ans = x 4 * x = x 5 ans = (x 5 ) 2 = x 10 ans = (x 10 ) 2 = x 20 ans = x 20 * x = x 21 ans = (x 21 ) 2 = x 42 ans = x 42 * x = x 43 13

Does this solve our problem with mult times? Nope, still squaring ans everytime We ll have to live with huge output sizes This does, however, save us recursive call overhead Practical savings in runtime 14

Greatest Common Divisor GCD(a, b) Largest int g that evenly divides both a and b (g a and g b) Recall that g a if i Z such that g*i = a Easiest approach: BRUTE FORCE i = min(a, b) while(a % i!= 0 b % i!= 0): Runtime? i-- Θ(min(a, b)) Linear! in the value of min(a, b) Exponential in n Assuming a, b are n-bit integers 15

Euclid s algorithm b GCD(a, b) = GCD(b, a % b) a % b a 16

Euclidean example 1 GCD(30, 24) = GCD(24, 30 % 24) = GCD(24, 6) = GCD(6, 24 % 6) = GCD(6, 0)... Base case! Overall GCD is 6 17

Euclidean example 2 = GCD(99, 78) 99 = 78 * 1 + 21 = GCD(78, 21) 78 = 21 * 3 + 15 = GCD(21, 15) 21 = 15 * 1 + 6 = GCD (15, 6) 15 = 6 * 2 + 3 = GCD(6, 3) 6 = 3 * 2 + 0 = 3 18

Analysis of Euclid s algorithm Runtime? Tricky to analyze, has been shown to be linear in n Where, again, n is the number of bits in the input 19

Extended Euclidean algorithm In addition to the GCD, the Extended Euclidean algorithm (XGCD) produces values x and y such that: GCD(a, b) = i = ax + by Examples: GCD(30,24) = 6 = 30 * 1 + 24 * -1 GCD(99,78) = 3 = 99 * -11 + 78 * 14 Can be done in the same linear runtime! 20

Extended Euclidean example = GCD(99, 78) 99 = 78 * 1 + 21 = GCD(78, 21) 78 = 21 * 3 + 15 = GCD(21, 15) 21 = 15 * 1 + 6 = GCD (15, 6) 15 = 6 * 2 + 3 = GCD(6, 3) = 3 6 = 3 * 2 + 0 3 = 15 - (2 * 6) 6 = 21-15 3 = 15 - (2 * (21-15)) = 15 - (2 * 21) + (2 * 15) = (3 * 15) - (2 * 21) 15 = 78 - (3 * 21) 3 = (3 * (78 - (3 * 21))) - (2 * 21) = (3 * 78) - (11 * 21) 21 = 99-78 3 = (3 * 78) - (11 * (99-78)) = (14 * 78) - (11 * 99) = 99 * -11 + 78 * 14 21

OK, but why? This and all of our large integer algorithms will be handy when we look at algorithms for implementing CRYPTOGRAPHY 22