4 Clever Design Examples Max Sum
4 Clever Design Examples Max Sum
Algorithms
Brute force to clever design
1
Design Example
Very important, algorithms exist with
different time complexities
A Maximum Sub Sequence Sum Problem
The maximum subsequence problem
finds a contiguous subsequence of
the largest sum of a sequence of n
numbers.
applications of computational biology
2
A Maximum Sub
Sequence Sum Problem
Given (possibly negative) integers
a1, a2, a3, …. an, find the maximum
value of k=i j , the maximum
subsequence sum is 0 if all the
integers are negative.
Example: for input –2,11,-4,13,-5,-2,
the answer ?
is 20 (a2 through a4)
3
A Maximum Sub Sequence
Sum Problem
Design an efficient algorithm which
will give result in a fraction of a
second
for 10000 elements sequence and
machine with 1 micro second
instruction execution time
4
A Maximum Sub Sequence
Sum Problem
Algorithm 1: Simple Brute force
Approach
Next Sum
k i
when i = n, n-i+1
n
( n i 1)(n i 2)
j i
j i 1
2
n
(n i 1)(n i 2)
i1 2
9
A Maximum Sub Sequence
Sum Problem
Solve the summation
= c1n3 + c2n2 + c3n
= O(n3)
If n = 10000,
Time constant = 1 micro sec
=> T 1012/(106*24*60*60)
106/(24*60*60 )
11.57 days
Not feasible at all
10
A Maximum Sub Sequence
Sum Problem
Not feasible for large inputs
Try to identify the inefficiencies in
the algorithm and see if we can
find some feasible algorithm
Go back to the example
11
To compute all the sub-sequence
starting from a point i, we don’t
have to come back again and
again.
The last line in the diagram
contains all the subsequence
starting from the same point.
12
A Maximum Sub Sequence
Sum Problem
Another Example for algorithm development:
-2 11 -4 13 -5 -2
All the subsequences starting from 1st element
14
A Maximum Sub Sequence
Sum Problem
O(n2)
if n = 10000
time constant = 1micro second
=> T = 108/(106*60)
= 102/(60)
1.66 minutes
15
A Maximum Sub Sequence
Sum Problem
Again Not feasible for very large
inputs
Try to identify the inefficiencies in
the algorithm and see if we can
find some feasible algorithm
If not, then we may have to think
in a different way with a different
approach
16
A Maximum Sub Sequence
Sum Problem…. (An Efficient
Way)
There could be many ways: A idea from the
example
-2 11 -12 13 -5 2 8 6 -8
-9
When the sum upto a certain point is
negative (less than zero), then that sum will
reduce the sum of the sequence beyond
this point. am am+1 S2
a1 S1 an
S1 < 0
S1 + S2 <= S2
In order to get maximum sum, we can ignore S1 and the
sub-sequence am+1 to an will have the maximum sum.
17
A Maximum Sub Sequence
Sum Problem
When the sum upto a certain point
is negative (less than zero), then
that sum will reduce the sum of
the sequence beyond this point.
18
A Maximum Sub Sequence Sum
Problem…. (An Efficient Way)
MSS (A[], N) //Where N is the size of the array
{ CS = 0, SE = 0, SS = 0, MS = 0, tempSS=0 ;
for( i = 0; i < N; i++)
{ CS = CS + A[i];
if(CS > MS)
MS = CS;
SE = i ;
SS = tempSS;
else if (CS < 0)
{
CS = 0 ;
tempSS = i+1 ;
}
}
return MS, SS, SE;
}
It is an on-line algorithm with complexity Θ(n)
19
A Maximum Sub Sequence
Sum Problem
O(n)
if n = 10000
time constant = 1micro second
=> T = 104/(106)
= 1/100 Sec
.01 Sec
Excellent
20