CS61A Lecture 7 2011-06-29 Colleen Lewis. Clicker Test When do you think homework should be due? A)10pm B)11pm C)Midnight D)11am the next day.

Slides:



Advertisements
Similar presentations
SEARCHING AND SORTING HINT AT ASYMPTOTIC COMPLEXITY Lecture 9 CS2110 – Spring 2015 We may not cover all this material.
Advertisements

Intro to Analysis of Algorithms. Algorithm “A sequence of unambiguous instructions for solving a problem, i.e., for obtaining a required output for any.
HST 952 Computing for Biomedical Scientists Lecture 10.
Fall 2006CENG 7071 Algorithm Analysis. Fall 2006CENG 7072 Algorithmic Performance There are two aspects of algorithmic performance: Time Instructions.
CSE332: Data Abstractions Lecture 2: Math Review; Algorithm Analysis Tyler Robison Summer
© 2004 Goodrich, Tamassia 1 Lecture 01 Algorithm Analysis Topics Basic concepts Theoretical Analysis Concept of big-oh Choose lower order algorithms Relatives.
CS 307 Fundamentals of Computer Science 1 Asymptotic Analysis of Algorithms (how to evaluate your programming power tools) based on presentation material.
Not all algorithms are created equally Insertion of words from a dictionary into a sorted list takes a very long time. Insertion of the same words into.
Runtime Analysis CSC 172 SPRING 2002 LECTURE 9 RUNNING TIME A program or algorithm has running time T(n), where n is the measure of the size of the input.
Analysis of Algorithms1 Estimate the running time Estimate the memory space required. Time and space depend on the input size.
Analysis of Algorithms
Analysis of Algorithms (pt 2) (Chapter 4) COMP53 Oct 3, 2007.
Fall 2006CSC311: Data Structures1 Chapter 4 Analysis Tools Objectives –Experiment analysis of algorithms and limitations –Theoretical Analysis of algorithms.
Functional Design and Programming Lecture 4: Sorting.
CS2336: Computer Science II
CSE373: Data Structures and Algorithms Lecture 4: Asymptotic Analysis Aaron Bauer Winter 2014.
COMP s1 Computing 2 Complexity
1 i206: Lecture 6: Math Review, Begin Analysis of Algorithms Marti Hearst Spring 2012.
Exponential and Logarithmic Equations
MA/CSSE 473 Day 03 Asymptotics A Closer Look at Arithmetic With another student, try to write a precise, formal definition of “t(n) is in O(g(n))”
Week 2 CS 361: Advanced Data Structures and Algorithms
SEARCHING, SORTING, AND ASYMPTOTIC COMPLEXITY Lecture 12 CS2110 – Fall 2009.
CSC 201 Analysis and Design of Algorithms Lecture 04: CSC 201 Analysis and Design of Algorithms Lecture 04: Time complexity analysis in form of Big-Oh.
The Growth of Functions Rosen 2.2 Basic Rules of Logarithms log z (xy) log z (x/y) log z (x y ) If x = y If x < y log z (-|x|) is undefined = log z (x)
6.3A – Logarithms and Logarithmic Functions Objective: TSW evaluate logarithmic expressions.
Analysis of Algorithms
1 Lecture 16: Lists and vectors Binary search, Sorting.
Recursion: Linear and Tree Recursive Processes and Iteration CMSC Introduction to Computer Programming October 7, 2002.
Object-Oriented Design CSC 212. Announcements Ask more questions!  Your fellow students have the same questions (remember, I grade the daily quizzes)
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.
Asymptotic Analysis-Ch. 3
Program Efficiency & Complexity Analysis. Algorithm Review An algorithm is a definite procedure for solving a problem in finite number of steps Algorithm.
CS 61B Data Structures and Programming Methodology July 10, 2008 David Sun.
CSE373: Data Structures and Algorithms Lecture 3: Math Review; Algorithm Analysis Lauren Milne Summer 2015.
3.3 Complexity of Algorithms
DATA STRUCTURES AND ALGORITHMS Lecture Notes 2 Prepared by İnanç TAHRALI.
David Evans CS150: Computer Science University of Virginia Computer Science Lecture 5: Recursing on Lists.
Algorithm Analysis (Big O)
CSE 373: Data Structures and Algorithms
CS61A Lecture Colleen Lewis. Clicker Test How often do you read piazza posts? A)Whenever I receive an B)Once or twice a day C)When.
Searching Topics Sequential Search Binary Search.
DS.A.1 Algorithm Analysis Chapter 2 Overview Definitions of Big-Oh and Other Notations Common Functions and Growth Rates Simple Model of Computation Worst.
A Introduction to Computing II Lecture 5: Complexity of Algorithms Fall Session 2000.
CS61A Lecture Colleen Lewis. Clicker poll Where do you think you’re learning the most? I assume you’ll learn the most in lab & disc (so.
Lecture # 6 1 Advance Analysis of Algorithms. Divide-and-Conquer Divide the problem into a number of subproblems Similar sub-problems of smaller size.
Intro to Analysis of Algorithms. Algorithm “A sequence of unambiguous instructions for solving a problem, i.e., for obtaining a required output for any.
The Beauty and Joy of Computing Lecture #7 Algorithmic Complexity “Data scientists at Yahoo are using prediction markets – along with polls, sentiment.
Announcement We will have a 10 minutes Quiz on Feb. 4 at the end of the lecture. The quiz is about Big O notation. The weight of this quiz is 3% (please.
Concepts of Algorithms CSC-244 Unit 3 and 4 Algorithm Growth Rates Shahid Iqbal Lone Computer College Qassim University K.S.A.
מבוא מורחב שיעור 7 1 Lecture 7 Data Abstraction. Pairs and Lists. (Sections – 2.2.1)
CS61A Lecture Colleen Lewis. Clicker poll How do you think the midterm compared to previous midterms online? A)Harder B)A little harder.
Problem of the Day  On the next slide I wrote today’s problem of the day. It has 3 possible answers. Can you guess which 1 of the following is the solution?
Section 1.7 Comparing Algorithms: Big-O Analysis.
1 Algorithms Searching and Sorting Algorithm Efficiency.
Lecture 3COMPSCI.220.S1.T Running Time: Estimation Rules Running time is proportional to the most significant term in T(n) Once a problem size.
Data Structures I (CPCS-204) Week # 2: Algorithm Analysis tools Dr. Omar Batarfi Dr. Yahya Dahab Dr. Imtiaz Khan.
CMSC201 Computer Science I for Majors Lecture 23 – Sorting
Yahoo predicts contest already!
Lecture 16 Streams continue Infinite Streams מבוא מורחב - שיעור 16.
GC 211:Data Structures Week 2: Algorithm Analysis Tools
Pairs and Lists. Data Abstraction. SICP: Sections – 2.2.1
COMP 53 – Week Seven Big O Sorting.
Big O Notation.
Searching, Sorting, and Asymptotic Complexity
CSE 2010: Algorithms and Data Structures Algorithms
CSE 1342 Programming Concepts
At the end of this session, learner will be able to:
Searching and Sorting Hint at Asymptotic Complexity
Searching and Sorting Hint at Asymptotic Complexity
Presentation transcript:

CS61A Lecture Colleen Lewis

Clicker Test When do you think homework should be due? A)10pm B)11pm C)Midnight D)11am the next day

Abstraction, List, & Cons

Goals To talk about things using meaning not how it is represented in the computer Invented by: Turing Award Winner: Barbara Liskov

Very sad code (define (total hand) (if (empty? hand) 0 (+ (butlast (last hand)) (total (butlast hand))))) STk> (total ‘(3h 10c 4d)) 17 STk> (total ‘(3h ks 4d)) ;;;EEEK!

Happier Code (define (total hand) (if (empty? hand) 0 (+ (rank (one-card hand)) (total (remaining-cards hand) )))) (define (rank card) (butlast card)) (define (suit card) (last card)) (define suit last) (define (one-card hand) (last hand)) (define (remaining-cards hand) (bl hand)) Selectors

Goals To talk about things using meaning not how it is represented in the computer To be able to change how it is represented in the computer without people who use our program caring

Constructors GOAL: To talk about things using meaning not how it is represented in the computer STk> (total ‘(3h 10c 4d)) ;  STk> (total (make-hand (make-card 3 ‘heart) (make-card 10 ‘club) (make-card 4 ‘diamond))) You still have to teach people to use your program

Constructors STk> (total (make-hand (make-card 3 ‘heart) (make-card 10 ‘club) (make-card 4 ‘diamond))) (define (make-card rank suit) (word rank (first suit))) (define make-hand se) Constructors

Data Abstraction (define (total hand) (if (empty? hand) 0 (+ (rank (one-card hand)) (total (remaining-cards hand) )))) (define (rank card) (butlast card)) (define (suit card) (last card)) (define (one-card hand) (last hand)) (define (remaining-cards hand) (bl hand)) (define (make-card rank suit) (word rank (first suit))) (define make-hand se)

Try It! Rewrite what you need to: Cards are represented as numbers 1-52 – 1-13 is A-K of Hearts – is A-K of Spades – is A-K of Diamonds – is A-K of Clubs

CONSTRUCTOR SOLUTION (define (make-card rank suit) (cond ((equal? suit 'heart) rank) ((equal? suit 'spade) (+ rank 13)) ((equal? suit 'diamond) (+ rank 26)) ((equal? suit 'club) (+ rank 39)) (else (error "say what?")) ))

SELECTOR SOLUTION (define (card-rank card) (remainder card 13)) (define (suit card) (cond ((> 14 card) 'heart) ((> 27 card) 'spade) ((> 40 card) 'diamond) (else 'club)))

Runtimes Continued

Exponential Runtime 2n2n

(cc 99 5) (cc 49 5) (cc -1 5)(cc 49 4)(cc 24 4)(cc -1 4)(cc 24 3)(cc 49 3)(cc 39 3)(cc 49 2) (cc 99 4) (cc 74 4)(cc 49 4)(cc 24 4)(cc 49 3)(cc 74 3)(cc 64 3)(cc 74 2)(cc 99 3)(cc 89 3)(cc 79 3)(cc 89 2)(cc 99 2)(cc 94 2)(cc 99 1)

branches = 1 call 1 branches = 3 2 branches = 7 3 branches = 15 b branches = 2 b calls

~32~ Fibonacci fib n = fib n-1 + fib n-2 n branches = 2 n calls

Logarithmic Runtime Log 2 (N)

Number Guessing Game I’m thinking of a number between 1 and 100 How many possible guesses could it take you? (WORST CASE) Between 1 and ? How many possible guesses could it take you? (WORST CASE)

b branches = 2 b calls n possible numbers & h calls n=2 h+1 -1

Divide and Conquer If we can divide the problem up in half each time – like the number guessing game How many recursive calls will it take? n is the original problem size if h calls then: n=2 h+1 -1

// Take the log of both sides // Remember:

Log 2 (N) When we’re able to keep dividing the problem in half (or thirds etc.) Looking through a phone book

Asymptotic Cost We want to express the speed of an algorithm independently of a specific implementation on a specific machine. We examine the cost of the algorithms for large input sets i.e. the asymptotic cost. In later classes (CS70/CS170) you’ll do this in more detail

Which one is fastest? Asymptotic Cost

“Woah! One of these is WAY better after this point. Let’s call that point N ”

Important Big-Oh Sets FunctionCommon Name O(1)Constant O(log n)Logarithmic O( log 2 n)Log-squared O( )Root-n O(n)Linear O(n log n)n log n O(n 2 )Quadratic O(n 3 )Cubic O(n 4 )Quartic O(2 n )Exponential O(e n )Bigger exponential Subset of

Which is fastest after some value N ? WAIT – who cares? These are all proportional! Sometimes we do care, but for simplicity we ignore constants

if and only if for all n > N Formal definition

Simplifying stuff is important

Break

Cons and Lists DEMO

cons STk> (cons 1 2) (1. 2) STk> (define a (cons 1 2)) a STk> (define b (cons ‘hi ‘bye)) b STk> b (hi. bye)

Data Abstraction Pairs car cdr cons carcdr Constructors Selectors

car / cdr STk> (car a) 1 STk> (cdr a) 2 STk> (car b) hi STk> (cdr b) bye 12 carcdr hibye carcdr a b

Uses of Pair from textbook XY carcdr numden carcdr lowhigh carcdr realcomplex carcdr Points Intervals Fractions Complex # XY carcdr XY carcdr carcdr Lines

Lists Demo

list STk> (list ) ( ) STk> (list 1) (1) STk> (list) () STk> (list +) (#[closure arglist=args 7ff53de8])

Lists are made with pairs! STk> (define a (list )) 1 carcdr 2 carcdr 3 carcdr 4 carcdr a STk> (define b (list 1 2)) 1 carcdr 2 carcdr b

The Empty List STk> (cons 2 ‘()) (2) 2 carcdr

How can you make the list (1 2) ? a)(define a (cons 1 2 ‘())) b)(define a (cons 1 (cons 2))) c)(define a (cons 1 (cons 2 ‘()))) d)(define a (cons (cons 2 ‘()) 1))) e)??? 1 carcdr 2 carcdr a

How many calls to cons are made? STk> (define a (list )) 1 carcdr 2 carcdr 3 carcdr 4 carcdr a A)2B) 3C) 4D) 5E) 6 SOLUTION: (cons 1 (cons 2 (cons 3 (cons 4 ‘()))))

How many calls to cons are made? STk> (define a (list 1 2 (list 3 4) 5)) 1 carcdr 2 carcdrcarcdr 5 carcdr a 3 carcdr 4 carcdr A)2B) 3C) 4D) 5E) 6 Solution: (cons 1 (cons 2 (cons (cons 3 (cons 4 ‘())) (cons 5 ‘())))))

Accessing Elements Using car and cdr

The Empty List w/ car & cdr STk> (define x (cons 2 ‘()) x STk> x (2) STk> (car x) 2 STk> (cdr x) () 2 carcdr x

How do you get the 2? STk> (define a (list )) 1 carcdr 2 carcdr 3 carcdr 4 carcdr a A)(car (cdr a)) B)(cdr (car a)) C)(cdr (cdr (car a))) D)(car (cdr (cdr a))) E)(cdr (car (car a)))

How do you get the 3? STk> (define a (list 1 2 (list 3 4) 5)) 1 carcdr 2 carcdrcarcdr 5 carcdr a 3 carcdr 4 carcdr A)(car (car (cdr (cdr a)))) B)(cdr (cdr (car (car a)))) C)(cdr (car (cdr (car a)))) D)(car (cdr (car (cdr a)))) E)???

Cons makes a pair (cons a b) AB carcdr

Dots

STk> (cons 1 2) (1. 2) STk> (cons 1 ‘()) (1) STk> (cons 1 ‘()) (1.( )) 1 carcdr 12 carcdr

Dots STk> (cons 1 ‘()) (1. ()) (1) STk> (cons 1 (cons 2 ‘())) (1. (2.( ))) (1 2) 1 carcdr 1 carcdr 2 carcdr