\documentclass[11pt]{article}

\setlength{\topmargin}{-.8in}  
\setlength{\oddsidemargin}{0in}
\setlength{\evensidemargin}{0in}
\setlength{\textwidth}{6.5in}
\setlength{\textheight}{9.5in}


\begin{document}

\title{In-class work: The  divide-and-conquer technique\\
(CLRS 4.1: The maximum sub-array problem)}

\date{}
\maketitle


The \emph{maximum partial sum} (MPS) problem is defined as
follows. Given an array $A$ of $n$ integers, find values of $i$ and
$j$ with $0 \le i \le j < n$ such that $$A[i] + A[i+1] + ... + A[j] = \sum_{k=i}^{j}A[k]$$ is
maximized.

\vspace{\baselineskip}

{\bf Example}: For $A=[4,-5,6,7,8,-10,5]$, the solution to MPS is
$i=2$ and $j=4$ ($6+7+8=21$).


\vspace{\baselineskip}


(1) Consider the following array: $$A = [ 13, -3, -25, 20, -3, -16,
-23, 18, 20, -7, 12, -5, -22, 15, -4, 7 ]$$

Try to find MPS by hand. This will give an example of how MPS can
include negative numbers.

\vspace{5cm}



(2) Describe an algorithm to find the MPS and analyze its running
time. We'll refer to this as the simple, or straightforward algorithm.

\vspace{7cm}



\newpage As always, the question is: Can we do better? For e.g., can
we solve MPS in $O(n \lg n)$ time?  

As it turns out, a very neat $O(n \lg n)$ algorithm for MPS is
possible via divide-and-conquer.  We'll come up with it in a few
steps.

\vspace{\baselineskip} 

(3) First, we consider the \emph{left position $\ell$ maximal partial
  sum} problem ($LMPS_\ell$). Here the left index is given and the
problem is to find the index $j$ ( $\ell \leq j < n$) such that
$$A[\ell] + A[\ell+1] + ... + A[j] = \sum_{k=\ell}^{j}A[k]$$ is maximized. 

{\bf Example}: For the array [4,-5,6,7,8,-10,5] the solution to $LMPS_3$ is $j=4$ ($7+8=15$).

\vspace{\baselineskip}

Describe an $O(n)$ time algorithms for solving $LMPS_{\ell}$ (given
$A$ and $\ell$).

\vspace{8cm}

(4) Similarly, the \emph{right position $r$ maximal partial sum}
problem ($RMPS_r$), consists of finding value $i$ ($0 \leq i\le r$)
such that $$A[i] + A[i+1] + ... + A[r] =\sum_{k=i}^{r}A[k]$$ is
maximized.

{\bf Example}: For $A=[4,-5,6,7,8,-10,5]$ the solution to $RMPS_6$ is
$i=2$ ($5-10+8+7+6=16$).

\vspace{\baselineskip} Describe an $O(n)$ time algorithms for solving
$RMPS_r$ (given $A$ and $r$). 

\vspace{3cm}



\newpage 

(5) Using the algorithm for $LMPS_{\ell}$, describe a simple $O(n^2)$
algorithm for $MPS$.



\vspace{10cm}


(6) Using $O(n)$ time algorithms for $LMPS_{\ell}$ and $RMPS_r$, describe an
  $O(n\log n)$ divide-and-conquer algorithm for solving $MPS$.




% %\item 
% (CLRS 2-4) Let $A[1..n]$ be an array of $n$ distinct numbers. If
%   $i<j$ and $A[i]> A[j]$, then the pair $(i,j)$ is called an inversion
%   of $A$.

% a. List the inversions of the array $<2,3,8,6,1>$. 

% b. What array with elements from the set $\{ 1,2,...,n\}$ has the most
% inversions? How many does it have? 

% c. Give an algorihm that determines the number of inversions in an
% array in  $O(n^2)$ time. 

% d.  Give an algorihm that determines the number of inversions in an
% array in  $O(n\lg n)$ time worst-case (Hint: modify merge sort). 


\end{document}
%%% Local Variables: 
%%% mode: latex
%%% TeX-master: t
%%% End: 
