Download as pdf or txt
Download as pdf or txt
You are on page 1of 15

Lab Session Unit IV: Dynamic Programming

2D Maximum Subarray Problem

Carlos Cotta

Departamento de Lenguajes y Ciencias de la Computación


Universidad de Málaga

http://www.lcc.uma.es/∼ccottap

Comput Eng, Softw Eng, Comput Sci & Math – 2021-2022

C. Cotta Dynamic Programming Lab 1 / 15


Index

Index

1 Lab Session Unit IV: Dynamic Programming


Problem Statement
A Naive Approach
Kadane’s Algorithm

C. Cotta Dynamic Programming Lab 2 / 15


Problem Statement
Lab Session Unit IV: Dynamic Programming A Naive Approach
Kadane’s Algorithm

Problem Context
We are given a photography and we wish to automatically identify
where the brightest spot is.

We can convert the image to a grayscale 2D matrix. We then


define a reference gray level as baseline: values above it are
positive, and values below it are negative.
C. Cotta Dynamic Programming Lab 3 / 15
Problem Statement
Lab Session Unit IV: Dynamic Programming A Naive Approach
Kadane’s Algorithm

Problem Formulation

Finding the brightest spot in this matrix amounts to the 2D


Maximum Subarray Problem.

2D Maximum Subarray Problem


Let A be a m × n matrix of real values. We want to find a
bounding box given by the coordinates of the upper left corner
(i1 , j1 ) and the coordinates of the bottom right corner (i2 , j2 ), such
that
j2
i2 X
X
BBox(i1 , j1 , i2 , j2 ) = Aij
i=i1 j=j1

is maximal.

C. Cotta Dynamic Programming Lab 4 / 15


Problem Statement
Lab Session Unit IV: Dynamic Programming A Naive Approach
Kadane’s Algorithm

Exhaustive Search
We can naively explore all possible bounding boxes.

Naive approach
best ← −∞
for i1 ← 1 to m do
for j1 ← 1 to n do
for i2 ← i1 to m do
for j2 ← j1 to n do
sum ← BBox(i1 , j1 , i2 , j2 )
if sum > best then
best ← sum
it ← i1 ; jt ← j1 ; ib ← i2 ; jb ← j2
endif
endfor
endfor
endfor
endfor

C. Cotta Dynamic Programming Lab 5 / 15


Problem Statement
Lab Session Unit IV: Dynamic Programming A Naive Approach
Kadane’s Algorithm

Complexity of the Naive Approach

The naive approach makes Θ(m2 n2 ) calls to the BBox function.


The latter function can be also naively done in Θ(mn) time,
resulting in an unpractical complexity Θ(m3 n3 ).
This can be improved as follows: let Cij = jk=1 Aik . Then,
P

i2
X
BBox(i1 , j1 , i2 , j2 ) = (Ck,j2 − Ck,j1 −1 )
k=i1

Hence,
(
0 i2 < i1
BBox(i1 , j1 , i2 , j2 ) =
BBox(i1 , j1 , i2 − 1, j2 ) + Ci2 ,j2 − Ci2 ,j1 −1 i2 ⩾ i1

C. Cotta Dynamic Programming Lab 6 / 15


Problem Statement
Lab Session Unit IV: Dynamic Programming A Naive Approach
Kadane’s Algorithm

Complexity of the Naive Approach

Therefore, if the loops are rearranged so that the innermost loop


runs for increasing values of i2 , each iteration is done in time Θ(1).
Since C can be pre-computed in Θ(mn), the naive approach would
thus require Θ(m2 n2 + mn) = Θ(m2 n2 ), which is still large.
The space complexity is Θ(mn).

C. Cotta Dynamic Programming Lab 7 / 15


Problem Statement
Lab Session Unit IV: Dynamic Programming A Naive Approach
Kadane’s Algorithm

An improved Solution: Kadane’s Algorithm

Let’s consider the one-dimensional case first.

1 2 3 4 5 6 7 8 9
-3 1 -8 12 0 -3 5 -9 4

A brute-force approach can find the maximum subarray on Θ(n2 )


(enumerating all pairs (start, end), and incrementally computing
sums).
We can easily reduce this to Θ(n log n) using Divide & Conquer,
and further down to Θ(n) using dynamic programming.

C. Cotta Dynamic Programming Lab 8 / 15


Problem Statement
Lab Session Unit IV: Dynamic Programming A Naive Approach
Kadane’s Algorithm

1D-Kadane’s Algorithm

Subproblems considered
Si = maximum sum of any subarray finishing in position i (i.e.,
including position i).

Knowing Si−1 , we have two possibilities for Si , namely extend the


subarray with Ai or start a new sequence, i.e.,

Si = max(Si−1 + Ai , Ai )

Note that whenever Si−1 < 0, it is better to start a new sequence.


Also, for i = 1 we only have this possibility (S1 = A1 ).

C. Cotta Dynamic Programming Lab 9 / 15


Problem Statement
Lab Session Unit IV: Dynamic Programming A Naive Approach
Kadane’s Algorithm

1D-Kadane’s Algorithm
The final solution is S = max(Si | 1 ⩽ i ⩽ n).

1D-Kadane’s Algorithm
proc Kadane (↓A[1 . . . n]: R, ↑start, end: N, ↑S: R)
begin
S ← −∞
cur ← 0; j ← 1
for i ← 1 to n do
if cur < 0 then
cur ← Ai ; j ← i
else
cur ← cur + Ai
endif
if cur > S then
S ← cur ; start ← j; end ← i
endif
endfor
end

C. Cotta Dynamic Programming Lab 10 / 15


Problem Statement
Lab Session Unit IV: Dynamic Programming A Naive Approach
Kadane’s Algorithm

2D-Kadane’s Algorithm
In the 2D case, given j1 and j2 we use 1D-Kadane’s
Pj2 algorithm on
cumulative values from the matrix (Bi = j=j1 Aij ).

j1 j2
1 2 3 4 5 6 7 8 B

1 3 2 5 0 -4 5 1 1 3 1

2 -3 -2 -2 -4 4 0 4 -1 -4 2

3 -1 2 -4 7 3 4 -3 2 8 3

4 -4 -3 0 1 -4 -2 -3 4 -6 4

5 0 1 3 4 5 -1 -2 -3 13 5

6 1 -1 2 -3 -3 -4 -3 1 -5 6

We need to iterate this for every possible value of j1 and j2 .

C. Cotta Dynamic Programming Lab 11 / 15


Problem Statement
Lab Session Unit IV: Dynamic Programming A Naive Approach
Kadane’s Algorithm

2D-Kadane’s Algorithm

2D-Kadane’s Algorithm
best ← −∞
for j1 ← 1 to n do
for i ← 1 to m do Bi ← 0 endfor
for j2 ← j1 to n do
for i ← 1 to m do Bi ← Bi + Ai,j2 endfor
Kadane (B, start, end, S)
if S > best then
best ← S
it ← start; jt ← j1 ; ib ← end; jb ← j2
endif
endfor
endfor

C. Cotta Dynamic Programming Lab 12 / 15


Problem Statement
Lab Session Unit IV: Dynamic Programming A Naive Approach
Kadane’s Algorithm

Complexity of 2D-Kadane’s Algorithm

The two outer nested loops (j1 and j2 ) perform Θ(n2 ) iterations.
In each of these, B is computed in Θ(m) and 1D-Kadane’s
algorithm is run in Θ(m) as well.
The overall time complexity is therefore Θ(mn2 ).
The space complexity is Θ(m).

C. Cotta Dynamic Programming Lab 13 / 15


Problem Statement
Lab Session Unit IV: Dynamic Programming A Naive Approach
Kadane’s Algorithm

Complementary Bibliography

J. Bentley,
“Programming Pearls: Algorithm Design Techniques”,
Communications of the ACM 27 (9): 865–873, 1984
doi: 10.1145/358234.381162

C. Cotta Dynamic Programming Lab 14 / 15


Problem Statement
Lab Session Unit IV: Dynamic Programming A Naive Approach
Kadane’s Algorithm

Image Credits

Hyades star cluster by ESA/Hubble. CC BY 4.0. NASA, ESA,


and STScI.

C. Cotta Dynamic Programming Lab 15 / 15

You might also like