Big-Oh Notation. Agenda  What is Big-Oh Notation?  Example  Guidelines  Theorems.

Slides:



Advertisements
Similar presentations
Analysis of Algorithms
Advertisements

CSE 373: Data Structures and Algorithms Lecture 5: Math Review/Asymptotic Analysis III 1.
ACM/JETT Workshop - August 4-5, 2005 Big-O Performance Analysis Based on a presentation by Dr. Simon Garrett, The University of Wales, Aberystwyth:
Introduction to Analysis of Algorithms
Analysis of Algorithms Review COMP171 Fall 2005 Adapted from Notes of S. Sarkar of UPenn, Skiena of Stony Brook, etc.
Algorithm Analysis. Math Review – 1.2 Exponents –X A X B = X A+B –X A /X B =X A-B –(X A ) B = X AB –X N +X N = 2X N ≠ X 2N –2 N+ 2 N = 2 N+1 Logarithms.
25 June 2015Comp 122, Spring 2004 Asymptotic Notation, Review of Functions & Summations.
Algorithm Analysis CS 201 Fundamental Structures of Computer Science.
Data Structures, Spring 2004 © L. Joskowicz 1 Data Structures – LECTURE 2 Elements of complexity analysis Performance and efficiency Motivation: analysis.
Data Structure Algorithm Analysis TA: Abbas Sarraf
Cpt S 223 – Advanced Data Structures
Analysis of Algorithms COMP171 Fall Analysis of Algorithms / Slide 2 Introduction * What is Algorithm? n a clearly specified set of simple instructions.
CSE 373 Data Structures and Algorithms Lecture 4: Asymptotic Analysis II / Math Review.
Algorithm Analysis. Algorithm Def An algorithm is a step-by-step procedure.
Algorithm Analysis 2P03 © Dave Bockus Acknowledgments to Mark Allen Weiss 2014 Data Structures & Algorithm Analysis in Java.
Introduction to complexity Prof. Sin-Min Lee Department of Computer Science San Jose State University.
Program Efficiency and Complexity
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
Analysis CS 367 – Introduction to Data Structures.
Introduction to complexity. 2 Analysis of Algorithms Why do we need to analyze algorithms? –To measure the performance –Comparison of different 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.
CS 3343: Analysis of Algorithms
Iterative Algorithm Analysis & Asymptotic Notations
CS 221 Analysis of Algorithms Instructor: Don McLaughlin.
Asymptotic Analysis-Ch. 3
Coursenotes CS3114: Data Structures and Algorithms Clifford A. Shaffer Department of Computer Science Virginia Tech Copyright ©
Introduction to Analysis of Algorithms COMP171 Fall 2005.
©Silberschatz, Korth and Sudarshan3.1 Algorithms Analysis Algorithm efficiency can be measured in terms of:  Time  Space  Other resources such as processors,
Program Efficiency & Complexity Analysis. Algorithm Review An algorithm is a definite procedure for solving a problem in finite number of steps Algorithm.
Analysis of Algorithms [ Section 4.1 ] Examples of functions important in CS: the constant function:f(n) =
Algorithm Analysis CS 400/600 – Data Structures. Algorithm Analysis2 Abstract Data Types Abstract Data Type (ADT): a definition for a data type solely.
Zeinab EidAlgorithm Analysis1 Chapter 4 Analysis Tools.
Algorithm Efficiency There are often many approaches (algorithms) to solve a problem. How do we choose between them? At the heart of a computer program.
Asymptotic Analysis (based on slides used at UMBC)
Analysis of Algorithms Algorithm Input Output An algorithm is a step-by-step procedure for solving a problem in a finite amount of time.
September 9, Algorithms and Data Structures Lecture II Simonas Šaltenis Nykredit Center for Database Research Aalborg University
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.
Time Complexity of Algorithms (Asymptotic Notations)
General rules: Find big-O f(n) = k = O(1) f(n) = a k n k + a k-1 n k a 1 n 1 + a 0 = O(n k ) Other functions, try to find the dominant term according.
DATA STRUCTURES AND ALGORITHMS Lecture Notes 2 Prepared by İnanç TAHRALI.
Algorithm Analysis Part of slides are borrowed from UST.
Chapter 2 Computational Complexity. Computational Complexity Compares growth of two functions Independent of constant multipliers and lower-order effects.
Time Complexity. Solving a computational program Describing the general steps of the solution –Algorithm’s course Use abstract data types and pseudo code.
CSC Devon M. Simmonds University of North Carolina, Wilmington CSC231 Data Structures.
1/6/20161 CS 3343: Analysis of Algorithms Lecture 2: Asymptotic Notations.
COSC 1P03 Data Structures and Abstraction 2.1 Analysis of Algorithms Only Adam had no mother-in-law. That's how we know he lived in paradise.
13 February 2016 Asymptotic Notation, Review of Functions & Summations.
A Introduction to Computing II Lecture 5: Complexity of Algorithms Fall Session 2000.
Asymptotic Notation Faculty Name: Ruhi Fatima
Computability Sort homework. Formal definitions of time complexity. Big 0. Homework: Exercises. Searching. Shuffling.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI Dale Roberts, Lecturer Computer Science, IUPUI
1 Chapter 2 Algorithm Analysis All sections. 2 Complexity Analysis Measures efficiency (time and memory) of algorithms and programs –Can be used for the.
1 Chapter 2 Algorithm Analysis Reading: Chapter 2.
Algorithm Complexity & Big-O Notation: From the Basics CompSci Club 29 May 2014.
Ch03-Algorithms 1. Algorithms What is an algorithm? An algorithm is a finite set of precise instructions for performing a computation or for solving a.
September 18, Algorithms and Data Structures Lecture II Simonas Šaltenis Aalborg University
Chapter 2 Algorithm Analysis
Introduction to complexity
Complexity Analysis.
Time Complexity Analysis Neil Tang 01/19/2010
Algorithm Analysis Neil Tang 01/22/2008
Fundamentals of Algorithms MCS - 2 Lecture # 9
Complexity Analysis (Part II)
Presentation transcript:

Big-Oh Notation

Agenda  What is Big-Oh Notation?  Example  Guidelines  Theorems

Big-Oh Notation (O)  f(x) is O(g(x))iff there exists constants ‘c’and ‘k’ such that f(x) k  Pronounced as f(x) is Big-Oh of g(x)  This gives the upper bound value of a function

Example  f(n) = 10n + 5 and g(n) = n  To show f(n) is O(g(n)) we must show constants c and k such that f(n) =k or 10n+5 = k  We are allowed to choose c and k to be integers we want as long as they are positive.

Contd..  They can be as big as we want, but they can't be functions of n.  Try c = 15. Then we need to show: 10n + 5 <= 15n. Solving for n we get: 5 <= 5n or 1 <= n. So f(n) = 10+5 = 1. (c = 15, k = 1). Therefore we have shown f(n) is O(g(n)).

How do we calculate big-O? 1Loops 2Nested loops 3Consecutive statements 4If-then-else statements 5Logarithmic complexity 6 Five guidelines for finding out the time complexity of a piece of code

Guideline 1: Loops 7 The running time of a loop is, at most, the running time of the statements inside the loop (including tests) multiplied by the number of iterations. for (i=1; i<=n; i++) { m = m + 2; } constant time executed n times Total time = a constant c * n = cn = O(N)

Guideline 2: Nested loops 8 Analyse inside out. Total running time is the product of the sizes of all the loops. for (i=1; i<=n; i++) { for (j=1; j<=n; j++) { k = k+1; } constant time outer loop executed n times inner loop executed n times Total time = c * n * n * = cn 2 = O(N 2 )

Guideline 3: Consecutive statements 9 Add the time complexities of each statement. Total time = c 0 + c 1 n + c 2 n 2 = O(N 2 ) x = x +1; for (i=1; i<=n; i++) { m = m + 2; } for (i=1; i<=n; i++) { for (j=1; j<=n; j++) { k = k+1; } inner loop executed n times outer loop executed n times constant time executed n times constant time

Guideline 4: If-then-else statements 10 Worst-case running time: the test, plus either the then part or the else part (whichever is the larger). if (depth( ) != otherStack.depth( ) ) { return false; } else { for (int n = 0; n < depth( ); n++) { if (!list[n].equals(otherStack.list[n])) return false; } then part: constant else part: (constant + constant) * n test: constant another if : constant + constant (no else part) Total time = c 0 + c 1 + (c 2 + c 3 ) * n = O(N)

Guideline 5: Logarithmic complexity 11 An algorithm is O(log N) if it takes a constant time to cut the problem size by a fraction (usually by ½) Example algorithm (binary search): finding a word in a dictionary of n pages Look at the centre point in the dictionary Is word to left or right of centre? Repeat process with left or right part of dictionary until the word is found

Theorems  Let d(n),e(n),f(n) and g(n) be functions mapping non negative integers real. Then  If d(n) is O(f(n)),then ad(n) is O(f(n)),for any constant a>0  If d(n) is O(f(n)) and e(n) is O(g(n)),then d(n)+e(n) is O(f(n)+g(n))  If d(n) is O(f(n)) and e(n) is O(g(n)),then d(n).e(n) is O(f(n) g(n))  If d(n) is O(f(n)) and f(n) is O(g(n)),then d(n) is O(g(n)).

Contd..  If f(n) is a polynomial of degree d i.e, f(n)=(a 0 +a 1 n+…+a d n d ) then f(n) is O(n d ).  n x is O(a n ) for any fixed x>0 and a>1  log n x is O(log n) for any fixed x>0  log x n is O(n y ) for any fixed constants x>0 and y>0

Example  2n 3 +4n 2 logn is O(n 3 )  Proof: log n is O(n) – Rule 8 4n 2 logn is O(4n 3 ) – Rule 3 2n 3 +4n 2 log n is O(2n 3 +4 n 3 ) – Rule 2 2n 3 +4n 2 log n is O(n 3 ) – Rule 1 2n 3 +4n 2 log n is O(n 3 ) – Rule 4 Hence,Proved.

Relatives of Big-Oh  big-Omega f(n) is Ω(g(n)) if there is a constant c > 0 and an integer constant n 0 ≥ 1 such that f(n) ≥ c*g(n) for n ≥ n 0  big-Theta f(n) is θ(g(n)) if there are constants c ’ > 0 and c’’ > 0 and an integer constant n 0 ≥ 1 such that c’g(n) > f(n) > c’’g(n) for n ≥ n 0

Contd…  little-oh f(n) is o(g(n)) if, for any constant c > 0, there is an integer constant n 0 ≥ 0 such that f(n) ≤ cg(n) for n ≥ n 0  little-omega f(n) is w(g(n)) if, for any constant c > 0, there is an integer constant n ≥ 0 such that f(n) ≥cg(n) for n ≥n 0

Intuition for Asymptotic Notation  Big-Oh : f(n) is O(g(n)) if f(n) is asymptotically less than or equal to g(n)  big-Omega : f(n) is W(g(n)) if f(n) is asymptotically greater than or equal to g(n)  big-Theta :f(n) is Q(g(n)) if f(n) is asymptotically equal to g(n)  little-oh : f(n) is o(g(n)) if f(n) is asymptotically strictly less than g(n)  little-omega: f(n) is w(g(n)) if is asymptotically strictly greater than g(n)

Bubble Sort – O(n) 2 main() { int a[6]={7,5,3,4,2,1}; int i=0,j=0; int n=6; for(j=n-1;j>0;j--) { for(i=0;i<j;i++) { if(a[i]>a[i+1]) { int temp=a[i]; //swap a[i]=a[i+1]; a[i+1]=temp; } }}

Assignment  Find the complexity of the following Algorithm in Big-Oh – Binary Search for (i=1;i<10 ;i++ ) { for (j=0;j<i ;j++ ) { if (arr[j]>arr[i]) { temp=arr[j]; arr[j]=arr[i]; for (k=i;k>j ;k-- ) arr[k]=arr[k-1]; arr[k+1]=temp; } }}

References  Fundamentals of Computer Algorithms Ellis Horowitz,Sartaj Sahni,Sanguthevar Rajasekaran  Algorithm Design Micheal T. GoodRich,Robert Tamassia  Analysis of Algorithms Jeffrey J. McConnell

THANK YOU