CSE 20 DISCRETE MATH Prof Shachar Lovett http

  • Slides: 26
Download presentation
CSE 20 DISCRETE MATH Prof. Shachar Lovett http: //cseweb. ucsd. edu/classes/wi 15/cse 20 -a/

CSE 20 DISCRETE MATH Prof. Shachar Lovett http: //cseweb. ucsd. edu/classes/wi 15/cse 20 -a/ Clicker frequency: CA

Todays topics • Proofs for algorithms: • Euclid’s algorithm • Section 3. 7 in

Todays topics • Proofs for algorithms: • Euclid’s algorithm • Section 3. 7 in Jenkyns, Stephenson

GCD • GCD = Greatest Common Divisor • GCD(a, b): largest n such that

GCD • GCD = Greatest Common Divisor • GCD(a, b): largest n such that n|a and n|b • One way to compute it: • Factor a, b to prime factors • n = product of common prime factors • No efficient way to do so (since we don’t know how to factor to prime numbers efficiently) • Euclid’s algorithm provides a much faster way

Euclid’s algorithm •

Euclid’s algorithm •

Euclid’s algorithm • (a, b) (b, a mod b)

Euclid’s algorithm • (a, b) (b, a mod b)

Euclid’s algorithm •

Euclid’s algorithm •

Euclid’s algorithm • Example run: a=20, b=30 a 20 30 20 10 b 30

Euclid’s algorithm • Example run: a=20, b=30 a 20 30 20 10 b 30 20 10 0

Euclid’s algorithm • The same basic questions 1. Does it always terminate? 2. Does

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

Euclid’s algorithm: termination •

Euclid’s algorithm: termination •

Euclid’s algorithm: termination •

Euclid’s algorithm: termination •

Euclid’s algorithm: termination • The value of a keeps decreasing, which proves termination

Euclid’s algorithm: termination • The value of a keeps decreasing, which proves termination

Euclid’s algorithm: correctness • g=gcd(a, b)

Euclid’s algorithm: correctness • g=gcd(a, b)

Euclid’s algorithm: correctness •

Euclid’s algorithm: correctness •

Euclid’s algorithm: correctness •

Euclid’s algorithm: correctness •

Euclid’s algorithm: correctness •

Euclid’s algorithm: correctness •

Euclid’s algorithm: correctness •

Euclid’s algorithm: correctness •

Euclid’s algorithm: correctness • g=gcd(a, b) Proved!

Euclid’s algorithm: correctness • g=gcd(a, b) Proved!

Euclid’s algorithm: speed • How many iterations?

Euclid’s algorithm: speed • How many iterations?

Euclid’s algorithm: speed •

Euclid’s algorithm: speed •

Euclid’s algorithm: speed •

Euclid’s algorithm: speed •

Euclid’s algorithm: speed •

Euclid’s algorithm: speed •

Euclid’s algorithm: speed •

Euclid’s algorithm: speed •

Extended Euclid’s algorithm •

Extended Euclid’s algorithm •

Extended Euclid’s algorithm •

Extended Euclid’s algorithm •

Extended Euclid’s algorithm •

Extended Euclid’s algorithm •

Next class • Relations • Read section 6. 1 in Jenkyns, Stephenson

Next class • Relations • Read section 6. 1 in Jenkyns, Stephenson