CS116 – Tutorial 3 Accumulative Recursion/Runtime.

Slides:



Advertisements
Similar presentations
CSE Lecture 3 – Algorithms I
Advertisements

1 Programming Languages (CS 550) Lecture Summary Functional Programming and Operational Semantics for Scheme Jeremy R. Johnson.
Getting started with ML ML is a functional programming language. ML is statically typed: The types of literals, values, expressions and functions in a.
CSE 341 Lecture 16 More Scheme: lists; helpers; let/let*; higher-order functions; lambdas slides created by Marty Stepp
CS 116 Tutorial 2 Functional Abstraction. Reminders Assignment 2 is due this Wednesday at Noon.
CS Section 600 CS Section 002 Dr. Angela Guercio Spring 2010.
Lambda Calculus and Lisp PZ03J. Lambda Calculus The lambda calculus is a model for functional programming like Turing machines are models for imperative.
CSE332: Data Abstractions Lecture 21: Amortized Analysis Dan Grossman Spring 2010.
Recursion. Recursion is a powerful technique for thinking about a process It can be used to simulate a loop, or for many other kinds of applications In.
Lower bound for sorting, radix sort COMP171 Fall 2006.
Loops in Scheme, II c. Kathi Fisler, 2001 (early slides assume map/filter)
Algorithm Analysis CS 201 Fundamental Structures of Computer Science.
מבוא מורחב - שיעור 81 Lecture 8 Lists and list operations (continue).
The Efficiency of Algorithms
Functional programming: LISP Originally developed for symbolic computing First interactive, interpreted language Dynamic typing: values have types, variables.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 23 Algorithm Efficiency.
7/2/20151 Programming Languages and Compilers (CS 421) Elsa L Gunter 2112 SC, UIUC Based in part on slides by Mattox.
Analysis of Algorithm.
Analysis of Algorithms Spring 2015CS202 - Fundamentals of Computer Science II1.
CS 116 Tutorial 5 Introduction to Python. Review Basic Python Python is a series of statements def f(p1, p2,…pn): x = 5 if x > p1: x = p1 + p2 return.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 23 Algorithm Efficiency.
1 Chapter 24 Developing Efficient Algorithms. 2 Executing Time Suppose two algorithms perform the same task such as search (linear search vs. binary search)
Analysis of Algorithms
CS 152: Programming Language Paradigms February 17 Class Meeting Department of Computer Science San Jose State University Spring 2014 Instructor: Ron Mak.
Searching. RHS – SOC 2 Searching A magic trick: –Let a person secretly choose a random number between 1 and 1000 –Announce that you can guess the number.
Analysis of Algorithms These slides are a modified version of the slides used by Prof. Eltabakh in his offering of CS2223 in D term 2013.
The Selection Problem. 2 Median and Order Statistics In this section, we will study algorithms for finding the i th smallest element in a set of n elements.
Sorting Fun1 Chapter 4: Sorting     29  9.
1 Append: process  (append list1 list2) (cons 1 (append ‘(2) list2)) (cons 1 (cons 2 (append ‘() list2))) (cons 1 (cons 2 list2)) (define (append list1.
1 Analysis of Algorithms CS 105 Introduction to Data Structures and Algorithms.
Extracting Performance Functions. Basic Operations n The number of Basic Operations performed must be proportional to the run time n Counting techniques.
Divide & Conquer  Themes  Reasoning about code (correctness and cost)  recursion, induction, and recurrence relations  Divide and Conquer  Examples.
Program Efficiency & Complexity Analysis. Algorithm Review An algorithm is a definite procedure for solving a problem in finite number of steps Algorithm.
Chapter 10 Algorithm Analysis.  Introduction  Generalizing Running Time  Doing a Timing Analysis  Big-Oh Notation  Analyzing Some Simple Programs.
CS 361 – Chapters 8-9 Sorting algorithms –Selection, insertion, bubble, “swap” –Merge, quick, stooge –Counting, bucket, radix How to select the n-th largest/smallest.
Abstraction: Procedures as Parameters CMSC Introduction to Computer Programming October 14, 2002.
3.3 Complexity of Algorithms
Generalizing Over Functions CS 5010 Program Design Paradigms “Bootcamp” Lesson TexPoint fonts used in EMF. Read the TexPoint manual before you delete.
CS116 Tutorial 1 Review of CS115. Reminders Assignment 1 is due Wednesday, January 21th at 12pm (Noon) Submit Assignment 0 if you have not done so already.
PPL Lazy Lists. Midterm 2012 (define sum-vals (λ (ts) (if (ts-simple? ts) (ts-val ts) (accumulate + 0 (map ts-val (ts-inner-slots ts))))))
CS 150: Analysis of Algorithms. Goals for this Unit Begin a focus on data structures and algorithms Understand the nature of the performance of algorithms.
PPL CPS. Moed A 2007 Solution (define scale-tree (λ (tree factor) (map (λ (sub-tree) (if (list? sub-tree) (scale-tree sub-tree factor) (* sub-tree.
Tutorial 9 Iteration. Reminder Assignment 8 is due Wednesday.
Using the List Template CS 5010 Program Design Paradigms “Bootcamp” Lesson 4.2 TexPoint fonts used in EMF. Read the TexPoint manual before you delete this.
Search Algorithms Written by J.J. Shepherd. Sequential Search Examines each element one at a time until the item searched for is found or not found Simplest.
CSE373: Data Structures & Algorithms Lecture 8: Amortized Analysis Dan Grossman Fall 2013.
3/8/20161 Programming Languages and Compilers (CS 421) Reza Zamani Based in part on slides by Mattox Beckman, as updated.
PPL CPS. Moed A 2007 Solution (define scale-tree (λ (tree factor) (map (λ (sub-tree) (if (list? sub-tree) (scale-tree sub-tree factor) (* sub-tree.
Chapter 15 Running Time Analysis. Topics Orders of Magnitude and Big-Oh Notation Running Time Analysis of Algorithms –Counting Statements –Evaluating.
1 Algorithms Searching and Sorting Algorithm Efficiency.
LECTURE 9 CS203. Execution Time Suppose two algorithms perform the same task such as search (linear search vs. binary search) and sorting (selection sort.
Lets and Loops Tail Recursion.
Algorithm Analysis 1.
CS314 – Section 5 Recitation 10
Analysis of Algorithms
PPL Lazy Lists.
Racket CSC270 Pepper major portions credited to
September 4, 1997 Programming Languages (CS 550) Lecture 6 Summary Operational Semantics of Scheme using Substitution Jeremy R. Johnson TexPoint fonts.
David Kauchak CS52 – Spring 2015
Introduction to Functional Programming in Racket
Algorithm An algorithm is a finite set of steps required to solve a problem. An algorithm must have following properties: Input: An algorithm must have.
CS 201 Fundamental Structures of Computer Science
Lecture #8 מבוא מורחב.
CS 36 – Chapter 11 Functional programming Features Practice
(early slides assume map/filter)
Introduction to Functional Programming in Racket
List and list operations (continue).
Lecture # , , , , מבוא מורחב.
ormap, andmap, and filter
CS203 Lecture 15.
Presentation transcript:

CS116 – Tutorial 3 Accumulative Recursion/Runtime

Reminders: Assignment 3 is due Wednesday at 10 am.

Review Accumulative Recursion Run Time

Accumulative Recursion (define (fn lst) (local [(define (acc-fn whats-left acc1 acc2 … accn) (cond [(base-case whats-left)…acck…] … [else (acc-fn update-parameters)]))] (acc-fn initial-whats-left initial-acck … )) Accumulators “keep track” of something so that you can quickly produce the expected result Accumulators: can have as many as you need Usually use in place of lst when recursing Use accs to help produce the correct result Put more cases in if you need them

Question 1 Write an accumulatively recursive function sum-list which consumes a list of numbers and produces their sum. Trace (sum-list (list 1 2 3)) Compare this accumulative version with the structural recursive version from earlier.

Trace Comparison Without Accumulative Recursion: (sum-list (list 1 2 3)) => (+ 1 (sum-list (list 2 3))) => (+ 1 (+ 2 (sum-list (list 3)))) => (+ 1 (+ 2 (+ 3 (sum-list empty)))) => (+ 1 (+ 2 (+ 3 0))) => (+ 1 (+ 2 3)) => (+ 1 5) => 6

Trace Comparison With Accumulative Recursion (sum-list (list 1 2 3)) => (sum-list-acc (list 1 2 3) 0) => (sum-list-acc (list 2 3) (+ 1 0)) => (sum-list-acc (list 2 3) 1) => (sum-list-acc (list 3) (+ 2 1)) => (sum-list-acc (list 3) 3) => (sum-list-acc empty (+ 3 3)) => (sum-list-acc empty 6) => 6

Question 2 Develop an accumulatively recursive function list-to-num that consumes a nonempty list, digits,of numbers between 0 and 9, and returns the number corresponding to digits. For example, (list-to-num (list 9 0 8)) should return 908, while (list-to-num (list 8 6)) should return 86. Trace the application (list-to-num (list 8 0 2))

Trace of list-to-num (list-to-num ‘(8 0 2)) ⇒ (list-to-num-acc ‘(8 0 2) 0) ⇒ (list-to-num-acc ‘(0 2) (+ (* 10 0) 8)) ⇒ (list-to-num-acc ‘(0 2) 8) ⇒ (list-to-num-acc ‘(2) (+ (* 10 8) 0)) ⇒ (list-to-num-acc ‘(2) 80) ⇒ (list-to-num-acc empty (+ (* 10 80) 2)) ⇒ (list-to-num-acc empty 802) ⇒ 802

Question 3 Write an accumulatively recursive function find that consumes a list of symbols alos and a symbol sym, and returns the list of indices of positions in alos with symbol sym. Recall that the first position in a list has index 0. You may use reverse. For example, (find (list ‘a ‘v ‘d ‘v) 'v) should return (list 1 3), while (find (list ‘ a ‘ v ‘ d ‘ v) 'q) should return empty.

Question 4 Write an accumulatively recursive function count-max that consumes a nonempty list of numbers alon and returns the number of times the largest number in alon appears. For example, ◦ (count-max (list )) => 2 ◦ since the largest element of the list, 5, appears twice. Your function should pass through the list only once.

Runtime Review Look at the “worst case” scenario (i.e. longest time) Only for code that gets executed when you run it Assume function works (i.e. will not produce an error when you run it)

Runtime Review O(1) – Constant ◦ does not depend on the size of the input (first x), (rest x), (symbol? x), (map sqr (list 1 2 3)) Ex: simple functions, or does not do something n times O(n) – Linear ◦ depends on the size of the input (map sqr (list1 2 … n)) Ex: abstract list functions, function call once (on a single cond case)

Runtime Review O(n 2 ) – Quadratic ◦ time proportional to square of input (map sqr (build-list n add1)) Ex: nested abstract list function, O(n) done multiple times O(2 n ) – Exponential ◦ As size of input increases, run time doubles Module 3, Slide 22: fib Ex: Multiple function calls (>1 calls in one cond case)

Question 5 For each of the following determine what the worst-case runtime is.

Determine the worst-case run-time in terms of n, assuming n elements in lon (define (sum-list1 lon) (cond [(empty? lon) 0] [else (+ (first lon) (sum-list1 (rest lon)))]))

Determine worst-case run-time in terms of n, assuming n elements in loi (define (evens loi) (filter even? loi))

Determine the worst-case run-time in terms of n (define (create-number-lists n) (cond [(= n 0) empty] [else (cons (build-list n identity) (create-number-lists (sub1 n)))]))

Determine the worst-case run-time in terms of n (define (create-a-list n) (cond [(even? n) empty] [else (build-list 3 (lambda (y) (+ n y)))]))