CSE332: Data Abstractions Lecture 2: Math Review; Algorithm Analysis Tyler Robison Summer 2010 1.

Slides:



Advertisements
Similar presentations
Analysis of Algorithms
Advertisements

Discrete Structures CISC 2315
Lecture: Algorithmic complexity
CSE 373: Data Structures and Algorithms Lecture 5: Math Review/Asymptotic Analysis III 1.
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 Dan Grossman Spring 2010.
Chapter 3 Growth of Functions
The Growth of Functions
Analysis of Algorithms (Chapter 4)
Algorithm Analysis CS 201 Fundamental Structures of Computer Science.
Elementary Data Structures and Algorithms
DATA STRUCTURES AND ALGORITHMS Lecture Notes 1 Prepared by İnanç TAHRALI.
CSE 373 Data Structures and Algorithms Lecture 4: Asymptotic Analysis II / Math Review.
CSE373: Data Structures and Algorithms Lecture 4: Asymptotic Analysis Aaron Bauer Winter 2014.
COMPSCI 102 Introduction to Discrete Mathematics.
1 Chapter 2 Program Performance – Part 2. 2 Step Counts Instead of accounting for the time spent on chosen operations, the step-count method accounts.
Algorithm Analysis. Algorithm Def An algorithm is a step-by-step procedure.
CS Algorithm Analysis1 Algorithm Analysis - CS 312 Professor Tony Martinez.
Program Efficiency and Complexity
Algorithm Analysis & Complexity We saw that a linear search used n comparisons in the worst case (for an array of size n) and binary search had logn comparisons.
Week 2 CS 361: Advanced Data Structures and Algorithms
Algorithm Efficiency CS 110: Data Structures and Algorithms First Semester,
Analysis of Algorithms
Mathematics Review and Asymptotic Notation
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.
1 Computer Algorithms Lecture 3 Asymptotic Notation Some of these slides are courtesy of D. Plaisted, UNC and M. Nicolescu, UNR.
CSE373: Data Structures and Algorithms Lecture 2b: Proof by Induction and Powers of Two Nicki Dell Spring 2014.
Iterative Algorithm Analysis & Asymptotic Notations
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
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-Ch. 3
Fundamentals CSE 373 Data Structures Lecture 5. 12/26/03Fundamentals - Lecture 52 Mathematical Background Today, we will review: ›Logs and exponents ›Series.
©Silberschatz, Korth and Sudarshan3.1 Algorithms Analysis Algorithm efficiency can be measured in terms of:  Time  Space  Other resources such as processors,
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.
CSE373: Data Structures and Algorithms Lecture 3: Math Review; Algorithm Analysis Lauren Milne Summer 2015.
CSE373: Data Structures and Algorithms Lecture 3: Math Review; Algorithm Analysis Nicki Dell Spring 2014.
Chapter 5 Algorithms (2) Introduction to CS 1 st Semester, 2015 Sanghyun Park.
Java Methods Big-O Analysis of Algorithms Object-Oriented Programming
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)
CSC – 332 Data Structures Generics Analysis of Algorithms Dr. Curry Guinn.
Introduction to Analysis of Algorithms CS342 S2004.
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 Lecture 4: Math Review/Asymptotic Analysis II 1.
CSE332: Data Abstractions Lecture 3: Asymptotic Analysis Tyler Robison Summer
1 Chapter 2 Algorithm Analysis All sections. 2 Complexity Analysis Measures efficiency (time and memory) of algorithms and programs –Can be used for the.
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.
1 Chapter 2 Algorithm Analysis Reading: Chapter 2.
Complexity of Algorithms Fundamental Data Structures and Algorithms Ananda Guna January 13, 2005.
Algorithmic Foundations COMP108 COMP108 Algorithmic Foundations Algorithm efficiency Prudence Wong.
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
CSE373: Data Structures and Algorithms Lecture 2: Math Review; Algorithm Analysis Kevin Quinn Fall 2015.
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
CSE373: Data Structures and Algorithms Lecture 2: Proof by Induction
Introduction to Algorithms Analysis
CSE373: Data Structures and Algorithms Lecture 2: Math Review; Algorithm Analysis Dan Grossman Fall 2013.
CSE 373 Data Structures Lecture 5
CSE 373: Data Structures & Algorithms
CSE 373 Optional Section Led by Yuanwei, Luyi Apr
Presentation transcript:

CSE332: Data Abstractions Lecture 2: Math Review; Algorithm Analysis Tyler Robison Summer

Proof via mathematical induction 2 Suppose P(n) is some rule involving n  Example: n ≥ n/2 + 1, for all n ≥2 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.)

Example 3 P(n) = “the sum of the first n powers of 2 (starting at ) is the next power of 2 minus 1” Theorem: P(n) holds for all n ≥ 1 1= = =8-1 So far so good…

Example 4 Theorem: P(n) holds for all n ≥ 1 Proof: By induction on n  Base case, n=1:  Inductive case:  Inductive hypothesis: Assume the sum of the first k powers of 2 is 2 k -1  Show, given the hypothesis, that the sum of the first (k+1) powers of 2 is 2 k+1 -1 From our inductive hypothesis we know: Add the next power of 2 to both sides… We have what we want on the left; massage the right a bit

Note for homework 5 Proofs by induction will come up a fair amount on the homework When doing them, be sure to state each part clearly:  What you’re trying to prove  The base case  The inductive case  The inductive hypothesis  In many inductive proofs, you’ll prove the inductive case by just starting with your inductive hypothesis, and playing with it a bit, as shown above

Powers of 2 6  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

Therefore… 7 We could give a unique id to…  Every person in this room with 4 bits  Every person in the U.S. with 29 bits  Every person in the world with 33 bits  Every person to have ever lived with 38 bits (estimate)  Every atom in the universe with bits So if a password is 128 bits long and randomly generated, do you think you could guess it?

Logarithms and Exponents 8  Since so much is binary in CS, log almost always means log 2  Definition: log 2 x = y if x = 2 y  So, log 2 1,000,000 = “a little under 20” Just as exponents grow very quickly, logarithms grow very slowly See Excel file for plot data – play with it!

Logarithms and Exponents 9

10

Properties of logarithms 11  log(A*B) = log A + log B  So log(N k )= k log N  log(A/B) = log A – log B  x=  log(log x) is written log log x  Grows as slowly as 2 2 grows fast  Ex:  (log x)(log x) is written log 2 x  It is greater than log x for all x > 2 y

Log base doesn’t matter (much) 12 “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  Say, to convert from base B to base A: log B x = (log A x) / (log A B)

Algorithm Analysis 13 As the “size” of an algorithm’s input grows (length of array to sort, size of queue to search, etc.):  How much longer does the algorithm take (time)  How much more memory does the algorithm need (space) We are generally concerned about approximate runtimes  Whether T(n)=3n+2 or T(n)=n/4+8, we say it runs in linear time  Common categories:  Constant: T(n)=1  Linear: T(n)=n  Logarithmic: T(n)=logn  Exponential: T(n)=2 n

Example 14  First, what does this pseudocode return? x := 0; for i=1 to n do for j=1 to i do x := x + 3; return x;  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 case:  Inductive hypothesis: 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

Example 15  How long does this pseudocode run? x := 0; for i=1 to n do for j=1 to i do x := x + 3; return x;  Find running time in terms of n, for any n ≥ 0  Assignments, additions, returns take “1 unit time”  Constant 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…

Example 16  How long does this pseudocode run? x := 0; for i=1 to n do for j=1 to i do x := x + 3; return x;  n=1 -> 1 time; n=2 -> 3 times; n=3 -> 6 times  The total number of loop iterations is n*(n+1)/2  You’ll get to prove it in the homework  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

Lower-order terms don’t matter 17 n*(n+1)/2 vs. just n 2 /2

Big Oh (also written Big-O) 18  Big Oh is used for comparing asymptotic behavior of functions; which is ‘faster’?  We’ll get into the definition later, but for now:  ‘f(n) is O(g(n))’ roughly means  The function f(n) is at least as small as g(n) as they go toward infinity  Think of it as ≤  BUT: Big Oh ignores constant factors  n+10 is O(n); we drop out the ‘+10’  5n is O(n); we drop out the ‘x5’  The following is NOT true though: n 2 is O(n)  Note that ‘f(n) is O(g(n))’ gives an upper bound for f(n)  n is O(n 2 )  5 is O(n)

Big Oh: Common Categories 19 From fastest to slowest 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) Usage note: “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?)