CSE 20: Discrete Mathematics for Computer Science Prof. Shachar Lovett

Slides:



Advertisements
Similar presentations
CSE 311 Foundations of Computing I Lecture 13 Number Theory Autumn 2012 CSE
Advertisements

CSE 20 DISCRETE MATH Prof. Shachar Lovett Clicker frequency: CA.
Ancient Wisdom: Primes, Continued Fractions, The Golden Ratio, and Euclid’s GCD Great Theoretical Ideas In Computer Science Anupam GuptaCS Fall.
Induction Sections 41. and 4.2 of Rosen Fall 2008 CSCE 235 Introduction to Discrete Structures Course web-page: cse.unl.edu/~cse235 Questions:
Elementary Number Theory and Methods of Proof. Basic Definitions An integer n is an even number if there exists an integer k such that n = 2k. An integer.
3 Gallon Jug5 Gallon Jug Greatest Common Divisor Lecture 8: Sep 30.
6/20/2015 5:05 AMNumerical Algorithms1 x x1x
CSE 20 DISCRETE MATH Prof. Shachar Lovett Clicker frequency: CA.
Proofs, Recursion, and Analysis of Algorithms Mathematical Structures for Computer Science Chapter 2 Copyright © 2006 W.H. Freeman & Co.MSCS SlidesProofs,
Lecture 4 Discrete Mathematics Harper Langston. Algorithms Algorithm is step-by-step method for performing some action Cost of statements execution –Simple.
Great Theoretical Ideas in Computer Science.
Great Theoretical Ideas in Computer Science for Some.
CSE 311 Foundations of Computing I Lecture 12 Primes, GCD, Modular Inverse Spring
Introduction Dr. Ying Lu RAIK 283: Data Structures & Algorithms.
Great Theoretical Ideas in Computer Science.
Chapter 2 The Fundamentals: Algorithms, the Integers, and Matrices
COMP 170 L2 Page 1 L05: Inverses and GCDs l Objective: n When does have an inverse? n How to compute the inverse? n Need: Greatest common dividers (GCDs)
Module :MA3036NI Cryptography and Number Theory Lecture Week 7
Mathematics Review Exponents Logarithms Series Modular arithmetic Proofs.
Chapter 3 (Part 3): Mathematical Reasoning, Induction & Recursion  Recursive Algorithms (3.5)  Program Correctness (3.6)
© by Kenneth H. Rosen, Discrete Mathematics & its Applications, Sixth Edition, Mc Graw-Hill, 2007 Chapter 4 (Part 3): Mathematical Reasoning, Induction.
Greatest Common Divisor
Additional Problems.
CSE 20: Discrete Mathematics for Computer Science Prof. Shachar Lovett.
The Integers. The Division Algorithms A high-school question: Compute 58/17. We can write 58 as 58 = 3 (17) + 7 This forms illustrates the answer: “3.
Primes in P Deterministic polynomial-time algorithm of Agrawal, Kayal and Saxena Presented by Vladimir Braverman.
Copyright © Zeph Grunschlag, Basic Number Theory Zeph Grunschlag.
Greatest Common Divisor Jordi Cortadella Department of Computer Science.
CSE 20: Discrete Mathematics for Computer Science Prof. Shachar Lovett.
CSE 311: Foundations of Computing Fall 2014 Lecture 12: Primes, GCD.
Algorithms 1.Notion of an algorithm 2.Properties of an algorithm 3.The GCD algorithm 4.Correctness of the GCD algorithm 5.Termination of the GCD algorithm.
MA/CSSE 473 Day 06 Euclid's Algorithm. MA/CSSE 473 Day 06 Student Questions Odd Pie Fight Euclid's algorithm (if there is time) extended Euclid's algorithm.
CSE 20: Discrete Mathematics for Computer Science Prof. Shachar Lovett.
COMPSCI 102 Introduction to Discrete Mathematics.
MA/CSSE 473 Day 08 Extended Euclid's Algorithm Modular Division Fermat's little theorem.
CSE 311 Foundations of Computing I Lecture 14 Euclid’s Algorithm Mathematical Induction Autumn 2012 CSE
Application: Algorithms Lecture 20 Section 3.8 Wed, Feb 21, 2007.
Properties of the gcd Theorem: For any two integers a,b there exist integers x,y such that xa + yb = gcd(a,b). A proof will not be given at this point.
CSE 311: Foundations of Computing Fall 2013 Lecture 12: Primes, GCD, modular inverse.
Application: Algorithms Lecture 19 Section 3.8 Tue, Feb 20, 2007.
MA/CSSE 473 Day 07 Euclid's Algorithm. MA/CSSE 473 Day 07 Student Questions Review topics not covered in class Euclid's algorithm (if there is time) extended.
AF2. Turn off your phones Primes, gcd, some examples, reading.
Chapter 4 With Question/Answer Animations 1. Chapter Summary Divisibility and Modular Arithmetic - Sec 4.1 – Lecture 16 Integer Representations and Algorithms.
Great Theoretical Ideas in Computer Science.
CSE 20: Discrete Mathematics for Computer Science Prof. Shachar Lovett.
CSE 20: Discrete Mathematics for Computer Science Prof. Shachar Lovett.
Dr Nazir A. Zafar Advanced Algorithms Analysis and Design Advanced Algorithms Analysis and Design By Dr. Nazir Ahmad Zafar.
If more pigeons than pigeonholes, Pigeonhole Principle.
ALGORITHMS PROVING ALGORITHMS (PROGRAMS) CORRECT WITH AND WITHOUT INDUCTION.
Recursive Algorithms Section 5.4.
Chapter 4 (Part 3): Mathematical Reasoning, Induction & Recursion
Math/CSE 1019C: Discrete Mathematics for Computer Science Fall 2012
Advanced Algorithms Analysis and Design
CSE 20: Discrete Mathematics for Computer Science Prof. Shachar Lovett
CSE 311 Foundations of Computing I
Quick reviews / corrections
CSE 20: Discrete Mathematics for Computer Science Prof. Shachar Lovett
CSE 311 Foundations of Computing I
Great Theoretical Ideas in Computer Science
Greatest Common Divisor
CSE 311 Foundations of Computing I
Number Theory and Modular Arithmetic
Number Theory (Chapter 7)
Great Theoretical Ideas in Computer Science
CSE 20: Discrete Mathematics for Computer Science Prof. Shachar Lovett
CS 3343: Analysis of Algorithms
Discrete Math for CS CMPSC 360 LECTURE 12 Last time: Stable matching
CSE 311: Foundations of Computing
Application: Algorithms
Application: Algorithms
Presentation transcript:

CSE 20: Discrete Mathematics for Computer Science Prof. Shachar Lovett

Today’s Topics: GCD Euclid’s algorithm Extended Euclid’s algorithm

1. GCD The poor man version of prime factorization

GCD Greatest common divisor Given two positive integers a,b, their GCD is the largest integer n such that n|a and n|b

GCD What is GCD(20,30)? 5 10 20 30 Other

GCD GCD is the same as finding simplified rational expressions, just like you did in school GCD(20,30)=10 means that 20 30 = 2∗10 3∗10 = 2 3 and it can’t be simplified further

GCD What is GCD(101281371,347832984723)?

GCD How can we compute GCD(a,b)? Simple way: Compute prime factorization of a,b Take common primes and prime powers Example: if a=2103859 and b=21735 then GCD(a,b)=21035 However, we believe that computing the prime factorization of large numbers is hard… Euclid’s algorithm provides a much faster way

2. Euclid’s algorithm Fast GCD

Euclid’s algorithm Euclid(a,b): Input: 𝑎,𝑏∈𝑁 Output: 𝑔=gcd⁡(𝑎,𝑏) While b>0: x=a mod b a=b b=x Return a

Euclid’s algorithm Euclid(a,b): Input: 𝑎,𝑏∈𝑁 Output: 𝑔=gcd⁡(𝑎,𝑏) While b>0: x=a mod b a=b b=x Return a (a,b) (b,a mod b)

Euclid’s algorithm Euclid(a,b): Input: 𝑎,𝑏∈𝑁 Output: 𝑔=gcd⁡(𝑎,𝑏) While b>0: 𝑎,𝑏 ←(𝑏, 𝑎 𝑚𝑜𝑑 𝑏) Return a

Euclid’s algorithm Euclid(a,b): Input: 𝑎,𝑏∈𝑁 Output: 𝑔=gcd⁡(𝑎,𝑏) While b>0: 𝑎,𝑏 ←(𝑏, 𝑎 𝑚𝑜𝑑 𝑏) Return a Example run: a=20, b=30 a 20 30 10 b 30 20 10

Euclid’s algorithm The same basic questions Does it always terminate? Does it return the correct answer? How fast is it?

Euclid’s algorithm: termination Euclid(a,b): Input: 𝑎,𝑏∈𝑁 Output: 𝑔=gcd⁡(𝑎,𝑏) While 𝑏>0: 𝑎,𝑏 ←(𝑏, 𝑎 𝑚𝑜𝑑 𝑏) Return a Loop invariant (after 1st iteration): 𝑎>𝑏

Euclid’s algorithm: termination Euclid(a,b): Input: 𝑎,𝑏∈𝑁 Output: 𝑔=gcd⁡(𝑎,𝑏) While 𝑏>0: 𝑎,𝑏 ←(𝑏, 𝑎 𝑚𝑜𝑑 𝑏) Return a Loop invariant (after 1st iteration): 𝑎>𝑏 Loop beginning: (a,b) Loop end: (b, a mod b) By definition, 𝑎 𝑚𝑜𝑑 𝑏∈ 0,…,𝑏−1 and hence (a mod b) < b

Euclid’s algorithm: termination Euclid(a,b): Input: 𝑎,𝑏∈𝑁 Output: 𝑔=gcd⁡(𝑎,𝑏) While 𝑏>0: 𝑎,𝑏 ←(𝑏, 𝑎 𝑚𝑜𝑑 𝑏) Return a Loop invariant (after 1st iteration): 𝑎>𝑏 The value of a keeps decreasing, which proves termination

Euclid’s algorithm: correctness Euclid(a,b): Input: 𝑎,𝑏∈𝑁 Output: 𝑔=gcd⁡(𝑎,𝑏) While 𝑏>0: 𝑎,𝑏 ←(𝑏, 𝑎 𝑚𝑜𝑑 𝑏) Return a g=gcd(a,b)

Euclid’s algorithm: correctness Need to prove the following lemma Lemma: ∀𝑎,𝑏≥1, gcd 𝑎,𝑏 =gcd⁡(𝑏,𝑎 𝑚𝑜𝑑 𝑏)

Euclid’s algorithm: correctness Need to prove the following lemma Lemma: ∀𝑎,𝑏≥1, gcd 𝑎,𝑏 =gcd⁡(𝑏,𝑎 𝑚𝑜𝑑 𝑏) Proof: we will actually show ∀𝑚≥1, 𝑚 𝑎∧𝑚 𝑏 ⇔(𝑚 𝑏∧𝑚 𝑎 𝑚𝑜𝑑 𝑏 ) In particular, the largest such m is (by definition) the GCD, and so gcd 𝑎,𝑏 =gcd⁡(𝑏,𝑎 𝑚𝑜𝑑 𝑏)

Euclid’s algorithm: correctness Lemma: ∀𝑎,𝑏≥1, gcd 𝑎,𝑏 = gcd 𝑏,𝑎 𝑚𝑜𝑑 𝑏 Proof (⇒): 𝑚 𝑎∧𝑚 𝑏 ⇒(𝑚 𝑏∧𝑚 𝑎 𝑚𝑜𝑑 𝑏 ) Let 𝑎 = 𝑏𝑝+𝑞, where 𝑝=𝑎 𝑑𝑖𝑣 𝑏, 𝑞 = 𝑎 𝑚𝑜𝑑 𝑏. If 𝑚|𝑎,𝑚|𝑏 then 𝑎=𝑚𝑥, 𝑏=𝑚𝑦, where 𝑥,𝑦∈𝑍. Then: 𝑎 𝑚𝑜𝑑 𝑏=𝑞=𝑎−𝑏𝑝=𝑚𝑥−𝑚𝑦⋅𝑝=𝑚 𝑥−𝑦𝑝 So, 𝑚|(𝑎 𝑚𝑜𝑑 𝑏).

Euclid’s algorithm: correctness Lemma: ∀𝑎,𝑏≥1, gcd 𝑎,𝑏 = gcd 𝑏,𝑎 𝑚𝑜𝑑 𝑏 Proof (⇐): 𝑚 𝑎∧𝑚 𝑏 ⇐(𝑚 𝑏∧𝑚 𝑎 𝑚𝑜𝑑 𝑏 ) Let 𝑎 = 𝑏𝑝+𝑞, where 𝑝=𝑎 𝑑𝑖𝑣 𝑏, 𝑞 = 𝑎 𝑚𝑜𝑑 𝑏. If 𝑚|𝑏,𝑚|𝑞 then b=𝑚𝑥, q=𝑚𝑦, where 𝑥,𝑦∈𝑍. Then: 𝑎=𝑏𝑝+𝑞=𝑚𝑥⋅𝑝+𝑚𝑦=𝑚(𝑥𝑝+𝑦) So, 𝑚|𝑎.

Euclid’s algorithm: correctness Euclid(a,b): Input: 𝑎,𝑏∈𝑁 Output: 𝑔=gcd⁡(𝑎,𝑏) While 𝑏>0: 𝑎,𝑏 ←(𝑏, 𝑎 𝑚𝑜𝑑 𝑏) Return a g=gcd(a,b) Proved!

Euclid’s algorithm: speed Euclid(a,b): Input: 𝑎,𝑏∈𝑁 Output: 𝑔=gcd⁡(𝑎,𝑏) While 𝑏>0: 𝑎,𝑏 ←(𝑏, 𝑎 𝑚𝑜𝑑 𝑏) Return a How many iterations?

Euclid’s algorithm: speed Consider one iteration: 𝑎,𝑏 ←(𝑏, 𝑎 𝑚𝑜𝑑 𝑏) We know that a,b become smaller But by how much?

Euclid’s algorithm: speed Consider one iteration: 𝑎,𝑏 ←(𝑏, 𝑎 𝑚𝑜𝑑 𝑏) We know that a,b become smaller But by how much? Intuition: Since 𝑎 𝑚𝑜𝑑 𝑏∈{0,..,𝑏−1}, on “average” 𝑎 𝑚𝑜𝑑 𝑏≈𝑏/2. Hence, value of b decreases by a factor of 2 at each iteration (so log(b) iterations will be needed) Can we justify this?

Euclid’s algorithm: speed Consider one iteration: 𝑎,𝑏 ←(𝑏, 𝑎 𝑚𝑜𝑑 𝑏) Lemma: 𝑎+𝑏≥ 3 2 (𝑏+ 𝑎 𝑚𝑜𝑑 𝑏 ) Sum decreases by 3/2 at each iteration; So, we need at most ~log(a+b) iterations In fact, at most ~log(b) iterations, since after the first iteration, both values are at most b

Euclid’s algorithm: speed Consider one iteration: 𝑎,𝑏 ←(𝑏, 𝑎 𝑚𝑜𝑑 𝑏) Lemma: 𝑎+𝑏≥ 3 2 (𝑏+ 𝑎 𝑚𝑜𝑑 𝑏 ) Proof: Since 𝑎>𝑏>𝑎 𝑚𝑜𝑑 𝑏, and since b|𝑎−(𝑎 𝑚𝑜𝑑 𝑏), we must have 𝑎− 𝑎 𝑚𝑜𝑑 𝑏 ≥𝑏. So: 𝑎≥𝑏+(𝑎 𝑚𝑜𝑑 𝑏) 𝑏= 𝑏 2 + 𝑏 2 ≥ 1 2 𝑏+ 𝑎 𝑚𝑜𝑑 𝑏 ⇒𝑎+𝑏≥ 3 2 (𝑏+ 𝑎 𝑚𝑜𝑑 𝑏 )

3. Extended Euclid’s algorithm Using algorithms to do math!

Extended Euclid’s algorithm Theorem: ∀𝑎,𝑏∈𝑁 ∃𝑥,𝑦∈𝑍 𝑎𝑥+𝑏𝑦=gcd⁡(𝑎,𝑏) (this is called Extended Euclid’s algorithm) Example: a=3, b=5, gcd(a,b)=1 3*(-3)+5*2 = 1 (solution: x=-3, y=2)

Extended Euclid’s algorithm Theorem: ∀𝑎,𝑏∈𝑁 ∃𝑥,𝑦∈𝑍 𝑎𝑥+𝑏𝑦=gcd⁡(𝑎,𝑏) (this is called Extended Euclid’s algorithm) Example: a=3, b=5, gcd(a,b)=1 3*(-3)+5*2 = 1 (solution: x=-3, y=2) The proof will use our analysis of Euclid’s algorithm So, even though the theorem has nothing to do with algorithms, the proof will use an algorithm!

Extended Euclid’s algorithm Theorem: ∀𝑎≥𝑏≥1 ∃𝑥,𝑦∈𝑍 𝑎𝑥+𝑏𝑦=gcd⁡(𝑎,𝑏) Proof (by strong induction on b): Base case: b=1, so gcd(a,1)=a, can take x=1,y=1 Inductive case: We use the identity: gcd 𝑎,𝑏 =gcd⁡(𝑏,𝑎 𝑚𝑜𝑑 𝑏). Since 𝑏>𝑎 𝑚𝑜𝑑 𝑏, by the inductive assumption ∃ 𝑥 ′ , 𝑦 ′ ∈𝑍, 𝑏 𝑥 ′ + 𝑎 𝑚𝑜𝑑 𝑏 𝑦 ′ =gcd b,a mod b = gcd 𝑎,𝑏 Let 𝑎 = 𝑏𝑝+𝑞, where 𝑝=𝑎 𝑑𝑖𝑣 𝑏, 𝑞 = 𝑎 𝑚𝑜𝑑 𝑏. Then: 𝑏𝑥′+ 𝑎−𝑏𝑝 𝑦′= gcd 𝑎,𝑏 Take 𝑥= 𝑦 ′ ,𝑦= 𝑥 ′ −𝑝𝑦′ so that 𝑎𝑥+𝑏𝑦= gcd 𝑎,𝑏 .