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

Slides:



Advertisements
Similar presentations
Analysis of Algorithms
Advertisements

CSE 373: Data Structures and Algorithms Lecture 5: Math Review/Asymptotic Analysis III 1.
HST 952 Computing for Biomedical Scientists Lecture 10.
Algorithms Recurrences. Definition – a recurrence is an equation or inequality that describes a function in terms of its value on smaller inputs Example.
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.
Chapter 3 Growth of Functions
CS 307 Fundamentals of Computer Science 1 Asymptotic Analysis of Algorithms (how to evaluate your programming power tools) based on presentation material.
Analysis of Algorithms (Chapter 4)
© 2006 Pearson Addison-Wesley. All rights reserved6-1 More on Recursion.
CS503: First Lecture, Fall 2008 Michael Barnathan.
Algorithm Analysis CS 201 Fundamental Structures of Computer Science.
Elementary Data Structures and Algorithms
CS2336: Computer Science II
Search Lesson CS1313 Spring Search Lesson Outline 1.Searching Lesson Outline 2.How to Find a Value in an Array? 3.Linear Search 4.Linear Search.
Analysis of Algorithms Spring 2015CS202 - Fundamentals of Computer Science II1.
DATA STRUCTURES AND ALGORITHMS Lecture Notes 1 Prepared by İnanç TAHRALI.
CSE373: Data Structures and Algorithms Lecture 4: Asymptotic Analysis Aaron Bauer Winter 2014.
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.
Week 2 CS 361: Advanced Data Structures and Algorithms
1 ©2008 DEEDS Group Introduction to Computer Science 2 - SS 08 Asymptotic Complexity Introduction in Computer Science 2 Asymptotic Complexity DEEDS Group.
Discrete Mathematics Algorithms. Introduction  An algorithm is a finite set of instructions with the following characteristics:  Precision: steps are.
Mon 29 Sep 2014Lecture 4 1. Running Time Performance analysis Techniques until now: Experimental Cost models counting execution of operations or lines.
Algorithm Efficiency CS 110: Data Structures and Algorithms First Semester,
Analysis of Algorithms
CSE373: Data Structures and Algorithms Lecture 2b: Proof by Induction and Powers of Two Nicki Dell Spring 2014.
CS 3343: Analysis of Algorithms
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.
Data Structures and Algorithms in Java Chapter 2 Complexity 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.
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 Nicki Dell Spring 2014.
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.
CSC – 332 Data Structures Generics Analysis of Algorithms Dr. Curry Guinn.
Introduction to Analysis of Algorithms CS342 S2004.
Algorithmic Analysis Charl du Plessis and Robert Ketteringham.
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.
1/6/20161 CS 3343: Analysis of Algorithms Lecture 2: Asymptotic Notations.
CSE 373: Data Structures and Algorithms
Complexity of Algorithms Fundamental Data Structures and Algorithms Ananda Guna January 13, 2005.
Analysis of Algorithms Spring 2016CS202 - Fundamentals of Computer Science II1.
CSE373: Data Structures and Algorithms Lecture 3: Math Review; Algorithm Analysis Linda Shapiro Winter 2015.
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu Lecture 2.
Algorithm Analysis 1.
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
CS 3343: Analysis of Algorithms
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
CS210- Lecture 2 Jun 2, 2005 Announcements Questions
Presentation transcript:

CSE332: Data Abstractions Lecture 2: Math Review; Algorithm Analysis Dan Grossman Spring 2010

Announcements 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 Spring 20102CSE332: Data Abstractions

Today 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) Spring 20103CSE332: Data Abstractions

Mathematical induction Suppose P(n) is some predicate (mentioning integer n) –Example: 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.) Spring 20104CSE332: Data Abstractions

Example 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 Spring 20105CSE332: Data Abstractions

Powers of 2 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 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 Spring 20106CSE332: Data Abstractions

Therefore… Could give a unique id to… 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? Spring 20107CSE332: Data Abstractions

Logarithms and Exponents 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 Spring 20108CSE332: Data Abstractions See Excel file for plot data – play with it!

Logarithms and Exponents Since so much is binary log in CS 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 Spring 20109CSE332: Data Abstractions See Excel file for plot data – play with it!

Logarithms and Exponents Since so much is binary log in CS 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 Spring CSE332: Data Abstractions See Excel file for plot data – play with it!

Logarithms and Exponents Since so much is binary log in CS 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 Spring CSE332: Data Abstractions See Excel file for plot data – play with it!

Properties of logarithms 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 grows fast (log x)(log x) is written log 2 x –It is greater than log x for all x > 2 Spring CSE332: Data Abstractions y

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, log B x = (log A x) / (log A B) Spring CSE332: Data Abstractions

Algorithm Analysis 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 Spring CSE332: Data Abstractions

Example What does this pseudocode return? x := 0; for i=1 to N do for j=1 to i do x := x + 3; return x; Correctness: For any N ≥ 0, it returns… Spring CSE332: Data Abstractions

Example What does this pseudocode return? x := 0; for i=1 to N do for j=1 to i do x := x + 3; return x; 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 Spring CSE332: Data Abstractions

Example 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; 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… Spring CSE332: Data Abstractions

Example 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; 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 Spring CSE332: Data Abstractions

Lower-order terms don’t matter N*(N+1)/2 vs. just N 2 /2 Spring CSE332: Data Abstractions

Recurrence Equations 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 high- order 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 Spring CSE332: Data Abstractions

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 Spring CSE332: Data Abstractions