# day4 - COP 3503 Computer Science II CLASS NOTES DAY#4 A...

This preview shows pages 1–3. Sign up to view the full content.

COP 3503 – Computer Science II CLASS NOTES - DAY #4 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 , …, A N , 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 i k k A 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.

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
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
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### Page1 / 10

day4 - COP 3503 Computer Science II CLASS NOTES DAY#4 A...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online