0% found this document useful (0 votes)
84 views14 pages

Maximum Subarray Problem in 1D and 2D Via Weighted Paths in Directed Acyclic Graphs

This document discusses algorithms for solving the maximum subarray problem in one and two dimensions. It presents a linear-time algorithm for the 1D problem by reducing it to finding the maximum weighted path in a directed acyclic graph. It also introduces "red-blue graphs" as a new data structure for encoding all contiguous submatrices of a matrix to try and extend the approach to the 2D problem, though this only results in a cubic-time algorithm. The author hopes this new technique may help develop faster algorithms for maximum weighted path problems in the future.

Uploaded by

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

Maximum Subarray Problem in 1D and 2D Via Weighted Paths in Directed Acyclic Graphs

This document discusses algorithms for solving the maximum subarray problem in one and two dimensions. It presents a linear-time algorithm for the 1D problem by reducing it to finding the maximum weighted path in a directed acyclic graph. It also introduces "red-blue graphs" as a new data structure for encoding all contiguous submatrices of a matrix to try and extend the approach to the 2D problem, though this only results in a cubic-time algorithm. The author hopes this new technique may help develop faster algorithms for maximum weighted path problems in the future.

Uploaded by

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

Maximum Subarray Problem in 1D and 2D

via Weighted Paths in Directed Acyclic


Graphs Yann Barsamian

To cite this version:


Yann Barsamian. Maximum Subarray Problem in 1D and 2D via Weighted Paths in Directed Acyclic
Graphs. 2016. hal-01585324

HAL Id: hal-01585324


https://hal.archives-ouvertes.fr/hal-01585324
Preprint submitted on 11 Sep 2017

HAL is a multi-disciplinary open access


L’archive ouverte pluridisciplinaire HAL,
archive for the deposit and dissemination of sci-
est destinée au dépôt et à la diffusion de
entific research documents, whether they are
documents scientifiques de niveau recherche,
pub- lished or not. The documents may come
publiés ou non, émanant des établissements
from teaching and research institutions in
d’enseignement et de recherche français ou
France or abroad, or from public or private
étrangers, des laboratoires publics ou privés.
research centers.
Maximum Subarray Problem in 1D and 2D
via Weighted Paths in Directed Acyclic Graphs

Yann Barsamian1

Universit´e de Strasbourg, 300 boulevard S´ebastien


Brant, CS 10413, F-67412 Illkirch Cedex, France
[email protected]

Abstract. The Maximum Subarray Problem was encountered by Ulf


Grenander in [8] for maximum likelihood estimation in pattern
analysis. We are given a vector (or matrix) of numbers, and we have
to find the contiguous sub-vector (or sub-matrix) which has the
maximum sum of numbers in it. Apart from the original application,
the problem also arises for example in biological sequence analysis
([9]).
We present here a linear-time algorithm in one dimension which is dif-
ferent from the one known due to Kadane ([4]), and present a way of
extending it to two dimensions. To achieve the latter, we provide a new
technique, the red-blue graphs, which encodes all the contiguous sub-
matrices of an m × n matrix in size O(m × n).

1 Introduction
The Maximum Subarray Problem (MSP) has been studied since 1977 (see for
example [2, Chapter 1] for a review). Grenander devised an O(n3) algorithm to
solve the MSP in one dimension (1D). In [4], Jon Bentley presented two O(n2)
algorithms, an O(n log n) one and an optimal O(n) algorithm due to Jay Kadane.
Here, we present an alternative O(n) algorithm for the MSP 1D. This al-
gorithm is a reduction to a single-source maximum weighted path problem in
a Directed Acyclic Graph (DAG). In [5, Footnote 1], the authors cite a per-
sonal communication which seems to imply this reduction, but to the best of
our knowledge, this – simple but useful – reduction has not been published
yet. We believe that this technique should be available in a written context.
Following Jon Bentley’s orders (Readers who feel that the linear-time algo-
rithm for the one-dimensional problem is ”obvious” are therefore urged to find
an ”obvious” algorithm for [the two-dimensional problem] ! ), we have designed
a similar technique for the MSP in two dimensions (2D). To scan all the con-
tiguous sub-matrices of a given matrix, we introduce a new type of graphs :
the red-blue graphs. With this data structure, we could nevertheless only come
up with a cubic algorithm, as is the case when we extend Kadane’s algorithm
in two dimensions. Algorithms exist with better complexity (Takaoka, in [10],
reduced this problem to (min, +) Matrix Multiplication for which we have
a
sub-cubic-time algorithm – there is an O(n3 log3 log n/ log2 n) algorithm for
All Pairs Shortest Path by Chan in [6] which shares the same asymptotical
time complexity, cf. [1, pp. 211–212] – leading to an O(m2n log3 log m/ log2
m) algo- rithm for the MSP 2D), but we hope that this new data structure
will lead to
an algorithm for finding maximum weighted paths with less complexity in the
future.
The main contributions of this paper are the presentations of :
1. a reduction of the MSP 1D to a graph problem with optimal complexity
2. a new data structure to scan all the contiguous sub-matrices of a given matrix
The remainder of this paper is organized as follows : Section 2 presents the
problem in 1D and 2D. Section 3 presents a reduction from the MSP 1D to a
single-source maximum weighted path problem in a DAG. Section 4 presents a
reduction from the MSP 2D to a single-source maximum weighted path
problem in a new type of graphs.

2 Presentation of the Problem


2.1 MSP 1D
In one dimension, we are given a vector of n numbers, and we have to find
the contiguous sub-vector which has the maximum sum of numbers in it. If
the
vector only has non-negative (≥ 0) numbers in it, a trivial solution is the
vector itself, and the maximum sum is the total sum. Alternatively if the vector
only has non-positive (≤ 0) numbers, a trivial solution is the empty sub-
vector, and the maximum sum is 0. So an interesting problem only arises
when we look at a vector which contains both positive (> 0) and negative (<
0) numbers. Let us
take the example from [4], and look at the vector :
( 31−415926−535897−93−23 )
V = 84
( )
75 is the sum of elements in V [0..3] = 31 −41 59 26 , −19 is the sum of
( )
elements in V [6..8] = 97 −93 −23 , etc. The maximum sum here is 187, and
( )
lies inside V [2..6] = 59 26 −53 58 97 .
2.2 MSP 2D
In two dimensions, we are given a matrix of m × n numbers, and we have to
find the contiguous sub-matrix which has the maximum sum of numbers in it.
The
previous considerations about positive and negative numbers of course apply.
Let us take the example from [2, Chapter 1], and look at the matrix :
 
−1 2−35−4−8 3 −3
2
M
= 4 1
−4−6−82−5
3
1 −5 2
−29−9−110 2 −6
−35−78−2
 
1
2
6 is the sum of elements in M [1..3] 3 , −14 is the sum of elements
[0..0] = ( 2 −3 5 )
in M [0..1][1..3] = , etc. The maximum sum here is 15, and lies
−4 −6 −8
(−1 10 )
inside M [2..3][4..5] = .
8 −2

3 Reduction of the MSP 1D to Maximum Weighted Path


in Directed Acyclic Graphs
3.1 An Example
( )
Let us look at the vector W = 31 −41 59 26 −53 58 97 . We put the values of
this vector as weights of arcs in a graph. Because we chain the arcs in the same
order as the values in W , the sum of a contiguous sub-vector in W will be the
weight of the corresponding path in the graph, as shown in Fig. 1.

31 −41 59 26 −53 58 97
v0 v1 v2 v3 v4 v5 v6 v7

Fig. 1.

To simplify matters, we add a source vertex s and a sink vertex p, so that


we only have to care about the length of paths between s and p. A contiguous
sub-vector of W is characterized by the location of the first and the last value
in W . From s we simply add 0-weighted arcs to the beginning of each of these
arcs, and to p we add 0-weighted arcs from the end of each of those arcs. In
the end, we have the graph G shown in Fig. 2.

0 0 0 0
0 0 0

31 −41 59 26 −53 58 97
0v0 v1 v2 v3 v4 v5 v6 v7

0 0 0 0 0 0 0
p
Fig. 2.

In this graph, (s, v2, v3, v4, v5, v6, v7,(p) is the path of maximum
) weight, and
corresponds to the sub-vector W [2..6] = 59 26 −53 58 97 .
This example highlighted the reason why looking at the path of maximum
weight between s and p gives the contiguous sub-vector of maximum sum. We
will show in Sect. 3.2 that it is the case, and why doing so takes linear time.

3.2 Proof of Equivalence


In the general case, to a vector V [0..n − 1] of size n, we associate the DAG
G = (S, A) defined as follows :
 L
 S = {s, p} ∪0≤i≤n
 
{vi}
 A = {(s, p)} ∪  {(v , v )} ∪ {(s, v )} ∪  {(v , p)}
0 i

i 


0≤Li≤n−1  L  L 
i+1
0≤i≤n−1 1≤i≤n

With the weights : ∀ 0 ≤ i ≤ n − 1, w((vi, vi+1)) = V [i] and for every


other arc a, w(a) = 0.
Property. This graph has a linear (O(n)) size.
Proof. It has n + 3 vertices and 3n + 1 arcs. □

Property. This graph is acyclic.


Proof. There is no cycle containing s because there is no arc entering s.
There is no cycle containing p because there is no arc leaving p. There is no
cycle containing only vis because there is no arc going from vj to vi if j ≥ i. □
We can derive the topological ordering ([7, Chapter 22]) of that graph from
this proof : s < v0 < · · · < vn < p. We can then apply Bellman’s equations
([3]) to find the path of maximum weight between s and p, leading to a linear-
time algorithm for the MSP 1D shown in Fig. 3. Bellman’s equations lead to an
O(|S|+|A|) algorithm on DAGs, here O(n) because |S| = n +3 and |A| = 3n +1.

Bellman’s algorithm. Kadane’s algorithm ([4]).


1 d[s] ← 0 1 MaxSoFar ← 0
2 For each x in {v0 , . . . , vn, p} 2 MaxEndingHere ← 0
in that order, do 3 For i from 0 to n − 1, do
3 d[x] ← −∞ 4 MaxEndingHere ← max(0,
4 For each predecessor y of x, do MaxEndingHere + V [i])
5 d[x] ← max(d[x], d[y] + w(y, 5 MaxSoFar ← max(MaxSoFar,
x)) MaxEndingHere)
6 End for 6 End for
7 End for 7 Return MaxSoFar
8 Return d[p]

Fig. 3.

We can improve this algorithm to have the same comparisons than Kadane’s
ones. At each vertex vi we make two comparisons because there are two ongoing
arcs : (s, vi) and (vi−1, vi). But (s, vi) weights 0, so we can initialize d with 0
and avoid looking at that arc in the loop. We will end up with one comparison,
as in Kadane’s algorithm to update MaxEndingHere. In Kadane’s algorithm, we
update MaxSoFar at each step. Here, we compute this at the end (when we scan
the predecessors of p), doing the same comparisons.
Theorem. The two algorithms return the same value on every input.
L
Proof. We will show that to every sub-vector V [i..j]i≤j of sum σ = V [k]
i≤k≤j
corresponds a path in G of weight σ. Then we will show that there is no other
path of non-zero weight by a counting argument.
To the empty sub-vector corresponds the path (s, v) of weight 0. To each
con- tiguous sub-vector V [i..j]i≤j corresponds the path (s, vi , vi+1 , . . . , vj ,
vj+1 , p). The weight of this path is w((s, vi)) + w((vi, vi+1)) + · · · + w((vj ,
vj+1)) +
L
w((vj+1 , p)) = 0 + V [i] + · · · + V [j] + 0 = V [k]. Thus :
i≤k≤j

Each contiguous sub-vector sums up to the weight of a path. (*)


By construction, there is exactly one path from vn to p, and for every i ∈
{1, . . . , n − 1} the number of paths from vi to p is equal to the number of
paths from vi+1 to p plus one (because there are only two arcs from vi : the
one to vi+1, and the one to p). And the number of paths from v0 to p is equal
to the number of paths from v1 to p (because there is only one arc from v0 :
the one to v1). It leads to (2 + · · · + n) + n paths from {v0, . . . , vn−1} to p.
Because s has arcs to each of those vertices plus one extra arc to p, it leads
to a total of (2 +· · ·+n)+n +1 paths from s to p. In those paths, we have the
1+(1+· · ·+n) paths which correspond to contiguous sub-vectors of V (for
1 ≤ i ≤ n there are n − i + 1 contiguous sub-vectors of length i in V , plus
one empty sub-vector), to which we add the n − 1 paths (s, vi, p)1≤i≤n−1,
each of weight 0. Thus :
Each path weights either 0 either the sum of a contiguous sub-vector. (**)
Taken together, (*) and (**) imply that the maximum weight in G is equal
to the maximum sum in V . □

4 Reduction of the MSP 2D to Maximum Weighted Path


in Red-Blue Graphs
4.1 General Idea
The main idea is the same as in 1D : put arcs with weights equal to the matrix
elements (thick plain line on next figures). The chosen chaining of the arcs en-
sures that we scan the columns contiguously : we chain the elements of the 0-
th column, then of the 1-st, etc. The blue (normal plain line on next figures)
and red (dotted line on next figures) arcs ensure that the rows are contiguous.
These arcs have weight 0 like in 1D, but have additional labels for row
constraints. A blue
arc labelled x− means that the path corresponds to a contiguous sub-matrix for
which the 0-th (upper when we write the matrix) row is the x-th row in the full
matrix. A red arc labelled y+ means that the path corresponds to a contiguous
sub-matrix for which the last (lower when we write the matrix) row is the y-th
row in the full matrix. To be valid, all blue arcs of a path have to be labelled
with the same number, and the same goes for the red arcs. As in 1D, we
will see that a valid path corresponds to a contiguous sub-matrix, and its
weight is the sum of the elements of the sub-matrix.

Remark : Of course there is a dual approach for the construction of the


graph : chaining the elements of the 0-th row, then of the 1-st, etc. to ensure
that the rows are scanned contiguously, and adding extra constraint arcs (the
blue and red arcs) to ensure that the columns are contiguous.

4.2 An Example

 
Let us take as example the matrix N 1 2 −1 −4
−8 −3 4 −2 . Its associated red-blue
= graph is shown in Fig. 4.  3 8 10 1 

0− 2−1−0−

1− + + + +
n0,0 2+ 1 0 n0,1 0− n0,2 2
+
1 0 n0,3

2− 1 2 −1 −4

n1,0 0+ n1,1 1− n1,2 0+ n1,3

0 −8 −3 4 −2

n2,0 1+ n2,1 2− n2,2 1+ n2,3

3 8 10 1

− −
n3,0 2+ n3,1 0 1 2− n3,2 2+ n3,3 −
0− 1 2−

2+0+ 1+ 2+ 0+ 1+

Fig. 4.
Let us take the path (s, n1,2, n2,2, n3,2, n3,3, n2,3, n1,3, p) shown in Fig. 5,
( 4 −2 )
which corresponds to the sub-matrix N [1..2][2..3] = 10 1 .
⋆ This path leaves the vertex s with a blue arc labelled 1−. It means that
the 0-th row of the contiguous sub-matrix is the 1-st row of N .
⋆ Then it takes the black arcs weighted with 10 and 1. It means that this
sub-matrix will contain these values, located in N [1][2] and N [2][2].
⋆ Then it takes the red arc labelled 2+. It means that the last row of this
sub-matrix is the 2-nd row of N .
⋆ In the next column, the black arcs mean that the sub-matrix contains both
N [2][3] and N [1][3]. The blue arc still indicates that the 0-th row is 1.
The black part of the graph (thick plain line) shows the values inside
the matrix, as in the 1D problem. We will explain in Sect. 4.3 why we put
the arcs alternatively down and up.
The blue part of the graph (normal plain line) shows the 0-th row of the
sub-matrix considered. To be valid, all the blue arcs in a path have to be
labelled with the same number. It means that every column starts at the same
row.
For example, Fig. 6 shows the path (s, n1,0, n2,0, n2,1, n1,1, n0,1, p) which
is not valid because the blue arc (s, n1,0) is labelled 1− but the blue arc (n0,1,
p) ( )
2
is labelled 0−. Of course, is not a sub-matrix !
−8 −3
The red part of the graph (dotted line) shows the last row of the sub-
matrix considered. To be valid, all the red arcs in a path have to be labelled
with the same number. It means that every column ends at the same row.
For example, Fig. 7 shows the path (s, n0,0, n1,0, n1,1, n0,1, n0,2, n1,2, n2,2, p)
which is not valid because the red arc (n1,0, n1,1) is labelled 0+ but the red arc
1 2 −1
(n2,2 , p) is labelled 1+ . Of course, ( ) is not a sub-matrix !
4 why looking at the valid path of
This example highlighted the reason
maxi- mum weight between s and p gives the contiguous sub-matrix of
maximum sum. We will show in Sect. 4.3 that it is the case.
Remark : Because blue and red arcs bear additional information (they have
additional labels to ensure row contiguity), we cannot use standard graph
algo- rithms to compute the maximum weighted path. If we forget these
constraints and just take the maximum weighted path in the underlying
weighted graph
(recall that the weight of those arcs is 0), the resulting maximum weight is an
upper bound for the maximum sum of contiguous sub-matrices.
In the example, the maximum weighted path (if we abstract from the row
constraints) is depicted on Fig. 8. But  
2 −1
−3 4 is not a sub-matrix.
3 8 10 1 
Bellman’s algorithm, which would take quadratic time, only gives us the
upper bound 24, the maximum among contiguous sub-matrices being 22 in
( )
the sub-matrix 3 8 10 1 .
In the general case, we have no more than an upper bound. It can even
happen that the maximum weighted path (if we abstract from the row
constraints) is disjoint from the sub-matrix of maximum sum. In Sect. 4.3 we
will then only focus on valid paths, and show that finding the sub-matrix of
maximum sum and finding the valid path of maximum weight is the same
problem.

s s


1

1
n0,0 n0,1 n0,2 n0,3 n0,0 n0,1 n0,2 n0,3

2
n1,0 n1,1 n1,2 n1,3
n1,0 n1,1 n1,2 n1,3

4 −2
−8 −3
n2,0 n2,1 n2,2 n2,3
n2,0 1+ n2,1 n2,2 n2,3
10 1
+
n3,0 n3,1 n n
,
3 3 −
1
n3 ,0 n3, 10− n3 ,2 n3 ,3
2

p p

Fig. 5. Fig. 6.

s s

0


n0,0 n0,1 0 n0,2 n0,3 n0,0 n0,1 0 n0,2 n0,3

1 2 −1 0 2 −1

n1,0 0+ n1,1 n1,2 n1,3 n1,0 n1,1 n1,2 n1,3

4 −3 4

n2,0 n2,1 n2,2 n2,3 n2,0 n2,1 n2,2 n2,3

3 8 10 1

n3,0 n3,1 n3,2 1+ n3,3 n3,0 0 n3,1 n3,2 0 n3,3


0

p p

Fig. 7. Fig. 8.

4.3 Proof of Equivalence


In the general case, to a matrix M [0..m − 1][0..n − 1] of size m × n, we
associate the red-blue graph H = (T, B) defined as follows :
 T = {s, p} ∪  {vi,j }
  L 
 
0≤j≤n−1
 
0≤i≤m
    
 
{ , v } ∪ { , v } ∪
 B = {(s, p)} ∪
 L
 (vi,2k i+1,2k ) L (vi+1,2k+1 i,2k+1 )

  0≤i≤m−1   0≤i≤m−1 
 0≤2k≤n−1
 1≤2k+1≤n−1 
  L   
L 
 {(vi,2k+1, p)} ∪ L {(vi,2k+1, vi,2k+2)}
  {(s, vi,2k)}∪  0≤i≤m−1 
0≤i≤m−1   0≤i≤m−1
0≤2k≤n−1  1≤2k+1≤n−1  2≤2k+2≤n−1 
     
 L  L 
   {(s, vi,2k+1)} ∪  {(vi,2k, p)} ∪ L (vi,2k i,2k+1 )}
{ , v
∪ 


  
 1≤i≤m  1≤i≤m  1≤i≤m
 1≤2k+1≤n−1  0≤2k≤n−1  1≤2k+1≤n−1
 
With the weights : ∀ 0 ≤ i ≤ m − 1, ∀ 0 ≤ 2k ≤ n − 1, w((vi,2k,
vi+1,2k)) = M [i][2k] and w((vi+1,2k+1, vi,2k+1)) = M [i][2k + 1], and for every
other arc a, w(a) = 0.
And with additional labels for blue and red arcs, that denote the 0-th
and last row of the sub-matrix associated with a path containing these arcs :
blue arcs that enter or leave a vi,j vertex are labelled i− and red arcs that enter
or leave a vi,j vertex are labelled (i − 1)+.
Property. This graph has a quadratic (O(m × n)) size.
Proof. It has (m + 1)n + 2 vertices and 3mn + m(n − 1) + 1 arcs. □

Remark : The black arcs in the graph go down in the first column, then up
in the second one, then down, etc. There are of course other ways of
constructing a similar graph that would allow paths corresponding to sub-
matrices : having all the arcs going down for example. But in that case, we
would need an arc
from each vertex of one column to each vertex which is upper than it in the
next column. It would create (m − 1) + (m − 2) + · · · + 1 arcs per column
hence a
cubic (O(m2n)) number of arcs. If we want a less-than-cubic-time algorithm, we
cannot afford this.
Property. This graph is acyclic.
Proof. There is no cycle containing s because there is no arc going to s.
There is no cycle containing p because there is no arc going from p. There
is no cycle containing only vi,js because there is no arc going from vi,2k to
vj,2k if j ≥ i, there is no arc going from vi,2k+1 to vj,2k+1 if j ≤ i and there is no
arc from vi,k to vj,l if l < k. □

We can derive the topological ordering of that graph from this proof :

⋆ s < v0,0 < · · · < vm,0 < vm,1 < · · · < v0,1 < · · · < v0,n−1 < · · · < vm,n−1 < p
if n is odd and
⋆ s < v0,0 < · · · < vm,0 < vm,1 < · · · < v0,1 < · · · < vm,n−1 < · · · < v0,n−1 < p
if n is even.
Nevertheless, as noted in Sect. 4.2, we cannot apply standard graph algo-
rithms to find the path of maximum weight between s and p, because we need
to check that the paths are valid.
Theorem. The sum of elements in the contiguous sub-matrix of maximum
sum is the same as the weight of the valid path of maximum weight in the
associated red-blue graph.
Proof. We will show that to every sub-matrix N [i..j] of sum σ =
[k..l]i≤j
k≤l
L
L
N [a][b] corresponds a valid path in H of weight σ. Then we will show
i≤a≤j
k≤b≤l
that there is no other valid path of non-zero weight.
To a contiguous sub-matrix N [i..j][k..l]i≤j , if both k and l are even, corre-
k≤l
sponds the path (s, vi,k , . . . , vj+1,k , vj+1,k+1 , . . . , vi,k+1 , . . . , vi,l , . . . vj+1,l , p).
The weight of this path is :
]
w((s, vi,k )) + w((vi,k , vi+1,k )) + · · · + w((vj,k , vj+1,k )) +
]
w((vj+1,k , vj+1,k+1 )) + w((vj+1,k+1 , vj,k+1 )) + · · · + w((vi+1,k+1 , vi,k+1 )) +
]
· · · + w((vi,l , vi+1,l )) + · · · + w((vj,l , vj+1,l )) + w((vj+1,l , p))
( ) ( )
= 0 + N [i][k] + · · · + N [j][k] + 0 + N [j][k + 1] + · · · + N [i][k + 1] + · · · +
( )
N [i][l] + · · · + N [j][l] + 0
L L L L
= N [a][k] + N [a][k + 1] + · · · + N [a][l] =L N [a][b].
i≤a≤j i≤a≤ i≤a≤j i≤a≤j
j k≤b≤l

The computation is similar if k and/or l are odd, and to the empty sub-
vector corresponds the path (s, v) of weight 0. Thus :
Each contiguous sub-matrix sums up to the weight of a valid path. (i)
Let us now take a valid path in the red-blue graph, different from (s, p)
which weights 0. This path thus has at least one blue arc and one red arc (if it
starts
with a blue arc, entering an even column, the only arcs that leave this column
are red ; if it starts with a red arc, entering an odd column, the only arcs that
leave this column are blue). Let us call x− the label of all blue arcs in the path
and y+ the label of all red arcs in the path. Blue arcs enter only even columns (or
p) and red arcs enter only odd columns (or p). Blue arcs leave only odd
columns (or s) and red arcs leave only even columns (or s). There are only two
cases :
⋆ First case : y = x − 1. By construction, the x−-labelled arcs can only
enter or leave vertices in {vx,j | 0 ≤ j ≤ n − 1} and the y+-labelled arcs
can only enter of leave vertices in {vy+1,j | 0 ≤ j ≤ n − 1} (apart from s
and p). Because
y = x − 1, the only vertices apart from s and p are thus vx,j vertices. Thus the
path cannot contain black arcs. Its weight is 0 (an example of that kind of
paths is (s, n1,0, n1,1, n1,2, p)).
⋆ Second case : y ≥ x. For the same reason as in the first case, the path
enters the k-th even column via the vertex in vx,2k and leaves this column
via the vertex vy+1,2k. Thus the black arcs in that column are the arcs in
{(vi,2k, vi+1,2k) | x ≤ i ≤ y} which have the weights {N [i][2k] | x ≤ i ≤ y}.
And the same goes in the k-th odd column : the black arcs are {(vi+1,2k+1,
vi,2k+1) | y ≥ i ≥ x} which have the weights {N [i][2k + 1] | y ≥ i ≥ x}. All
in all, the total weight of the arcs is N [a][b] for some z ≤ t. This
corresponds
L to the sum of elements in
L
x≤a≤y
z≤b≤t

the contiguous sub-matrix N [x..y][z..t].


⋆ The case y < x − 1 is impossible : there are no arcs going up in even
columns so a path cannot enter the k-th even column at vertex vx,2k and leave it
at vertex vy+1,2k if y + 1 < x. The same goes for odd columns : there are no arcs
going down in odd columns so a path cannot enter the k-th odd column at
vertex vy+1,2k+1 and leave it at vertex vx,2k+1 if y + 1 < x. Thus :
Each valid path weights 0 or the sum of a contiguous sub-matrix. (ii)
Taken together, (i) and (ii) imply that the maximum weight of valid paths
in H is equal to the maximum sum of contiguous sub-matrices in N . □

5 Conclusion
In this paper, we have presented a reduction from the maximum subarray
prob- lem in one dimension to a well known graph problem. We think that this
reduc- tion highlights the reason why we have a linear-time algorithm for this
problem.
To extend this idea to two dimensions, we have designed a new data
structure, red-blue graphs, that allows us to scan all the contiguous sub-
matrices of a given matrix. By looking at the valid paths in this data structure,
we obtain a reduction from the maximum subarray problem in two
dimensions. Unfortunately, this reduction does not ”obviously” lead to an
optimal algorithm. Nevertheless, we think that this data structure may help in
finding a better algorithm for the MSP 2D, and maybe for other matrix-related
problems that need to scan contiguous sub-matrices.
A natural extension for the MSP is searching for the k maximum sums,
instead of only the maximum sum. We can allow these sums to overlap (we
have an optimal O(n + k) algorithm in one dimension : [5]) or we can instead
want to
have disjoint sums (we also have an optimal O(n) algorithm in one dimension
: [9]). It’s interesting to note, like Brodal and Jørgensen, that an alternative
optimal algorithm when the overlap is allowed use the weighted graph presented.
If the 1D problem is essentially solved, there remains a lot of work in
higher dimensions. We hope that in the future, part of the ideas presented in
this paper will lead to better algorithms.
Last but not least, we would like to thank our advisor, E´ ric Violard, for his
help on the writing of this article.

References
[1] Alfred V. Aho, John E. Hopcroft, and Jeffrey D. Ullman. The Design and
Analysis of Computer Algorithms. Reading, MA: Addison-Wesley, 1974.
[2] Sung Eun Bae. “Sequential and parallel algorithms for the generalized
maximum subarray problem”. PhD thesis. University of Canterbury,
2007. url: http://hdl.handle.net/10092/1202.
[3] Richard Bellman. On a routing problem. Tech. rep. 1956. url: http://ww
w.dtic.mil/dtic/tr/fulltext/u2/606258.pdf.
[4] Jon Bentley. “Programming Pearls: Algorithm Design Techniques”. In:
Commun. ACM 27.9 (Sept. 1984), pp. 865–873. url: http://www.akira.
ruc.dk/~keld/teaching/algoritmedesign_f07/Artikler/05/Bentley8
4.pdf.
[5] Gerth Stølting Brodal and Allan Grønlund Jørgensen. “A Linear Time Al-
gorithm for the k Maximal Sums Problem”. In: Mathematical
Foundations of Computer Science 2007. Ed. by Ludˇek Kuˇcera and
Anton´ın Kuˇcera. Vol. 4708. Springer Berlin Heidelberg, 2007, pp. 442–
453. url: https:// users-cs.au.dk/gerth/papers/mfcs07sum.pdf.
[6] Timothy M. Chan. “More Algorithms for All-pairs Shortest Paths in
Weighted Graphs”. In: Proceedings of the Thirty-ninth Annual ACM
Symposium on Theory of Computing. STOC’07. 2007, pp. 590–598. url:
https://cs.uw aterloo.ca/~tmchan/moreapsp.pdf.
[7] Thomas H. Cormen, Charles E. Leiserson, Ronald L. Rivest, and Clifford
Stein. Introduction to Algorithms. 3rd ed. MIT Press and McGraw-Hill,
2009.
[8] Ulf Grenander. Pattern analysis: Lectures in Pattern Theory 2. New York:
Springer, 1978.
[9] Walter L. Ruzzo and Martin Tompa. “A Linear Time Algorithm for
Find- ing All Maximal Scoring Subsequences”. In: Proceedings of the
Seventh In- ternational Conference on Intelligent Systems for Molecular
Biology. AAAI Press, 1999, pp. 234–241. url:
http://homes.cs.washington.edu/~ruz
zo/papers/maxseq.pdf.
[10] Tadao Takaoka. “Efficient Algorithms for the Maximum Subarray
Prob- lem by Distance Matrix Multiplication”. In: Electronic Notes in
Theoretical Computer Science. CATS’02, Computing: the Australasian
Theory Sym- posium 61 (2002), pp. 191–200. url:
http://www.cosc.canterbury.ac. nz/tad.takaoka/cats02.pdf.

You might also like