CSE 326 Asymptotic Analysis David Kaplan Dept of Computer Science & Engineering Autumn 2001.

Slides:



Advertisements
Similar presentations
MATH 224 – Discrete Mathematics
Advertisements

Lecture: Algorithmic complexity
CSE 373: Data Structures and Algorithms Lecture 5: Math Review/Asymptotic Analysis III 1.
Algorithmic Complexity Nelson Padua-Perez Bill Pugh Department of Computer Science University of Maryland, College Park.
Fall 2006CENG 7071 Algorithm Analysis. Fall 2006CENG 7072 Algorithmic Performance There are two aspects of algorithmic performance: Time Instructions.
CSE 326 Analyzing Recursion David Kaplan Dept of Computer Science & Engineering Autumn 2001.
Introduction to Analysis of Algorithms
Complexity Analysis (Part I)
CSE 326: Data Structures Lecture #3 Analysis of Recursive Algorithms Alon Halevy Fall Quarter 2000.
CSE 326: Data Structures Lecture #2 Analysis of Algorithms Alon Halevy Fall Quarter 2000.
Data Structure Algorithm Analysis TA: Abbas Sarraf
Analysis of Algorithms 7/2/2015CS202 - Fundamentals of Computer Science II1.
Analysis of Algorithms COMP171 Fall Analysis of Algorithms / Slide 2 Introduction * What is Algorithm? n a clearly specified set of simple instructions.
CS Discrete Mathematical Structures Mehdi Ghayoumi MSB rm 132 Ofc hr: Thur, 9:30-11:30a.
Analysis of Algorithms Spring 2015CS202 - Fundamentals of Computer Science II1.
Algorithm Analysis (Big O)
CSE373: Data Structures and Algorithms Lecture 4: Asymptotic Analysis Aaron Bauer Winter 2014.
Asymptotic Notations Iterative Algorithms and their analysis
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
Introduction to complexity. 2 Analysis of Algorithms Why do we need to analyze algorithms? –To measure the performance –Comparison of different algorithms.
1 Recursion Algorithm Analysis Standard Algorithms Chapter 7.
Algorithm Analysis. Algorithm An algorithm is a clearly specified set of instructions which, when followed, solves a problem. recipes directions for putting.
Iterative Algorithm Analysis & Asymptotic Notations
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
Algorithm Evaluation. What’s an algorithm? a clearly specified set of simple instructions to be followed to solve a problem a way of doing something What.
CS 221 Analysis of Algorithms Instructor: Don McLaughlin.
1 COMP3040 Tutorial 1 Analysis of algorithms. 2 Outline Motivation Analysis of algorithms Examples Practice questions.
Analysis of Algorithms These slides are a modified version of the slides used by Prof. Eltabakh in his offering of CS2223 in D term 2013.
Fundamentals CSE 373 Data Structures Lecture 5. 12/26/03Fundamentals - Lecture 52 Mathematical Background Today, we will review: ›Logs and exponents ›Series.
Introduction to Analysis of Algorithms COMP171 Fall 2005.
Program Efficiency & Complexity Analysis. Algorithm Review An algorithm is a definite procedure for solving a problem in finite number of steps Algorithm.
Algorithm Analysis CS 400/600 – Data Structures. Algorithm Analysis2 Abstract Data Types Abstract Data Type (ADT): a definition for a data type solely.
1 CSE 326 Data Structures: Complexity Lecture 2: Wednesday, Jan 8, 2003.
Computer Science and Software Engineering University of Wisconsin - Platteville 8. Comparison of Algorithms Yan Shi CS/SE 2630 Lecture Notes Part of this.
Asymptotic Growth Rates  Themes  Analyzing the cost of programs  Ignoring constants and Big-Oh  Recurrence Relations & Sums  Divide and Conquer 
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.
1 Asymptotic Notations Iterative Algorithms and their analysis Asymptotic Notations –Big O,  Notations Review of Discrete Math –Summations –Logarithms.
Algorithm Analysis Part of slides are borrowed from UST.
CSE 373: Data Structures and Algorithms
Algorithm Complexity L. Grewe 1. Algorithm Efficiency There are often many approaches (algorithms) to solve a problem. How do we choose between them?
E.G.M. PetrakisAlgorithm Analysis1  Algorithms that are equally correct can vary in their utilization of computational resources  time and memory  a.
CSE 326: Data Structures Asymptotic Analysis Hannah Tang and Brian Tjaden Summer Quarter 2002.
DS.A.1 Algorithm Analysis Chapter 2 Overview Definitions of Big-Oh and Other Notations Common Functions and Growth Rates Simple Model of Computation Worst.
CSE 326: Data Structures Lecture #3 Asymptotic Analysis Steve Wolfman Winter Quarter 2000.
CSE 326: Data Structures Class #4 Analysis of Algorithms III Analysis of Recursive Algorithms Henry Kautz Winter 2002.
Analysis of Algorithms Spring 2016CS202 - Fundamentals of Computer Science II1.
CSE 3358 NOTE SET 2 Data Structures and Algorithms 1.
Algorithm Analysis 1.
Analysis of Non – Recursive Algorithms
Analysis of Non – Recursive Algorithms
Analysis of Algorithms
Analysis of Algorithms
Analysis of Algorithms
Introduction to Algorithms
Growth of functions CSC317.
Time Complexity Analysis Neil Tang 01/19/2010
Algorithm Analysis Neil Tang 01/22/2008
Algorithm Analysis (not included in any exams!)
CSE373: Data Structures and Algorithms Lecture 4: Asymptotic Analysis
Analysis of Algorithms
DS.A.1 Algorithm Analysis Chapter 2 Overview
David Kauchak cs161 Summer 2009
Estimating Algorithm Performance
Analysis of Algorithms
Divide-and-Conquer 7 2  9 4   2   4   7
Presentation transcript:

CSE 326 Asymptotic Analysis David Kaplan Dept of Computer Science & Engineering Autumn 2001

Asymptotic AnalysisCSE 326 Autumn Housekeeping  Homework 1 status  Join  Poll:  Slide format(s)?  Office hours today?

Asymptotic AnalysisCSE 326 Autumn Analyzing Algorithms Analyze algorithms to gauge:  Time complexity (running time)  Space complexity (memory use) Input size is indicated by a number n  sometimes have multiple inputs, e.g. m and n Running time is a function of n n, n 2, n log n, n(log n 2 ) + 5n 3

Asymptotic AnalysisCSE 326 Autumn RAM Model RAM (random access machine)  Ideal single-processor machine (serialized operations)  “Standard” instruction set (load, add, store, etc.)  All operations take 1 time unit (including, for our purposes, each C++ statement)

Asymptotic AnalysisCSE 326 Autumn Order Notation (aka Big-O) Big-O T ( n ) = O( f ( n ) ) Exist positive constants c, n 0 such that T(n)  cf(n) for all n  n 0 Upper bound Omega T(n) =  ( f ( n ) ) Exist positive constants c, n 0 such that T(n)  cf(n) for all n  n 0 Lower bound Theta T ( n ) = θ( f ( n ) ) T(n) = O(f(n)) AND T(n) =  (f(n)) Tight bound little-o T ( n ) = o ( f ( n ) ) T(n) = O(f(n)) AND T(n) != θ(f(n)) Strict upper bound

Asymptotic AnalysisCSE 326 Autumn Simplifying with Big-O By definition, Big-O allows us to: Eliminate low order terms  4n + 5  4n  0.5 n log n - 2n + 7  0.5 n log n Eliminate constant coefficients  4n  n  0.5 n log n  n log n  log n 2 = 2 log n  log n  log 3 n = (log 3 2) log n  log n But when might constants or low-order terms matter?

Asymptotic AnalysisCSE 326 Autumn Big-O Examples n n = O(n 2 ) follows from … ( n n )  2 n 2 for n  10 n n =  (n 2 ) follows from …( n n )  1 n 2 for n  0 n n =  (n 2 ) by definition n log n = O(n 2 ) n log n =  (n log n) n log n =  (n)

Asymptotic AnalysisCSE 326 Autumn Big-O Usage Order notation is not symmetric:  we can say2n 2 + 4n = O(n 2 )  … but neverO(n 2 ) = 2n 2 + 4n Right-hand side is a crudification of the left Order expressions on left can produce unusual- looking, but true, statements: O(n 2 ) = O(n 3 )  (n 3 ) =  (n 2 )

Asymptotic AnalysisCSE 326 Autumn Big-O Comparisons Function A n 3 + 2n 2 n 0.1 n + 100n 0.1 5n 5 n n / log n Function #2 100n log n 2n + 10 log n n! 1000n 15 3n 7 + 7n vs.

Asymptotic AnalysisCSE 326 Autumn Race 1 100n vs. n 3 + 2n 2

Asymptotic AnalysisCSE 326 Autumn Race 2 n 0.1 log n vs. In this one, crossover point is very late! So, which algorithm is really better???

Asymptotic AnalysisCSE 326 Autumn Race C n + 100n 0.1 2n + 10 log n vs. Is the “better” algorithm asymptotically better???

Asymptotic AnalysisCSE 326 Autumn Race 4 5n 5 n! vs.

Asymptotic AnalysisCSE 326 Autumn Race 5 n n / n 15 vs.

Asymptotic AnalysisCSE 326 Autumn Race VI 8 2log(n) 3n 7 + 7n vs.

Asymptotic AnalysisCSE 326 Autumn Big-O Winners (i.e. losers) Function A n 3 + 2n 2 n 0.1 n + 100n 0.1 5n 5 n n / log n Function #2 100n log n 2n + 10 log n n! 1000n 15 3n 7 + 7n vs. Winner O(n 2 ) O(log n) O(n) TIE O(n 5 ) O(n 15 ) O(n 6 ) why???

Asymptotic AnalysisCSE 326 Autumn Big-O Common Names constant:O(1) logarithmic:O(log n) linear:O(n) log-linear:O(n log n) superlinear:O(n 1+c )(c is a constant > 0) quadratic:O(n 2 ) polynomial:O(n k )(k is a constant) exponential:O(c n )(c is a constant > 1)

Asymptotic AnalysisCSE 326 Autumn Kinds of Analysis Running time may depend on actual input, not just length of input Distinguish  Worst case  Your worst enemy is choosing input  Average case  Assume probability distribution of inputs  Amortized  Average time over many runs  Best case (not too useful)

Asymptotic AnalysisCSE 326 Autumn Analyzing Code C++ operations Consecutive stmts Conditionals Loops Function calls Recursive functions constant time sum of times larger branch plus test sum of iterations cost of function body solve recursive equation

Asymptotic AnalysisCSE 326 Autumn Nested Loops for i = 1 to n do for j = 1 to n do sum = sum + 1

Asymptotic AnalysisCSE 326 Autumn Dependent Nested Loops for i = 1 to n do for j = i to n do sum = sum + 1

Asymptotic AnalysisCSE 326 Autumn Recursion  A recursive procedure can often be analyzed by solving a recursive equation  Basic form: T(n) = base case: some constant recursive case: T(subproblems) + T(combine)  Result depends upon  how many subproblems  how much smaller are subproblems  how costly to combine solutions (coefficients)

Asymptotic AnalysisCSE 326 Autumn Sum of Queue SumQueue(Q) if (Q.length == 0 ) return 0 else return Q.dequeue() + SumQueue(Q) One subproblem Linear reduction in size (decrease by 1) Combining: constant (cost of 1 add) T(0)  b T(n)  c + T(n – 1) for n>0

Asymptotic AnalysisCSE 326 Autumn Sum of Queue Solution T(n)  c + c + T(n-2)  c + c + c + T(n-3)  kc + T(n-k) for all k  nc + T(0) for k=n  cn + b = O(n) T(0)  b T(n)  c + T(n – 1) for n>0 Equation: Solution:

Asymptotic AnalysisCSE 326 Autumn Binary Search BinarySearch(A, x) Search A, a sorted array, for item x One subproblem, half as large T(1)  b T(n)  T(n/2) + c for n>1 Equation:

Asymptotic AnalysisCSE 326 Autumn Binary Search: Solution T(n)  T(n/2) + c  T(n/4) + c + c  T(n/8) + c + c + c  T(n/2 k ) + kc  T(1) + c log n where k = log n  b + c log n = O(log n) T(1)  b T(n)  T(n/2) + c for n>1 Equation: Solution: