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

Slides:



Advertisements
Similar presentations
PPL Lecture 3 Slides by Dr. Daniel Deutch, based on lecture notes by Prof. Mira Balaban.
Advertisements

מבוא מורחב 1 Lecture 4 Material in the textbook on Pages 44-46, of 2nd Edition Sections and Hanoy towers.
מבוא מורחב למדעי המחשב בשפת Scheme תרגול 4. Outline Repeated f Accelerating computations – Fibonacci Let and let* Recursion examples – Palindrome? – Log.
CSE332: Data Abstractions Lecture 2: Math Review; Algorithm Analysis Tyler Robison Summer
CS 206 Introduction to Computer Science II 09 / 10 / 2008 Instructor: Michael Eckmann.
6.001 SICP – September Introduction
מבוא מורחב למדעי המחשב בשפת Scheme תרגול 2. Reminder: Recursive algorithm Reduce problem to one or more sub-problems of smaller sizes (linear or tree.
1 Gentle Introduction to Programming Session 4: Arrays, Sorting, Efficiency.
מבוא מורחב - שיעור 4 1 Lecture 4 Order of Growth Fun with recursion  Fast exponentiation  Hanoi towers.
מבוא מורחב למדעי המחשב בשפת Scheme תרגול 4. Outline High order procedures –Finding Roots –Compose Functions Accelerating Computations –Fibonacci 2.
© 2006 Pearson Addison-Wesley. All rights reserved6-1 More on Recursion.
מבוא מורחב 1 Lecture 3 Material in the textbook Sections to
מבוא מורחב - שיעור 2 1 Lecture 2 - Substitution Model (continued) - Recursion - Block structure and scope (if time permits)
מבוא מורחב למדעי המחשב בשפת Scheme תרגול 3. 2 Outline Let High order procedures.
CS 206 Introduction to Computer Science II 09 / 05 / 2008 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 01 / 28 / 2009 Instructor: Michael Eckmann.
מבוא מורחב למדעי המחשב בשפת Scheme תרגול 4. Outline High order procedures –Finding Roots –Compose Functions Accelerating Computations –Fibonacci 2.
1 Gentle Introduction to Programming Session 4: Arrays.
Time Complexity Dr. Jicheng Fu Department of Computer Science University of Central Oklahoma.
SEARCHING, SORTING, AND ASYMPTOTIC COMPLEXITY Lecture 12 CS2110 – Fall 2009.
1 Recursion Algorithm Analysis Standard Algorithms Chapter 7.
Iterative Algorithm Analysis & Asymptotic Notations
מבוא מורחב 1 Your turn (Review) What does a lambda expression return when it is evaluated? the value of a lambda expression is a procedure What three things.
Analysis of Algorithms
1 7.Algorithm Efficiency What to measure? Space utilization: amount of memory required  Time efficiency: amount of time required to process the data Depends.
מבוא מורחב 1 Review: scheme language things that make up scheme programs: self-evaluating 23, "hello", #t names +, pi combinations (+ 2 3) (* pi 4) special.
1 7.Algorithm Efficiency What to measure? Space utilization: amount of memory required  Time efficiency: amount of time required to process the data.
Fundamentals CSE 373 Data Structures Lecture 5. 12/26/03Fundamentals - Lecture 52 Mathematical Background Today, we will review: ›Logs and exponents ›Series.
Program Efficiency & Complexity Analysis. Algorithm Review An algorithm is a definite procedure for solving a problem in finite number of steps Algorithm.
Today’s topics Orders of growth of processes Relating types of procedures to different orders of growth.
Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Modified for use by MSU Dept. of Computer Science.
Algorithm Analysis CS 400/600 – Data Structures. Algorithm Analysis2 Abstract Data Types Abstract Data Type (ADT): a definition for a data type solely.
Principles Of Programming Languages Lecture 2 Today Design-By-Contract Iteration vs. Recursion.
Fall 2008Programming Development Techniques 1 Topic 4 Orders of Growth September 2008.
Principles Of Programming Languages Lecture 2 Outline Design-By-Contract Iteration vs. Recursion Scope and binding High-order procedures.
3.3 Complexity of Algorithms
Principles Of Programming Languages Lecture 2 Today Design-By-Contract Iteration vs. Recursion If we have time: live demo!!!
CS 206 Introduction to Computer Science II 09 / 18 / 2009 Instructor: Michael Eckmann.
CS220 Programming Principles 프로그래밍의 이해 2003 가을학기 Class 2 한 태숙.
CS 206 Introduction to Computer Science II 01 / 30 / 2009 Instructor: Michael Eckmann.
1/32 This Lecture Substitution model An example using the substitution model Designing recursive procedures Designing iterative procedures Proving that.
A Introduction to Computing II Lecture 5: Complexity of Algorithms Fall Session 2000.
מבוא מורחב 1 Lecture 4 Material in the textbook on Pages 44-46, of 2nd Edition Sections and Hanoy towers.
מבוא מורחב למדעי המחשב בשפת Scheme תרגול 3. Outline High order procedures –Finding Roots –Compose Functions Accelerating Computations –Fibonacci 2.
1 Binding names קשירת שמות A occurrence of a name z is bound by the innermost procedure that contains the name and either 1. z is a formal parameter of.
Analyzing Programs: Order of Growth CMSC Introduction to Computer Programming October 11, 2002.
Principles Of Programming Languages Lecture 2 Today Design-By-Contract Data Structures: Lists and Pairs Iteration vs. Recursion If we have time: live.
Higher-Order Programming: Iterative computation (CTM Section 3
Lecture #5 מבוא מורחב.
Analysis of Algorithms
Material in the textbook on pages
CS21b: Structure and Interpretation
Your turn (Review) What does a lambda expression return when it is evaluated? the value of a lambda expression is a procedure What three things are in.
Lecture #5 מבוא מורחב.
Module #7: Algorithmic Complexity
Chapter 14 Time Complexity.
Binding names קשירת שמות
This Lecture Substitution model
Material in the textbook on
Chapter 2.
CSE 373 Data Structures Lecture 5
Searching, Sorting, and Asymptotic Complexity
Lecture 13 - Assignment and the environments model Chapter 3
Material in the textbook Sections to 1.2.1
Module #7: Algorithmic Complexity
Lecture 2 מבוא מורחב.
This Lecture Substitution model
At the end of this session, learner will be able to:
This Lecture Substitution model
Lecture 2 מבוא מורחב.
Presentation transcript:

מבוא מורחב 1 Lecture 3 Material in the textbook on Pages of 2nd Edition Sections 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 ) (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 ) (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 ) (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=4 12

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

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

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

מבוא מורחב 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 ) (exp-iter ) 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 ) (exp-iter ) 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 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)))))))

מבוא מורחב 16 The conditional form (cond ( ) ( ) …. ( ) (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 * (exp-fast ) 6561 * (exp-fast ) 6561 * * (exp-fast ) 6561 * * (exp-fast ) 6561 * * * (exp-fast.. 0) 6561 * * (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)+2 and 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 = …. b = … <= a,b <= TimeSpace exp-R (recursive)  b  exp-I (iterative)  b  1  exp-fast  log  b 

מבוא מורחב 20 Is exp-R feasible? exp-R takes  b  space. We need at least storage bits. That’s about 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 operations. We can run 1 billion (10 9 ) operations a second. We need about seconds. That’s about years. That’s about 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 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 seconds. That’s about years. That’s about 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): 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

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

מבוא מורחב 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   (n) n  O(n 2 ) 2 n   (n) 2 10   (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 ) <= 2 b 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 ) (exp-iter ) 81 Space  1  Time  b  Linear Iterative Process

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

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