CCE20003 HGU
CSEg1101 Introduction to Computing
Lecture 19
Department of Computer Science and
Engineering
The School of EE & Computing
Adama Science & Technology Univer-
sity
OUTLINE CCE20003 HGU
Maximum subsequence sum problem
Brute force enumeration
Incremental computation
Divide and conquer
Dynamic programming
MAX. SUBSEQUENCE SUM PROBLEM
CCE20003 HGU
Given a sequence of n numbers, X = [, ….. , , find a sub-
sequence in X such that
(1) The numbers in is contiguous in X
(2) The sum of the numbers in is the maximum
over all contiguous subsequences of S.
(3) The sum of numbers in is positive. Does this
needed?
X = [ 31, -41, 59, 26, -53, 58, 97, -93, -23, 84]
2 6
= X[2 : 7]
CCE20003 HGU
Observations
X = [, , ….. ,
What if > 0 for all 0 i n ?
What if < 0 for all 0 i n ?
BRUTE FORCE ENUMERATION CCE20003 HGU
Basic idea
For all possible subsequences, find their sums and
compare the results to choose the subsequence
with
the maximum sum.
How many subsequences?
How to enumerate them?
CCE20003 HGU
Pseudo code
1. Enumerate all subsequences.
2. For each subsequence, compute the sum of ele-
ments.
3. Compute the maximum by comparing the sum
of
every subsequence
CCE20003 HGU
Step 1: How many subsequences?
X[L : U]
n + (n – 1) + …… + 1 =
L U
∴ subsequences
0 1 X[0 : 1]
n ..…. ………….
How to enumerate?
Nested loop.
n X[0 : n]
1 2 X[1 : 2]
for L in range(n):
n-1 ..…. ………...
for U in range(L+1, n+1):
n X[1 : n]
…. ..…. …………. What to do here?
CCE20003 HGU
Performance Analysis
n = len(X)
MaxSoFar = 0
for L in range(n):
for U in range(L+1, n+1): Why?
sum= 0
for i in range(L, U): Computing the sum of
sum = sum + X[i] X[L, U]
if MaxSoFar < sum:
Finding the
MaxL, MaxU, MaxSoFar = L, U, sum maximum
CCE20003 HGU
Computing the sum of X[L:U]
sum = 0 How many elements in X[L:U]?
for i in range(L, U): U - L elements.
sum = sum + X[i] How many additions?
U - L - 1 additions
∴ At most n - 1 additions. Why?
n – 1 additions when L = 0 and U = n..
∴ At most n – 1 additions to compute the sum of
a subsequence.
CCE20003 HGU
How many additions to find the solution?
n = len(X)
MaxSoFar = 0
for L in range(n): n(n + 1) / 2
for U in range(L+1, n+1): subsequences
sum = 0
for i in range(L, U):
sum = sum + X[i]
At most n - 1 addi-
if MaxSoFar < sum:
tions
MaxL, MaxU, MaxSoFar = L, U, for a subsequence
sum
CCE20003 HGU
∴ At most x (n -1) = ( - n) additions
Time complexity: O()
Space complexity O(n) Why?
X = [, , ….. ,
CCE20003 HGU
Observation
n = len(X)
MaxSoFar = 0
This many additions
for L in range(n):
needed?
X[L : L+1] : 0 addition
for U in range(L+1, n+1): …………………...
sum = 0 X[L : k] : k - L - 1 addi-
tions
for i in range(L, U): X[L : k + 1] : k - L addi-
sum = sum + X[i] tions
if MaxSoFar < sum: …………………….
X[L : n] : n - L - 1 additions
MaxL, MaxU, MaxSoFar = L, U, sum
INCREMENTAL COMPUTATION CCE20003 HGU
X[L : k] : k - L - 1 additions
X[L : k + 1] : k – L additions
sum of X[L : k] = X[L] + X[L+1] + …… + X[k-1]
sum of X[L : k+1] = X[L] + X[L+1] + …… + X[k-1]
+ X[k]
sum of X[L : k]
CCE20003 HGU
n = len(X)
MaxSoFar = 0
for L in range(n):
for U in range(L+1, n+1):
sum = 0
for i in range(L, U): sum = sum + X[U-1]
sum = sum + X[i]
if MaxSoFar < sum:
MaxL, MaxU, MaxSoFar = L, U, sum
CCE20003 HGU
n = len(X)
MaxSoFar = 0
for L in range(n):
sum = 0
for U in range(L+1, n):
sum = sum + X[U-1]
if MaxSoFar < sum:
MaxL, MaxU, MaxSoFar = L, U, sum
O() additions !
DIVIDE AND CONQUER CCE20003 HGU
Basic Idea
1. If the sequence X has only one element, then
return max(0, X[0]).
base case
2. Otherwise, divide the sequence into two sub-se-
quences of almost equal size, and compute the max-
imum sum for each of sub-sequences recursively.
recursive case
3. Combine the solutions in step 2 to solve the original
problem.
CCE20003 HGU
Step 3: How to combine the solutions of sub-
problems X
XL m+ XR
L m U
1
MaxL MaxM MaxR
m = (L + U) / 2
MaxX = max(MaxL, MaxR, MaxM)
CCE20003 HGU
How to compute MaxM
L m+ U
m
1 MaxMR
MaxML
MaxM = MaxML + MaxMR
MaxML = max(sum(L,m), sum(L+1,m), ….., sum(m-1,m), sum(m,m))
MaxMR = max(sum(m+1,m+1), sum(m+1,m+2), ….. , sum(m+1, U),
sum(m+1,U))
How to compute MaxML and MaxMR ?
CCE20003 HGU
How to compute MaxML
MaxML = max(sum(L,m), sum(L+1,m), ….. Sum(m-1,m),
sum(m,m))
sum(i , m) sum(i + 1, m) + X[i]
=O(n)
time already com-
puted
How to compute MAXMR
MaxMR = max(sum(m+1,m+1), sum(m+1,m+2), …..
sum(m+1,U)) sum(m + 1, i - 1) + X[i ]
sum(m + 1, i) =
O(n)
already computed
time
CCE20003 HGU
sum(i , U) =sum(i+1, U) + X[i], L
def comp_MaxML(L, U, X):
sum = 0
MaxML = 0
for i in range(U – L + 1):
sum = sum + X[U - i ]
if sum > MaxML :
MaxML = sum
return MaxML
O(n)
time
CCE20003 HGU
sum(L, i-1) + X[i],
sum(L, i) =
def comp_MaxMR(L, U, X):
sum = 0
MaxMR = 0
for i in range(L, U+1):
sum = sum + X[i ]
if sum > MaxMR :
MaxMR = sum
return MaxMR
O(n)
time
CCE20003 HGU
def max_sub(L, U, X):
if L == U:
return max(0, X[L]) basis case
m = (L + U) / 2
MaxL = max_sub(L, m, X) Divide the prob-
MaxR = max_sub(m+1, U, X) lem
MaxML = comp_MaxML(L, m, X)
Combine solu-
MaxMR = comp_MaxMR(m+1, U, X)
tions
MaxM = max(0, MaxML+MaxMR) O(n) per
round
return max(MaxL, MaxR, MaxM)
How many rounds of recursion ?
CCE20003 HGU
[ 31, -41, 59, 26, -53, 58, 97, -93, -23, 84]
[ 31, -41, 59, 26, -53][58, 97, -93, -23, 84]
[ 31, -41][59, 26, -53][58, 97][-93, -23, 84]
[ 31, -41][59, 26, -53][58, 97][-93, -23, 84]
[ 31][-41][59][26, -53][58][97][-93][-23, 84]
[26][-53] [-23][84]
n/ =1 k = rounds
∴ O(n
DYNAMIC PROGRAMMIMG CCE20003 HGU
Basic idea
X[0:i+1]
X[0:i]
MaxSo-
MaxTail
Far
MaxSoFar: The sum of the maximum subsequence in
X[0 : i]
MaxTail : The sum of the maximum subsequence that
ends at X[i-1]
Given MaxSoFar and MaxTail for X[0 : i], how can we
find those for X[0 : i+1} ?
CCE20003 HGU
X = [ 31, -41, 59, 26,-53, 58, 97, -93, -23, 84]
For X[0 : 5],
MaxSoFar = 59 + 26 = 85
MaxTail = 59 + 26 + (-53) = 32
What is MaxSoFar and MaxTail for X[0:6]?
MaxTail = max((0, MaxTail+X[5])
= max(0, 32+58) =90
MaxSoFar = max(MaxSoFar, MaxTail) Why?
= max(85, 90) =90
O(1) time
CCE20003 HGU
Recursive equations
MaxTail[i] max(0, X[0]) if i = 0
= max(0, MaxTail[i-1] + X[i]), otherwise
MaxSoFar[i] max(0, MaxTail[0]) if i =0
= max(MaxSoFar[i-1], MaxTail[i]), other-
wise
CCE20003 HGU
def max_sub(X):
MaxTail = 0
MaxSoFar = 0
for i in range(len(X)):
MaxTail = max(0, MaxTail + X[i])
MaxSoFar = max(MaxSoFar, MaxTail)O(1) time
return MaxSoFar
O(n) time
CCE20003 HGU
Example: X = [ 31, -41, 59, 26, -53, 58, 97, -93, -
23, 84]
MaxTail = max(0, Maxtail + X[i])
MaxSoFar
i =MaxTail
max(MaxSoFar, MaxTail)
MaxSoFar
0 31 31
1 0 31
2 59 59
3 85 85
4 32 85
5 90 90
6 187 187
7 94 187
8 71 187
9 155 187