0% found this document useful (0 votes)
5 views20 pages

4 Clever Design Examples Max Sum

The document discusses the Maximum Subsequence Sum Problem, which aims to find the contiguous subsequence with the largest sum from a sequence of integers. It presents various algorithms, starting from a brute force approach with O(n^3) complexity to more efficient methods that achieve O(n) complexity. The final algorithm proposed significantly reduces execution time, making it feasible for large inputs like a sequence of 10,000 elements.

Uploaded by

Ayesha Jalil
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views20 pages

4 Clever Design Examples Max Sum

The document discusses the Maximum Subsequence Sum Problem, which aims to find the contiguous subsequence with the largest sum from a sequence of integers. It presents various algorithms, starting from a brute force approach with O(n^3) complexity to more efficient methods that achieve O(n) complexity. The final algorithm proposed significantly reduces execution time, making it feasible for large inputs like a sequence of 10,000 elements.

Uploaded by

Ayesha Jalil
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 20

Design & Analysis of

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

Sum all the subsequences and


compare the sum for the maximum
sum

How to write pseudo code /


Develop algorithm
5
A Maximum Sub Sequence
Sum Problem
 Another Example for algorithm development:
-2 11 -4 13 -5 -2
All the subsequences starting from 1st element

All the subsequences starting from 2nd element

All the subsequences starting from 3rd element

All the subsequences starting from 4th element


All the subsequences starting from 5th element

All the subsequences starting from 6th element


Start point varies from first element to the last
6
End point varies from the starting point to the last element of the list.
A Maximum Sub
Sequence Sum Problem
start = i, end = j
for(i = 0; i < N; i++)
{
for(j = i; j < N; j++)
{
thisSum = 0;
for(k = i; k <= j; k++)
thisSum = thisSum + Array[k];
if(thisSum > MaxSum)
MaxSum = thisSum;
}
}
return MaxSum
7
A Maximum Sub Sequence
Sum Problem
Algorithm:
Max-Subsequence-Sum(Array, N) //Where N is size of Array
{ int this-sum = 0, Max-sum = 0;
for(int i = 0; i < N; i++)
{ for(int j = i; j < N; j++)
{
this-sum = 0;
for(int k = i; k <= j; k++)
this-sum = this-sum + Array[k];
if(this-sum > Max-sum)
Max-sum = this-sum;
}
}
return(Max-sum);
} 8
A Maximum Sub
Sequence Sum Problem
Analysis:
Instructions inside the third loop take constant
time. 10
n n j

 cons tan t =1  1 10  5 1 6


k 5
i 1 j i k i
First Sum N
N ( N  1)
j i  2
1  j  i 1 i 1

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)
i1 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

All the subsequences starting from 2nd element

All the subsequences starting from 3rd element

All the subsequences starting from 4th element


All the subsequences starting from 5th element

All the subsequences starting from 6th element


Start point varies from first element to the last
13
End point varies from the starting point to the last element of the list.
A Maximum Sub Sequence
Sum Problem
M.S.S.S()
{ Max-sum = 0;
for(i = 0; i < N; i++)
{ this-sum = 0;
for(j = i; j < N; j++)
{ this-sum = this-sum + Array[j];
if(this-sum > Max-Sum)
Max-Sum = this-sum
}
}
return Max-Sum;
}

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

Not feasible again

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

You might also like