Picking up tennis balls

Similar documents
CSCI0170. Today s topics. Predicates Natural Number recursion Recursion Diagrams List recursion A first glance at the design recipe

Repetition Through Recursion

Summer 2017 Discussion 10: July 25, Introduction. 2 Primitives and Define

Intro. Scheme Basics. scm> 5 5. scm>

Discussion 11. Streams

CS61A Summer 2010 George Wang, Jonathan Kotker, Seshadri Mahalingam, Eric Tzeng, Steven Tang

Computer Science 21b (Spring Term, 2015) Structure and Interpretation of Computer Programs. Lexical addressing

Evaluating Scheme Expressions

Functional Programming. Pure Functional Languages

Principles of Programming Languages Topic: Functional Programming Professor L. Thorne McCarty Spring 2003

Scheme in Scheme: The Metacircular Evaluator Eval and Apply

User-defined Functions. Conditional Expressions in Scheme

SCHEME 10 COMPUTER SCIENCE 61A. July 26, Warm Up: Conditional Expressions. 1. What does Scheme print? scm> (if (or #t (/ 1 0)) 1 (/ 1 0))

STREAMS 10. Basics of Streams. Practice with Streams COMPUTER SCIENCE 61AS. 1. What is a stream?

RACKET BASICS, ORDER OF EVALUATION, RECURSION 1

Functional Programming. Pure Functional Languages

Functional Programming. Pure Functional Programming

STREAMS 10. Basics of Streams. Practice with Streams COMPUTER SCIENCE 61AS. 1. What is a stream? 2. How does memoization work?

CS450 - Structure of Higher Level Languages

Hi. I m a three. I m always a three. I never ever change. That s why I m a constant.

Lecture 20: Satisfiability Steven Skiena. Department of Computer Science State University of New York Stony Brook, NY

Lambda Calculus and Lambda notation in Lisp II. Based on Prof. Gotshalks notes on Lambda Calculus and Chapter 9 in Wilensky.

CPSC 320: Intermediate Algorithm Design and Analysis. Tutorial: Week 3

CIS4/681 { Articial Intelligence 2 > (insert-sort '( )) ( ) 2 More Complicated Recursion So far everything we have dened requires

0x1A Great Papers in Computer Security

SCHEME 8. 1 Introduction. 2 Primitives COMPUTER SCIENCE 61A. March 23, 2017

Problem Set 4: Streams and Lazy Evaluation

Fall 2017 Discussion 7: October 25, 2017 Solutions. 1 Introduction. 2 Primitives

Functional Programming - 2. Higher Order Functions

SCHEME 7. 1 Introduction. 2 Primitives COMPUTER SCIENCE 61A. October 29, 2015

Computational Complexity and Implications for Security DRAFT Notes on Infeasible Computation for MA/CS 109 Leo Reyzin with the help of Nick Benes

val(y, I) α (9.0.2) α (9.0.3)

Lambda Calculus. Gunnar Gotshalks LC-1

Syntactic Sugar: Using the Metacircular Evaluator to Implement the Language You Want

Lambda Calculus LC-1

Student s booklet. Bills, Ladders and other Number Diagrams. Meeting 23 Student s Booklet. Contents. May 18, UCI. Town Bills 2 Big and Small

Lisp. Versions of LISP

Building a system for symbolic differentiation

Building a system for symbolic differentiation

Documentation for LISP in BASIC

CSE413: Programming Languages and Implementation Racket structs Implementing languages with interpreters Implementing closures

Project 2: Scheme Interpreter

Fall 2018 Discussion 8: October 24, 2018 Solutions. 1 Introduction. 2 Primitives

Quiz 1. Queues , Fall 2007 Recitation 10 Solutions 1

6.001: Structure and Interpretation of Computer Programs

CS61A Discussion Notes: Week 11: The Metacircular Evaluator By Greg Krimer, with slight modifications by Phoebus Chen (using notes from Todd Segal)

Programming Languages

CS 61A Interpreters, Tail Calls, Macros, Streams, Iterators. Spring 2019 Guerrilla Section 5: April 20, Interpreters.

How to Design Programs

Scheme Basics > (butfirst '(help!)) ()

Spring 2018 Discussion 7: March 21, Introduction. 2 Primitives

Excel for Algebra 1 Lesson 5: The Solver

CS61A Notes Week 6: Scheme1, Data Directed Programming You Are Scheme and don t let anyone tell you otherwise

CS 314 Principles of Programming Languages. Lecture 16

CS 314 Principles of Programming Languages

Procedural abstraction SICP Data abstractions. The universe of procedures forsqrt. Procedural abstraction example: sqrt

Macros & Streams Spring 2018 Discussion 9: April 11, Macros

Typed Scheme: Scheme with Static Types

Project 1: Part 1. Project 1 will be to calculate orthogonal polynomials. It will have several parts. x = b± b 2 4ac., x 2 = b b 2 4ac.

Combinatorial Optimization

Midterm 2 Solutions. CS70 Discrete Mathematics and Probability Theory, Spring 2009

Understanding Recursion

Chapter 6 Practice Test

6.001 Notes: Section 8.1

cs61amt2_4 CS 61A Midterm #2 ver March 2, 1998 Exam version: A Your name login: cs61a- Discussion section number TA's name

Scheme. Functional Programming. Lambda Calculus. CSC 4101: Programming Languages 1. Textbook, Sections , 13.7

A Brief Introduction to Scheme (II)

Programming Languages. Streams Wrapup, Memoization, Type Systems, and Some Monty Python

FP Foundations, Scheme

Statistics Case Study 2000 M. J. Clancy and M. C. Linn

SCHEME AND CALCULATOR 5b

6.001 Notes: Section 6.1

Lecture 6: Sequential Sorting

CSc 520 Principles of Programming Languages

Informed (Heuristic) Search. Idea: be smart about what paths to try.

6.184 Lecture 4. Interpretation. Tweaked by Ben Vandiver Compiled by Mike Phillips Original material by Eric Grimson

Exponential Numbers ID1050 Quantitative & Qualitative Reasoning

Animator Friendly Rigging Part 1

Part II Composition of Functions

Lecture #24: Programming Languages and Programs

Announcements. The current topic: Scheme. Review: BST functions. Review: Representing trees in Scheme. Reminder: Lab 2 is due on Monday at 10:30 am.

Discussion 4. Data Abstraction and Sequences

CSE413 Midterm. Question Max Points Total 100

Programming Languages. Thunks, Laziness, Streams, Memoization. Adapted from Dan Grossman s PL class, U. of Washington

15100 Fall 2005 Final Project

Lisp Basic Example Test Questions

Using Symbols in Expressions (1) evaluate sub-expressions... Number. ( ) machine code to add

CS61A Notes 02b Fake Plastic Trees. 2. (cons ((1 a) (2 o)) (3 g)) 3. (list ((1 a) (2 o)) (3 g)) 4. (append ((1 a) (2 o)) (3 g))

Type Inference: Constraint Generation

Catalan Numbers. Table 1: Balanced Parentheses

Principles of Programming Languages 2017W, Functional Programming

Streams, Delayed Evaluation and a Normal Order Interpreter. CS 550 Programming Languages Jeremy Johnson

CS 61A Midterm #2 ver March 2, 1998 Exam version: A. Your name. login: cs61a- Discussion section number. TA's name

Homework 1. Reading. Problems. Handout 3 CSCI 334: Spring, 2012

Deferred operations. Continuations Structure and Interpretation of Computer Programs. Tail recursion in action.

CS61A Midterm 2 Review (v1.1)

CS1102: Macros and Recursion

SCHEME The Scheme Interpreter. 2 Primitives COMPUTER SCIENCE 61A. October 29th, 2012

Lesson 19: Four Interesting Transformations of Functions

Project 1: Part 1. x = b± b 2 4ac., x 2 = b b 2 4ac.

Transcription:

Picking up tennis balls Called a traveling-salesman tour. Computing the best tour is an infamously difficult computational problem.

Traveling-salesman problem http://xkcd.com/399/ (Randall Munro) What are those strange symbols and why is this funny? This is about algorithms.

Algorithms I do algorithm design for a living. The goal: clever methods for solving computational problems.

Pillow Talk http://xkcd.com/69/ (Randall Munro)

1337: Part 2 http://xkcd.com/342/ (Randall Munro) Again with the strange symbols?

Computational Problems A computational problem is specified by specifying what kind of inputs are acceptable Exactly what I call an what kind of outputs are acceptable input-output spec. Some computational problems: Example input and output input: number a output: the product a a input: numbers a and b output: the product a b 9,876,543 9,876,543 97,546,101,630,849 9,876,543 12,345 121,925,923,335 input: a Sudoku puzzle output: the solution (values of missing entries) input: coordinates of tennis balls output: shortest tour visiting all the balls (3,6) (4,8) (5,3) (7,8) (8,6)

Optimization Problems A computational problem is specified by specifying what kind of inputs are acceptable what kind of outputs are acceptable It is an optimization problem if the output is chosen among many candidates by selecting the candidate with the smallest/biggest value (however value is defined). input: coordinates of tennis balls output: shortest tour visiting all the balls (3,6) (4,8) (5,3) (7,8) (8,6)

Algorithms An algorithm for a computational problem Not a formula Not a program/app. A precisely specified computational method that, for any legal input, is guaranteed to produce a legal output. Example: Exponentiation using multiplication 3 N =3 3 3 3 {z } N times input: a number b output: 3 b 3 16 =3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 It seems that computing 3 16 by multiplying requires fifteen multiplications. Generalizing gives you an algorithm for exponentiation. However, squaring a number requires one multiplication: Use repeated squaring: 3 16 =(((3 2 ) 2 ) 2 ) 2 x 2 = x x Two ways of computing 3 16 : naive (takes fifteen mults) and clever (four mults) Four multiplications instead of fifteen doesn t seem like a big deal However, suppose the exponent is large: 3 1048576 =((((((((((((((((((3 2 ) 2 ) 2 ) 2 ) 2 ) 2 ) 2 ) 2 ) 2 ) 2 ) 2 ) 2 ) 2 ) 2 ) 2 ) 2 ) 2 ) 2 ) 2 ) 2 Practical need: In, need exponents that are hundreds of digits.

Security (http://xkcd.com/538/, by Randall Munroe) 4096-bit RSA requires exponents with over a thousand digits. We need to know how fast exponentiation can be done! Practical need: In encryption, need exponents that are hundreds of digits.

Algorithms for exponentiating 3 input: a number b output: 3 b Measure the input b by number n of digits of b Naive algorithm N number of multiplications 1 0 2 1 3 2 4 3 5 4 6 5 7 6 Clever algorithm N number of multiplications 1 0 2 1 3 2 4 2 5 3 6 3 7 4 3 4digits z} { 9876 3 n digits z } { 98765432109876 1024 1023 1025 1024 1026 1025 1027 1026 1024 10 1025 11 1026 11 1027 12

Algorithms for exponentiating 3 input: a number b output: 3 b Measure the input b by number n of digits of b Naive algorithm number of digits number of multiplications 1 8 2 98 3 998 4 9998 5 99998 6 999998 7 9999998 Clever algorithm N number of multiplications 1 0 2 1 3 2 4 2 5 3 6 3 7 4 4digits z} { 9876 3 3 n digits z } { 98765432109876 100 101 102 103 9999999999999999999999998 99999999999999999999999998 999999999999999999999999998 9999999999999999999999999998 1024 10 1025 11 1026 11 1027 12

Algorithms for exponentiating 3 input: a number b output: 3 b Measure the input b by number n of digits of b Naive algorithm number of digits number of multiplications 1 8 2 98 3 998 4 9998 5 99998 6 999998 7 9999998 number of digits number of 7.5 multiplications 1 5 2 11 3 17 4 25 5 31 6 37 7 45 10 Clever algorithm 5 2.5 0 1 2 3 4 5 6 7 8 9 10 7500 3 n digits z } { 98765432109876 horizontal axis = input size n vertical axis = number of computational steps 100 101 102 103 9999999999999999999999998 99999999999999999999999998 999999999999999999999999998 9999999999999999999999999998 100 10 5000 1025 11 1026 11 2500 1027 12 y = 10 n y = n/0.3 0 10 20 30 40 50 60 70 80 90 100

Analysis of algorithms (or computational complexity) 7500 5000 2500 y = 10 n y = n 2 /0.3 horizontal axis = input size n vertical axis = number of computation steps y = n/0.3 0 10 20 30 40 50 60 70 80 90 100 We use formulas to describe how many operations are needed depending on size of input: O(10 n ) O(n 2 ) O(n) bad pretty good best

Analysis of algorithms: Predicting how long a given algorithm will take Why? Help you decide whether a computational task is feasible. Help you decide which of several algorithms is best for task. Help you decide when you have an algorithm that suffices. Helps you focus on what part of a big complicated program will be the computational bottleneck. How? Start with a model of the computer. The model captures some aspects of how computer works. Every model is an approximation of the thing it models. We use models because they are simpler than reality, easier to work with. More faithful models might give you more accurate predictions but harder to work with. Need a good compromise. Depends on use.

Very simple model: counting operations We will define basic operations: cons car cdr binding a formal argument to an actual argument Measure computation time by how many basic operations are performed. Because I know the Rules of Evaluation, I can pretend to be the computer, so I can add up the number of operations. As you will see, the exact number of operations is not vital.

How many operations for evaluating the following? (* (+ a b) (+ c d)) For now, let s assume each of multiplying two numbers adding two numbers is a single operation. Each of evaluating a variable binding a formal to an actual is one operation. variable-evaluations: 7 bindings: 6 additions: 2 + multiplications: 1 Total: 16

Quiz 1: Write the len procedure input: a list L output: number of items in L (You ve seen this before.) (define len (lambda (L) (cond Longer computations ((empty? L) 0) ((cons? L) (+ 1 (len (cdr rest L))))))) (define len (lambda (L) (if (empty? L) 0 (check-expect (len (quote (A (B C) D)) or 3) (+ 1 (len (rest L)))))

Quiz 2: Write the English2cow procedure: input: a list L output: a list of the same length as L all of whose elements are the symbol moo (check-expect (English2cow (quote (How are you?)) (quote (moo moo moo))) Solution: define English2cow (lambda (L) (if (empty? L) empty (cons (quote moo) (English2cow (rest L)))))

Analyzing a recursive procedure Analyze English2cow What does analyze mean? We want a formula that predicts how long the procedure will take (number of operations). Unit cost That depends on length of input list! if Let s make a table quote cons rest empty? Binding define English2cow (lambda (L) (if (empty? L) empty (cons (quote moo) (English2cow (rest L)))))