Lecture 3. kf(n) is O(f(n)) for any positive constant k f(n) + g(n) is O(f(n)) if g(n) is O(f(n)) T 1 (n) is O(f(n)), T 2 (n) is O(g(n)) T 1 (n) T 2 (n)

Slides:



Advertisements
Similar presentations
CMSC 341 Asymptotic Analysis. 2 Mileage Example Problem: John drives his car, how much gas does he use?
Advertisements

CSE 373: Data Structures and Algorithms Lecture 5: Math Review/Asymptotic Analysis III 1.
Lecture 3: Algorithm Complexity. Recursion A subroutine which calls itself, with different parameters. Need to evaluate factorial(n) = n  factorial(n-1)
Fall 2006CENG 7071 Algorithm Analysis. Fall 2006CENG 7072 Algorithmic Performance There are two aspects of algorithmic performance: Time Instructions.
Chapter 2: Algorithm Analysis
1 ICS 353 Design and Analysis of Algorithms Spring Semester (062) King Fahd University of Petroleum & Minerals Information & Computer Science.
Lecture 4. kf(n) is O(f(n)) for any positive constant k n r is O(n p ) if r  p since lim n  n r /n p = 0, if r < p = 1 if r = p f(n) is O(g(n)), g(n)
Analysis of Algorithms Review COMP171 Fall 2005 Adapted from Notes of S. Sarkar of UPenn, Skiena of Stony Brook, etc.
Algorithm Analysis. Analysis of Algorithms / Slide 2 Introduction * Data structures n Methods of organizing data * What is Algorithm? n a clearly specified.
Complexity Analysis (Part II)
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.
Asymptotic Analysis Motivation Definitions Common complexity functions
Data Structure Algorithm Analysis TA: Abbas Sarraf
Cpt S 223 – Advanced Data Structures
David Luebke 1 8/17/2015 CS 332: Algorithms Asymptotic Performance.
CS-2852 Data Structures LECTURE 3A Andrew J. Wozniewicz Image copyright © 2010 andyjphoto.com.
Asymptotic Growth Rates Themes –Analyzing the cost of programs –Ignoring constants and Big-Oh –Recurrence Relations & Sums –Divide and Conquer Examples.
Asymptotic Notations Iterative Algorithms and their analysis
CS 201 Data Structures and Algorithms Chapter 2: Algorithm Analysis - II Text: Read Weiss, §2.4.3 – Izmir University of Economics.
Lecture 2 We have given O(n 3 ), O(n 2 ), O(nlogn) algorithms for the max sub-range problem. This time, a linear time algorithm! The idea is as follows:
Program Performance & Asymptotic Notations CSE, POSTECH.
Vishnu Kotrajaras, PhD.1 Data Structures. Vishnu Kotrajaras, PhD.2 Introduction Why study data structure?  Can understand more code.  Can choose a correct.
Mathematics Review and Asymptotic Notation
Iterative Algorithm Analysis & Asymptotic Notations
Algorithm Analysis An algorithm is a clearly specified set of simple instructions to be followed to solve a problem. Three questions for algorithm analysis.
CS223 Advanced Data Structures and Algorithms 1 Sorting and Master Method Neil Tang 01/21/2009.
Analysis of Algorithms
2IL50 Data Structures Fall 2015 Lecture 2: Analysis of Algorithms.
CS 340Chapter 2: Algorithm Analysis1 Time Complexity The best, worst, and average-case complexities of a given algorithm are numerical functions of the.
Asymptotic Analysis-Ch. 3
Program Efficiency & Complexity Analysis. Algorithm Review An algorithm is a definite procedure for solving a problem in finite number of steps Algorithm.
Data Structure Introduction.
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.
Chapter 5 Algorithms (2) Introduction to CS 1 st Semester, 2015 Sanghyun Park.
Asymptotic Growth Rates  Themes  Analyzing the cost of programs  Ignoring constants and Big-Oh  Recurrence Relations & Sums  Divide and Conquer 
1 Asymptotic Notations Iterative Algorithms and their analysis Asymptotic Notations –Big O,  Notations Review of Discrete Math –Summations –Logarithms.
Asymptotic Behavior Algorithm : Design & Analysis [2]
Chapter 2 Computational Complexity. Computational Complexity Compares growth of two functions Independent of constant multipliers and lower-order effects.
Algorithm Analysis Chapter 5. Algorithm An algorithm is a clearly specified set of instructions which, when followed, solves a problem. –recipes –directions.
Big-Oh Notation. Agenda  What is Big-Oh Notation?  Example  Guidelines  Theorems.
Algorithm Analysis Algorithm Analysis Lectures 3 & 4 Resources Data Structures & Algorithms Analysis in C++ (MAW): Chap. 2 Introduction to Algorithms (Cormen,
David Luebke 1 1/6/2016 CS 332: Algorithms Asymptotic Performance.
1/6/20161 CS 3343: Analysis of Algorithms Lecture 2: Asymptotic Notations.
Algorithm Analysis. What is an algorithm ? A clearly specifiable set of instructions –to solve a problem Given a problem –decide that the algorithm is.
Fundamentals of Algorithm Analysis Algorithm : Design & Analysis [Tutorial - 1]
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.
Chapter 2 Algorithm Analysis Mathematical Background Figure 2.1 Typical growth rates.
Vishnu Kotrajaras, PhD.1 Data Structures
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.
Chapter 2 Algorithm Analysis
Analysis of Algorithms
CS 3343: Analysis of Algorithms
Time Complexity Analysis Neil Tang 01/19/2010
Algorithm Analysis Neil Tang 01/22/2008
Algorithm Analysis Lectures 3 & 4
CSC 413/513: Intro to Algorithms
Programming and Data Structure
DS.A.1 Algorithm Analysis Chapter 2 Overview
CSE 373 Data Structures and Algorithms
Programming and Data Structure
How much does your program take ?
At the end of this session, learner will be able to:
At the end of this session, learner will be able to:
Complexity Analysis (Part II)
Big O notation f = O(g(n)) c g(n)
Presentation transcript:

Lecture 3

kf(n) is O(f(n)) for any positive constant k f(n) + g(n) is O(f(n)) if g(n) is O(f(n)) T 1 (n) is O(f(n)), T 2 (n) is O(g(n)) T 1 (n) T 2 (n) is O(f(n)g(n)) T 1 (n) + T 2 (n) is O(max(f(n),g(n))) n r is O(n p ) if r  p n r is O(exp(n)) log n is O (n r ) if r  0 f(n) is O(g(n)), g(n) is O(h(n)), Is f(n) O(h(n)) ? Is kn O(n 2 ) ?

Algorithm Complexity Analysis diff = sum = 0; For (k=0: k < N; k++) sum  sum + 1; diff  diff - 1; For (k=0: k < 3N; k++) sum  sum - 1;

First line takes 2 basic steps Every iteration of first loop takes 2 basic steps. First loop runs N times Every iteration of second loop takes 1 basic step Second loop runs for 3N times Overall, 2 + 2N + 3N steps This is O(N)

Rules Complexity of a loop: O(Number of iterations in a loop * maximum complexity of each iteration) Nested Loops: Analyze the innermost loop first, complexity of next outer loop = number of iterations in this loop * complexity of inner loop, etc….. sum = 0; For (i=0; i < N; i++) For (j=0; j < N; j++) sum  sum + 1;

If (Condition) S1 Else S2 Maximum of the two If (yes) print(1,2,….1000N) Else print(1,2,….N 2 )

Maximum Subsequence Problem There is an array of N elements Need to find i, j such that the sum of all elements between the ith and jth position is maximum for all such sums Maxsum = 0; For (i=0; i < N; i++) For (j=i; j < N; j++) { Thissum = sum of all elements between ith and jth positions; Maxsum = max(Thissum, Maxsum);}

Analysis Inner loop:  j=i N-1 (j-i + 1) = (N – i + 1)(N-i)/2 Outer Loop:  i=0 N-1 (N – i + 1)(N-i)/2 = (N 3 + 3N 2 + 2N)/6

Maxsum = 0; For (i=0; i < N; i++) For (Thissum=0;j=i; j < N; j++) { Thissum = Thissum + A[i]; Maxsum = max(Thissum, Maxsum);} Complexity?

Divide and Conquer Break a big problem into two small sub-problems Solve each of them efficiently. Combine the two solutions

Maximum subsequence sum by divide and conquer Divide the array into two parts: left part, right part Max. subsequence lies completely in left, or completely in right or spans the middle. If it spans the middle, then it includes the max subsequence in the left ending at the last element and the max subsequence in the right starting from the center

4 –3 5 – Max subsequence sum for first half = 6 second half = 8 Max subsequence sum for first half ending at the last element is 4 Max subsequence sum for sum second half starting at the first element is 7 Max subsequence sum spanning the middle is ? Max subsequence spans the middle

Maxsubsum(A[], left, right) { if left = right, maxsum = max(A[left], 0); Center =  (left + right)/2  maxleftsum = maxsubsum(A[],left, center); maxrightsum = maxsubsum(A[],center+1,right); maxleftbordersum = 0; leftbordersum = 0; for (i=center; i>=left; i--) leftbordersum+=A[i]; Maxleftbordersum=max(maxleftbordersum, leftbordersum);

Find maxrightbordersum….. return(max(maxleftsum, maxrightsum, maxrightbordersum + maxleftbordersum);

Complexity Analysis T(1)=1 T(n) = 2T(n/2) + cn = 2.cn/2 + 4T(n/4) + cn = 4T(n/4) + 2cn = 8T(n/8) + 3cn =………….. = 2 i T(n/2 i ) + icn =………………… (reach a point when n = 2 i i=log n = n.T(1) + cnlog n

n + cnlogn = O(nlogn)

Master Theorem T(1)=p T(n) = aT(n/b) + cn k Case (1): a  b k then T(n) is O(n log b a ) Case(2): a = b k then T(n) is O(n k logn) Case(3): a < b k then T(n) is O(n k ) Cormen, Leiserson, Rivest

Linear Complexity Algorithm Maxsum = 0; Thissum = 0; For (j=0; j<N; j++) { Thissum = Thissum + A[j]; If (Thissum  0), Thissum = 0; If (Maxsum  Thissum), Maxsum = Thissum; } O(N) complexity

Binary Search You have a sorted list of numbers You need to search the list for the number If the number exists find its position. If the number does not exist you need to detect that

Search(num, A[],left, right) { if (left = right) { if (A[left ]=num) return(left) and exit; else conclude NOT PRESENT and exit; } center =  (left + right)/2  ; If (A[center]  num) Search(num,A[],center + 1,right); If (A[center]>num) Search(num,A[],left,center ); If (A[center]=num) return(center) and exit; }

Complexity Analysis T(n) = T(n/2) + c O(log n) complexity

Other Complexity Notation Intuitively, (not exactly) f(n) is O(g(n)) means f(n)  g(n) g(n) is an upper bound for f(n). Now a lower bound notation,  (n) f(n) is  (g(n)) if f(n)  cg(n) for some positive constant c, and all large n. Lim n  (f(n)/g(n)) > 0

f(n) is  (g(n)) if f(n) is O(g(n)) and  (g(n))  (g(n)) is ``asymptotic equality’’ Lim n  (f(n)/g(n)) is a positive constant f(n) is o(g(n)) if f(n) is O(g(n)) but not  (g(n)) ``asymptotic strict inequality’’ f(n) is o(g(n)) if given any positive constant c, there exists some m such that f(n)  cg(n) for all n  m If lim n  (f(n)/g(n)) = 0, then f(n) is o(g(n))

Asymptotically less than or equal to O Asymptotically greater than or equal to  Asymptotically equal to  Asymptotically strictly less o

Examples

Implication of the  Notation Suppose, an algorithm has complexity  (f(n)). This means that there exists a positive constant c such that for all sufficiently large n, there exists at least one input for which the algorithm consumes at least cf(n) steps.

Complexity of a Problem Vs Algorithm A problem is O(f(n)) means there is some O(f(n)) algorithm to solve the problem. A problem is  (f(n)) means every algorithm that can solve the problem is  (f(n))