Lect 3 Part 2 Analysis of Algorithm
Lect 3 Part 2 Analysis of Algorithm
Algorithm 1 for Max Subsequence Sum Algorithm 1 for Max Subsequence Sum
• Given A1,…,An , find the maximum value of Ai+Ai+1+···+Aj • Given A1,…,An , find the maximum value of Ai+Ai+1+···+Aj
0 if the max value is negative 0 if the max value is negative
for( int i = 0; i < a.size( ); i++ ) for( int i = 0; i < a.size( ); i++ )
for( int j = i; j < a.size( ); j++ ) for( int j = i; j < a.size( ); j++ )
{ { O (1)
int thisSum = 0; int thisSum = 0; n -1 n -1
O(å å ( j - i + 1))
n -1
for( int k = i; k <= j; k++ ) for( int k = i; k <= j; k++ ) O( j - i + 1) O(å ( j - i + 1))
i= 0 j =i
thisSum += a[ k ]; thisSum += a[ k ]; O (1) j =i
Algorithm 3 for Max Subsequence Sum (cont.) Algorithm 3 for Max Subsequence Sum (cont.)
Maxsubsum(A[], left, right)
• Example: { if (left = right) return (maxsum = max(A[left], 0));
left half | right half Center = ë (left + right)/2û ; // floor function - the maximum integer
4 -3 5 -2 | -1 2 6 -2 maxleftsum = Maxsubsum(A[],left, center);
• Max subsequences in each half found by recursion. maxrightsum = Maxsubsum(A[],center+1,right);
• How do we find the spanning max subsequence? maxleftbordersum = 0; leftbordersum = 0;
• Key Observation: for (i=center; i>=left; i--)
– Left half of the spanning sequence is the max subsequence ending with leftbordersum+=A[i];
-2. maxleftbordersum = max(maxleftbordersum, leftbordersum);
– Right half is the max subsequence beginning with -1. maxrightbordersum = 0; rightbordersum = 0;
for (i=center+1; i<=right; i++)
• A linear scan lets us compute these in O(n) time. rightbordersum+=A[i];
maxlrightbordersum = max(maxleftbordersum, leftbordersum);
return(max(maxleftsum, maxrightsum, maxrightbordersum + maxleftbordersum));
9 }
Algorithm 3 for Max Subsequence Sum (cont.) Algorithm 4 for Max Subsequence Sum
• The divide and conquer is best analyzed through recurrence: • Max subsequence cannot start or end at a negative Ai.
T(1) = 1 • More generally, the max subsequence cannot have a prefix
T(n) = 2T(n/2) + O(n) with a negative sum.
= 2.cn/2 + 4T(n/4) + cn Ex: -2 11 -4 13 -5 -2
= 4T(n/4) + 2cn • Thus, if we ever find that Ai through Aj sums to < 0, then we
= 8T(n/8) + 3cn can advance i to j+1
=…………..
– Proof.
= 2iT(n/2i) + icn
• Suppose j is the first index after i when the sum becomes < 0
=………………… (reach a point when n = 2i i=log n
• The max subsequence cannot start at any p between i and j.
= n.T(1) + cnlogn
Because Ai through Ap-1 is positive, so starting at i would have been
= O(nlogn) even better.
• Therefore, algorithm 3 has O(n log n) time complexity.
11 12
Algorithm 4 for Max Subsequence Sum Algorithm 4 for Max Subsequence Sum
for( int j = 0; j < a.size( ); j++ ) for( int j = 0; j < a.size( ); j++ )
{ {
thisSum += a[ j ]; thisSum += a[ j ];
• The algorithm resets whenever thisSum is < 0. Otherwise, it • The algorithm resets whenever thisSum is < 0. Otherwise, it
forms new sums and updates maxSum in one pass. forms new sums and updates maxSum in one pass.
14
13
Summary for Max Subsequence Problem Summary for Max Subsequence Problem