Presentation is loading. Please wait.

Presentation is loading. Please wait.

מבוא מורחב 1 Lecture 3 Material in the textbook on Pages 32-46 of 2nd Edition Sections 1.2.1 to 1.2.4.

Similar presentations


Presentation on theme: "מבוא מורחב 1 Lecture 3 Material in the textbook on Pages 32-46 of 2nd Edition Sections 1.2.1 to 1.2.4."— Presentation transcript:

1

2 מבוא מורחב 1 Lecture 3 Material in the textbook on Pages 32-46 of 2nd Edition Sections 1.2.1 to 1.2.4

3 מבוא מורחב 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))

4 מבוא מורחב 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.

5 מבוא מורחב 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))

6 מבוא מורחב 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))

7 מבוא מורחב 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=4 12

8 מבוא מורחב 7 a b (Recursive Approach) wishful thinking : base case: a b = a * a (b-1) a 0 = 1 (define exp-1 (lambda (a b) (if (= b 0) 1 (* a (exp-1 a (- b 1))))))

9 מבוא מורחב 8 a b (Iterative Approach) Another approach: Operationally: Halting condition: result  result * a counter  counter - 1 counter = 0 a b = a 2 *a*…*a= a 3 *…*a Which is: a b = a * a * a*…*a b

10 מבוא מורחב 9 a b (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.

11 מבוא מורחב 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

12 מבוא מורחב 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

13 מבוא מורחב 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

14 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

15 מבוא מורחב 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?

16 מבוא מורחב 15 Another algorithm for computing a b If b is even, then a b = (a 2 ) (b/2) If b is odd, then a b = a*a (b-1) Note that here, we reduce the problem in half in one step. (define (exp-fast a b) ; computes a b (cond ((= b 0) 1) ((even? b) (exp-fast (* a a) (/ b 2))) (else (* a (exp-fast a (- b 1)))))))

17 מבוא מורחב 16 The conditional form (cond ( ) ( ) …. ( ) (else )) (define (abs x) (cond ((> x 0) x) ((= x 0) 0) ((< x 0) (- x)))) (else (- x))))

18 מבוא מורחב 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 * 1853020188851841 523347633027360537213511521 (define (exp-fast a b) (cond ((= b 0) 1) ((even? b) (exp-fast (* a a) (/ b 2))) (else (* a (exp-fast a (- b 1)))))))

19 מבוא מורחב 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)+2 and if b is odd then: T(b) = T((b-1)/2)+3

20 מבוא מורחב 19 Comparing the three exponentiation procedures Assume a,b are integers, written in binary with 400 digits. a = 100101010101010111110100110101…. b = 101001010101011000101001010101…. 2 400 <= a,b <= 2 401 TimeSpace exp-R (recursive)  b  exp-I (iterative)  b  1  exp-fast  log  b 

21 מבוא מורחב 20 Is exp-R feasible? exp-R takes  b  space. We need at least 2 400 storage bits. That’s about 2 370 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…..

22 מבוא מורחב 21 Is exp-I feasible? exp-I takes at least 2 400 operations. We can run 1 billion (10 9 ) operations a second. We need about 2 370 seconds. That’s about 2 343 years. That’s about 2 340 millenniums. Might be longer then the universe age…. Might be longer than the time our plant will last…. Infeasible !!!!

23 מבוא מורחב 22 Let ’ s buy a faster computer and make exp-I feasible. Our new computer can run giga billion (10 18 ) operations a second. Absolutely the last word in the field of computing. We need about 2 340 seconds. That’s about 2 313 years. That’s about 2 310 millenniums. Does not help much. Infeasible !!!!

24 מבוא מורחב 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 !!!!

25 מבוא מורחב 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 !!!!

26 מבוא מורחב 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.

27 מבוא מורחב 26 Orders of Growth Want to estimate the “order of growth” of R(n): R 1 (n)=100n 2 R 2 (n)=2n 2 +10n+2 R 3 (n) = n 2 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 n 2

28 מבוא מורחב 27 Orders of Growth We say R(n) has order of growth  (f(n))  if there are constants c 1  0 and c 2  0 such that for all n > c 0 c 1 f(n) <= R(n) <= c 2 f(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)

29 מבוא מורחב 28 Orders of Growth t t f f 100n 2  O(n) 100n 2   (n) 100n 2   (n) 100n 2   (n 2 ) True or False? t t f f 2 100   (n) 2 100 n  O(n 2 ) 2 n   (n) 2 10   (1) True or False?

30 מבוא מורחב 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 ) <= 2 b which is  b  Linear Recursive Process

31 מבוא מורחב 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

32 מבוא מורחב 31 Example for Recursion: Factorial wishful thinking : base case: n! = n * (n-1)! n = 1 (define fact (lambda (n) (if (= n 1) 1 (* n (fact (- n 1))))))

33 מבוא מורחב 32 Example: Factorial ==>(fact 3) ([[n](if(= n 1) 1 (* n (fact (- n 1))))] 3) (if (= 3 1) 1 (* 3 (fact (- 3 1)))) (if #f 1 (* 3 (fact (- 3 1)))) (* 3 (fact (- 3 1))) (* 3 ([[n](if(= n 1) 1 (* n (fact (- n 1))))] (- 3 1))) (* 3 (if (= 2 1) 1 (* 2 (fact (- 2 1))))) (* 3 (if #f 1 (* 2 (fact (- 2 1))))) (* 3 (* 2 (fact (-2 1)))) (* 3 (* 2 (([[n](if(= n 1) 1 (* n (fact (- n 1))))] (- 2 1)))) (* 3 (* 2 (if (= 1 1) 1 (* (fact (- 1 1))))))) (* 3 (* 2 (if #t 1 (* 1 (fact (- 1 1)))))) (* 3 (* 2 1)) (* 3 2) 6


Download ppt "מבוא מורחב 1 Lecture 3 Material in the textbook on Pages 32-46 of 2nd Edition Sections 1.2.1 to 1.2.4."

Similar presentations


Ads by Google