Algorithm Analysis (Big O)

Slides:



Advertisements
Similar presentations
HST 952 Computing for Biomedical Scientists Lecture 10.
Advertisements

Razdan with contribution from others 1 Algorithm Analysis What is the Big ‘O Bout? Anshuman Razdan Div of Computing.
Algorithm Analysis (Big O) CS-341 Dick Steflik. Complexity In examining algorithm efficiency we must understand the idea of complexity –Space complexity.
Analysys & Complexity of Algorithms Big Oh Notation.
Chapter 1 – Basic Concepts
Fall 2006CENG 7071 Algorithm Analysis. Fall 2006CENG 7072 Algorithmic Performance There are two aspects of algorithmic performance: Time Instructions.
Chapter 10 Algorithm Efficiency
CSC401 – Analysis of Algorithms Lecture Notes 1 Introduction
© 2004 Goodrich, Tamassia 1 Lecture 01 Algorithm Analysis Topics Basic concepts Theoretical Analysis Concept of big-oh Choose lower order algorithms Relatives.
Algorithm Analysis (Big O) CS-341 Dick Steflik. Complexity In examining algorithm efficiency we must understand the idea of complexity –Space complexity.
Introduction to Analysis of Algorithms
Complexity Analysis (Part I)
Analysis of Algorithms (Chapter 4)
Analysis of Algorithms1 Estimate the running time Estimate the memory space required. Time and space depend on the input size.
The Efficiency of Algorithms
Analysis of Algorithms 7/2/2015CS202 - Fundamentals of Computer Science II1.
Elementary Data Structures and Algorithms
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.
Algorithm Analysis (Big O)
COMP s1 Computing 2 Complexity
Analysis of Performance
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.
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
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.
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)
Complexity Analysis Chapter 1.
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.
© 2011 Pearson Addison-Wesley. All rights reserved 10 A-1 Chapter 10 Algorithm Efficiency and Sorting.
Chapter 10 A Algorithm Efficiency. © 2004 Pearson Addison-Wesley. All rights reserved 10 A-2 Determining the Efficiency of Algorithms Analysis of algorithms.
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.
Asymptotic Analysis-Ch. 3
Analysis of Algorithms CSCI Previous Evaluations of Programs Correctness – does the algorithm do what it is supposed to do? Generality – does it.
Asymptotic Notation (O, Ω, )
Program Efficiency & Complexity Analysis. Algorithm Review An algorithm is a definite procedure for solving a problem in finite number of steps Algorithm.
Chapter 5 Algorithms (2) Introduction to CS 1 st Semester, 2015 Sanghyun Park.
E.G.M. PetrakisAlgorithm Analysis1  Algorithms that are equally correct can vary in their utilization of computational resources  time and memory  a.
Computer science is a field of study that deals with solving a variety of problems by using computers. To solve a given problem by using computers, you.
Searching Topics Sequential Search Binary Search.
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.
Program Performance 황승원 Fall 2010 CSE, POSTECH. Publishing Hwang’s Algorithm Hwang’s took only 0.1 sec for DATASET1 in her PC while Dijkstra’s took 0.2.
BITS Pilani Pilani Campus Data Structure and Algorithms Design Dr. Maheswari Karthikeyan Lecture1.
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.
GC 211:Data Structures Week 2: Algorithm Analysis Tools Slides are borrowed from Mr. Mohammad Alqahtani.
CSE 3358 NOTE SET 2 Data Structures and Algorithms 1.
Data Structures I (CPCS-204) Week # 2: Algorithm Analysis tools Dr. Omar Batarfi Dr. Yahya Dahab Dr. Imtiaz Khan.
Algorithm Analysis 1.
Analysis of Algorithms
GC 211:Data Structures Week 2: Algorithm Analysis Tools
Introduction to Algorithms
GC 211:Data Structures Algorithm Analysis Tools
Complexity In examining algorithm efficiency we must understand the idea of complexity Space complexity Time Complexity.
Algorithm Analysis (not included in any exams!)
GC 211:Data Structures Algorithm Analysis Tools
Algorithm Efficiency Chapter 10.
CS 201 Fundamental Structures of Computer Science
Analysys & Complexity of Algorithms
Programming and Data Structure
DS.A.1 Algorithm Analysis Chapter 2 Overview
Chapter 2.
Programming and Data Structure
GC 211:Data Structures Algorithm Analysis Tools
Revision of C++.
CS210- Lecture 2 Jun 2, 2005 Announcements Questions
Estimating Algorithm Performance
Presentation transcript:

Algorithm Analysis (Big O) Lecture 9

Complexity In examining algorithm efficiency we must understand the idea of complexity Space complexity Time Complexity

Space Complexity When memory was expensive we focused on making programs as space efficient as possible and developed schemes to make memory appear larger than it really was (virtual memory and memory paging schemes) Space complexity is still important in the field of embedded computing (hand held computer based equipment like cell phones, palm devices, etc)

Time Complexity Is the algorithm “fast enough” for my needs How much longer will the algorithm take if I increase the amount of data it must process Given a set of algorithms that accomplish the same thing, which is the right one to choose

Algorithm Efficiency a measure of the amount of resources consumed in solving a problem of size n time space Benchmarking: implement algorithm, run with some specific input and measure time taken better for comparing performance of processors than for comparing performance of algorithms Big Oh (asymptotic analysis) associates n, the problem size, with t, the processing time required to solve the problem

Cases to examine Best case if the algorithm is executed, the fewest number of instructions are executed Average case executing the algorithm produces path lengths that will on average be the same Worst case executing the algorithm produces path lengths that are always a maximum

Algorithm Analysis Analyze in terms of Primitive Operations: e.g., An addition = 1 operation Assignment = 1 operation Calling a method or returning from a method = 1 operation Index in an array = 1 operation Comparison = 1 operation Analysis: count the number of primitive operations executed by the algorithm

Frequency Count examine a piece of code and predict the number of instructions to be executed e.g. for each instruction predict how many times each will be encountered as the code runs Inst # 1 2 3 Code for (int i=0; i< n ; i++) { cout << i; p = p + i; } F.C. n+1 n ____ 3n+1 totaling the counts produces the F.C. (frequency count)

Another example F.C. n+1 n(n+1) n*n F.C. n+1 n2+n n2 ____ 3n2+2n+1 Inst # 1 2 3 4 Code for (int i=0; i< n ; i++) for int j=0 ; j < n; j++) { cout << i; p = p + i; } discarding constant terms produces : 3n2+2n clearing coefficients : n2+n picking the most significant term: n2 Big O = O(n2)

Analyzing Running Time 1. n = read input from user 2. sum = 0 3. i = 0 4. while i < n 5. number = read input from user 6. sum = sum + number 7. i = i + 1 8. mean = sum / n Statement Number of times executed 1 1 2 1 3 1 4 n+1 5 n 6 n 7 n 8 1 The computing time for this algorithm in terms on input size n is: T(n) = 4n + 5.

How many foos? for (j = 0; j < N; ++j) { for (k = 0; k < j; ++k) { foo( ); } for (k = 0; k < M; ++k) { N(N + 1)/2 NM

What is Big O Big O For example: rate at which algorithm performance degrades as a function of the amount of data it is asked to handle For example: O(n) -> performance degrades at a linear rate O(n2) -> quadratic degradation

Common growth rates

Big Oh - Formal Definition Definition of "big oh": f(n)=O(g(n)), iff there exist constants c and n0 such that: f(n) <= c  g(n) for all n>=n0 Thus, g(n) is an upper bound on f(n) Note: f(n) = O(g(n)) is NOT the same as O(g(n)) = f(n) The '=' is not the usual mathematical operator "=" (it is not reflexive)

big Oh measures an algorithm’s growth rate how fast does the time required for an algorithm to execute increase as the size of the problem increases? is an intrinsic property of the algorithm independent of particular machine or code based on number of instructions executed for some algorithms is data-dependent meaningful for “large” problem sizes

Iterative Power function double IterPow (double X, int N) { double Result = 1; while (N > 0) { Result *= X; N--; { return Result; } 1 n+1 n Total instruction count: 3n+3 critical region algorithm's computing time (t) as a function of n is: 3n + 3 t is on the order of f(n) - O[f(n)] O[3n + 3] is n

Find the maximum element of an array. 1. int findMax(int *A, int n) { 2. int currentMax = A[0] 3. for (int i= 1 ; i < n; i++) 4. if (currentMax < A[i] ) 5. currentMax = A[i]; 6. return currentMax; } How many operations ? Declaration: no time Line 2: 2 count Line 6: 1 count Lines 4 and 5: 4 counts * the number of times the loop is iterated. Line 3: 1 + n + n-1 (because loop is iterated n – 1 times). Total: 2 + 1 +n + (n-1) + 4*(n-1) + 1= 6n - 1

Common big Ohs constant O(1) logarithmic O(log2 N) linear O(N) n log n O(N log2 N) quadratic O(N2) cubic O(N3) exponential O(2N)

Comparing Growth Rates n log2 n n T(n) log2 n Problem Size

Uses of big Oh compare algorithms which perform the same function search algorithms sorting algorithms comparing data structures for an ADT each operation is an algorithm and has a big Oh data structure chosen affects big Oh of the ADT's operations

Comparing algorithms Sequential search growth rate is O(n) average number of comparisons done is n/2 Binary search growth rate is O(log2 n) average number of comparisons done is 2((log2 n) -1) n n/2 2((log2 n)-1) 100 50 12 500 250 16 1000 500 18 5000 2500 24

Common time complexities BETTER WORSE O(1) constant time O(log n) log time O(n) linear time O(n log n) log linear time O(n2) quadratic time O(n3) cubic time O(2n) exponential time