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.

Slides:



Advertisements
Similar presentations
3/25/2017 Chapter 16 Recursion.
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.
Recursion, Divide and Conquer Lam Chi Kit (George) HKOI2007.
Lecture 3: Algorithm Complexity. Recursion A subroutine which calls itself, with different parameters. Need to evaluate factorial(n) = n  factorial(n-1)
Fall 2008Programming Development Techniques 1 Topic 3 Linear Recursion and Iteration September 2008.
Fall 2006CENG 7071 Algorithm Analysis. Fall 2006CENG 7072 Algorithmic Performance There are two aspects of algorithmic performance: Time Instructions.
מבוא מורחב למדעי המחשב בשפת Scheme תרגול 2. 2 Outline Scoping and block structure Recursive and iterative processes Orders of growth.
Scott Grissom, copyright 2004 Chapter 5 Slide 1 Analysis of Algorithms (Ch 5) Chapter 5 focuses on: algorithm analysis searching algorithms sorting algorithms.
6.001 SICP – September Introduction
מבוא מורחב למדעי המחשב בשפת Scheme תרגול 2. Reminder: Recursive algorithm Reduce problem to one or more sub-problems of smaller sizes (linear or tree.
מבוא מורחב - שיעור 4 1 Lecture 4 Order of Growth Fun with recursion  Fast exponentiation  Hanoi towers.
1 CSCD 300 Data Structures Recursion. 2 Proof by Induction Introduction only - topic will be covered in detail in CS 320 Prove: N   i = N ( N + 1.
Recursion. 2 CMPS 12B, UC Santa Cruz Solving problems by recursion How can you solve a complex problem? Devise a complex solution Break the complex problem.
Chapter 10 Recursion. Copyright © 2005 Pearson Addison-Wesley. All rights reserved Chapter Objectives Explain the underlying concepts of recursion.
מבוא מורחב 1 Lecture 3 Material in the textbook Sections to
מבוא מורחב למדעי המחשב בשפת Scheme תרגול 3. 2 Outline Let High order procedures.
Analysis of Recursive Algorithms
Lecture 2: Algorithm Complexity. Recursion A subroutine which calls itself, with different parameters. Need to evaluate factorial(n) factorial(n) = n.
מבוא מורחב 1 Lecture 3 Material in the textbook on Pages of 2nd Edition Sections to
Lecture 3: Algorithms and Complexity Study Chapter COMP 555 Bioalgorithms Fall 2014.
Data Structures Mohamed Mustaq Ahmed Chapter 2- Algorithms.
A Review of Recursion Dr. Jicheng Fu Department of Computer Science University of Central Oklahoma.
Analysis of Algorithm Lecture 3 Recurrence, control structure and few examples (Part 1) Huma Ayub (Assistant Professor) Department of Software Engineering.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved ADT Implementation:
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved ADT Implementation:
CHAPTER 02 Recursion Compiled by: Dr. Mohammad Omar Alhawarat.
מבוא מורחב 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.
Chapter 8 Recursion Modified.
Chapter 4 Recursion. Copyright © 2004 Pearson Addison-Wesley. All rights reserved.1-2 Chapter Objectives Explain the underlying concepts of recursion.
Today’s topics Orders of growth of processes Relating types of procedures to different orders of growth.
Lecture 5 Jianjun Hu Department of Computer Science and Engineering University of South Carolina CSCE350 Algorithms and Data Structure.
Chapter 6.1: Recurrence Relations Discrete Mathematical Structures: Theory and Applications.
Principles Of Programming Languages Lecture 2 Today Design-By-Contract Iteration vs. Recursion.
Lecture - 8 On Stacks, Recursion. Prepared by, Jesmin Akhter, Lecturer, IIT,JU Lecture Outline Quick sort Algorithm Recursion –Calculate n factorial –Fibonacci.
Principles Of Programming Languages Lecture 2 Outline Design-By-Contract Iteration vs. Recursion Scope and binding High-order procedures.
Principles Of Programming Languages Lecture 2 Today Design-By-Contract Iteration vs. Recursion If we have time: live demo!!!
Algorithm Analysis Part of slides are borrowed from UST.
1 Recursion Recursion is a powerful programming technique that provides elegant solutions to certain problems. Chapter 11 focuses on explaining the underlying.
CS220 Programming Principles 프로그래밍의 이해 2003 가을학기 Class 2 한 태숙.
1/32 This Lecture Substitution model An example using the substitution model Designing recursive procedures Designing iterative procedures Proving that.
Recursion Chapter 17 Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013.
מבוא מורחב 1 Lecture 4 Material in the textbook on Pages 44-46, of 2nd Edition Sections and Hanoy towers.
Analyzing Programs: Order of Growth CMSC Introduction to Computer Programming October 11, 2002.
1 Recursion Recursive function: a function that calls itself (directly or indirectly). Recursion is often a good alternative to iteration (loops). Its.
chap10 Chapter 10 Recursion chap10 2 Recursive Function recursive function The recursive function is a kind of function that calls.
Principles Of Programming Languages Lecture 2 Today Design-By-Contract Data Structures: Lists and Pairs Iteration vs. Recursion If we have time: live.
1 Data Structures CSCI 132, Spring 2016 Notes 16 Tail Recursion.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Recursion,
Algorithm Analysis 1.
Chapter 10 Recursion Instructor: Yuksel / Demirer.
ADT Implementation: Recursion, Algorithm Analysis, and Standard Algorithms Chapter 10 Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second.
Abdulmotaleb El Saddik University of Ottawa
Analysis of Algorithms
Lecture 06: Linked Lists (2), Big O Notation
Recursive Thinking Chapter 9 introduces the technique of recursive programming. As you have seen, recursive programming involves spotting smaller occurrences.
Java Software Structures: John Lewis & Joseph Chase
CS21b: Structure and Interpretation
Recursive Thinking Chapter 9 introduces the technique of recursive programming. As you have seen, recursive programming involves spotting smaller occurrences.
Algorithm design and Analysis
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.
Binding names קשירת שמות
This Lecture Substitution model
Material in the textbook on
Fundamentals of the Analysis of Algorithm Efficiency
Material in the textbook Sections to 1.2.1
At the end of this session, learner will be able to:
This Lecture Substitution model
Fundamentals of the Analysis of Algorithm Efficiency
Presentation transcript:

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 F 2. z is a name defined internally in F (define (F x y z) (define (h y) (* y y) (+ (h x) (h y) (h z)))

מבוא מורחב 2 עידון מודל ההצבה When you apply F replace only occurences bound by F of each formal parameter. Do not replace occurences bound by an internal definition!

מבוא מורחב 3 (define (h x) (define (f y) (+ x y)) (define (g x) (f x)) (g (* 2 x))) => (h 1) (define (f y) (+ 1 y)) (define (g x) (f x)) (g (* 2 1)) (g 2) (f 2) (+ 1 2) 3

מבוא מורחב 4 Scheme uses Lexical Scoping ערך משתנה חופשי בפונקציה F נקבע על פי מקום הגדרת F. (ולא על פי מקום הפעלתה: dynamic binding) פונקציה המכילה את הגדרת F תקבע את ערכו.

מבוא מורחב 5 Orders of growth of processes R(n)  O(f(n)) if there is a constant k such that R(n) <= k f(n) for all n (positive integer) R(n)   (f(n)) if there is a constant k such that k f(n) <= R(n) for all n, k  0 R(n)   (f(n)) if R(n)  O(f(n)) and R(n)   (f(n)) 100n n  O(n 3 ) 100n n   (n 3 ) 100n n   (n 3 ) 10*log(n) + loglog(n)   ( ) log(n)

מבוא מורחב 6 Factorial (define fact (lambda (n) (if (= n 1) 1 (* n (fact (- n 1)))))) (fact 4) (if (= 4 1) 1 (* 4 (fact (- 4 1)))) (* 4 (fact 3)) (* 4 (if (= 3 1) 1 (* 3 (fact (- 3 1))))) (* 4 (* 3 (fact 2))) (* 4 (* 3 (if (= 2 1) 1 (* 2 (fact (- 2 1)))))) (* 4 (* 3 (* 2 (fact 1)))) (* 4 (* 3 (* 2 (if (= 1 1) 1 (* 1 (fact (- 1 1))))))) (* 4 (* 3 (* 2 1))) (* 4 (* 3 2)) (* 4 6) 24

מבוא מורחב 7 Examples of orders of growth FACT Space S(n)   (n) – linear Time T(n)   (n) – linear T(n) = The time it takes for fact to compute n! in general: The maximum time it takes to compute the result for an input of size n.

8 Iterative factorial (define (ifact n) (define ifact-helper (lambda (product count n) (if (> count n) product (ifact-helper (* product count) (+ count 1) n)))) (ifact-helper 1 1 n)) (ifact 4) (ifact-helper 1 1 4) (if (> 1 4) 1 (ifact-helper (* 1 1) (+ 1 1) 4)) (ifact-helper 1 2 4) (if (> 2 4) 1 (ifact-helper (* 1 2) (+ 2 1) 4)) (ifact-helper 2 3 4) (if (> 3 4) 2 (ifact-helper (* 2 3) (+ 3 1) 4)) (ifact-helper 6 4 4) (if (> 4 4) 6 (ifact-helper (* 6 4) (+ 4 1) 4)) (ifact-helper ) (if (> 5 4) 24 (ifact-helper (* 24 5) (+ 5 1) 4)) 24

מבוא מורחב 9 Examples of orders of growth FACT Space  (n) – linear Time  (n) – linear IFACT Space  (1) – constant Time  (n) – linear

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

מבוא מורחב 11 The else clause (cond ( ) ( ) …. ( )) (define (abs x) (cond ((> x 0) x) ((= x 0) 0) (else (- x))))

מבוא מורחב 12 Towers of Hanoi Three posts, and a set of different size disks any stack must be sorted in decreasing order from bottom to top 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

מבוא מורחב 13 Towers of Hanoi (problem definition) Start out with all disks on one peg from largest to smallest Find a sequence of instructions (compliant with the rules) to move all the disks from one peg to another using the third

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

מבוא מורחב 15 Towers of Hanoi (define move-tower (lambda (size from to extra) (cond (move-tower (- size 1) from extra to) (print-move from to) (move-tower (- size 1) extra to from))))) (define print-move (lambda (from to) (write-line ``Move top disk from ``) (write-line from) (write-line `` to ``) (write-line to))) ((= size 1) (print-move from to)) (else

מבוא מורחב 16 Towers of Hanoi -- trace (move-tower ) 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

מבוא מורחב 17 A tree recursion Move 4 Move 3 Move 2 Move 1

מבוא מורחב 18 Orders of growth for towers of Hanoi Let T(n) be the number of steps that we need to take to solve the case for n disks. Then T(n) = 2T(n-1) + 1 = 2(2T(n-2) +1) + 1 = 2 n -1 T(1) = 1 So in time we have  (2 n ) -- exponential space --  (n) -- linear We have at most two pending subproblem of any size at any given time.

מבוא מורחב 19 Another kind of process Let’s compute a b just using multiplication and addition 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 fast-exp-1 (lambda (a b) (cond ((= b 1) a) ((even? b) (fast-exp-1 (* a a) (/ b 2))) (else (* a (fast-exp-1 a (- b 1)))))))

מבוא מורחב 20 Orders of growth If b even, then 1 step reduces to b/2 sized problem If b odd, 2 steps reduces to b/2 sized problem Thus in 2k steps reduces to b/2 k sized problem We are done when the problem size is just 1, which implies order of growth in time of  (log b) -- logarithmic Space is similarly  (log b) -- logarithmic

מבוא מורחב 21 Procedures and their processes Have seen that procedures have different patterns that give rise to different kinds of process evolution Constant Linear Exponential Logarithmic Goal is to begin to learn to recognize these patterns and how to use them in capturing your own procedures