Analysis of Algorithms CS 302 - Data Structures Section 2.6.

Slides:



Advertisements
Similar presentations
Computational Complexity, Choosing Data Structures Svetlin Nakov Telerik Corporation
Advertisements

College of Information Technology & Design
College of Information Technology & Design
Chapter 3 Brute Force Brute force is a straightforward approach to solving a problem, usually directly based on the problem’s statement and definitions.
Analysis of Algorithms CS 477/677
MATH 224 – Discrete Mathematics
CSE Lecture 3 – Algorithms I
Time Analysis Since the time it takes to execute an algorithm usually depends on the size of the input, we express the algorithm's time complexity as a.
Lecture3: Algorithm Analysis Bohyung Han CSE, POSTECH CSED233: Data Structures (2014F)
Razdan with contribution from others 1 Algorithm Analysis What is the Big ‘O Bout? Anshuman Razdan Div of Computing.
CMPT 225 Sorting Algorithms Algorithm Analysis: Big O Notation.
Ver. 1.0 Session 5 Data Structures and Algorithms Objectives In this session, you will learn to: Sort data by using quick sort Sort data by using merge.
CS Section 600 CS Section 002 Dr. Angela Guercio Spring 2010.
Chapter 3 Growth of Functions
Chapter 10 Algorithm Efficiency
CSC401 – Analysis of Algorithms Lecture Notes 1 Introduction
Introduction to Analysis of Algorithms
Analysis of Algorithms (Chapter 4)
Lecture 3 Aug 31, 2011 Goals: Chapter 2 (algorithm analysis) Examples: Selection sorting rules for algorithm analysis discussion of lab – permutation generation.
Copyright © Cengage Learning. All rights reserved. CHAPTER 11 ANALYSIS OF ALGORITHM EFFICIENCY ANALYSIS OF ALGORITHM EFFICIENCY.
Lecture 3 Feb 7, 2011 Goals: Chapter 2 (algorithm analysis) Examples: Selection sorting rules for algorithm analysis Image representation Image processing.
Analysis of Algorithms COMP171 Fall Analysis of Algorithms / Slide 2 Introduction * What is Algorithm? n a clearly specified set of simple instructions.
Chapter 1 Introduction Definition of Algorithm An algorithm is a finite sequence of precise instructions for performing a computation or for solving.
Analysis of Performance
Chapter 2.6 Comparison of Algorithms modified from Clifford A. Shaffer and George Bebis.
Week 2 CS 361: Advanced Data Structures and Algorithms
Lecture 2 Computational Complexity
Algorithm Input Output An algorithm is a step-by-step procedure for solving a problem in a finite amount of time. Chapter 4. Algorithm Analysis (complexity)
Iterative Algorithm Analysis & Asymptotic Notations
Analysis of Algorithms
Complexity of algorithms Algorithms can be classified by the amount of time they need to complete compared to their input size. There is a wide variety:
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.
Order Statistics. Order statistics Given an input of n values and an integer i, we wish to find the i’th largest value. There are i-1 elements smaller.
L. Grewe.  An array ◦ stores several elements of the same type ◦ can be thought of as a list of elements: int a[8]
CSE 2341 Honors Professor Mark Fontenot Southern Methodist University Note Set 03.
Algorithms and Algorithm Analysis The “fun” stuff.
Analysis of algorithms Analysis of algorithms is the branch of computer science that studies the performance of algorithms, especially their run time.
Program Efficiency & Complexity Analysis. Algorithm Review An algorithm is a definite procedure for solving a problem in finite number of steps Algorithm.
1 Asymptotic Notations Iterative Algorithms and their analysis Asymptotic Notations –Big O,  Notations Review of Discrete Math –Summations –Logarithms.
1 Algorithms  Algorithms are simply a list of steps required to solve some particular problem  They are designed as abstractions of processes carried.
Algorithm Analysis (Big O)
Algorithm Complexity L. Grewe 1. Algorithm Efficiency There are often many approaches (algorithms) to solve a problem. How do we choose between them?
Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “ Introduction to the Design & Analysis of Algorithms, ” 2 nd ed., Ch. 1 Chapter.
E.G.M. PetrakisAlgorithm Analysis1  Algorithms that are equally correct can vary in their utilization of computational resources  time and memory  a.
1. Searching The basic characteristics of any searching algorithm is that searching should be efficient, it should have less number of computations involved.
Searching Topics Sequential Search Binary Search.
Algorithmics - Lecture 41 LECTURE 4: Analysis of Algorithms Efficiency (I)
Onlinedeeneislam.blogspot.com1 Design and Analysis of Algorithms Slide # 1 Download From
Lecture 3 Sorting and Selection. Comparison Sort.
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.
Chapter 15 Running Time Analysis. Topics Orders of Magnitude and Big-Oh Notation Running Time Analysis of Algorithms –Counting Statements –Evaluating.
Data Structures & Algorithm CS-102 Lecture 12 Asymptotic Analysis Lecturer: Syeda Nazia Ashraf 1.
Chapter 9: Selection of Order Statistics What are an order statistic? min, max median, i th smallest, etc. Selection means finding a particular order statistic.
Design and Analysis of Algorithms
Design and Analysis of Algorithms Chapter -2
Recap RAM model is used to measure the run time of an algorithm by counting the number of steps. Space complexity of an algorithm refer to the amount of.
Course Description Algorithms are: Recipes for solving problems.
DATA STRUCTURES Introduction: Basic Concepts and Notations
Algorithms + Data Structures = Programs -Niklaus Wirth
Algorithm An algorithm is a finite set of steps required to solve a problem. An algorithm must have following properties: Input: An algorithm must have.
CS 3343: Analysis of Algorithms
Algorithms + Data Structures = Programs -Niklaus Wirth
Analysis of Algorithms
Chapter 2.
Revision of C++.
Analysis of Algorithms
CS 583 Analysis of Algorithms
Course Description Algorithms are: Recipes for solving problems.
Estimating Algorithm Performance
Presentation transcript:

Analysis of Algorithms CS 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, a 1, a 2, …, a n : distinct integers) i := 1 while (i  n  x  a i ) 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[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; c 1 for(i=0; i<N; i++) c 2 arr[1] = 0; c 1 arr[i] = 0; c 1 arr[2] = 0; c 1... arr[N-1] = 0; c c 1 +c c 1 = c 1 x N (N+1) x c 2 + N x c 1 = (c 2 + c 1 ) x N + c 2

How do we find f(n)? (cont.) Cost sum = 0; c 1 for(i=0; i<N; i++) c 2 for(j=0; j<N; j++) c 2 sum += arr[i][j]; c c 1 + c 2 x (N+1) + c 2 x N x (N+1) + c 3 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 n n n + 50 Approximation: n 4 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 f A (n)=30n+8 microseconds to process any n records, while program B takes f B (n)=n 2 +1 microseconds to process the n records. Which program would you choose, knowing you’ll want to support millions of users? Compare rates of growth: 30n+8 ~ n and n 2 +1 ~ n 2

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

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: O notation: asymptotic “less than”: f(n)=O(g(n)) implies: f(n) “≤” c g(n) in the limit * (used in worst-case analysis) * formal definition in CS477/677 c is a constant

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

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

f A (n)=30n+8 f B (n)=n n 3 + 2n 2 n 3 - n Big-O Notation - Examples is O(n) is O(n 2 ) is O(n 3 ) is O(1)

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

f A (n)=30n+8 is O(n) f B (n)=n 2 +1 is O(n 2 ) 10n 3 + 2n 2 is O(n 3 ) n 3 - n 2 is O(n 3 ) 1273 is O(1) Big-O Notation - Examples But it is important to use as “tight” bounds as possible! or O(n 2 ) or O(n 4 ) or O(n 5 ) or O(n)

Common orders of magnitude

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

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; c 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]; c c1 + c2 x (N+1) + c2 x N x (N+1) + c3 x N x N O(N 2 )

Running time of various statements while-loopfor-loop

i = 0; 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(N 2 ) Examples

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

Analyze the complexity of the following code segments: