CS61A Lecture 6 2011-06-28 Colleen Lewis. Clicker Test How often do you read piazza posts? A)Whenever I receive an email B)Once or twice a day C)When.

Slides:



Advertisements
Similar presentations
Growth-rate Functions
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.
Lecture3: Algorithm Analysis Bohyung Han CSE, POSTECH CSED233: Data Structures (2014F)
HST 952 Computing for Biomedical Scientists Lecture 10.
Razdan with contribution from others 1 Algorithm Analysis What is the Big ‘O Bout? Anshuman Razdan Div of Computing.
CS50 SECTION: WEEK 3 Kenny Yu. Announcements  Watch Problem Set 3’s walkthrough online if you are having trouble.  Problem Set 1’s feedback have been.
Algorithms Recurrences. Definition – a recurrence is an equation or inequality that describes a function in terms of its value on smaller inputs Example.
Computational Complexity 1. Time Complexity 2. Space Complexity.
CSE332: Data Abstractions Lecture 2: Math Review; Algorithm Analysis Tyler Robison Summer
CSE332: Data Abstractions Lecture 2: Math Review; Algorithm Analysis Dan Grossman Spring 2010.
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.
CISC220 Spring 2010 James Atlas Lecture 06: Linked Lists (2), Big O Notation.
CS 206 Introduction to Computer Science II 09 / 05 / 2008 Instructor: Michael Eckmann.
1 Algorithm Efficiency, Big O Notation, and Role of Data Structures/ADTs Algorithm Efficiency Big O Notation Role of Data Structures Abstract Data Types.
CS503: First Lecture, Fall 2008 Michael Barnathan.
Complexity (Running Time)
CS 206 Introduction to Computer Science II 01 / 28 / 2009 Instructor: Michael Eckmann.
Data Structure Algorithm Analysis TA: Abbas Sarraf
CS2336: Computer Science II
Abstract Data Types (ADTs) Data Structures The Java Collections API
COMP s1 Computing 2 Complexity
CS10 Final Review by Glenn Sugden is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License.Glenn Sugdenmons Attribution-NonCommercial-ShareAlike.
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.
Analysis of Algorithms
CS61A Lecture Colleen Lewis. Clicker Query What else are you doing this summer? A)Taking another class B)Working C)Taking another class and.
Analysis of Algorithms CSCI Previous Evaluations of Programs Correctness – does the algorithm do what it is supposed to do? Generality – does it.
Algorithm Analysis (Algorithm Complexity). Correctness is Not Enough It isn’t sufficient that our algorithms perform the required tasks. We want them.
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.
Problem of the Day  I am thinking of a question and propose 3 possible answers. Exactly one of the following is the solution. Which is it? A. Answer 1.
CSE373: Data Structures and Algorithms Lecture 3: Math Review; Algorithm Analysis Nicki Dell Spring 2014.
3.3 Complexity of Algorithms
CMSC 341 Asymptotic Analysis. 2 Complexity How many resources will it take to solve a problem of a given size? –time –space Expressed as a function of.
Algorithm Analysis (Big O)
ANALYSING COSTS COMP 103. RECAP  ArrayList: add(), ensuring capacity, iterator for ArrayList TODAY  Analysing Costs 2 RECAP-TODAY.
CSE 373: Data Structures and Algorithms
27-Jan-16 Analysis of Algorithms. 2 Time and space To analyze an algorithm means: developing a formula for predicting how fast an algorithm is, based.
Searching Topics Sequential Search Binary Search.
CISC220 Spring 2010 James Atlas Lecture 07: Big O Notation.
A Introduction to Computing II Lecture 5: Complexity of Algorithms Fall Session 2000.
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.
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.
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.
Big O David Kauchak cs302 Spring Administrative Assignment 1: how’d it go? Assignment 2: out soon… Lab code.
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.
Section 1.7 Comparing Algorithms: Big-O Analysis.
1 Algorithms Searching and Sorting Algorithm Efficiency.
CSE373: Data Structures and Algorithms Lecture 3: Math Review; Algorithm Analysis Linda Shapiro Winter 2015.
Introduction to Analysing Costs 2013-T2 Lecture 10 School of Engineering and Computer Science, Victoria University of Wellington  Marcus Frean, Rashina.
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.
(Complexity) Analysis of Algorithms Algorithm Input Output 1Analysis of Algorithms.
Yahoo predicts contest already!
Introduction to Algorithms
Lecture 06: Linked Lists (2), Big O Notation
COMP 53 – Week Seven Big O Sorting.
David Kauchak CS52 – Spring 2015
Big-Oh and Execution Time: A Review
Algorithm design and Analysis
Big O Notation.
Searching, Sorting, and Asymptotic Complexity
Asymptotic complexity Searching/sorting
Asymptotic complexity
Analysis of Algorithms
Sum this up for me Let’s write a method to calculate the sum from 1 to some n public static int sum1(int n) { int sum = 0; for (int i = 1; i
David Kauchak cs161 Summer 2009
Algorithm Analysis How can we demonstrate that one algorithm is superior to another without being misled by any of the following problems: Special cases.
Presentation transcript:

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 I have a question Current average response time: 6 minutes!

How long does a function take to run? It depends on what computer it is run on!

Assumptions We want something independent of the speed of the computer We typically use N which is some reference to the “size” of the data. – It might be the variable N (in factorial) – It might be the size of your sentence We don’t care about constant factors – This is pretty silly in some cases but makes the math more beautiful We typically think about the worst case! It only matters for BIG input!

Colleen’s Morning Routine Eat breakfast (10 minutes) Brush teeth (5 minutes) Go swimming Read Piazza (0 minutes – ???) Takes a consistent or “constant” amount of time Depends “linearly” upon the number of laps L Depends upon the number of posts P and the words per post W

Constant Runtime O(1)

Constant Runtime The runtime doesn’t depend upon the “size” of the input (define (square x) (* x x)) (define (average a b) (/ (+ a b) 2)) (define (max val1 val2) (if (> val1 val2) val1 val2))

Demo

Assumptions We want something independent of the speed of the computer We typically use N which is some reference to the “size” of the data. – It might be the variable N (in factorial) – It might be the size of your sentence We don’t care about constant factors – This is pretty silly in some cases but makes the math more beautiful We typically think about the worst case! It only matters for BIG input!

Write a constant runtime procedure? (Brushing your teeth)

Linear Runtime O(N)

Linear Runtime The runtime DOES depend upon the “size” of the input – but just with a linear relationship (define (sum-up-to x) (if (< x 0) 0 (+ x (sum-up-to (- x 1))))) (define (count sent) (if (empty? sent) sent (+ 1 (count (bf sent)))))

Demo

Assumptions We want something independent of the speed of the computer We typically use N which is some reference to the “size” of the data. – It might be the variable N (in factorial) – It might be the size of your sentence We don’t care about constant factors – This is pretty silly in some cases but makes the math more beautiful We typically think about the worst case! It only matters for BIG input!

Write a linear runtime procedure? (Swimming laps)

What is the runtime of this? (define (factorial n) (if (= n 0) 1 (* n (factorial (- n 1))))) A)Constant runtime B)Linear runtime C)Linear*Linear (quadratic) D)Something else E)No clue! Correct Answer

Demo

Problem Solving Tip! THINK: Does this depend upon the “size” of the input? – Constant vs. Not Constant Think about this for ANY function that is called!

Linear * Linear (Quadratic) Runtime O(N 2 )

Linear * Linear Runtime (quadratic) (Reading Piazza) The runtime DOES depend upon the “size” of the first input For each thing in the first input, we call another linear thing (define (factorial-sent sent) (if (empty? sent) sent (se (factorial (first sent)) (factorial-sent (bf sent) ))))

Demo

Assumptions We want something independent of the speed of the computer We typically use N which is some reference to the “size” of the data. – It might be the variable N (in factorial) – It might be the size of your sentence We don’t care about constant factors – This is pretty silly in some cases but makes the math more beautiful We typically think about the worst case! It only matters for BIG input!

Linear * Linear Runtime (quadratic) (Reading Piazza) (define (factorial-sent sent) (if (empty? sent) sent (se (factorial (first sent)) (factorial-sent (bf sent) )))) Runtime: O( sent-length * value-of-elements )

(define (factorial-sent sent) (if (empty? sent) sent (se (factorial (first sent)) (factorial-sent (bf sent) )))) (define (square-sent sent) (if (empty? sent) sent (se (square (first sent)) (square-sent (bf sent)))))

Write a linear*linear (quadratic) runtime procedure? (Reading Piazza)

Bad Variable Names (Slight break from runtimes)

What is the runtime of this? (define (mystery a b) (cond ((empty? b) (se a b)) ((< a (first b)) (se a b)) (else (se (first b) (mystery a (bf b)))))) A) Constant runtime B) Linear runtime C) Linear*Linear (quadratic) D) Something else E) No clue Correct Answer Try to guess what type of thing the variables a & b will be Try to come up with (small) input that triggers each case

Bad variable names! Make your code harder to read! – Don’t do this! Even though we do – We will use bad variable names on exams so that you have to read/understand the code

Better variable names! (define (insert num sent) (cond ((empty? sent) (se num sent)) ((< num (first sent)) (se num sent)) (else (se (first sent) (insert num (bf sent)))))) STk>(insert 7 ’ ()) () STk>(insert 1 ’ (2 5 8)) ( )

Insert STk>(insert 8 ’ ( )) ( ) (se 8 ’ (9)) (insert 8 ’ ( )) (se 2 (insert 8 ’ (5 6 9)) (se 5 (insert 8 ’ (6 9)) (se 6 (insert 8 ’ (9)) (se 2 (se 5 (se 6 (se 8 ’ (9)))))

Best vs. Worst Case (define (insert num sent) (cond ((empty? sent) (se num sent)) ((< num (first sent)) (se num sent)) (else (se (first sent) (insert num (bf sent)))))) STk>(insert 1 ’ ( )) ( ) STk>(insert 9 ’ ( )) ( ) BEST CASE WORST CASE

Assumptions We want something independent of the speed of the computer We typically use N which is some reference to the “size” of the data. – It might be the variable N (in factorial) – It might be the size of your sentence We don’t care about constant factors – This is pretty silly in some cases but makes the math more beautiful We typically think about the worst case! It only matters for BIG input!

What is the runtime of this? (define (sort sent) (if (empty? sent) sent (insert (first sent) (sort (bf sent))))) A)Constant runtime B)Linear runtime C)Linear*Linear (quadratic) D)Something else E)No clue! Correct Answer

Sort STk>(sort ’ (8 3 9)) (3 8 9) ’ () (sort ’ (8 3 9)) (insert 8 (sort ’ (3 9)) (insert 3 (sort ’ (9)) (insert 9 (sort ’ ()) (insert 8 (insert 3 (insert 9 ’ ())))

(insert 3 (insert 6 (insert 4 (insert 7 (insert 1 (insert 8 (insert 3 (insert 9 ’ () )))))))) (sort sent) if sent length is N … + (N-1)+ N Add to sent size 0 Add to sent size 1 Add to sent size 2 Add to sent size 3 Add to sent size 4 Add to sent size 5 Add to sent size 6 Add to sent size 7

// Written backwards Some Algebra to calculate: 1+2+…+n

OR Some Algebra to calculate: 1+2+…+n

How long does it take you to read Piazza posts and check your and shower? P is the number of posts W p is the number of words per post P E is the number of s W e is the number of words per E S is the number of minutes you shower a)P*W p *E*W e *S b)P+W p +E+W e +S c)P*W p +E*W e +S d)P+E+S e)P*E*S Correct Answer

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)

branch = 2 calls 2 branches = 3 3 branches = 7 4 branches = 15 N branches = 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)

// 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?

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

if and only if for all n > N Formal definition

Which is fastest after some big value 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

Write sum-up-to to generate an iterative process! (define (sum-up-to x) (define (sum-up-to-iter x answer) (if (= x 0) answer (sum-up-to-iter (- x 1) (+ answer x)))) (sum-up-to-iter x 0))