CSE373: Data Structures and Algorithms Lecture 3: Math Review; Algorithm Analysis Nicki Dell Spring 2014.

Slides:



Advertisements
Similar presentations
Intro to Analysis of Algorithms. Algorithm “A sequence of unambiguous instructions for solving a problem, i.e., for obtaining a required output for any.
Advertisements

Lecture: Algorithmic complexity
CSE 373: Data Structures and Algorithms Lecture 5: Math Review/Asymptotic Analysis III 1.
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
CSE332: Data Abstractions Lecture 3: Asymptotic Analysis Dan Grossman Spring 2010.
CSE332: Data Abstractions Lecture 2: Math Review; Algorithm Analysis Dan Grossman Spring 2010.
Chapter 3 Growth of Functions
CSE 326 Asymptotic Analysis David Kaplan Dept of Computer Science & Engineering Autumn 2001.
Introduction to Analysis of Algorithms
CS503: First Lecture, Fall 2008 Michael Barnathan.
Algorithm Analysis CS 201 Fundamental Structures of Computer Science.
CS 206 Introduction to Computer Science II 01 / 28 / 2009 Instructor: Michael Eckmann.
Analysis of Algorithms COMP171 Fall Analysis of Algorithms / Slide 2 Introduction * What is Algorithm? n a clearly specified set of simple instructions.
CSE332: Data Abstractions Lecture 3: Asymptotic Analysis Dan Grossman Spring 2012.
CSE373: Data Structures and Algorithms Lecture 4: Asymptotic Analysis Aaron Bauer Winter 2014.
COMPSCI 102 Introduction to Discrete Mathematics.
1 ©2008 DEEDS Group Introduction to Computer Science 2 - SS 08 Asymptotic Complexity Introduction in Computer Science 2 Asymptotic Complexity DEEDS Group.
For Wednesday Read Weiss chapter 3, sections 1-5. This should be largely review. If you’re struggling with the C++ aspects, you may refer to Savitch, chapter.
Algorithm Efficiency CS 110: Data Structures and Algorithms First Semester,
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.
CSE373: Data Structures and Algorithms Lecture 2b: Proof by Induction and Powers of Two Nicki Dell Spring 2014.
CS 3343: Analysis of Algorithms
1 Chapter 1 Analysis Basics. 2 Chapter Outline What is analysis? What to count and consider Mathematical background Rates of growth Tournament method.
CSE373: Data Structures and Algorithms Lecture 2: Proof by Induction & Algorithm Analysis Lauren Milne Summer 2015.
DISCRETE MATHEMATICS I CHAPTER 11 Dr. Adam Anthony Spring 2011 Some material adapted from lecture notes provided by Dr. Chungsim Han and Dr. Sam Lomonaco.
Algorithm Analysis An algorithm is a clearly specified set of simple instructions to be followed to solve a problem. Three questions for algorithm analysis.
Analysis of Algorithms
Asymptotic Analysis-Ch. 3
CSE373: Data Structures and Algorithms Lecture 4: Asymptotic Analysis Catie Baker Spring 2015.
Fundamentals CSE 373 Data Structures Lecture 5. 12/26/03Fundamentals - Lecture 52 Mathematical Background Today, we will review: ›Logs and exponents ›Series.
Week 12 - Wednesday.  What did we talk about last time?  Asymptotic notation.
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.
Mathematical Background and Linked Lists. 2 Iterative Algorithm for Sum Find the sum of the first n integers stored in an array v : sum (v[], n) temp_sum.
CSE373: Data Structures and Algorithms Lecture 3: Math Review; Algorithm Analysis Lauren Milne Summer 2015.
Geoff Holmes and Bernhard Pfahringer COMP206-08S General Programming 2.
CSE373: Data Structures and Algorithms Lecture 3: Math Review; Algorithm Analysis Lauren Milne Summer 2015.
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.
Asymptotic Analysis (based on slides used at UMBC)
Introduction to Analysis of Algorithms CS342 S2004.
CS 2601 Runtime Analysis Big O, Θ, some simple sums. (see section 1.2 for motivation) Notes, examples and code adapted from Data Structures and Other Objects.
Analysis of algorithms. What are we going to learn? Need to say that some algorithms are “better” than others Criteria for evaluation Structure of programs.
Algorithm Analysis (Big O)
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.
CSE332: Data Abstractions Lecture 3: Asymptotic Analysis Tyler Robison Summer
Big O David Kauchak cs302 Spring Administrative Assignment 1: how’d it go? Assignment 2: out soon… Lab code.
Exponents – Logarithms xy -31/8 -2¼ ½ xy 1/8-3 ¼-2 ½ The function on the right is the inverse of the function on the left.
CSE373: Data Structures and Algorithms Lecture 4: Asymptotic Analysis Linda Shapiro Winter 2015.
Complexity of Algorithms Fundamental Data Structures and Algorithms Ananda Guna January 13, 2005.
Chapter 15 Running Time Analysis. Topics Orders of Magnitude and Big-Oh Notation Running Time Analysis of Algorithms –Counting Statements –Evaluating.
CSE373: Data Structures and Algorithms Lecture 3: Math Review; Algorithm Analysis Linda Shapiro Winter 2015.
Algorithmic Foundations COMP108 COMP108 Algorithmic Foundations Algorithm efficiency Prudence Wong
Algorithm Analysis 1.
CSE373: Data Structures and Algorithms Lecture 2: Math Review; Algorithm Analysis Kevin Quinn Fall 2015.
Algorithmic Efficency
CSE 373: Data Structures and Algorithms Pep Talk; Algorithm Analysis
CSE373: Data Structures and Algorithms Lecture 3: Math Review; Algorithm Analysis Catie Baker Spring 2015.
David Kauchak CS52 – Spring 2015
Instructor: Lilian de Greef Quarter: Summer 2017
Introduction to Algorithms Analysis
CSE373: Data Structures and Algorithms Lecture 2: Math Review; Algorithm Analysis Dan Grossman Fall 2013.
CS 201 Fundamental Structures of Computer Science
CSE 373 Data Structures Lecture 5
CSE 373: Data Structures & Algorithms
Asymptotic complexity Searching/sorting
Mathematical Background 2
CSE 373 Optional Section Led by Yuanwei, Luyi Apr
Presentation transcript:

CSE373: Data Structures and Algorithms Lecture 3: Math Review; Algorithm Analysis Nicki Dell Spring 2014

Today Registration should be done. Homework 1 due 11pm next Wednesday, April 9 th. Review math essential to algorithm analysis –Proof by induction (another example) –Exponents and logarithms –Floor and ceiling functions Begin algorithm analysis CSE 373 Spring 20142

Mathematical induction Suppose P(n) is some statement (mentioning integer n) Example: n ≥ n/2 + 1 We can use induction to prove P(n) for all integers n ≥ n 0. We need to 1.Prove the “base case” i.e. P(n 0 ). For us n 0 is usually 1. 2.Assume the statement holds for P(k). 3.Prove the “inductive case” i.e. if P(k) is true, then P(k+1) is true. Why we 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.) CSE 373 Spring 20143

Example P(n) = “the sum of the first n powers of 2 (starting at 2 0 ) is 2 n -1” … + 2 n-1 = 2 n - 1. in other words: … + 2 n-1 = 2 n - 1. CSE 373 Spring 20144

Example P(n) = “the sum of the first n powers of 2 (starting at 2 0 ) is 2 n -1” We will show that 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. CSE 373 Spring 20145

Example P(n) = “the sum of the first n powers of 2 (starting at 2 0 ) is 2 n -1” Inductive case: –Assume P(k) is true i.e. the sum of the first k powers of 2 is 2 k -1 –Show P(k+1) is true i.e. the sum of the first (k+1) powers of 2 is 2 k+1 -1 Using our assumption, we know the first k powers of 2 is … + 2 k-1 = 2 k - 1 Add the next power of 2 to both sides… … + 2 k k = 2 k k We have what we want on the left; massage the right a bit: … + 2 k k = 2(2 k ) – 1 = 2 k+1 – 1 Success! CSE 373 Spring 20146

Logarithms and Exponents Definition: x = 2 y if log 2 x = y –8 = 2 3, so log 2 8 = 3 –65536= 2 16, so log = 16 The exponent of a number says how many times to use the number in a multiplication. e.g. 2 3 = 2 × 2 × 2 = 8 (2 is used 3 times in a multiplication to get 8) A logarithm says how many of one number to multiply to get another number. It asks "what exponent produced this?” e.g. log 2 8 = 3 (2 makes 8 when used 3 times in a multiplication) CSE 373 Spring 20147

Logarithms and Exponents Definition: x = 2 y if log 2 x = y –8 = 2 3, so log 2 8 = 3 –65536= 2 16, so log = 16 Since so much is binary in CS, log almost always means log 2 log 2 n tells you how many bits needed to represent n combinations. So, log 2 1,000,000 = “a little under 20” Logarithms and exponents are inverse functions. Just as exponents grow very quickly, logarithms grow very slowly. CSE 373 Spring 20148

Logarithms and Exponents See Excel file for plot data – play with it! n CSE 373 Spring 20149

Logarithms and Exponents See Excel file for plot data – play with it! n CSE 373 Spring

Logarithms and Exponents See Excel file for plot data – play with it! n CSE 373 Spring

Logarithms and Exponents See Excel file for plot data – play with it! n CSE 373 Spring

Properties of logarithms log(A*B) = log A + log B 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 grows quickly (log x)(log x) is written log 2 x –It is greater than log x for all x > 2 –It is not the same as log log x y CSE 373 Spring

Log base doesn’t matter much! “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 we can convert log bases via a constant multiplier –To convert from base B to base A: log B x = (log A x) / (log A B) CSE 373 Spring

Floor and ceiling Floor function: the largest integer < X Ceiling function: the smallest integer > X CSE 373 Spring

Facts about floor and ceiling CSE 373 Spring

Algorithm Analysis As the “size” of an algorithm’s input grows (integer, length of array, size of queue, etc.), we want to know –How much longer does the algorithm take to run? (time) –How much more memory does the algorithm need? (space) Because the curves we saw are so different, often care about only “which curve we are like” Separate issue: Algorithm correctness – does it produce the right answer for all inputs –Usually more important, naturally CSE 373 Spring

Algorithm Analysis: A first example Consider the following program segment: x:= 0; for i = 1 to n do for j = 1 to i do x := x + 1; What is the value of x at the end? 11 to to to to 4 10 … n 1 to n ? = … + (n-1) + n i j x Number of times x gets incremented is = n*(n+1)/2 CSE 373 Spring

Analyzing the loop Consider the following program segment: x:= 0; for i = 1 to n do for j = 1 to i do x := x + 1; The total number of loop iterations is n*(n+1)/2 –This is a very common loop structure, worth memorizing –This is proportional to n 2, and we say O(n 2 ), “big-Oh of” n*(n+1)/2 = (n 2 + n)/2 For large enough n, the lower order and constant terms are irrelevant, as are the assignment statements See plot… (n 2 + n)/2 vs. just n 2 /2 CSE 373 Spring

Lower-order terms don’t matter n*(n+ 1)/2 vs. just n 2 /2 We just say O(n 2 ) CSE 373 Spring

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 any constant) O(k n )exponential (where k is any constant > 1) O(n!)factorial Note: “exponential” does not mean “grows really fast”, it means “grows at rate proportional to k n for some k>1” CSE 373 Spring

Big-O running times For a processor capable of one million instructions per second CSE 373 Spring

Analyzing code Basic operations take “some amount of” constant time –Arithmetic (fixed-width) –Assignment –Access one Java field or array index –Etc. (This is an approximation of reality: a very useful “lie”.) Consecutive statementsSum of times ConditionalsTime of test plus slower branch LoopsSum of iterations CallsTime of call’s body RecursionSolve recurrence equation (next lecture) CSE 373 Spring

Analyzing code 1.Add up time for all parts of the algorithm e.g. number of iterations = (n 2 + n)/2 2.Eliminate low-order terms i.e. eliminate n: (n 2 )/2 3.Eliminate coefficients i.e. eliminate 1/2: (n 2 ) Examples: –4n + 5 –0.5n log n + 2n + 7 –n n + 3n – n log (10n 2 ) 2n log (10n) = O(n) = O(n log n) = O(2 n ) = O(n log n) CSE 373 Spring