Analysis of Algorithms

Slides:



Advertisements
Similar presentations
Analysis of Algorithms CS 477/677
Advertisements

Analysis of Algorithms CS Data Structures Section 2.6.
Razdan with contribution from others 1 Algorithm Analysis What is the Big ‘O Bout? Anshuman Razdan Div of Computing.
Analysys & Complexity of Algorithms Big Oh Notation.
Chapter 3 Growth of Functions
Chapter 10 Algorithm Efficiency
Sorted Lists CS Data Structures. Sorted List Specification (partial) InsertItem (ItemType item) Function: Adds item to list Preconditions: (1) List.
Introduction to Analysis of Algorithms
Analysis of Algorithms COMP171 Fall Analysis of Algorithms / Slide 2 Introduction * What is Algorithm? n a clearly specified set of simple instructions.
David Luebke 1 8/17/2015 CS 332: Algorithms Asymptotic Performance.
Algorithm Analysis (Big O)
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.
Program Performance & Asymptotic Notations CSE, POSTECH.
Chapter 2.6 Comparison of Algorithms modified from Clifford A. Shaffer and George Bebis.
Week 2 CS 361: Advanced Data Structures and Algorithms
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.
Lecture 2 Computational Complexity
Mathematics Review and Asymptotic Notation
Iterative Algorithm Analysis & Asymptotic Notations
Analysis of Algorithms
Coursenotes CS3114: Data Structures and Algorithms Clifford A. Shaffer Department of Computer Science Virginia Tech Copyright ©
Analysis of Algorithms CSCI Previous Evaluations of Programs Correctness – does the algorithm do what it is supposed to do? Generality – does it.
Program Efficiency & Complexity Analysis. Algorithm Review An algorithm is a definite procedure for solving a problem in finite number of steps Algorithm.
Big-O. Algorithm Analysis Exact analysis: produce a function f(n) measuring how many basic steps are needed for a given inputs n On any input of size.
David Luebke 1 1/6/2016 CS 332: Algorithms Asymptotic Performance.
Asymptotic Performance. Review: Asymptotic Performance Asymptotic performance: How does algorithm behave as the problem size gets very large? Running.
Algorithm Analysis (Big O)
Analysis of Algorithm Lecture 2 Basics of Algorithms and Mathematics
Searching Topics Sequential Search Binary Search.
1 Chapter 2 Algorithm Analysis All sections. 2 Complexity Analysis Measures efficiency (time and memory) of algorithms and programs –Can be used for the.
BITS Pilani Pilani Campus Data Structure and Algorithms Design Dr. Maheswari Karthikeyan Lecture1.
Chapter 3 Chapter Summary  Algorithms o Example Algorithms searching for an element in a list sorting a list so its elements are in some prescribed.
GC 211:Data Structures Week 2: Algorithm Analysis Tools Slides are borrowed from Mr. Mohammad Alqahtani.
Data Structures & Algorithm CS-102 Lecture 12 Asymptotic Analysis Lecturer: Syeda Nazia Ashraf 1.
Chapter 10 Algorithm Efficiency
Analysis of Algorithms
Chapter 2 Algorithm Analysis
Analysis of Non – Recursive Algorithms
Analysis of Non – Recursive Algorithms
Analysis of Algorithms
COMP9024: Data Structures and Algorithms
Analysis of Algorithms
GC 211:Data Structures Week 2: Algorithm Analysis Tools
Analysis of Algorithms
Introduction to Algorithms
GC 211:Data Structures Algorithm Analysis Tools
Algorithms Algorithm Analysis.
Analysis of Algorithms
COMP9024: Data Structures and Algorithms
Data Structures Using The Big-O Notation 1.
Analysis of Algorithms
DATA STRUCTURES Introduction: Basic Concepts and Notations
Algorithm Analysis (not included in any exams!)
Algorithm design and Analysis
GC 211:Data Structures Algorithm Analysis Tools
CSC 413/513: Intro to Algorithms
Introduction to Algorithms Analysis
Algorithm Efficiency Chapter 10.
Analysis of Algorithms
Orders of Growth Rosen 5th ed., §2.2.
Analysis of Algorithms
Analysys & Complexity of Algorithms
Programming and Data Structure
Analysis of Algorithms
Analysis of Algorithms
CSE 2010: Algorithms and Data Structures Algorithms
CS210- Lecture 2 Jun 2, 2005 Announcements Questions
Estimating Algorithm Performance
Analysis of Algorithms
Analysis of Algorithms
Presentation transcript:

Analysis of Algorithms CS 302 - Data Structures Section 2.6

Analysis of Algorithms What is the goal? Analyze time requirements - predict how running time increases as the size of the problem increases: Why is it useful? To compare different algorithms. time = f(size)

Defining “problem size” Typically, it is straightforward to identify the size of a problem, e.g.: size of array size of stack, queue, list etc. vertices and edges in a graph But not always …

Time Analysis Provides upper and lower bounds of running time. Different types of analysis: - Worst case - Best case - Average case

Worst Case Provides an upper bound on running time. An absolute guarantee that the algorithm would not run longer, no matter what the inputs are.

Best Case Provides a lower bound on running time. Input is the one for which the algorithm runs the fastest.

Average Case Provides an estimate of “average” running time. Assumes that the input is random. Useful when best/worst cases do not happen very often (i.e., few input cases lead to best/worst cases).

Example: Searching Problem of searching an ordered list. Given a list L of n elements that are sorted into a definite order (e.g., numeric, alphabetical), And given a particular element x, Determine whether x appears in the list, and if so, return its index (i.e., position) in the list.

Linear Search procedure linear search (x: integer, a1, a2, …, an: distinct integers) i := 1 while (i  n  x  ai) i := i + 1 if i  n then location := i else location := 0 return location NOT EFFICIENT!

How do we analyze an algorithm? Need to define objective measures. (1) Compare execution times? Not good: times are specific to a particular machine. (2) Count the number of statements? Not good: number of statements varies with programming language and programming style.

Example Algorithm 1 Algorithm 2 arr[0] = 0; for(i=0; i<N; i++)   arr[0] = 0; for(i=0; i<N; i++) arr[1] = 0; arr[i] = 0; arr[2] = 0; ... arr[N-1] = 0;

How do we analyze an algorithm? (cont.) (3) Express running time t as a function of problem size n (i.e., t=f(n) ). Given two algorithms having running times f(n) and g(n), find which functions grows faster. - Such an analysis is independent of machine time, programming style, etc.

How do we find f(n)? (1) Associate a "cost" with each statement. (2) Find total number of times each statement is executed. (3) Add up the costs. Algorithm 1 Algorithm 2 Cost Cost arr[0] = 0; c1 for(i=0; i<N; i++) c2 arr[1] = 0; c1 arr[i] = 0; c1 arr[2] = 0; c1 ... arr[N-1] = 0; c1  ----------- ------------- c1+c1+...+c1 = c1 x N (N+1) x c2 + N x c1 = (c2 + c1) x N + c2

How do we find f(n)? (cont.) Cost   sum = 0; c1 for(i=0; i<N; i++) c2 for(j=0; j<N; j++) c2 sum += arr[i][j]; c3 ------------ c1 + c2 x (N+1) + c2 x N x (N+1) + c3 x N x N

Comparing algorithms Given two algorithms having running times f(n) and g(n), how do we decide which one is faster? Compare “rates of growth” of f(n) and g(n)

Understanding Rate of Growth Consider the example of buying elephants and goldfish: Cost: (cost_of_elephants) + (cost_of_goldfish) Approximation: Cost ~ cost_of_elephants

Understanding Rate of Growth (cont’d) The low order terms of a function are relatively insignificant for large n n4 + 100n2 + 10n + 50 Approximation: n4 Highest order term determines rate of growth!

Example Suppose you are designing a website to process user data (e.g., financial records). Suppose program A takes fA(n)=30n+8 microseconds to process any n records, while program B takes fB(n)=n2+1 microseconds to process the n records. Which program would you choose, knowing you’ll want to support millions of users? A Compare rates of growth: 30n+8 ~ n and n2+1 ~ n2

Visualizing Orders of Growth On a graph, as you go to the right, a faster growing function eventually becomes larger... fA(n)=30n+8 Value of function  fB(n)=n2+1 Increasing n 

Rate of Growth ≡Asymptotic Analysis Using rate of growth as a measure to compare different functions implies comparing them asymptotically (i.e., as n  ) If f(x) is faster growing than g(x), then f(x) always eventually becomes larger than g(x) in the limit (i.e., for large enough values of x).

Asymptotic Notation O notation: asymptotic “less than”: f(n)=O(g(n)) implies: f(n) “≤” c g(n) in the limit* c is a constant (used in worst-case analysis) *formal definition in CS477/677

Asymptotic Notation  notation: asymptotic “greater than”: f(n)=  (g(n)) implies: f(n) “≥” c g(n) in the limit* c is a constant (used in best-case analysis) *formal definition in CS477/677

Asymptotic Notation  notation: asymptotic “equality”: f(n)=  (g(n)) implies: f(n) “=” c g(n) in the limit* c is a constant (provides a tight bound of running time) (best and worst cases are same) *formal definition in CS477/677

Big-O Notation - Examples fA(n)=30n+8 fB(n)=n2+1 10n3 + 2n2 n3 - n2 1273 is O(n) is O(n2) is O(n3) is O(n3) is O(1)

More on big-O O(g(n)) is a set of functions f(n) f(n) ϵ O(g(n)) if “f(n)≤cg(n)”

Big-O Notation - Examples fA(n)=30n+8 is O(n) fB(n)=n2+1 is O(n2) 10n3 + 2n2 is O(n3) n3 - n2 is O(n3) 1273 is O(1) or O(n2) or O(n4) But it is important to use as “tight” bounds as possible! or O(n4) or O(n5) or O(n)

Common orders of magnitude

Algorithm speed vs function growth An O(n2) algorithm will be slower than an O(n) algorithm (for large n). But an O(n2) function will grow faster than an O(n) function. fA(n)=30n+8 Value of function  fB(n)=n2+1 Increasing n 

Estimating running time Algorithm 1 Algorithm 2 Cost Cost arr[0] = 0; c1 for(i=0; i<N; i++) c2 arr[1] = 0; c1 arr[i] = 0; c1 arr[2] = 0; c1 ... arr[N-1] = 0; c1  ----------- ------------- c1+c1+...+c1 = c1 x N (N+1) x c2 + N x c1 = (c2 + c1) x N + c2 O(N)

Estimate running time (cont.) Cost   sum = 0; c1 for(i=0; i<N; i++) c2 for(j=0; j<N; j++) c2 sum += arr[i][j]; c3 ------------ c1 + c2 x (N+1) + c2 x N x (N+1) + c3 x N x N O(N2)

Running time of various statements while-loop for-loop

Examples The body of the while loop: O(N) Loop is executed: N times while (i<N) { X=X+Y; // O(1) result = mystery(X); // O(N), just an example... i++; // O(1) } The body of the while loop: O(N) Loop is executed: N times N x O(N) = O(N2)

Examples (cont.’d) if (i<j) for ( i=0; i<N; i++ ) X = X+i; else Max ( O(N), O(1) ) = O (N) O(N) O(1)

Examples (cont.’d)

Examples (cont.’d)

Examples (cont.’d) Analyze the complexity of the following code segments: