Binding names קשירת שמות

Slides:



Advertisements
Similar presentations
1 Recursive Algorithm Analysis Dr. Ying Lu RAIK 283: Data Structures & Algorithms September 13, 2012.
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.
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.
מבוא מורחב למדעי המחשב בשפת 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.
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
מבוא מורחב - שיעור 2 1 Lecture 2 - Substitution Model (continued) - Recursion - Block structure and scope (if time permits)
מבוא מורחב למדעי המחשב בשפת Scheme תרגול 3. 2 Outline Let High order procedures.
CS 106 Introduction to Computer Science I 03 / 28 / 2008 Instructor: Michael Eckmann.
Recursion A recursive function is a function that calls itself either directly or indirectly through another function. The problems that can be solved.
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.
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.
מבוא מורחב 1 Review: scheme language things that make up scheme programs: self-evaluating 23, "hello", #t names +, pi combinations (+ 2 3) (* pi 4) special.
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.
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.
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.
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.
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,
Recursion To understand recursion, you first have to understand recursion.
CS314 – Section 5 Recitation 9
Algorithm Analysis 1.
Theoretical analysis of time efficiency
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
Decrease-and-Conquer Approach
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.
This Lecture Substitution model
Material in the textbook on
Fundamentals of the Analysis of Algorithm Efficiency
CS 2210 Discrete Structures Advanced Counting
Lecture 13 - Assignment and the environments model Chapter 3
Material in the textbook Sections to 1.2.1
This Lecture Substitution model
At the end of this session, learner will be able to:
This Lecture Substitution model
Fundamentals of the Analysis of Algorithm Efficiency
Time Complexity and the divide and conquer strategy
Presentation transcript:

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

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

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

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

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)  Q(f(n)) if R(n)  O(f(n)) and R(n)  (f(n)) 100n3 + 10n  O(n3) 100n3 + 10n   (n3) 100n3 + 10n  Q (n3) 10*log(n) + loglog(n)  Q ( ) log(n) מבוא מורחב

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

Examples of orders of growth FACT Space S(n)  Q(n) – linear Time T(n)  Q(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. מבוא מורחב

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 24 5 4) (if (> 5 4) 24 (ifact-helper (* 24 5) (+ 5 1) 4)) 24

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

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

The else clause (cond (<test-1> <consequent-1>) …. (<else> <consequent-n>)) (define (abs x) (cond ((> x 0) x) ((= x 0) 0) (else (- x)))) מבוא מורחב

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

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

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

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

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 3 to 2 Move top disk from 3 to 1 2 1 3 2 1 3 2 1 3 2 1 3 2 1 3 2 1 3 מבוא מורחב

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

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 = 2n -1 T(1) = 1  So in time we have Q(2n) -- exponential space -- Q(n) -- linear We have at most two pending subproblem of any size at any given time. מבוא מורחב

Another kind of process Let’s compute ab just using multiplication and addition 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 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))))))) מבוא מורחב

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/2k sized problem We are done when the problem size is just 1, which implies order of growth in time of Q(log b) -- logarithmic Space is similarly Q(log b) -- logarithmic מבוא מורחב

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