Data Structures & Algorithm CS-102 Lecture 12 Asymptotic Analysis Lecturer: Syeda Nazia Ashraf 1.

Slides:



Advertisements
Similar presentations
Analysis of Algorithms CS 477/677
Advertisements

Analysis of Algorithms CS Data Structures Section 2.6.
Lecture: Algorithmic complexity
 The running time of an algorithm as input size approaches infinity is called the asymptotic running time  We study different notations for asymptotic.
Razdan with contribution from others 1 Algorithm Analysis What is the Big ‘O Bout? Anshuman Razdan Div of Computing.
Estimating Running Time Algorithm arrayMax executes 3n  1 primitive operations in the worst case Define a Time taken by the fastest primitive operation.
Chapter 3 Growth of Functions
Chapter 10 Algorithm Efficiency
CSC401 – Analysis of Algorithms Lecture Notes 1 Introduction
Sorted Lists CS Data Structures. Sorted List Specification (partial) InsertItem (ItemType item) Function: Adds item to list Preconditions: (1) List.
Cutler/HeadGrowth of Functions 1 Asymptotic Growth Rate.
© 2004 Goodrich, Tamassia 1 Lecture 01 Algorithm Analysis Topics Basic concepts Theoretical Analysis Concept of big-oh Choose lower order algorithms Relatives.
Introduction to Analysis of Algorithms
Analysis of Algorithms1 Estimate the running time Estimate the memory space required. Time and space depend on the input size.
Analysis of Algorithms
Cmpt-225 Algorithm Efficiency.
Analysis of Algorithms (pt 2) (Chapter 4) COMP53 Oct 3, 2007.
1 Analysis of Algorithms & Orders of Growth Rosen 6 th ed., §
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.
Spring2012 Lecture#10 CSE 246 Data Structures and Algorithms.
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
Algorithm Efficiency CS 110: Data Structures and Algorithms First Semester,
Mathematics Review and Asymptotic Notation
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.
1 Computer Algorithms Lecture 3 Asymptotic Notation Some of these slides are courtesy of D. Plaisted, UNC and M. Nicolescu, UNR.
Analyzing algorithms & Asymptotic Notation BIO/CS 471 – Algorithms for Bioinformatics.
Asymptotic Analysis-Ch. 3
CMPT 438 Algorithms Chapter 3 Asymptotic Notations.
Program Efficiency & Complexity Analysis. Algorithm Review An algorithm is a definite procedure for solving a problem in finite number of steps Algorithm.
MS 101: Algorithms Instructor Neelima Gupta
Algorithm Analysis CS 400/600 – Data Structures. Algorithm Analysis2 Abstract Data Types Abstract Data Type (ADT): a definition for a data type solely.
Growth of Functions. 2 Analysis of Bubble Sort 3 Time Analysis – Best Case The array is already sorted – no swap operations are required.
Introduction to Analysis of Algorithms CS342 S2004.
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.
Lecture 2 Analysis of Algorithms How to estimate time complexity? Analysis of algorithms Techniques based on Recursions ACKNOWLEDGEMENTS: Some contents.
David Luebke 1 1/6/2016 CS 332: Algorithms Asymptotic Performance.
1/6/20161 CS 3343: Analysis of Algorithms Lecture 2: Asymptotic Notations.
Asymptotic Performance. Review: Asymptotic Performance Asymptotic performance: How does algorithm behave as the problem size gets very large? Running.
2-0 Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “ Introduction to the Design & Analysis of Algorithms, ” 2 nd ed., Ch. 2 Theoretical.
2IS80 Fundamentals of Informatics Fall 2015 Lecture 5: Algorithms.
Analysis of Algorithm Lecture 2 Basics of Algorithms and Mathematics
Lecture 7. Asymptotic definitions for the analysis of algorithms 1.
BITS Pilani Pilani Campus Data Structure and Algorithms Design Dr. Maheswari Karthikeyan Lecture1.
Analysis of Algorithms Spring 2016CS202 - Fundamentals of Computer Science II1.
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.
LECTURE 2 : fundamentals of analysis of algorithm efficiency Introduction to design and analysis algorithm 1.
1 COMP9024: Data Structures and Algorithms Week Two: Analysis of Algorithms Hui Wu Session 2, 2014
CSE 3358 NOTE SET 2 Data Structures and Algorithms 1.
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu Lecture 2.
Data Structures I (CPCS-204) Week # 2: Algorithm Analysis tools Dr. Omar Batarfi Dr. Yahya Dahab Dr. Imtiaz Khan.
Chapter 10 Algorithm Efficiency
Analysis of Algorithms
Analysis of Non – Recursive Algorithms
Analysis of Non – Recursive Algorithms
Analysis of Algorithms & Orders of Growth
Algorithms Algorithm Analysis.
Analysis of Algorithms
Analysis of Algorithms
CS 3343: Analysis of Algorithms
Analysis of Algorithms
Analysis of Algorithms
Analysis of Algorithms
Chapter 2.
Analysis of Algorithms
Analysis of Algorithms
Presentation transcript:

Data Structures & Algorithm CS-102 Lecture 12 Asymptotic Analysis Lecturer: Syeda Nazia Ashraf 1

2 Analysis of Algorithms An algorithm is a finite set of precise instructions for performing a computation or for solving a problem. What is the goal of analysis of algorithms? –To compare algorithms mainly in terms of running time but also in terms of other factors (e.g., memory requirements, programmer's effort etc.) What do we mean by running time analysis? –Determine how running time increases as the size of the problem increases.

3 Input Size Input size (number of elements in the input) –size of an array –polynomial degree –# of elements in a matrix –# of bits in the binary representation of the input –vertices and edges in a graph

4 Types of Analysis 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 a prediction about the running time –Assumes that the input is random

5 How do we compare algorithms? We need to define a number of objective measures. (1) Compare execution times? Not good: times are specific to a particular computer !! (2) Count the number of statements executed? Not good: number of statements vary with the programming language as well as the style of the individual programmer.

6 Ideal Solution Express running time as a function of the input size n (i.e., f(n)). Compare different functions corresponding to running times. Such an analysis is independent of machine time, programming style, etc.

7 Example Associate a "cost" with each statement. Find the "total cost“ by finding the total number of times each statement is executed. 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 arr[N-1] = 0; c c 1 +c c 1 = c 1 x N (N+1) x c 2 + N x c 1 = Nxc2 + c2 + Nxc1= (c 2 + c 1 ) x N + c 2

8 Another Example Algorithm 3 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 2

9 Asymptotic Analysis To compare two algorithms with running times f(n) and g(n), we need a rough measure that characterizes how fast each function grows. Hint: use rate of growth Compare functions in the limit, that is, asymptotically! (i.e., for large values of n)

10 Rate of Growth Consider the example of buying elephants and goldfish: Cost: cost_of_elephants + cost_of_goldfish Cost ~ cost_of_elephants (approximation) The low order terms in a function are relatively insignificant for large n n n n + 50 ~ n 4 i.e., we say that n n n + 50 and n 4 have the same rate of growth

11 Asymptotic Notation O notation: asymptotic “less than”: –f(n)=O(g(n)) implies: f(n) “≤” g(n)  notation: asymptotic “greater than”: –f(n)=  (g(n)) implies: f(n) “≥” g(n)  notation: asymptotic “equality”: –f(n)=  (g(n)) implies: f(n) “=” g(n)

12 Big-O Notation We say f A (n)=30n+8 is order n, or O (n) It is, at most, roughly proportional to n. f B (n)=n 2 +1 is order n 2, or O(n 2 ). It is, at most, roughly proportional to n 2. In general, any O(n 2 ) function is faster- growing than any O(n) function.

13 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 

14 Back to Our Example 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 Both algorithms are of the same order: O(N)

15 Example (cont’d) Algorithm 3 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 2 = O(N 2 )

16 Asymptotic notations O-notation

17 Big-O Visualization O(g(n)) is the set of functions with smaller or same order of growth as g(n)

18 Examples –2n 2 = O(n 3 ): – n 2 = O(n 2 ): – 1000n n = O(n 2 ): – n = O(n 2 ): 2n 2 ≤ cn 3  2 ≤ cn  c = 1 and n 0 = 2 n 2 ≤ cn 2  c ≥ 1  c = 1 and n 0 = n n ≤ 1000n 2 + n 2 =1001n 2  c=1001 and n 0 = 1000 n ≤ cn 2  cn ≥ 1  c = 1 and n 0 = 1

19 More Examples Show that 30n+8 is O(n). –Show  c,n 0 : 30n+8  cn,  n>n 0. Let c=31, n 0 =8. Assume n>n 0 =8. Then cn = 31n = 30n + n > 30n+8, so 30n+8 < cn.

20 Note 30n+8 isn ’ t less than n anywhere (n>0). It isn ’ t even less than 31n everywhere. But it is less than 31n everywhere to the right of n=8. n>n 0 =8  Big-O example, graphically Increasing n  Value of function  n 30n+8 cn = 31n 30n+8  O(n)

21 Asymptotic notations (cont.)  - notation  (g(n)) is the set of functions with larger or same order of growth as g(n)

22 Examples – 5n 2 =  (n) –100n + 5 ≠  (n 2 ) –n =  (2n), n 3 =  (n 2 )  c, n 0 such that: 0  cn  5n 2  cn  5n 2  c = 1 and n 0 = 1  c, n 0 such that: 0  cn 2  100n n + 5  100n + 5n (  n  1) = 105n cn 2  105n  n(cn – 105)  0 Since n is positive  cn – 105  0  n  105/c  contradiction: n cannot be smaller than a constant

23 Asymptotic notations (cont.)  -notation  (g(n)) is the set of functions with the same order of growth as g(n)

24 Examples –n 2 /2 –n/2 =  (n 2 ) ½ n 2 - ½ n ≤ ½ n 2  n ≥ 0  c 2 = ½ ½ n 2 - ½ n ≥ ½ n 2 - ½ n * ½ n (  n ≥ 2 ) = ¼ n 2  c 1 = ¼ –n ≠  (n 2 ): c 1 n 2 ≤ n ≤ c 2 n 2  only holds for: n ≤ 1/ c 1

25 Subset relations between order-of-growth sets. Relations Between Different Sets RRRR  ( f ) O( f )  ( f ) f

26 Logarithms and properties In algorithm analysis we often use the notation “ log n ” without specifying the base Binary logarithm Natural logarithm