Time Complexity Analysis Neil Tang 01192010 CS 223

  • Slides: 17
Download presentation
Time Complexity Analysis Neil Tang 01/19/2010 CS 223 Advanced Data Structures and Algorithms 1

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

Outline Ø Algorithm and Time Complexity Ø Asymptotic Notations Ø Examples CS 223 Advanced

Outline Ø Algorithm and Time Complexity Ø Asymptotic Notations Ø Examples CS 223 Advanced Data Structures and Algorithms 2

Algorithm and Complexity Ø Algorithm: A clearly specified set of instructions to be followed

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. CS 223 Advanced Data Structures and Algorithms 3

Asymptotic Notations Ø T(N) = O(f(N)) if there exist positive constants c and n

Asymptotic Notations Ø T(N) = O(f(N)) if there exist positive constants c and n 0, s. t. T(N) cf(N) when N n 0 Ø T(N) = (g(N)) if there exist positive constants c and n 0, s. t. T(N) cg(N) when N n 0 Ø 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. CS 223 Advanced Data Structures and Algorithms 4

Properties Ø Rule 1: If T 1(N) = O(f(N)), T 2(N) = O(g(N)) -

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

Examples log. N = O(N), N = O(N 2), N 2 = O(N 3),

Examples log. N = O(N), N = O(N 2), N 2 = O(N 3), N 3 = O(2 N); N 3 = (N 2); N 4 + 1000 N = (N 4); 1 + 2 +. . . + N = O(N 2). CS 223 Advanced Data Structures and Algorithms 6

Running Time The running time of algorithms for the Max Subsequence Sum problem (in

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

Running Time CS 223 Advanced Data Structures and Algorithms 8

Running Time CS 223 Advanced Data Structures and Algorithms 8

Running Time Calculation Ø The simple example in pp. 35 Ø Rule 1: for

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

The Max Subsequence Sum Problem Given integers A 1, A 2, …, AN, find

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

Algorithm 1 T(N) = Θ(N 3) CS 223 Advanced Data Structures and Algorithms 11

Algorithm 1 T(N) = Θ(N 3) CS 223 Advanced Data Structures and Algorithms 11

Algorithm 2 T(N) = O(N 2) CS 223 Advanced Data Structures and Algorithms 12

Algorithm 2 T(N) = O(N 2) CS 223 Advanced Data Structures and Algorithms 12

CS 223 Advanced Data Structures and Algorithms 13

CS 223 Advanced Data Structures and Algorithms 13

Algorithm 3 T(N) = 2 T(N/2) + N T(N) = (Nlog. N) CS 223

Algorithm 3 T(N) = 2 T(N/2) + N T(N) = (Nlog. N) CS 223 Advanced Data Structures and Algorithms 14

Algorithm 4 T(N) = (N) CS 223 Advanced Data Structures and Algorithms 15

Algorithm 4 T(N) = (N) CS 223 Advanced Data Structures and Algorithms 15

Binary Search Algorithm T(N) = T(N/2)+1 -> T(N) = (log. N) CS 223 Advanced

Binary Search Algorithm T(N) = T(N/2)+1 -> T(N) = (log. N) CS 223 Advanced Data Structures and Algorithms 16

Verify Your Analysis CS 223 Advanced Data Structures and Algorithms 17

Verify Your Analysis CS 223 Advanced Data Structures and Algorithms 17