cphb/chapter08.tex

733 lines
19 KiB
TeX
Raw Permalink 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
2017-05-20 16:13:29 +02:00
\key{Amortized analysis} can be used to analyze
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-05-20 16:13:29 +02:00
The idea is to estimate the total time used to
2017-02-03 23:47:45 +01:00
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-05-20 16:13:29 +02:00
two pointers are used to
iterate through the array values.
Both pointers can move to one direction only,
which ensures that the algorithm works efficiently.
Next we discuss two problems that can be solved
2017-01-03 00:49:59 +01:00
using the two pointers method.
\subsubsection{Subarray sum}
2017-02-03 23:47:45 +01:00
As the first example,
2017-05-20 16:13:29 +02:00
consider a problem where we are
given an array of $n$ positive integers
2017-02-03 23:47:45 +01:00
and a target sum $x$,
2017-05-20 16:13:29 +02:00
and we want to find a subarray whose sum is $x$
2017-02-03 23:47:45 +01:00
or report that there is no such subarray.
2017-05-20 16:13:29 +02:00
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$};
\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$};
\end{tikzpicture}
\end{center}
2017-05-20 16:13:29 +02:00
This problem can be solved in
2017-02-03 23:47:45 +01:00
$O(n)$ time by using the two pointers method.
2017-05-25 22:05:43 +02:00
The idea is to maintain pointers that point to the
2017-05-20 16:13:29 +02:00
first and last value of a subarray.
2017-01-03 00:49:59 +01:00
On each turn, the left pointer moves one step
2017-05-20 16:13:29 +02:00
to the right, and the right pointer moves to the right
as long as the resulting subarray sum is at most $x$.
2017-02-03 23:47:45 +01:00
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-05-20 16:13:29 +02:00
and a 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$};
\end{tikzpicture}
\end{center}
2017-05-20 16:13:29 +02:00
The initial subarray contains the values
1, 3 and 2 whose sum is 6:
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);
\end{tikzpicture}
\end{center}
2017-05-20 16:13:29 +02:00
Then, the left pointer moves one step to the right.
2017-02-13 23:06:35 +01:00
The right pointer does not move, because otherwise
2017-05-20 16:13:29 +02:00
the subarray sum would exceed $x$.
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);
\end{tikzpicture}
\end{center}
2017-05-20 16:13:29 +02:00
Again, the left pointer moves one step to the right,
2017-01-03 00:49:59 +01:00
and this time the right pointer moves three
2017-05-20 16:13:29 +02:00
steps to the right.
The subarray sum is $2+5+1=8$, so a subarray
whose sum is $x$ has been found.
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);
\end{tikzpicture}
\end{center}
2017-05-20 16:13:29 +02:00
The running time of the algorithm depends on
2017-01-03 00:49:59 +01:00
the number of steps the right pointer moves.
2017-05-25 22:05:43 +02:00
While there is no useful upper bound on how many steps the
pointer can move on a \emph{single} turn.
we know that the pointer moves \emph{a total of}
2017-02-03 23:47:45 +01:00
$O(n)$ steps during the algorithm,
2017-05-20 16:13:29 +02:00
because it only moves to the right.
2017-01-03 00:49:59 +01:00
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,
2017-05-20 16:13:29 +02:00
the algorithm works in $O(n)$ time.
2017-01-03 00:49:59 +01:00
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-05-25 22:05:43 +02:00
given an array of $n$ numbers and
a target sum $x$, find
2017-05-20 16:13:29 +02:00
two array values such that their sum is $x$,
or report that no such values exist.
2017-01-03 00:49:59 +01:00
2017-02-03 23:47:45 +01:00
To solve the problem, we first
2017-05-20 16:13:29 +02:00
sort the array values in increasing order.
2017-02-03 23:47:45 +01:00
After that, we iterate through the array using
2017-02-13 23:06:35 +01:00
two pointers.
2017-05-20 16:13:29 +02:00
The left pointer starts at the first value
and moves one step to the right on each turn.
The right pointer begins at the last value
and always moves to the left until the sum of the
left and right value is at most $x$.
If the sum is 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
For example, consider the following array
2017-05-20 16:13:29 +02:00
and a 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$};
\end{tikzpicture}
\end{center}
2017-01-03 00:49:59 +01:00
The initial positions of the pointers
are as follows.
2017-05-20 16:13:29 +02:00
The sum of the values is $1+10=11$
2017-01-03 00:49:59 +01:00
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);
\end{tikzpicture}
\end{center}
2017-05-20 16:13:29 +02:00
Then the left pointer moves one step to the right.
The right pointer moves three steps to the left,
2017-01-03 00:49:59 +01:00
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);
\end{tikzpicture}
\end{center}
2017-05-20 16:13:29 +02:00
After this, the left pointer moves one step to the right 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);
\end{tikzpicture}
\end{center}
2017-05-20 16:13:29 +02:00
The running time of the algorithm is
$O(n \log n)$, because it first sorts
the array in $O(n \log n)$ time,
and then both pointers move $O(n)$ steps.
2017-01-03 00:49:59 +01:00
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-05-20 16:13:29 +02:00
and for each array value, we try to find another
value that yields the sum $x$.
2017-01-03 00:49:59 +01:00
This can be done by performing $n$ binary searches,
2017-05-20 16:13:29 +02:00
each of which 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-05-20 16:13:29 +02:00
find \emph{three} array values
whose 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}
2017-05-20 16:13:29 +02:00
Amortized analysis is often used to
estimate 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
2017-05-25 22:05:43 +02:00
of finding for each array element
the \key{nearest smaller element}, i.e.,
2017-05-20 16:13:29 +02:00
the first smaller element that precedes the element
in the array.
2017-01-03 01:06:48 +01:00
It is possible that no such element exists,
2017-02-03 23:47:45 +01:00
in which case the algorithm should report this.
2017-05-20 16:13:29 +02:00
Next we will see how the problem can be
efficiently solved using a stack structure.
We go through the array from left to right
and maintain a stack of array elements.
At each array position, we remove elements from the stack
until the top element is smaller than the
current element, or the stack is empty.
Then, we report that the top element is
the nearest smaller element of the current element,
or if the stack is empty, there is no such element.
Finally, we add the current element to the stack.
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$};
\end{tikzpicture}
\end{center}
2017-05-20 16:13:29 +02:00
First, the elements 1, 3 and 4 are added to the stack,
because each element is larger than the previous element.
2017-02-03 23:47:45 +01:00
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$};
2017-05-20 16:13:29 +02:00
\draw (0.2,0.2-1.2) rectangle (0.8,0.8-1.2);
\draw (1.2,0.2-1.2) rectangle (1.8,0.8-1.2);
\draw (2.2,0.2-1.2) rectangle (2.8,0.8-1.2);
\node at (0.5,0.5-1.2) {$1$};
\node at (1.5,0.5-1.2) {$3$};
\node at (2.5,0.5-1.2) {$4$};
\draw[->,thick] (0.8,0.5-1.2) -- (1.2,0.5-1.2);
\draw[->,thick] (1.8,0.5-1.2) -- (2.2,0.5-1.2);
2016-12-28 23:54:51 +01:00
\end{tikzpicture}
\end{center}
2017-05-20 16:13:29 +02:00
The next element 2 is smaller than the two top
elements in the stack.
Thus, the elements 3 and 4 are removed from the stack,
and then the element 2 is added to the stack.
2017-02-03 23:47:45 +01:00
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$};
2017-05-20 16:13:29 +02:00
\draw (0.2,0.2-1.2) rectangle (0.8,0.8-1.2);
\draw (3.2,0.2-1.2) rectangle (3.8,0.8-1.2);
\node at (0.5,0.5-1.2) {$1$};
\node at (3.5,0.5-1.2) {$2$};
\draw[->,thick] (0.8,0.5-1.2) -- (3.2,0.5-1.2);
2016-12-28 23:54:51 +01:00
\end{tikzpicture}
\end{center}
2017-05-20 16:13:29 +02:00
Then, the element 5 is larger than the element 2,
so it will be added to the stack, and
2017-02-03 23:47:45 +01:00
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$};
2017-05-20 16:13:29 +02:00
\draw (0.2,0.2-1.2) rectangle (0.8,0.8-1.2);
\draw (3.2,0.2-1.2) rectangle (3.8,0.8-1.2);
\draw (4.2,0.2-1.2) rectangle (4.8,0.8-1.2);
\node at (0.5,0.5-1.2) {$1$};
\node at (3.5,0.5-1.2) {$2$};
\node at (4.5,0.5-1.2) {$5$};
\draw[->,thick] (0.8,0.5-1.2) -- (3.2,0.5-1.2);
\draw[->,thick] (3.8,0.5-1.2) -- (4.2,0.5-1.2);
2016-12-28 23:54:51 +01:00
\end{tikzpicture}
\end{center}
2017-05-20 16:13:29 +02:00
After this, the element 5 is removed from the stack
and the elements 3 and 4 are added to the stack:
\begin{center}
\begin{tikzpicture}[scale=0.7]
\fill[color=lightgray] (6,0) rectangle (7,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 (0.2,0.2-1.2) rectangle (0.8,0.8-1.2);
\draw (3.2,0.2-1.2) rectangle (3.8,0.8-1.2);
\draw (5.2,0.2-1.2) rectangle (5.8,0.8-1.2);
\draw (6.2,0.2-1.2) rectangle (6.8,0.8-1.2);
\node at (0.5,0.5-1.2) {$1$};
\node at (3.5,0.5-1.2) {$2$};
\node at (5.5,0.5-1.2) {$3$};
\node at (6.5,0.5-1.2) {$4$};
\draw[->,thick] (0.8,0.5-1.2) -- (3.2,0.5-1.2);
\draw[->,thick] (3.8,0.5-1.2) -- (5.2,0.5-1.2);
\draw[->,thick] (5.8,0.5-1.2) -- (6.2,0.5-1.2);
\end{tikzpicture}
\end{center}
Finally, all elements except 1 are removed
from the stack and the last element 2
is added to the stack:
\begin{center}
\begin{tikzpicture}[scale=0.7]
\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) {$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 (0.2,0.2-1.2) rectangle (0.8,0.8-1.2);
\draw (7.2,0.2-1.2) rectangle (7.8,0.8-1.2);
\node at (0.5,0.5-1.2) {$1$};
\node at (7.5,0.5-1.2) {$2$};
\draw[->,thick] (0.8,0.5-1.2) -- (7.2,0.5-1.2);
\end{tikzpicture}
\end{center}
2017-01-03 01:06:48 +01:00
The efficiency of the algorithm depends on
2017-05-20 16:13:29 +02:00
the total number of stack operations.
If the current element is larger than
the top element in the stack, it is directly
added to the stack, which is efficient.
However, sometimes the stack can contain several
2017-02-13 23:06:35 +01:00
larger elements and it takes time to remove them.
2017-05-20 16:13:29 +02:00
Still, each element is added \emph{exactly once} to the stack
and removed \emph{at most once} from the stack.
Thus, each element causes $O(1)$ stack operations,
and the algorithm works in $O(n)$ time.
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
2017-05-20 16:13:29 +02:00
that moves from left to right through the array.
At each window position,
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.
2017-05-20 16:13:29 +02:00
In this section, we focus on the problem
of maintaining the \key{sliding window minimum},
which means that
we should report the smallest value inside each window.
2017-01-03 01:20:01 +01:00
2017-02-03 23:47:45 +01:00
The sliding window minimum can be calculated
2017-05-20 16:13:29 +02:00
using a similar idea that we used to calculate
2017-01-03 01:20:01 +01:00
the nearest smaller elements.
2017-05-20 16:13:29 +02:00
We maintain a queue
where each element is larger than
the previous element,
and the first element
always corresponds to the minimum element inside the window.
After each window move,
we remove elements from the end of the queue
until the last queue element
2017-05-25 22:05:43 +02:00
is smaller than the new window element,
2017-05-20 16:13:29 +02:00
or the queue becomes empty.
We also remove the first queue element
if it is not inside the window anymore.
2017-05-25 22:05:43 +02:00
Finally, we add the new window element
2017-05-20 16:13:29 +02:00
to the end of the queue.
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$};
\end{tikzpicture}
\end{center}
2017-02-03 23:47:45 +01:00
Suppose that the size of the sliding window is 4.
2017-05-20 16:13:29 +02:00
At the first window position, the smallest value 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$};
2017-05-20 16:13:29 +02:00
\draw (1.2,0.2-1.2) rectangle (1.8,0.8-1.2);
\draw (2.2,0.2-1.2) rectangle (2.8,0.8-1.2);
\draw (3.2,0.2-1.2) rectangle (3.8,0.8-1.2);
\node at (1.5,0.5-1.2) {$1$};
\node at (2.5,0.5-1.2) {$4$};
\node at (3.5,0.5-1.2) {$5$};
\draw[->,thick] (1.8,0.5-1.2) -- (2.2,0.5-1.2);
\draw[->,thick] (2.8,0.5-1.2) -- (3.2,0.5-1.2);
2016-12-28 23:54:51 +01:00
\end{tikzpicture}
\end{center}
2017-05-20 16:13:29 +02:00
Then the window moves one step right.
The new element 3 is smaller than the elements
4 and 5 in the queue, so the elements 4 and 5
are removed from the queue
and the element 3 is added to the queue.
The smallest value 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$};
2017-05-20 16:13:29 +02:00
\draw (1.2,0.2-1.2) rectangle (1.8,0.8-1.2);
\draw (4.2,0.2-1.2) rectangle (4.8,0.8-1.2);
\node at (1.5,0.5-1.2) {$1$};
\node at (4.5,0.5-1.2) {$3$};
\draw[->,thick] (1.8,0.5-1.2) -- (4.2,0.5-1.2);
2016-12-28 23:54:51 +01:00
\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-05-20 16:13:29 +02:00
Thus, it is removed from the queue and the smallest
value is now 3. Also the new element 4
is added to the queue.
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$};
2017-05-20 16:13:29 +02:00
\draw (4.2,0.2-1.2) rectangle (4.8,0.8-1.2);
\draw (5.2,0.2-1.2) rectangle (5.8,0.8-1.2);
\node at (4.5,0.5-1.2) {$3$};
\node at (5.5,0.5-1.2) {$4$};
\draw[->,thick] (4.8,0.5-1.2) -- (5.2,0.5-1.2);
2016-12-28 23:54:51 +01:00
\end{tikzpicture}
\end{center}
2017-01-03 01:20:01 +01:00
The next new element 1 is smaller than all elements
2017-05-20 16:13:29 +02:00
in the queue.
Thus, all elements are removed from the queue
2017-01-03 01:20:01 +01:00
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$};
2017-05-20 16:13:29 +02:00
\draw (6.2,0.2-1.2) rectangle (6.8,0.8-1.2);
\node at (6.5,0.5-1.2) {$1$};
2016-12-28 23:54:51 +01:00
\end{tikzpicture}
\end{center}
2017-01-03 01:20:01 +01:00
Finally the window reaches its last position.
2017-05-20 16:13:29 +02:00
The element 2 is added to the queue,
but the smallest value inside the window
2017-01-03 01:20:01 +01:00
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$};
2017-05-20 16:13:29 +02:00
\draw (6.2,0.2-1.2) rectangle (6.8,0.8-1.2);
\draw (7.2,0.2-1.2) rectangle (7.8,0.8-1.2);
\node at (6.5,0.5-1.2) {$1$};
\node at (7.5,0.5-1.2) {$2$};
\draw[->,thick] (6.8,0.5-1.2) -- (7.2,0.5-1.2);
2016-12-28 23:54:51 +01:00
\end{tikzpicture}
\end{center}
2017-05-20 16:13:29 +02:00
Since each array element
is added to the queue exactly once and
removed from the queue at most once,
the algorithm works in $O(n)$ time.
2016-12-28 23:54:51 +01:00