מבוא מורחב 1 lecture 3 material in the textbook on pages 32-46 of 2nd edition sections...

30
בבבב בבבבב1 Lecture 3 Material in the textbook on Pages 32-46 of 2nd Edition Sections 1.2.1 to 1.2.4

Post on 22-Dec-2015

222 views

Category:

Documents


1 download

TRANSCRIPT

מבוא מורחב1

Lecture 3

Material in the textbook

on Pages 32-46 of 2nd Edition

Sections 1.2.1 to 1.2.4

מבוא מורחב2

SQRT

(define (sqrt x)

(define (good-enough? guess x) (< (abs (- (square guess) x)) precision))(define (improve guess x) (average guess (/ x guess)))

(define (sqrt-iter guess x) (if (good-enough? guess x) guess (sqrt-iter (improve guess x) x)))

(define initial-guess 1.0)

(define precision 0.00001)(sqrt-iter initial-guess x))

מבוא מורחב3

Lexical Scoping

Every variable is:

• Recognized within the procedure where it is defined (its scope).• Not recognized outside it.

• Allows different procedures to use the same variable name. E.g., different procedures can use the variable name i as a counter. • A variable can be used globally within its scope. No need to pass it from one procedure to another.

מבוא מורחב4

SQRT again, x is used globally.(define (sqrt x)

(define (good-enough? guess) (< (abs (- (square guess) x)) precision))

(define (improve guess) (average guess (/ x guess)))

(define (sqrt-iter guess) (if (good-enough? guess) guess (sqrt-iter (improve guess)))) (define initial-guess 1.0) (define precision 0.00001)

(sqrt-iter initial-guess))

מבוא מורחב5

Variables scope(define (sqrt x)

(define (good-enough? guess) (< (abs (- (square guess) x)) precision))

(define (improve guess) (average guess (/ x guess)))

(define (sqrt-iter guess) (if (good-enough? guess) guess (sqrt-iter (improve guess)))) (define initial-guess 1.0) (define precision 0.00001)

(sqrt-iter initial-guess))

מבוא מורחב6

An example

(define (proc1 x) (define (proc2 y) (+ x y)) (define (proc3 x) (proc2 x)) (proc3 (* 2 x)))

Proc3.x

Proc1.x

(proc1 4) proc1.x = 4(proc3 8) proc3.x = 8(proc2 8) proc2.y = 8 proc2.x=proc1.x=412

מבוא מורחב7

ab (Recursive Approach)

• wishful thinking :

• base case:ab = a * a(b-1)

a0 = 1

(define exp-1 (lambda (a b)

(if (= b 0) 1 (* a (exp-1 a (- b 1))))))

מבוא מורחב8

ab (Iterative Approach)

• Another approach:

• Operationally:

• Halting condition:

result result * a

counter counter - 1

counter = 0

ab = a2 *a*…*a= a3 *…*a•Which is:

ab = a * a * a*…*a

b

מבוא מורחב9

ab (Iterative Approach)

(define (exp-2 a b)(define (exp-iter a b product)

(if (= b 0) product (exp-iter a (- b 1) (* a product)))) (exp-iter a b 1)

Syntactic Recursion

How then, do the two procedures differ?

They give rise to different processes – lets use our model to understand how.

מבוא מורחב10

Recursive Process(define exp-1

(lambda (a b) (if (= b 0) 1 (* a (exp-1 a (- b 1))))))

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

מבוא מורחב11

Iterative Process

(define (exp-2 a b)(define (exp-iter a b product)

(if (= b 0) product (exp-iter a (- b 1) (* a product)))) (exp-iter a b 1)

(exp-2 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

מבוא מורחב12

The Difference

(exp-2 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

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

81 Growing amount of space

Constant amount of space

13

Why More Space?

• Recursive exponentiation:(define exp-1 (lambda (a b)

(if (= b 0) 1 (* a (exp-1 a (- b 1)))))

operation pending

•Iterative exponentiation: (define (exp-2 a b)

(define (exp-iter a b product) (if (= b 0) product (exp-iter a (- b 1) (* a product)))) (exp-iter a b 1))

no pending operations

מבוא מורחב14

Summary

• Recursive process num of deferred operations “grows proportional to b”

• Iterative process num of deferred operations stays “constant” (actually its zero)

Can we better quantify these observations?

מבוא מורחב15

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)))))))

מבוא מורחב16

The conditional 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) (- x))))(else (- x))))

מבוא מורחב17

(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)))))))

מבוא מורחב18

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

מבוא מורחב19

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

מבוא מורחב20

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…..

מבוא מורחב21

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 !!!!

מבוא מורחב22

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 !!!!

מבוא מורחב23

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 !!!!

מבוא מורחב24

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 !!!!

מבוא מורחב25

Orders of Growth

• Suppose n is a parameter that measures the size of a problem (the size of its input)

•R(n)measures the amount of resources needed to compute a solution procedure of size n.

• Two common resources are space, measured by the number of deferred operations, and time, measured by the number of primitive steps.

מבוא מורחב26

Orders of Growth• Want to estimate the “order of growth” of R(n):

R1(n)=100n2

R2(n)=2n2+10n+2

R3(n) = n2

Are all the same in the sense that if we multiply the input by a factor of 2, the resource consumption increase by a factor of 4

Order of growth is proportional to n2

מבוא מורחב27

Orders of Growth• We say R(n)has order of growth (f(n))if

there are constants c1 0 and c2 0 such that for all n>c0

c1f(n)<= R(n)<= c2f(n)

•R(n)(f(n)) if there is a constant c 0 such that for all n

c f(n) <= R(n)

•R(n)O(f(n)) if there is a constant c 0 such that for all n

R(n) <= c f(n)

מבוא מורחב28

Orders of Growth

t

t

f

f100n2 O(n)

100n2 (n)

100n2 (n)

100n2 (n2)

True or False?

t

t

f

f2100 (n)

2100n O(n2)

2n (n)

210 (1)

True or False?

מבוא מורחב29

Resources Consumed by EXP-1

(exp-1 3 4) “n”=b=4(* 3 (exp-1 3 3))(* 3 (* 3 (exp-1 3 2)))(* 3 (* 3 (* 3 (exp-1 3 1))))(* 3 (* 3 (* 3 (* 3 (exp-1 3 0)))))(* 3 (* 3 (* 3 (* 3 1))))(* 3 (* 3 (* 3 3)))(* 3 (* 3 9))(* 3 27)81

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

Linear Recursive Process

מבוא מורחב30

Resources Consumed by EXP-2

(exp-2 3 4) “n”=b=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

• Space 1 • Time b

Linear Iterative Process