






Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!







COP 3503 – Computer Science II – CLASS NOTES - DAY # A Closer Look at Linear, Quadratic, and Cubic Algorithms In order to more closely examine the differences in running times of linear, quadratic, and cubic algorithms, consider the following problem: Maximum Contiguous Subsequence Sum: given (a possibly negative) integers A 1 , A 2 , …, AN, find (and identify the sequence corresponding to) the maximum value of For the degenerate case when all of the integers are negative, the maximum contiguous subsequence sum is zero. Example: If input is: {-2, 11, -4, 13 , -5, 2}. Then the output is: 20. If the input is {1, -3, 4, -2, -1, 6 }. Then the output is 7. In the degenerative case, since the sum is defined as zero, the subsequence is an empty string. An empty subsequence is contiguous and clearly, 0 > any negative number, so zero is the maximum contiguous subseqeunce sum_._
j k iAk Example: (second case) 1, 1+(-3) = -2, 1+(-3)+4 = 2, 1+(-3)+4+(-2) = 0, 1+(-3)+4+(-2)+(-1) = -1, 1+(-3)+4+(-2)+(-1)+6 = 5 -3, -3+4 = 1, -3+4+(-2) = -1, -3+4+(-2)+(-1) = -2, -3+4+(-2)+(-1)+6 = 4 4, 4+(-2) = 2, 4+(-2)+(-1) = 1, 4+(-2)+(-1)+6 = 7 -2, -2+(-1) = -3, -2+(-1)+6 = 3 -1, -1+6 = 5 6 Maximum contiguous subsequence has a value of 7 and involves the sequence of values 4, -2, -1, and 6.
The O(N^3 ) Algorithm (brute force method - exhaustive search – see example) Algorithm int MCSS = 0, sum = 0, start = 0, end = 0; for (i = 0; i < SIZE; i++) {for (j = 0; j < SIZE; j++) { sum = 0; for (k = i; k <= j; k++) sum += a[k]; if (sum > MCSS) { MCSS = sum; start = i; end = j; } } } return MCSS; Discussion of running time analysis General Observation Analysis Look at the three loops: The i loop executes SIZE (or N) times. The j loop executes SIZE-1 (or N-1) times. The k loop executes SIZE-1 times in the worst case (when i = 0). This gives a rough estimate that the algorithm is O(N^3 ). Precise Analysis Using Big-Oh Notation In all cases the number of times that, sum += a[k], is executed is equal to the number of ordered triplets (i, j, k) where 1 i k j N 2 (since i runs over the whole index, j runs from i to the end, and k runs from i to j ). Therefore, since i , j , k , can each only assume 1 of n values, we know that the number of triplets must be less than n(n)(n) = N^3 but i k j restricts this even further. By combinatorics it can be proven that the number of ordered triplets is n(n+1) (n+2)/6. Therefore, the algorithm is O(N^3 ). Combinatorics Example Suppose we have four values: 1, 2, 3, and 4. Then the number of triplets that can be form where 1 i k j N is (N(N+1)(N+2))/6 = 120/6 =
that it is still an exhaustive search, we’ve simply reduced the cost of computing the last subsequence down to a constant time (O(1)) compared with the linear time (O(N)) for this calculation in the cubic algorithm. The only way to obtain a subquadratic bound for this algorithm is to narrowed the search space by eliminating from consideration a large number of subsequences that cannot possibly affect the maximum value. How to eliminate subsequences from consideration (1) i j j+1 q A < 0 B Sj+1, q C < Sj+1, q If A < 0 then C < B j If Ar < 0 and if q > j , then Ai…Aq is not the MCSS! r = i So – if we test for sum < 0 and it is – then we can break out of the intter loop. However, this is not sufficient for reducing the running time below quadratic! (2) If Ai…Aj is the MCSS, then the sums that border it (on either end) must have sums which are less than 0, otherwise they would have been included in the MCSS. Once, again however, this has not reduced the complexity below quadratic! (3) j If Ar < 0 , then for any i p j and p q, Ap…Aq is: r = i (a) not the MCSS (b) or equal to a previously seen MCSS This is it! This says that if a negative MCSS is seen, we can break the loop and advance i to j+. Algorithm
int MCSS = 0, sum = 0, start = 0, end = 0, i = 0; for (j = 0; j < SIZE; j++) { sum += a[j]; if (sum > MCSS) { MCSS = sum; start = i; end = j; } else if (sum < 0) { i = j + 1; sum = 0; } } } return MCSS; Discussion of running time analysis The j loop runs N times, therefore the algorithm is O(N). The algorithm is more efficient but its correctness is less clear and requires proof of claims (2) and (3) [which are given in the proofs of Theorems 5.3].
initial array 2 - 4 1 6 3 MCSS = 10 Comparison of Subsequences Day 4 - 7