מבוא מורחב 1 Lecture 4 Material in the textbook on Pages 44-46, 50-53 of 2nd Edition...

33
בבבב בבבבב1 Lecture 4 Material in the textbook on Pages 44-46, 50-53 of 2nd Edition Sections 1.2.4 and 1.2.6 + Hanoy towers

Transcript of מבוא מורחב 1 Lecture 4 Material in the textbook on Pages 44-46, 50-53 of 2nd Edition...

מבוא מורחב1

Lecture 4

Material in the textbook on

Pages 44-46, 50-53 of 2nd Edition

Sections 1.2.4 and 1.2.6

+ Hanoy towers

מבוא מורחב2

Review: Recursive Process(define (exp-R a b) ; computes ab

(if (= b 0) 1 (* a (exp-R a (- b 1))))) ; ab = a * ab-1

• Space b <= R(b) <= b which is b • Time b <= R(b) <= 2b which is b

(exp-R 3 4)(* 3 (exp-R 3 3))(* 3 (* 3 (exp-R 3 2)))(* 3 (* 3 (* 3 (exp-R 3 1))))(* 3 (* 3 (* 3 (* 3 (exp-R 3 0)))))(* 3 (* 3 (* 3 (* 3 1))))(* 3 (* 3 (* 3 3)))(* 3 (* 3 9))(* 3 27) 81

Linear Recursive Process

מבוא מורחב3

Review – Iterative process (define (exp-iter a b product) (if (= b 0) product (exp-iter a (- b 1) (* a product))))

(define (exp-I a b) (exp-iter a b 1))

• Space 1 • Time b

Linear Iterative Process

(exp-I 3 4)(exp-iter 3 4 1)(exp-iter 3 3 3)(exp-iter 3 2 9)(exp-iter 3 1 27)(exp-iter 3 0 81)81

מבוא מורחב4

Another algorithm for computing ab

• If b is even, then ab = (a2)(b/2)

• If b is odd, then ab = a*a(b-1)

Note that here, we reduce the problem in half in one step.

(define (exp-fast a b) ; computes ab

(cond ((= b 0) 1) ((even? b) (exp-fast (* a a) (/ b 2))) (else (* a (exp-fast a (- b 1)))))))

מבוא מורחב5

Cond special form

(cond (<test-1> <consequent-1>)

(<test-2> <consequent-2>)

….

(<test-n> <consequent-n>)

(else <consequent-else>))

(define (abs x) (cond ((> x 0) x)

((= x 0) 0)

((< x 0) (- 0 x))))(else (- 0 x))))

מבוא מורחב6

(exp-fast 3 56)

(exp-fast 3 56) ; compute 3^56(exp-fast 9 28)(exp-fast 81 14)(exp-fast 6561 7)6561 * (exp-fast 6561 6)6561 * (exp-fast 43046721 3)6561 * 43046721 * (exp-fast 43046721 2)6561 * 43046721 * (exp-fast 1853020188851841 1)6561 * 43046721 * 1853020188851841 * (exp-fast .. 0)6561 * 43046721 * 1853020188851841523347633027360537213511521

(define (exp-fast a b)

(cond ((= b 0) 1) ((even? b) (exp-fast (* a a) (/ b 2))) (else (* a (exp-fast a (- b 1)))))))

מבוא מורחב7

How much time does exp-fast take?

The analysis is tight. The order of growth in time and space is (log b) --

logarithmic.

Denote T(b) the number of arithmetic operations it takes to compute (exp-fast a b).

T(b) <= T(b/2)+O(1)T(1) = O(1)

Conclusion: T(b)=O(log b)

If b is even: T(b) = T(b/2)+2and if b is odd then: T(b) = T((b-1)/2)+3

מבוא מורחב8

Comparing the three exponentiation procedures

Assume a,b are integers, written in binary with 400 digits.

a = 100101010101010111110100110101….b = 101001010101011000101001010101….

2400 <= a,b <= 2401

Time Space

exp-R (recursive) b bexp-I (iterative) b 1exp-fast logb logb

מבוא מורחב9

Is exp-R feasible?

exp-R takes bspace.We need at least 2400 storage bits.

That’s about 2370 giga bits.

Each gigabit costs a dollar…Never mind. Let’s go to the dealer

Absolutely infeasible !!!!

Sorry, that’s more the number of particles in the universe…..

מבוא מורחב10

Is exp-I feasible?

exp-I takes at least 2400 operations.We can run 1 billion (109 ) operations a second.

We need about 2370 seconds.That’s about 2343 years.That’s about 2340 millenniums.

Might be longer then the universe age….Might be longer than the time our plant will last….

Infeasible !!!!

מבוא מורחב11

Let’s buy a faster computerand make exp-I feasible.

Our new computer can run giga billion (1018 ) operations a second. Absolutely the last word in the field of computing.

We need about 2340 seconds.That’s about 2313 years.That’s about 2310 millenniums.

Does not help much.Infeasible !!!!

מבוא מורחב12

Exp-fast is feasible.

We use a first generation pc, manufactured at 1977 and executing one operation a second.

We need about 1200 operations.

That’s about 20 minutes.

We need 1200 storage bits.

Feasible !!!!

מבוא מורחב13

Let’s buy a faster computer..

We use a second generation pc, manufactured at 1987 and executing one million operations a second.

We need about 1200 operations.

That’s so much less than a second that we do not bother counting it.

We still need 1200 storage bits.

Very feasible !!!!

מבוא מורחב14

Moral

If you wish to see the result

Of your programming efforts

Better think ahead

What your algorithm

Is

מבוא מורחב15

Primality Testing

(define (prime? n)

(= n (find-smallest-divisor n 2)))

(define (divides? a b)

(= (remainder b a) 0))

(define (find-smallest-divisor n i)

; find smallest number that divides n and is

; at least I. (cond ((divides? i n) i) (else (find-smallest-divisor n (+ i 1)))))

n is a prime iff its only divisors are 1 and n

מבוא מורחב16

(Prime? 7)

(= 7 (find-smallest-divisor 7 2)) (= 7 (cond (divides? 2 7) 2) (else (find-smallest-divisor 7 3))))

(= 7 (find-smallest-divisor 7 3))

(= 7 (find-smallest-divisor 7 4))

(= 7 (find-smallest-divisor 7 5))

(= 7 (find-smallest-divisor 7 6))

(= 7 (find-smallest-divisor 7 7))

(= 7 7)#t

מבוא מורחב17

Primality Testing - II

(define (prime? n)

(= n (find-smallest-divisor n 2)))

(define (divides? a b)

(= (remainder b a) 0))

(define (find-smallest-divisor n i) (cond ((> i (sqrt n)) n) ((divides? i n) i) (else (find-smallest-divisor n (+ i 1)))))

n is a prime iff its only divisors are 1 and nIff it has no divisors between 2 and (sqrt n)

מבוא מורחב18

Analysis

• Correctness: If n is not a prime, then n=a * b for a,b>1.

Then at least one of them is n. So n must have a divisor smaller then n.

• Time complexity:

first test - (n)

second test -

(n) . For a number n, we test at most n numbers to see if they divide n.

If n is a 800 digit number, that’s very bad.Absolutely infeasible.

מבוא מורחב19

The Fermat Primality Test

Fermat’s little theorem:

If n is a prime number then:

an = a (mod n) for every 0 < a < n, integer

The Fermat Test:

Do 400 times:

Pick a random a < n and compute an (mod n)

If a then for sure n is not a prime.

If all 400 tests passed, declare that n is a prime.

20

Computing ab (mod m) fast.

(define (expmod a b m) ; computes ab (mod m)

(cond

((= b 0) 1)

((even? b)

(remainder (expmod

(remainder (* a a) m)

(/ b 2)

m) m))

(else

(remainder (* a (expmod a (- b 1) m))

m))))

מבוא מורחב21

Implementing Fermat test

(define (test a n)(= (expmod a n n) a))

(define (one-test n) (test (+ 1 (random (- n 1))) n))

(define (many-tests n t); calls one-test t times (cond ((= t 0) true) ((one-test n) (many-test n (- t 1))) (else false)))

מבוא מורחב22

Time complexity

To test if n is a prime. We run 400 tests.Each takes about log(n) multiplcations.

T(n) = O(log n)

מבוא מורחב23

Correctness – I (prime numbers)

Fermat’s theorem: Every prime will always pass the test.

It therefore follows that if n is a prime then for everya, test(a n) is true, hence we always pass the test, And we declare n to be a prime.

For a prime n: We are always right.

מבוא מורחב24

Correctness II – Carmichael numbers.

If n is a Carmichael number we always pass the test, hence we always declare that n is prime.

Definition: A Carmichael number, is a number such that •n is Composite, and•n always passes the test. For every a, an = a (mod n)

For a Carmichael number n: We are always wrong.

מבוא מורחב25

Correctness III – any other number

A fact: If n is not prime and not a Carmichael number then:

for at least half of the choices of a,

an <> a (mod n).

Hence, if we chose a at random, then with probability half the test fails and we declare that n is composite.

The probability all 100 tests fail is at most 2-400

For such n: We are wrong with probability at most 2-400

מבוא מורחב26

Correctness

• If n is a prime we are never wrong.

• If n is a composite number and not a Carmichael number we are wrong with probability at most 2-400 .

Error probability smaller than the chance the hardware is faulty.

Suppose we do the test t=400 times.

• If n is a Carmichael number, we are always wrong

מבוא מורחב27

A probabilistic algorithm

An algorithm that uses random coins,and for every input gives the right answer with a good probability.

Even though Carmichael numbers are very rareFermat test is not good enough.

There are inputs on which it is wrong.

There are modifications of Fermat’s test, that for every input give the right answer, with a high probability.

מבוא מורחב28

Towers of Hanoi

• Three posts, and a set of different size disks• A disk can be only on a larger size disk.• At the beginning all the disks are on the left post.

The goal is to move the disks one at a time, while preserving these conditions, until the entire stack has moved from one post to another

מבוא מורחב29

Use our paradigm

• Wishful thinking:

• Smaller problem: A problem with one disk less

• How do we use it ?

Move n-1 disks from peg A to peg B

Move the largest from peg A to peg C

Move n-1 disks from peg B to peg C

We solve 2 smaller problems !

מבוא מורחב30

Towers of Hanoi

(define (one-move from to) (display "Move top disk from ") (display from) (display " To ") (display to) (newline))

(define (move-tower size from to aux) (cond ((= size 1) (one-move from to)) (else (move-tower (- size 1) from aux to) (one-move from to) (move-tower (- size 1) aux to from))))

מבוא מורחב31

Towers of Hanoi -- trace

(move-tower 3 2 1 3)

Move top disk from 2 to 1

Move top disk from 2 to 3

Move top disk from 1 to 3

Move top disk from 2 to 1

Move top disk from 3 to 2

Move top disk from 3 to 1

Move top disk from 2 to 1

2 1 32 1 32 1 32 1 32 1 32 1 32 1 32 1 3

(move-tower 2 2 3 1)

(move-tower 2 3 1 2)

מבוא מורחב32

Tree Recursion (mt 3 2 1 3)

(mt 2 2 3 1)

(mt 1 3 2 1)

(move-one 2 1)

(move-one 2 1) (mt 2 3 1 2)

(move-one 3 1)(mt 1 1 3 2)(move-one 2 3)(mt 1 2 1 3) (mt 1 2 1 3)

(move-one 1 3)(move-one 3 2)(move-one 2 1)

מבוא מורחב33

Orders of growth for towers of HanoiDenote by T(n) be the number of steps that we need to take to solve the case for n disks.

T(n) = 2T(n-1) + 1T(1) = 1

This solves to: T(n) = 2n - 1 exponential

For the space complexity we have S(n) = S(n-1) + O(1) S(n) = O(n)