1 / 75

cs1321 Fall, AY2002 11/01/01 (that’s 35 16 ) Lecture 20

cs1321 Fall, AY2002 11/01/01 (that’s 35 16 ) Lecture 20. Agenda. 1. Tail Recursion 2. Graphs. Tail or Accumulator-Style Functions. Outline. Prerequisites Recursion Structures Graphs Objectives Need for Accumulation Accumulator Style Functions Transforming Recursive Functions

nmccullers
Download Presentation

cs1321 Fall, AY2002 11/01/01 (that’s 35 16 ) Lecture 20

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. cs1321 Fall, AY2002 11/01/01 (that’s 3516) Lecture 20

  2. Agenda 1. Tail Recursion 2. Graphs

  3. Tail or Accumulator-Style Functions

  4. Outline • Prerequisites • Recursion • Structures • Graphs • Objectives • Need for Accumulation • Accumulator Style Functions • Transforming Recursive Functions • Reference • HTDP Chapters 31, 32

  5. Background • Pure (augmenting or “head”) recursion follows the functional paradigm • The recursion you have seen so far • Does not permit memory • Forgets useful intermediate results • Unnecessarily repeats large computations • We now consider ways to improve the efficiency of recursion • Passing extra parameters to remember useful details • Basically collecting partial results as we go!! • Frequently referred to as “Tail Recursion”

  6. A Different View OK so we noted the cost of recursion can be large, if it’s not designed well. Each recursive call creates a new activation frame. This implied that recursive solutions could potentially take large amounts of memory.

  7. Augmentative Recursion Analysis: The problem was that in order to find what’s returned from one function call, we had to make a recursive call. This required us to save frames for each recursive call.

  8. Tail Recursion If we can “solve” the return result from each level, the computer will not have to save each activation frame. This approach is know as tail recursion--it’s a technique that allows the computer to optimize recursion so that it takes less memory. Instead of postponing multiplication: 3 * fact(2) … we can instead add extra parameters so that calculations can be done entirely inside the frame. Requires recursive call

  9. Tail Recursion We start with the usual function, but call a helper function: (define (fact n) (fact-helper 1 n)) (define (fact-helper product n) (cond ((zero? n) product) (else (fact-helper (* product n) (- n 1))))) This is similar to our previous solution, but look at the calculations--each term can be know inside the frame.

  10. (define (fact n) (fact-helper 1 n)) (define (fact-helper product n) (cond [ (zero? n) product ] [ else (fact-helper (* product n) (- n 1))])) Tail Recursive Augmentative/Head (fact-helper (* product n) (- n 1)) (* n (factorial (- n 1)) Tail Recursion We now have all the information needed to calculate the result inside the frame, without saving each recurse. Requires recursive call All-in-one

  11. Tail Recursion We can see the memory use of this approach by tracing (fact 3) (fact-helper 1 3) (fact-helper 3 2) (fact-helper 6 1) (fact-helper 6 0) ==>6 Each frame completed, and is not necessary to save.

  12. (fact-helper 6 0) (fact-helper 6 1) (fact-helper 3 2) (fact-helper 1 3) (fact 3) Tail Recursion (fact 3) (fact-helper 1 3) (fact-helper 3 2) (fact-helper 6 1) (fact-helper 6 0) ==>6 Potentially, the activation stack could grow just as we saw with augmentative recursion. But the Scheme interpreter (and many compilers) can optimize tail recursive expressions to constant memory use. (In fact, Scheme interpreters are required to optimize tail recursion.)

  13. Tail Recursion So tail recursion is nothing very different from what you’ve seen before, except that: It’s written with an awareness of memory use Each calculation is independent of subsequent recursive calls Extra parameters (usually) are used to eliminate dependence on calculations made in subsequent recursive calls. In plain English, they hold partial results!! 1 2 3

  14. Hints Can’t write it tail recursively? Just try this: Write the function recursively. Identify which calculations cannot be made entirely within a single frame Add sufficient extra parameters to eliminate the need to save information in a stack 1 2 3

  15. Example Recently, we considered a function that would multiply numbers by adding repeatedly. Reworked, we get: (define mult a b) (mult-iterative 0 a b)) (define (mult-iterative result num1 num2) (if (= num2 0) result (mult-iterative (+ result num1) num1 (- num2 1))))

  16. Example (define mult a b) (mult-iterative 0 a b)) (define (mult-iterative result num1 num2) (if (= num2 0) result (mult-iterative (+ result num1) num1 (- num2 1)))) (mult 3 4) (mult-it 0 3 4) (mult-it 3 3 3) (mult-it 6 3 2) (mult-it 9 3 1) (mult-it 12 3 0) A trace confirms the reduced use of memory. None of the ‘mult-it’ calls need to be kept on the activation stack; they do not depend on each other.

  17. Notes On Terminology The following questions are presented to suggest further reading: What’s a recursive process? What’s a recursive procedure? What’s the difference? See http://mitpress.mit.edu/sicp/full-text/book-Z-H-11.html

  18. Footnote • To solve problems using tail recursion, we normally develop at least two different functions. • In the integration example, these were (integrate lst) and (integrate-2 lst accum) • Leaving these “lying around” separate is a little tacky. • We use the idea of (local …) functions to clean this up.

  19. Recall (local …) • The syntax for a function using (local …) is as follows: (define (use-local …) (local ((<definition-1>) (<definition-2>) … (<definition-n>)) <expression> )) • Where the definition list is encapsulated, and when the function is invoked by evaluating (use-local … ) it actually evaluates the body <expression> which then uses the encapsulated definitions.

  20. For example: (define (integrate-2 lst accum) (cond [(empty? lst) empty] [else (cons (+ (first lst) accum) (integrate-2 (rest lst) (+ accum (first lst))))])) (define (integrate lst) (integrate-2 lst 0)) Unchanged (integrate-2 …) “absorbed” into its wrapper, (integrate …), and called by it (define (integrate lst) (local ( (define (integrate-2 lst accum) (cond [(empty? lst) empty] [else (cons (+ (first lst) accum) (integrate-2 (rest lst) (+ accum (first lst))))]))) (integrate-2 lst 0)))

  21. Generalized template • Deciding that a function will benefit from an accumulator requires experience borne of much practice. • When you decide this is probably the case, (local … ) provides an excellent template. (define (<old-fn> <params>) (local ( (define (<aux-fn> <params> <accum>) (cond [(<end?> <params>) … ] [else … <params> … <accum> … (<aux-fn> <params> … <accum> …))]))) (<aux-fn> <params> <initial-acc>)))

  22. Dissecting this Template What you really want (define (<old-fn> <params>) (local ( (define (<aux-fn> <params> <accum>) (cond [(<end?> <params>) … ] [else … <params> … <accum> … (<aux-fn> <params> … <accum> …))]))) (<aux-fn> <params> <initial-acc>))) What you need to do the job efficiently Use parameters and accumulator as required Call the new function with initial accumulator value Update accumulator value in the recursive call

  23. Commentary on the Design Process • First, draw a picture of the problem • Write a normal recursive solution or focus on what your partial result at each stage will be • If you notice opportunities to avoid loss of useful information, • Start with the (local …) version of the template • Visualize how the accumulator computation will work • Figure out how to initialize the accumulation • Figure out how this affects the termination condition • Figure out how to update the accumulator for the recursive call

  24. Example Write a function to add all numbers from 0 to N. Write this using augmentative recursion and tail recursion. tail.scm

  25. Questions?

  26. Summary • You should now know… • Need for Accumulation • Accumulator Style Functions • Transforming Recursive Functions

  27. Intro to Graphs

  28. Outline • Prerequisites • List manipulation • Objectives • Basic Graph Terminology • Depth-First Search • Reference • HTDP Section 28.1

  29. Graphs • Have nothing to do with plotting data (i.e. a graph of the function y = sin(x). • Are widely used to solve a large number of totally unrelated problems in CS, Engineering, Math, Business, etc. • Are studied in Graph Theory • So what do we mean by a graph?

  30. Graph Terminology What is a graph? We can think of a graph as a combination of two things: a set of points (vertices) possibly in a plane and a set of line segments (edges). Sometimes, a graph is formally defined as: G = (V, E) where ‘G’, ‘V’ and ‘E’ represent Graph, Vertices (or points) and Edges (the line segments).

  31. Graph Terminology A graph may be directed, meaning that the line segments join points only in one direction. A B F C E D Can’t travel from E to D directly, only from D to E on this edge.

  32. Graph Terminology A graph also might be undirected, meaning that line segments join points from either direction. A B F C E D An undirected or bidirectional graph

  33. Graph Terminology A graph also might be undirected, meaning that line segments join points from either direction. A B F C E D When you see edges with no arrows, you may presume the graph is undirected, or bidirectional

  34. Graph Terminology When an edge connects two vertices or nodes, we say that they are ‘adjacent’. A B F C E D E is adjacent to F, A, B, and D, but not C. Sure, you can still get to C from E, but not directly.

  35. A B F C E D Graph Terminology Unlike trees, which are special types of graphs, general graphs may have cycles, meaning that children can be the parents of their ‘ancestor nodes’. A B E C Put another way, it’s possible to walk in circles through a graph, if you don’t keep track of where you’ve been.

  36. A B F C E D Graph Terminology A collection of individual points (or edges) in a graph may represent a path. A path is just a way of getting from a start node to another node.

  37. A B F C E D Graph Terminology In the wacky world of graphs, a node may be connected even to itself. Note It seems odd, but since a graph is made of edges and vertices, an edge just might connect a node to itself.

  38. A B F C E D Graph Terminology We can also assign ‘weights’ or values to edges. 3 8 12 5 9 25 7 12 In such a case, we have a weighted graph. The weights can represent cost, distance, opportunity costs--anything.

  39. A B F C E D Graph Terminology In the beginning, we will mainly concern ourselves with unweighted graphs. We’ll just be interested in establishing if there’s a connection or relationship between nodes. We can later add weights to provide a richer graph data set.

  40. Quick Quiz So far, we’ve worked with trees. Are trees really just graphs? Consider the definition of a graph... Do trees have vertices and edges? Yup. Are they graphs? Of course.

  41. Graph Examples

  42. Map of Distilleries in Scotland. Is this a graph? (What is a graph?) Does this provide useful information? What’s missing? (Nothing; We’re only missing NSF Funding for research on this graph.)

  43. Graph/Network Uses • Graphs can also track the flows and movements of individuals in society. • E.g., Krempel’s map of Duisburg zoo visitors. The width of the lines indicates the number of visitors taking an edge. (Note: the Autobahn divides the zoo in half. Can you see evidence of this in the graph?)

  44. Summary Graphs (cont’d) Graphs are also useful for modeling hierarchy networks. A good example is the internet, with various ‘tiers’ of providers that link portions of the net together. A graph can be used to model the network relationship between computers.

More Related