Announcements. CSE332: Data Abstractions Lecture 2: Math Review; Algorithm Analysis. Today. Mathematical induction. Dan Grossman Spring 2010

Similar documents
CSE373: Data Structures and Algorithms Lecture 2: Math Review; Algorithm Analysis. Hunter Zahn Summer 2016

CSE373: Data Structures and Algorithms Lecture 3: Math Review; Algorithm Analysis. Catie Baker Spring 2015

CSE332: Data Structures & Parallelism Lecture 2: Algorithm Analysis. Ruth Anderson Winter 2018

CSE332: Data Structures & Parallelism Lecture 2: Algorithm Analysis. Ruth Anderson Winter 2018

CSE332: Data Structures & Parallelism Lecture 2: Algorithm Analysis. Ruth Anderson Winter 2019

CSE373: Data Structures and Algorithms Lecture 2: Proof by & Algorithm Analysis. Lauren Milne Summer 2015

CSE 373: Data Structures and Algorithms Pep Talk; Algorithm Analysis. Riley Porter Winter 2017

ASYMPTOTIC COMPLEXITY SEARCHING/SORTING

CS 310 Advanced Data Structures and Algorithms

Lecture 2. More Algorithm Analysis, Math and MCSS By: Sarah Buchanan

Growth of Functions (CLRS 2.3,3)

CSC236 Week 4. Larry Zhang

Topic 17. Analysis of Algorithms

Taking Stock. IE170: Algorithms in Systems Engineering: Lecture 3. Θ Notation. Comparing Algorithms

Introduction. How can we say that one algorithm performs better than another? Quantify the resources required to execute:

CS383, Algorithms Spring 2009 HW1 Solutions

Mathematical Background. Unsigned binary numbers. Powers of 2. Logs and exponents. Mathematical Background. Today, we will review:

Lecture 1: Asymptotic Complexity. 1 These slides include material originally prepared by Dr.Ron Cytron, Dr. Jeremy Buhler, and Dr. Steve Cole.

CS1210 Lecture 23 March 8, 2019

Data Structures and Algorithms

CSC2100B Data Structures Analysis

Announcements. CompSci 102 Discrete Math for Computer Science. Chap. 3.1 Algorithms. Specifying Algorithms

Computer Algorithms CISC4080 CIS, Fordham Univ. Outline. Last class. Instructor: X. Zhang Lecture 2

Computer Algorithms CISC4080 CIS, Fordham Univ. Instructor: X. Zhang Lecture 2

CSE 373: Data Structures and Algorithms. Asymptotic Analysis. Autumn Shrirang (Shri) Mare

Asymptotic Notation. such that t(n) cf(n) for all n n 0. for some positive real constant c and integer threshold n 0

CS 4407 Algorithms Lecture 2: Iterative and Divide and Conquer Algorithms

Big O 2/14/13. Administrative. Does it terminate? David Kauchak cs302 Spring 2013

Lecture 3. Big-O notation, more recurrences!!

Ch 01. Analysis of Algorithms

CSE 311: Foundations of Computing. Lecture 14: Induction

Analysis of Algorithm Efficiency. Dr. Yingwu Zhu

UCSD CSE 21, Spring 2014 [Section B00] Mathematics for Algorithm and System Analysis

Asymptotic Algorithm Analysis & Sorting

Ch01. Analysis of Algorithms

Asymptotic Running Time of Algorithms

Recursion: Introduction and Correctness

Algorithm efficiency analysis

Asymptotic Analysis 1

When we use asymptotic notation within an expression, the asymptotic notation is shorthand for an unspecified function satisfying the relation:

Algorithm Analysis, Asymptotic notations CISC4080 CIS, Fordham Univ. Instructor: X. Zhang

Computational Complexity - Pseudocode and Recursions

Fall Lecture 5

CISC 235: Topic 1. Complexity of Iterative Algorithms

Adam Blank Spring 2017 CSE 311. Foundations of Computing I. * All slides are a combined effort between previous instructors of the course

What is Performance Analysis?

The Time Complexity of an Algorithm

Data Structures and Algorithms. Asymptotic notation

Math 391: Midterm 1.0 Spring 2016

CSC 5170: Theory of Computational Complexity Lecture 4 The Chinese University of Hong Kong 1 February 2010

Time-bounded computations

Lecture 2. Fundamentals of the Analysis of Algorithm Efficiency

An analogy from Calculus: limits

Lecture 2: Asymptotic Notation CSCI Algorithms I

CSE373: Data Structures and Algorithms Lecture 2: Proof by Induction. Linda Shapiro Spring 2016

Algorithms and Their Complexity

Lecture 1: Asymptotics, Recurrences, Elementary Sorting

CSED233: Data Structures (2017F) Lecture4: Analysis of Algorithms

3.1 Asymptotic notation

Computational complexity and some Graph Theory

CSE 241 Class 1. Jeremy Buhler. August 24,

It ain t no good if it ain t snappy enough. (Efficient Computations) COS 116, Spring 2011 Sanjeev Arora

CS 161 Summer 2009 Homework #2 Sample Solutions

4.4 Recurrences and Big-O

Analysis of Algorithms [Reading: CLRS 2.2, 3] Laura Toma, csci2200, Bowdoin College

CSC236 Week 3. Larry Zhang

Reading 10 : Asymptotic Analysis

Remainders. We learned how to multiply and divide in elementary

Math101, Sections 2 and 3, Spring 2008 Review Sheet for Exam #2:

UCSD CSE 21, Spring 2014 [Section B00] Mathematics for Algorithm and System Analysis

Computational Complexity. This lecture. Notes. Lecture 02 - Basic Complexity Analysis. Tom Kelsey & Susmit Sarkar. Notes

Running Time Evaluation

MA008/MIIZ01 Design and Analysis of Algorithms Lecture Notes 2

Chapter 2: The Basics. slides 2017, David Doty ECS 220: Theory of Computation based on The Nature of Computation by Moore and Mertens

CIS 121. Analysis of Algorithms & Computational Complexity. Slides based on materials provided by Mary Wootters (Stanford University)

CSE 101. Algorithm Design and Analysis Miles Jones Office 4208 CSE Building Lecture 1: Introduction

When we use asymptotic notation within an expression, the asymptotic notation is shorthand for an unspecified function satisfying the relation:

Great Theoretical Ideas in Computer Science. Lecture 7: Introduction to Computational Complexity

Submit Growable Array exercise Answer Q1-3 from today's in-class quiz.

MATH 22 FUNCTIONS: ORDER OF GROWTH. Lecture O: 10/21/2003. The old order changeth, yielding place to new. Tennyson, Idylls of the King

Lecture 6: Recurrent Algorithms:

Design of Digital Circuits Reading: Binary Numbers. Required Reading for Week February 2017 Spring 2017

CS Data Structures and Algorithm Analysis

CS 4104 Data and Algorithm Analysis. Recurrence Relations. Modeling Recursive Function Cost. Solving Recurrences. Clifford A. Shaffer.

Algorithm. Executing the Max algorithm. Algorithm and Growth of Functions Benchaporn Jantarakongkul. (algorithm) ก ก. : ก {a i }=a 1,,a n a i N,

Analysis of Algorithms

Warm-up Simple methods Linear recurrences. Solving recurrences. Misha Lavrov. ARML Practice 2/2/2014

What if the characteristic equation has a double root?

Algorithms Design & Analysis. Analysis of Algorithm

How many hours would you estimate that you spent on this assignment?

Analysis of Algorithms

Module 1: Analyzing the Efficiency of Algorithms

Loop Convergence. CS 536: Science of Programming, Fall 2018

Divide and Conquer Algorithms. CSE 101: Design and Analysis of Algorithms Lecture 14

Data structures Exercise 1 solution. Question 1. Let s start by writing all the functions in big O notation:

NP, polynomial-time mapping reductions, and NP-completeness

5 + 9(10) + 3(100) + 0(1000) + 2(10000) =

Data Structures and Algorithms

Introduction to Machine Learning

Nondeterministic finite automata

Transcription:

Announcements CSE332: Data Abstractions Lecture 2: Math Review; Algorithm Analysis Dan Grossman Spring 2010 Project 1 posted Section materials on using Eclipse will be very useful if you have never used it (Could also start in a different environment if necessary) Section material on generics will be very useful for Phase B Homework 1 posted Feedback on typos is welcome Won t announce every time I update posted materials with minor fixes 2 Today Mathematical induction Finish discussing queues Review math essential to algorithm analysis Proof by induction Powers of 2 Exponents and logarithms Begin analyzing algorithms Using asymptotic analysis (continue next time) Suppose P(n) is some predicate (mentioning integer n) : n n/2 + 1 To prove P(n) for all integers n c, it suffices to prove 1. P(c) called the basis or base case 2. If P(k) then P(k+1) called the induction step or inductive case Why we will care: To show an algorithm is correct or has a certain running time no matter how big a data structure or input value is (Our n will be the data structure or input size.) 3 4

Powers of 2 P(n) = the sum of the first n powers of 2 (starting at 0) is 2 n -1 Theorem: P(n) holds for all n 1 Proof: By induction on n Base case: n=1. Sum of first 1 power of 2 is 2 0, which equals 1. And for n=1, 2 n -1 equals 1. Inductive case: Assume the sum of the first k powers of 2 is 2 k -1 Show the sum of the first (k+1) powers of 2 is 2 k+1-1 Using assumption, sum of the first (k+1) powers of 2 is (2 k -1) + 2 (k+1)-1 = (2 k -1) + 2 k = 2 k+1-1 A bit is 0 or 1 A sequence of n bits can represent 2 n distinct things For example, the numbers 0 through 2 n -1 2 10 is 1024 ( about a thousand, kilo in CSE speak) 2 20 is about a million, mega in CSE speak 2 30 is about a billion, giga in CSE speak Java: an int is 32 bits and signed, so max int is about 2 billion a long is 64 bits and signed, so max long is 2 63-1 5 6 Therefore Could give a unique id to Every person in the U.S. with 29 bits Every person in the world with 33 bits Since so much is binary in CS log almost always means log 2 Every person to have ever lived with 38 bits (estimate) Every atom in the universe with 250-300 bits So if a password is 128 bits long and randomly generated, do you think you could guess it? 7 8

Since so much is binary log in CS almost always means log 2 Since so much is binary log in CS almost always means log 2 9 10 Properties of logarithms Since so much is binary log in CS almost always means log 2 log(a*b) = log A + log B So log(n k )= k log N log(a/b) = log A log B log(log x) is written log log x Grows as slowly as 2 2 y grows fast (log x)(log x) is written log 2 x It is greater than log x for all x > 2 11 12

Log base doesn t matter much! Algorithm Analysis Any base B log is equivalent to base 2 log within a constant factor And we are about to stop worrying about constant factors! In particular, log 2 x = 3.22 log 10 x In general, log B x = (log A x) / (log A B) As the size of an algorithm s input grows (integer, length of array, size of queue, etc.): How much longer does the algorithm take (time) How much more memory does the algorithm need (space) Because the curves we saw are so different, we often only care about which curve we are like Separate issue: Algorithm correctness does it produce the right answer for all inputs Usually more important, naturally 13 14 What does this pseudocode return? Correctness: For any N 0, it returns 15 What does this pseudocode return? Correctness: For any N 0, it returns 3N(N+1)/2 Proof: By induction on n P(n) = after outer for-loop executes n times, x holds 3n(n+1)/2 Base: n=0, returns 0 Inductive: From P(k), x holds 3k(k+1)/2 after k iterations. Next iteration adds 3(k+1), for total of 3k(k+1)/2 + 3(k+1) = (3k(k+1) + 6(k+1))/2 = (k+1)(3k+6)/2 = 3(k+1)(k+2)/2 16

How long does this pseudocode run? Running time: For any N 0, Assignments, additions, returns take 1 unit time Loops take the sum of the time for their iterations So: 2 + 2*(number of times inner loop runs) And how many times is that How long does this pseudocode run? The total number of loop iterations is N*(N+1)/2 This is a very common loop structure, worth memorizing Proof is by induction on N, known for centuries This is proportional to N 2, and we say O(N 2 ), big-oh of For large enough N, the N and constant terms are irrelevant, as are the first assignment and return See plot N*(N+1)/2 vs. just N 2 /2 17 18 Lower-order terms don t matter Recurrence Equations N*(N+1)/2 vs. just N 2 /2 For running time, what the loops did was irrelevant, it was how many times they executed. Running time as a function of input size n (here loop bound): T(n) = n + T(n-1) (and T(0) = 2ish, but usually implicit that T(0) is some constant) Any algorithm with running time described by this formula is O(n 2 ) Big-Oh notation also ignores the constant factor on the highorder term, so 3N 2 and 17N 2 and (1/1000) N 2 are all O(N 2 ) As N grows large enough, no smaller term matters Next time: Many more examples + formal definitions 19 20

Big-O: Common Names O(1) constant (same as O(k) for constant k) O(log n) logarithmic O(n) linear O(n log n) n log n O(n 2 ) quadratic O(n 3 ) cubic O(n k ) polynomial (where is k is an constant) O(k n ) exponential (where k is any constant > 1) Pet peeve: exponential does not mean grows really fast, it means grows at rate proportional to k n for some k>1 A savings account accrues interest exponentially (k=1.01?) If you don t know k, you probably don t know it s exponential 21