Presentation is loading. Please wait.

Presentation is loading. Please wait.

Time Complexity Analysis Neil Tang 01/19/2010

Similar presentations


Presentation on theme: "Time Complexity Analysis Neil Tang 01/19/2010"— Presentation transcript:

1 Time Complexity Analysis Neil Tang 01/19/2010
CS223 Advanced Data Structures and Algorithms

2 CS223 Advanced Data Structures and Algorithms
Outline Algorithm and Time Complexity Asymptotic Notations Examples CS223 Advanced Data Structures and Algorithms

3 Algorithm and Complexity
Algorithm: A clearly specified set of instructions to be followed to solve a problem. Characteristics of an algorithm: - input - output - stop on any input Time complexity: The number of operations required. Best vs. average vs. worst case complexity. Space complexity: The amount of memory required. CS223 Advanced Data Structures and Algorithms

4 CS223 Advanced Data Structures and Algorithms
Asymptotic Notations T(N) = O(f(N)) if there exist positive constants c and n0, s.t. T(N)  cf(N) when N n0 T(N) = (g(N)) if there exist positive constants c and n0, s.t. T(N)  cg(N) when N n0 T(N) = (h(N)) iff T(N) = O(h(N)) and T(N) = (h(N)) T(N) = o(p(N)) if T(N) = O(p(N)) and T(N)  (p(N)) O-notation is used to determine an upper bound on the order of growth of a function. -notation is used to determine a lower bound on the order of growth of a function. CS223 Advanced Data Structures and Algorithms

5 CS223 Advanced Data Structures and Algorithms
Properties Rule 1: If T1(N) = O(f(N)), T2(N) = O(g(N)) - T1(N) + T2(N) = O(f(N)+g(N)) - T1(N) * T2(N) = O(f(N)*g(N)) Rule 2: If T(N) is a polynomial of degree k, T(N) = (Nk). Rule 3: logkN = O(N) for any constant k. CS223 Advanced Data Structures and Algorithms

6 CS223 Advanced Data Structures and Algorithms
Examples logN = O(N), N = O(N2), N2 = O(N3), N3 = O(2N); N3 =  (N2); N N = (N4); N = O(N2). CS223 Advanced Data Structures and Algorithms

7 CS223 Advanced Data Structures and Algorithms
Running Time The running time of algorithms for the Max Subsequence Sum problem (in sec) CS223 Advanced Data Structures and Algorithms

8 CS223 Advanced Data Structures and Algorithms
Running Time CS223 Advanced Data Structures and Algorithms

9 Running Time Calculation
The simple example in pp.35 Rule 1: for loop Rule 2: Nested loops e.g., for(…) for(…) Rule 3: Consecutive statements Rule 4: If/else Rule 5: Recursion: master method CS223 Advanced Data Structures and Algorithms

10 The Max Subsequence Sum Problem
Given integers A1, A2, …, AN, find the max value of the sum of a subsequence (return 0 if all integers are negative). CS223 Advanced Data Structures and Algorithms

11 CS223 Advanced Data Structures and Algorithms
T(N) = Θ(N3) CS223 Advanced Data Structures and Algorithms

12 CS223 Advanced Data Structures and Algorithms
T(N) = O(N2) CS223 Advanced Data Structures and Algorithms

13 CS223 Advanced Data Structures and Algorithms

14 CS223 Advanced Data Structures and Algorithms
T(N) = 2T(N/2) + N T(N) = (NlogN) CS223 Advanced Data Structures and Algorithms

15 CS223 Advanced Data Structures and Algorithms
T(N) = (N) CS223 Advanced Data Structures and Algorithms

16 Binary Search Algorithm
T(N) = T(N/2)+1 -> T(N) = (logN) CS223 Advanced Data Structures and Algorithms

17 CS223 Advanced Data Structures and Algorithms
Verify Your Analysis CS223 Advanced Data Structures and Algorithms


Download ppt "Time Complexity Analysis Neil Tang 01/19/2010"

Similar presentations


Ads by Google