Algorithm Analysis Neil Tang 01/22/2008 CS223 Advanced Data Structures and Algorithms
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
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
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
CS223 Advanced Data Structures and Algorithms Examples logN = O(N), N = O(N2), N2 = O(N3), N3 = O(2N); N3 = (N2); N4 + 1000N = (N4); 1 + 2 + ... + N = O(N2). CS223 Advanced Data Structures and Algorithms
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
CS223 Advanced Data Structures and Algorithms Running Time CS223 Advanced Data Structures and Algorithms
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
The Max Subsequence Sum Problem Given integers A1, A2, …, AN, find the max value of a subsequence (return 0 if all integers are negative). CS223 Advanced Data Structures and Algorithms
CS223 Advanced Data Structures and Algorithms T(N) = (N3+3N2+2N)/6 = (N3) CS223 Advanced Data Structures and Algorithms
CS223 Advanced Data Structures and Algorithms T(N) = O(N2) CS223 Advanced Data Structures and Algorithms
CS223 Advanced Data Structures and Algorithms
CS223 Advanced Data Structures and Algorithms T(N) = 2T(N/2) + N T(N) = (NlogN) CS223 Advanced Data Structures and Algorithms
CS223 Advanced Data Structures and Algorithms T(N) = (N) CS223 Advanced Data Structures and Algorithms
Binary Search Algorithm T(N) = T(N/2)+1 -> T(N) = (logN) CS223 Advanced Data Structures and Algorithms
CS223 Advanced Data Structures and Algorithms Verify Your Analysis CS223 Advanced Data Structures and Algorithms