cphb/chapter08.tex

841 lines
22 KiB
TeX
Raw Normal View History

2016-12-28 23:54:51 +01:00
\chapter{Amortized analysis}
2017-01-03 00:49:59 +01:00
\index{amortized analysis}
2017-02-03 23:47:45 +01:00
The time complexity of an algorithm
is often easy to analyze
just by examining the structure
2017-01-03 00:49:59 +01:00
of the algorithm:
2017-02-27 20:29:32 +01:00
what loops does the algorithm contain
2017-02-03 23:47:45 +01:00
and how many times the loops are performed.
2017-01-03 00:49:59 +01:00
However, sometimes a straightforward analysis
2017-02-03 23:47:45 +01:00
does not give a true picture of the efficiency of the algorithm.
2017-01-03 00:49:59 +01:00
\key{Amortized analysis} can be used for analyzing
2017-02-03 23:47:45 +01:00
algorithms that contain operations whose
2017-01-03 00:49:59 +01:00
time complexity varies.
2017-02-03 23:47:45 +01:00
The idea is to estimate the total time used for
all such operations during the
execution of the algorithm, instead of focusing
on individual operations.
2017-01-03 00:49:59 +01:00
\section{Two pointers method}
\index{two pointers method}
In the \key{two pointers method},
2017-02-03 23:47:45 +01:00
two pointers are used for
iterating through the elements in an array.
2017-01-03 00:49:59 +01:00
Both pointers can move during the algorithm,
2017-02-03 23:47:45 +01:00
but each pointer can move to one direction only.
This restriction ensures that the algorithm works efficiently.
2017-01-03 00:49:59 +01:00
We will next discuss two problems that can be solved
using the two pointers method.
\subsubsection{Subarray sum}
2017-02-03 23:47:45 +01:00
As the first example,
we consider a problem where we are
given an array of $n$ positive numbers
and a target sum $x$,
and we should find a subarray whose sum is $x$
or report that there is no such subarray.
2017-01-03 00:49:59 +01:00
For example, the array
2016-12-28 23:54:51 +01:00
\begin{center}
\begin{tikzpicture}[scale=0.7]
\draw (0,0) grid (8,1);
\node at (0.5,0.5) {$1$};
\node at (1.5,0.5) {$3$};
\node at (2.5,0.5) {$2$};
\node at (3.5,0.5) {$5$};
\node at (4.5,0.5) {$1$};
\node at (5.5,0.5) {$1$};
\node at (6.5,0.5) {$2$};
\node at (7.5,0.5) {$3$};
\footnotesize
\node at (0.5,1.4) {$1$};
\node at (1.5,1.4) {$2$};
\node at (2.5,1.4) {$3$};
\node at (3.5,1.4) {$4$};
\node at (4.5,1.4) {$5$};
\node at (5.5,1.4) {$6$};
\node at (6.5,1.4) {$7$};
\node at (7.5,1.4) {$8$};
\end{tikzpicture}
\end{center}
2017-02-03 23:47:45 +01:00
contains a subarray whose sum is 8:
2016-12-28 23:54:51 +01:00
\begin{center}
\begin{tikzpicture}[scale=0.7]
\fill[color=lightgray] (2,0) rectangle (5,1);
\draw (0,0) grid (8,1);
\node at (0.5,0.5) {$1$};
\node at (1.5,0.5) {$3$};
\node at (2.5,0.5) {$2$};
\node at (3.5,0.5) {$5$};
\node at (4.5,0.5) {$1$};
\node at (5.5,0.5) {$1$};
\node at (6.5,0.5) {$2$};
\node at (7.5,0.5) {$3$};
\footnotesize
\node at (0.5,1.4) {$1$};
\node at (1.5,1.4) {$2$};
\node at (2.5,1.4) {$3$};
\node at (3.5,1.4) {$4$};
\node at (4.5,1.4) {$5$};
\node at (5.5,1.4) {$6$};
\node at (6.5,1.4) {$7$};
\node at (7.5,1.4) {$8$};
\end{tikzpicture}
\end{center}
2017-01-03 00:49:59 +01:00
It turns out that the problem can be solved in
2017-02-03 23:47:45 +01:00
$O(n)$ time by using the two pointers method.
2017-02-27 20:29:32 +01:00
The idea is to use
left and right pointers that indicate the
2017-02-03 23:47:45 +01:00
first and last element of an subarray.
2017-01-03 00:49:59 +01:00
On each turn, the left pointer moves one step
forward, and the right pointer moves forward
2017-02-03 23:47:45 +01:00
as long as the subarray sum is at most $x$.
If the sum becomes exactly $x$,
2017-02-13 23:06:35 +01:00
a solution has been found.
2017-01-03 00:49:59 +01:00
2017-02-03 23:47:45 +01:00
As an example, consider the following array
2017-01-03 00:49:59 +01:00
with target sum $x=8$:
2016-12-28 23:54:51 +01:00
\begin{center}
\begin{tikzpicture}[scale=0.7]
\draw (0,0) grid (8,1);
\node at (0.5,0.5) {$1$};
\node at (1.5,0.5) {$3$};
\node at (2.5,0.5) {$2$};
\node at (3.5,0.5) {$5$};
\node at (4.5,0.5) {$1$};
\node at (5.5,0.5) {$1$};
\node at (6.5,0.5) {$2$};
\node at (7.5,0.5) {$3$};
\footnotesize
\node at (0.5,1.4) {$1$};
\node at (1.5,1.4) {$2$};
\node at (2.5,1.4) {$3$};
\node at (3.5,1.4) {$4$};
\node at (4.5,1.4) {$5$};
\node at (5.5,1.4) {$6$};
\node at (6.5,1.4) {$7$};
\node at (7.5,1.4) {$8$};
\end{tikzpicture}
\end{center}
2017-02-03 23:47:45 +01:00
Initially, the subarray contains the elements
1, 3 and 2, and the sum of the subarray is 6.
2017-02-13 23:06:35 +01:00
The subarray cannot be larger, because
2017-02-03 23:47:45 +01:00
the next element 5 would make the sum larger than $x$.
2016-12-28 23:54:51 +01:00
\begin{center}
\begin{tikzpicture}[scale=0.7]
\fill[color=lightgray] (0,0) rectangle (3,1);
\draw (0,0) grid (8,1);
\node at (0.5,0.5) {$1$};
\node at (1.5,0.5) {$3$};
\node at (2.5,0.5) {$2$};
\node at (3.5,0.5) {$5$};
\node at (4.5,0.5) {$1$};
\node at (5.5,0.5) {$1$};
\node at (6.5,0.5) {$2$};
\node at (7.5,0.5) {$3$};
\draw[thick,->] (0.5,-0.7) -- (0.5,-0.1);
\draw[thick,->] (2.5,-0.7) -- (2.5,-0.1);
\footnotesize
\node at (0.5,1.4) {$1$};
\node at (1.5,1.4) {$2$};
\node at (2.5,1.4) {$3$};
\node at (3.5,1.4) {$4$};
\node at (4.5,1.4) {$5$};
\node at (5.5,1.4) {$6$};
\node at (6.5,1.4) {$7$};
\node at (7.5,1.4) {$8$};
\end{tikzpicture}
\end{center}
2017-02-03 23:47:45 +01:00
Then, the left pointer moves one step forward.
2017-02-13 23:06:35 +01:00
The right pointer does not move, because otherwise
2017-01-03 00:49:59 +01:00
the sum would become too large.
2016-12-28 23:54:51 +01:00
\begin{center}
\begin{tikzpicture}[scale=0.7]
\fill[color=lightgray] (1,0) rectangle (3,1);
\draw (0,0) grid (8,1);
\node at (0.5,0.5) {$1$};
\node at (1.5,0.5) {$3$};
\node at (2.5,0.5) {$2$};
\node at (3.5,0.5) {$5$};
\node at (4.5,0.5) {$1$};
\node at (5.5,0.5) {$1$};
\node at (6.5,0.5) {$2$};
\node at (7.5,0.5) {$3$};
\draw[thick,->] (1.5,-0.7) -- (1.5,-0.1);
\draw[thick,->] (2.5,-0.7) -- (2.5,-0.1);
\footnotesize
\node at (0.5,1.4) {$1$};
\node at (1.5,1.4) {$2$};
\node at (2.5,1.4) {$3$};
\node at (3.5,1.4) {$4$};
\node at (4.5,1.4) {$5$};
\node at (5.5,1.4) {$6$};
\node at (6.5,1.4) {$7$};
\node at (7.5,1.4) {$8$};
\end{tikzpicture}
\end{center}
2017-01-03 00:49:59 +01:00
Again, the left pointer moves one step forward,
and this time the right pointer moves three
steps forward.
The sum is $2+5+1=8$, so we have found a subarray
where the sum of the elements is $x$.
2016-12-28 23:54:51 +01:00
\begin{center}
\begin{tikzpicture}[scale=0.7]
\fill[color=lightgray] (2,0) rectangle (5,1);
\draw (0,0) grid (8,1);
\node at (0.5,0.5) {$1$};
\node at (1.5,0.5) {$3$};
\node at (2.5,0.5) {$2$};
\node at (3.5,0.5) {$5$};
\node at (4.5,0.5) {$1$};
\node at (5.5,0.5) {$1$};
\node at (6.5,0.5) {$2$};
\node at (7.5,0.5) {$3$};
\draw[thick,->] (2.5,-0.7) -- (2.5,-0.1);
\draw[thick,->] (4.5,-0.7) -- (4.5,-0.1);
\footnotesize
\node at (0.5,1.4) {$1$};
\node at (1.5,1.4) {$2$};
\node at (2.5,1.4) {$3$};
\node at (3.5,1.4) {$4$};
\node at (4.5,1.4) {$5$};
\node at (5.5,1.4) {$6$};
\node at (6.5,1.4) {$7$};
\node at (7.5,1.4) {$8$};
\end{tikzpicture}
\end{center}
2017-01-03 00:49:59 +01:00
The time complexity of the algorithm depends on
the number of steps the right pointer moves.
2017-02-27 20:29:32 +01:00
There is no useful upper bound how many steps the
2017-01-03 00:49:59 +01:00
pointer can move on a single turn.
However, the pointer moves \emph{a total of}
2017-02-03 23:47:45 +01:00
$O(n)$ steps during the algorithm,
2017-01-03 00:49:59 +01:00
because it only moves forward.
2017-02-03 23:47:45 +01:00
Since both the left and right pointer
2017-01-03 00:49:59 +01:00
move $O(n)$ steps during the algorithm,
the time complexity is $O(n)$.
2017-02-20 22:23:10 +01:00
\subsubsection{2SUM problem}
2017-01-03 00:49:59 +01:00
2017-02-20 22:23:10 +01:00
\index{2SUM problem}
2017-01-03 00:49:59 +01:00
2017-02-03 23:47:45 +01:00
Another problem that can be solved using
the two pointers method is the following problem,
2017-02-20 22:23:10 +01:00
also known as the \key{2SUM problem}:
2017-02-27 20:29:32 +01:00
we are given an array of $n$ numbers and
2017-02-03 23:47:45 +01:00
a target sum $x$, and our task is to find two numbers
in the array such that their sum is $x$,
or report that no such numbers exist.
2017-01-03 00:49:59 +01:00
2017-02-03 23:47:45 +01:00
To solve the problem, we first
sort the numbers in the array in increasing order.
After that, we iterate through the array using
2017-02-13 23:06:35 +01:00
two pointers.
The left pointer starts at the first element
2017-01-03 00:49:59 +01:00
and moves one step forward on each turn.
2017-02-03 23:47:45 +01:00
The right pointer begins at the last element
and always moves backward until the sum of the
2017-02-13 23:06:35 +01:00
elements is at most $x$.
If the sum of the elements is exactly $x$,
a solution has been found.
2017-01-03 00:49:59 +01:00
2017-02-03 23:47:45 +01:00
For example, consider the following array
with target sum $x=12$:
2016-12-28 23:54:51 +01:00
\begin{center}
\begin{tikzpicture}[scale=0.7]
\draw (0,0) grid (8,1);
\node at (0.5,0.5) {$1$};
\node at (1.5,0.5) {$4$};
\node at (2.5,0.5) {$5$};
\node at (3.5,0.5) {$6$};
\node at (4.5,0.5) {$7$};
\node at (5.5,0.5) {$9$};
\node at (6.5,0.5) {$9$};
\node at (7.5,0.5) {$10$};
\footnotesize
\node at (0.5,1.4) {$1$};
\node at (1.5,1.4) {$2$};
\node at (2.5,1.4) {$3$};
\node at (3.5,1.4) {$4$};
\node at (4.5,1.4) {$5$};
\node at (5.5,1.4) {$6$};
\node at (6.5,1.4) {$7$};
\node at (7.5,1.4) {$8$};
\end{tikzpicture}
\end{center}
2017-01-03 00:49:59 +01:00
The initial positions of the pointers
are as follows.
The sum of the numbers is $1+10=11$
that is smaller than $x$.
2016-12-28 23:54:51 +01:00
\begin{center}
\begin{tikzpicture}[scale=0.7]
\fill[color=lightgray] (0,0) rectangle (1,1);
\fill[color=lightgray] (7,0) rectangle (8,1);
\draw (0,0) grid (8,1);
\node at (0.5,0.5) {$1$};
\node at (1.5,0.5) {$4$};
\node at (2.5,0.5) {$5$};
\node at (3.5,0.5) {$6$};
\node at (4.5,0.5) {$7$};
\node at (5.5,0.5) {$9$};
\node at (6.5,0.5) {$9$};
\node at (7.5,0.5) {$10$};
\draw[thick,->] (0.5,-0.7) -- (0.5,-0.1);
\draw[thick,->] (7.5,-0.7) -- (7.5,-0.1);
\footnotesize
\node at (0.5,1.4) {$1$};
\node at (1.5,1.4) {$2$};
\node at (2.5,1.4) {$3$};
\node at (3.5,1.4) {$4$};
\node at (4.5,1.4) {$5$};
\node at (5.5,1.4) {$6$};
\node at (6.5,1.4) {$7$};
\node at (7.5,1.4) {$8$};
\end{tikzpicture}
\end{center}
2017-01-03 00:49:59 +01:00
Then the left pointer moves one step forward.
The right pointer moves three steps backward,
and the sum becomes $4+7=11$.
2016-12-28 23:54:51 +01:00
\begin{center}
\begin{tikzpicture}[scale=0.7]
\fill[color=lightgray] (1,0) rectangle (2,1);
\fill[color=lightgray] (4,0) rectangle (5,1);
\draw (0,0) grid (8,1);
\node at (0.5,0.5) {$1$};
\node at (1.5,0.5) {$4$};
\node at (2.5,0.5) {$5$};
\node at (3.5,0.5) {$6$};
\node at (4.5,0.5) {$7$};
\node at (5.5,0.5) {$9$};
\node at (6.5,0.5) {$9$};
\node at (7.5,0.5) {$10$};
\draw[thick,->] (1.5,-0.7) -- (1.5,-0.1);
\draw[thick,->] (4.5,-0.7) -- (4.5,-0.1);
\footnotesize
\node at (0.5,1.4) {$1$};
\node at (1.5,1.4) {$2$};
\node at (2.5,1.4) {$3$};
\node at (3.5,1.4) {$4$};
\node at (4.5,1.4) {$5$};
\node at (5.5,1.4) {$6$};
\node at (6.5,1.4) {$7$};
\node at (7.5,1.4) {$8$};
\end{tikzpicture}
\end{center}
2017-01-03 00:49:59 +01:00
After this, the left pointer moves one step forward again.
2017-02-03 23:47:45 +01:00
The right pointer does not move, and a solution
2017-01-03 00:49:59 +01:00
$5+7=12$ has been found.
2016-12-28 23:54:51 +01:00
\begin{center}
\begin{tikzpicture}[scale=0.7]
\fill[color=lightgray] (2,0) rectangle (3,1);
\fill[color=lightgray] (4,0) rectangle (5,1);
\draw (0,0) grid (8,1);
\node at (0.5,0.5) {$1$};
\node at (1.5,0.5) {$4$};
\node at (2.5,0.5) {$5$};
\node at (3.5,0.5) {$6$};
\node at (4.5,0.5) {$7$};
\node at (5.5,0.5) {$9$};
\node at (6.5,0.5) {$9$};
\node at (7.5,0.5) {$10$};
\draw[thick,->] (2.5,-0.7) -- (2.5,-0.1);
\draw[thick,->] (4.5,-0.7) -- (4.5,-0.1);
\footnotesize
\node at (0.5,1.4) {$1$};
\node at (1.5,1.4) {$2$};
\node at (2.5,1.4) {$3$};
\node at (3.5,1.4) {$4$};
\node at (4.5,1.4) {$5$};
\node at (5.5,1.4) {$6$};
\node at (6.5,1.4) {$7$};
\node at (7.5,1.4) {$8$};
\end{tikzpicture}
\end{center}
2017-02-03 23:47:45 +01:00
The algorithm consists of two phases:
First, sorting the array takes $O(n \log n)$ time.
2017-01-03 00:49:59 +01:00
After this, the left pointer moves $O(n)$ steps
forward, and the right pointer moves $O(n)$ steps
backward. Thus, the total time complexity
of the algorithm is $O(n \log n)$.
2017-02-03 23:47:45 +01:00
Note that it is possible to solve the problem
2017-01-03 00:49:59 +01:00
in another way in $O(n \log n)$ time using binary search.
2017-02-03 23:47:45 +01:00
In such a solution, we iterate through the array
2017-01-03 00:49:59 +01:00
and for each number, we try to find another
number such that the sum is $x$.
This can be done by performing $n$ binary searches,
and each search takes $O(\log n)$ time.
2016-12-28 23:54:51 +01:00
2017-02-20 22:23:10 +01:00
\index{3SUM problem}
2017-02-13 23:06:35 +01:00
A more difficult problem is
2017-02-20 22:23:10 +01:00
the \key{3SUM problem} that asks to
2017-02-03 23:47:45 +01:00
find \emph{three} numbers in the array
such that their sum is $x$.
2017-02-21 00:17:36 +01:00
Using the idea of the above algorithm,
this problem can be solved in $O(n^2)$ time\footnote{For a long time,
it was thought that solving
the 3SUM problem more efficiently than in $O(n^2)$ time
would not be possible.
However, in 2014, it turned out \cite{gro14}
that this is not the case.}.
2017-02-20 22:23:10 +01:00
Can you see how?
2016-12-28 23:54:51 +01:00
2017-01-03 01:06:48 +01:00
\section{Nearest smaller elements}
\index{nearest smaller elements}
Amortized analysis is often used for
estimating the number of operations
2017-02-13 23:06:35 +01:00
performed on a data structure.
2017-01-03 01:06:48 +01:00
The operations may be distributed unevenly so
2017-02-03 23:47:45 +01:00
that most operations occur during a
certain phase of the algorithm, but the total
2017-01-03 01:06:48 +01:00
number of the operations is limited.
2017-02-03 23:47:45 +01:00
As an example, consider the problem
of finding for each element
2017-02-13 23:06:35 +01:00
of an array the
2017-01-03 01:06:48 +01:00
\key{nearest smaller element}, i.e.,
2017-02-03 23:47:45 +01:00
the first smaller element that precedes
2017-01-03 01:06:48 +01:00
the element in the array.
It is possible that no such element exists,
2017-02-03 23:47:45 +01:00
in which case the algorithm should report this.
It turns out that the problem can be solved
in $O(n)$ time using an appropriate data structure.
2017-01-03 01:06:48 +01:00
2017-02-03 23:47:45 +01:00
An efficient solution to the problem is to
2017-02-27 20:29:32 +01:00
iterate through the array from left to right
2017-01-03 01:06:48 +01:00
and maintain a chain of elements where the
2017-02-13 23:06:35 +01:00
first element is the current element
2017-01-03 01:06:48 +01:00
and each following element is the nearest smaller
element of the previous element.
If the chain only contains one element,
2017-02-27 20:29:32 +01:00
the current element does not have a nearest smaller element.
2017-02-03 23:47:45 +01:00
At each step, elements are removed from the chain
2017-01-03 01:06:48 +01:00
until the first element is smaller
2017-02-03 23:47:45 +01:00
than the current element, or the chain is empty.
After this, the current element is added to the chain.
2017-01-03 01:06:48 +01:00
As an example, consider the following array:
2016-12-28 23:54:51 +01:00
\begin{center}
\begin{tikzpicture}[scale=0.7]
\draw (0,0) grid (8,1);
\node at (0.5,0.5) {$1$};
\node at (1.5,0.5) {$3$};
\node at (2.5,0.5) {$4$};
\node at (3.5,0.5) {$2$};
\node at (4.5,0.5) {$5$};
\node at (5.5,0.5) {$3$};
\node at (6.5,0.5) {$4$};
\node at (7.5,0.5) {$2$};
\footnotesize
\node at (0.5,1.4) {$1$};
\node at (1.5,1.4) {$2$};
\node at (2.5,1.4) {$3$};
\node at (3.5,1.4) {$4$};
\node at (4.5,1.4) {$5$};
\node at (5.5,1.4) {$6$};
\node at (6.5,1.4) {$7$};
\node at (7.5,1.4) {$8$};
\end{tikzpicture}
\end{center}
2017-02-03 23:47:45 +01:00
First, the numbers 1, 3 and 4 are added to the chain,
because each number is larger than the previous number.
Thus, the nearest smaller element of 4 is 3,
and the nearest smaller element of 3 is 1.
2016-12-28 23:54:51 +01:00
\begin{center}
\begin{tikzpicture}[scale=0.7]
\fill[color=lightgray] (2,0) rectangle (3,1);
\draw (0,0) grid (8,1);
\node at (0.5,0.5) {$1$};
\node at (1.5,0.5) {$3$};
\node at (2.5,0.5) {$4$};
\node at (3.5,0.5) {$2$};
\node at (4.5,0.5) {$5$};
\node at (5.5,0.5) {$3$};
\node at (6.5,0.5) {$4$};
\node at (7.5,0.5) {$2$};
\draw[thick,->] (2.5,-0.25) .. controls (2.25,-1.00) and (1.75,-1.00) .. (1.6,-0.25);
\draw[thick,->] (1.4,-0.25) .. controls (1.25,-1.00) and (0.75,-1.00) .. (0.5,-0.25);
\footnotesize
\node at (0.5,1.4) {$1$};
\node at (1.5,1.4) {$2$};
\node at (2.5,1.4) {$3$};
\node at (3.5,1.4) {$4$};
\node at (4.5,1.4) {$5$};
\node at (5.5,1.4) {$6$};
\node at (6.5,1.4) {$7$};
\node at (7.5,1.4) {$8$};
\end{tikzpicture}
\end{center}
2017-02-03 23:47:45 +01:00
The next number 2 is smaller than the two first numbers in the chain.
Thus, the numbers 4 and 3 are removed from the chain,
and then the number 2
is added to the chain.
Its nearest smaller element is 1:
2016-12-28 23:54:51 +01:00
\begin{center}
\begin{tikzpicture}[scale=0.7]
\fill[color=lightgray] (3,0) rectangle (4,1);
\draw (0,0) grid (8,1);
\node at (0.5,0.5) {$1$};
\node at (1.5,0.5) {$3$};
\node at (2.5,0.5) {$4$};
\node at (3.5,0.5) {$2$};
\node at (4.5,0.5) {$5$};
\node at (5.5,0.5) {$3$};
\node at (6.5,0.5) {$4$};
\node at (7.5,0.5) {$2$};
\draw[thick,->] (3.5,-0.25) .. controls (3.00,-1.00) and (1.00,-1.00) .. (0.5,-0.25);
\footnotesize
\node at (0.5,1.4) {$1$};
\node at (1.5,1.4) {$2$};
\node at (2.5,1.4) {$3$};
\node at (3.5,1.4) {$4$};
\node at (4.5,1.4) {$5$};
\node at (5.5,1.4) {$6$};
\node at (6.5,1.4) {$7$};
\node at (7.5,1.4) {$8$};
\end{tikzpicture}
\end{center}
2017-02-03 23:47:45 +01:00
After this, the number 5 is larger than the number 2,
so it will be added to the chain, and
its nearest smaller element is 2:
2016-12-28 23:54:51 +01:00
\begin{center}
\begin{tikzpicture}[scale=0.7]
\fill[color=lightgray] (4,0) rectangle (5,1);
\draw (0,0) grid (8,1);
\node at (0.5,0.5) {$1$};
\node at (1.5,0.5) {$3$};
\node at (2.5,0.5) {$4$};
\node at (3.5,0.5) {$2$};
\node at (4.5,0.5) {$5$};
\node at (5.5,0.5) {$3$};
\node at (6.5,0.5) {$4$};
\node at (7.5,0.5) {$2$};
\draw[thick,->] (3.4,-0.25) .. controls (3.00,-1.00) and (1.00,-1.00) .. (0.5,-0.25);
\draw[thick,->] (4.5,-0.25) .. controls (4.25,-1.00) and (3.75,-1.00) .. (3.6,-0.25);
\footnotesize
\node at (0.5,1.4) {$1$};
\node at (1.5,1.4) {$2$};
\node at (2.5,1.4) {$3$};
\node at (3.5,1.4) {$4$};
\node at (4.5,1.4) {$5$};
\node at (5.5,1.4) {$6$};
\node at (6.5,1.4) {$7$};
\node at (7.5,1.4) {$8$};
\end{tikzpicture}
\end{center}
2017-02-13 23:06:35 +01:00
The algorithm continues in the same way
2017-02-03 23:47:45 +01:00
and finds the nearest smaller element
2017-01-03 01:06:48 +01:00
for each number in the array.
But how efficient is the algorithm?
The efficiency of the algorithm depends on
the total time used for manipulating the chain.
If an element is larger than the first
2017-02-13 23:06:35 +01:00
element of the chain, it is directly added
2017-02-03 23:47:45 +01:00
to the chain, which is efficient.
2017-01-03 01:06:48 +01:00
However, sometimes the chain can contain several
2017-02-13 23:06:35 +01:00
larger elements and it takes time to remove them.
2017-01-03 01:06:48 +01:00
Still, each element is added exactly once to the chain
2017-02-03 23:47:45 +01:00
and removed at most once from the chain.
Thus, each element causes $O(1)$ chain operations,
and the total time complexity
2017-01-03 01:06:48 +01:00
of the algorithm is $O(n)$.
2016-12-28 23:54:51 +01:00
2017-01-03 01:20:01 +01:00
\section{Sliding window minimum}
\index{sliding window}
\index{sliding window minimum}
2017-02-03 23:47:45 +01:00
A \key{sliding window} is a constant-size subarray
that moves through the array.
2017-01-03 01:20:01 +01:00
At each position of the window,
2017-02-13 23:06:35 +01:00
we want to calculate some information
2017-01-03 01:20:01 +01:00
about the elements inside the window.
An interesting problem is to maintain
2017-02-03 23:47:45 +01:00
the \key{sliding window minimum},
which means that at each position of the window,
2017-01-03 01:20:01 +01:00
we should report the smallest element inside the window.
2017-02-03 23:47:45 +01:00
The sliding window minimum can be calculated
2017-01-03 01:20:01 +01:00
using the same idea that we used for calculating
the nearest smaller elements.
2017-02-03 23:47:45 +01:00
We maintain a chain whose
first element is always the last element in the window,
and each element in the chain is smaller than the previous element.
2017-01-03 01:20:01 +01:00
The last element in the chain is always the
smallest element inside the window.
When the sliding window moves forward and
2017-02-03 23:47:45 +01:00
a new element appears, we remove from the chain
all elements that are larger than the new element.
After this, we add the new element to the chain.
Finally, if the last element in the chain
does not belong to the window anymore,
it is removed from the chain.
2017-01-03 01:20:01 +01:00
2017-02-03 23:47:45 +01:00
As an example, consider the following array:
2016-12-28 23:54:51 +01:00
\begin{center}
\begin{tikzpicture}[scale=0.7]
\draw (0,0) grid (8,1);
\node at (0.5,0.5) {$2$};
\node at (1.5,0.5) {$1$};
\node at (2.5,0.5) {$4$};
\node at (3.5,0.5) {$5$};
\node at (4.5,0.5) {$3$};
\node at (5.5,0.5) {$4$};
\node at (6.5,0.5) {$1$};
\node at (7.5,0.5) {$2$};
\footnotesize
\node at (0.5,1.4) {$1$};
\node at (1.5,1.4) {$2$};
\node at (2.5,1.4) {$3$};
\node at (3.5,1.4) {$4$};
\node at (4.5,1.4) {$5$};
\node at (5.5,1.4) {$6$};
\node at (6.5,1.4) {$7$};
\node at (7.5,1.4) {$8$};
\end{tikzpicture}
\end{center}
2017-02-03 23:47:45 +01:00
Suppose that the size of the sliding window is 4.
2017-01-03 01:20:01 +01:00
At the first window position, the smallest element is 1:
2016-12-28 23:54:51 +01:00
\begin{center}
\begin{tikzpicture}[scale=0.7]
\fill[color=lightgray] (0,0) rectangle (4,1);
\draw (0,0) grid (8,1);
\node at (0.5,0.5) {$2$};
\node at (1.5,0.5) {$1$};
\node at (2.5,0.5) {$4$};
\node at (3.5,0.5) {$5$};
\node at (4.5,0.5) {$3$};
\node at (5.5,0.5) {$4$};
\node at (6.5,0.5) {$1$};
\node at (7.5,0.5) {$2$};
\footnotesize
\node at (0.5,1.4) {$1$};
\node at (1.5,1.4) {$2$};
\node at (2.5,1.4) {$3$};
\node at (3.5,1.4) {$4$};
\node at (4.5,1.4) {$5$};
\node at (5.5,1.4) {$6$};
\node at (6.5,1.4) {$7$};
\node at (7.5,1.4) {$8$};
\draw[thick,->] (3.5,-0.25) .. controls (3.25,-1.00) and (2.75,-1.00) .. (2.6,-0.25);
\draw[thick,->] (2.4,-0.25) .. controls (2.25,-1.00) and (1.75,-1.00) .. (1.5,-0.25);
\end{tikzpicture}
\end{center}
2017-01-03 01:20:01 +01:00
Then the window moves one step forward.
The new number 3 is smaller than the numbers
5 and 4 in the chain, so the numbers 5 and 4
2017-02-03 23:47:45 +01:00
are removed from the chain
and the number 3 is added to the chain.
The smallest element is still 1.
2016-12-28 23:54:51 +01:00
\begin{center}
\begin{tikzpicture}[scale=0.7]
\fill[color=lightgray] (1,0) rectangle (5,1);
\draw (0,0) grid (8,1);
\node at (0.5,0.5) {$2$};
\node at (1.5,0.5) {$1$};
\node at (2.5,0.5) {$4$};
\node at (3.5,0.5) {$5$};
\node at (4.5,0.5) {$3$};
\node at (5.5,0.5) {$4$};
\node at (6.5,0.5) {$1$};
\node at (7.5,0.5) {$2$};
\footnotesize
\node at (0.5,1.4) {$1$};
\node at (1.5,1.4) {$2$};
\node at (2.5,1.4) {$3$};
\node at (3.5,1.4) {$4$};
\node at (4.5,1.4) {$5$};
\node at (5.5,1.4) {$6$};
\node at (6.5,1.4) {$7$};
\node at (7.5,1.4) {$8$};
\draw[thick,->] (4.5,-0.25) .. controls (4.25,-1.00) and (1.75,-1.00) .. (1.5,-0.25);
\end{tikzpicture}
\end{center}
2017-02-03 23:47:45 +01:00
After this, the window moves again,
and the smallest element 1
does not belong to the window anymore.
2017-01-03 01:20:01 +01:00
Thus, it is removed from the chain and the smallest
element is now 3. In addition, the new number 4
is added to the chain.
2016-12-28 23:54:51 +01:00
\begin{center}
\begin{tikzpicture}[scale=0.7]
\fill[color=lightgray] (2,0) rectangle (6,1);
\draw (0,0) grid (8,1);
\node at (0.5,0.5) {$2$};
\node at (1.5,0.5) {$1$};
\node at (2.5,0.5) {$4$};
\node at (3.5,0.5) {$5$};
\node at (4.5,0.5) {$3$};
\node at (5.5,0.5) {$4$};
\node at (6.5,0.5) {$1$};
\node at (7.5,0.5) {$2$};
\footnotesize
\node at (0.5,1.4) {$1$};
\node at (1.5,1.4) {$2$};
\node at (2.5,1.4) {$3$};
\node at (3.5,1.4) {$4$};
\node at (4.5,1.4) {$5$};
\node at (5.5,1.4) {$6$};
\node at (6.5,1.4) {$7$};
\node at (7.5,1.4) {$8$};
\draw[thick,->] (5.5,-0.25) .. controls (5.25,-1.00) and (4.75,-1.00) .. (4.5,-0.25);
\end{tikzpicture}
\end{center}
2017-01-03 01:20:01 +01:00
The next new element 1 is smaller than all elements
in the chain.
Thus, all elements are removed from the chain
and it will only contain the element 1:
2016-12-28 23:54:51 +01:00
\begin{center}
\begin{tikzpicture}[scale=0.7]
\fill[color=lightgray] (3,0) rectangle (7,1);
\draw (0,0) grid (8,1);
\node at (0.5,0.5) {$2$};
\node at (1.5,0.5) {$1$};
\node at (2.5,0.5) {$4$};
\node at (3.5,0.5) {$5$};
\node at (4.5,0.5) {$3$};
\node at (5.5,0.5) {$4$};
\node at (6.5,0.5) {$1$};
\node at (7.5,0.5) {$2$};
\footnotesize
\node at (0.5,1.4) {$1$};
\node at (1.5,1.4) {$2$};
\node at (2.5,1.4) {$3$};
\node at (3.5,1.4) {$4$};
\node at (4.5,1.4) {$5$};
\node at (5.5,1.4) {$6$};
\node at (6.5,1.4) {$7$};
\node at (7.5,1.4) {$8$};
\fill[color=black] (6.5,-0.25) circle (0.1);
%\draw[thick,->] (5.5,-0.25) .. controls (5.25,-1.00) and (4.75,-1.00) .. (4.5,-0.25);
\end{tikzpicture}
\end{center}
2017-01-03 01:20:01 +01:00
Finally the window reaches its last position.
The number 2 is added to the chain,
but the smallest element inside the window
is still 1.
2016-12-28 23:54:51 +01:00
\begin{center}
\begin{tikzpicture}[scale=0.7]
\fill[color=lightgray] (4,0) rectangle (8,1);
\draw (0,0) grid (8,1);
\node at (0.5,0.5) {$2$};
\node at (1.5,0.5) {$1$};
\node at (2.5,0.5) {$4$};
\node at (3.5,0.5) {$5$};
\node at (4.5,0.5) {$3$};
\node at (5.5,0.5) {$4$};
\node at (6.5,0.5) {$1$};
\node at (7.5,0.5) {$2$};
\footnotesize
\node at (0.5,1.4) {$1$};
\node at (1.5,1.4) {$2$};
\node at (2.5,1.4) {$3$};
\node at (3.5,1.4) {$4$};
\node at (4.5,1.4) {$5$};
\node at (5.5,1.4) {$6$};
\node at (6.5,1.4) {$7$};
\node at (7.5,1.4) {$8$};
\draw[thick,->] (7.5,-0.25) .. controls (7.25,-1.00) and (6.75,-1.00) .. (6.5,-0.25);
\end{tikzpicture}
\end{center}
2017-01-03 01:20:01 +01:00
Also in this algorithm, each element in the array
is added to the chain exactly once and
removed from the chain at most once.
Thus, the total time complexity of the algorithm is $O(n)$.
2016-12-28 23:54:51 +01:00