825 lines
22 KiB
TeX
825 lines
22 KiB
TeX
\chapter{Amortized analysis}
|
|
|
|
\index{amortized analysis}
|
|
|
|
Often the time complexity of an algorithm
|
|
is easy to analyze by looking at the structure
|
|
of the algorithm:
|
|
what loops there are and how many times
|
|
they are performed.
|
|
However, sometimes a straightforward analysis
|
|
doesn't give a true picture of the efficiency of the algorithm.
|
|
|
|
\key{Amortized analysis} can be used for analyzing
|
|
an algorithm that contains an operation whose
|
|
time complexity varies.
|
|
The idea is to consider all such operations during the
|
|
execution of the algorithm instead of a single operation,
|
|
and estimate the total time complexity of the operations.
|
|
|
|
\section{Two pointers method}
|
|
|
|
\index{two pointers method}
|
|
|
|
In the \key{two pointers method},
|
|
two pointers iterate through the elements in an array.
|
|
Both pointers can move during the algorithm,
|
|
but the restriction is that each pointer can move
|
|
to only one direction.
|
|
This ensures that the algorithm works efficiently.
|
|
|
|
We will next discuss two problems that can be solved
|
|
using the two pointers method.
|
|
|
|
\subsubsection{Subarray sum}
|
|
|
|
Given an array that contains $n$ positive integers,
|
|
our task is to find out if there is a subarray
|
|
where the sum of the elements is $x$.
|
|
For example, the array
|
|
\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}
|
|
contains a subarray with sum 8:
|
|
\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}
|
|
|
|
It turns out that the problem can be solved in
|
|
$O(n)$ time using the two pointers method.
|
|
The idea is to iterate through the array
|
|
using two pointers that define a range in the array.
|
|
On each turn, the left pointer moves one step
|
|
forward, and the right pointer moves forward
|
|
as long as the sum is at most $x$.
|
|
If the sum of the range becomes exactly $x$,
|
|
we have found a solution.
|
|
|
|
As an example, we consider the following array
|
|
with target sum $x=8$:
|
|
\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}
|
|
|
|
First, the pointers define a range with sum $1+3+2=6$.
|
|
The range can't be larger
|
|
because the next number 5 would make the sum
|
|
larger than $x$.
|
|
|
|
\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}
|
|
|
|
After this, the left pointer moves one step forward.
|
|
The right pointer doesn't move because otherwise
|
|
the sum would become too large.
|
|
|
|
\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}
|
|
|
|
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$.
|
|
|
|
\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}
|
|
|
|
The time complexity of the algorithm depends on
|
|
the number of steps the right pointer moves.
|
|
There is no upper bound how many steps the
|
|
pointer can move on a single turn.
|
|
However, the pointer moves \emph{a total of}
|
|
$O(n)$ steps during the algorithm
|
|
because it only moves forward.
|
|
|
|
Since both the left and the right pointer
|
|
move $O(n)$ steps during the algorithm,
|
|
the time complexity is $O(n)$.
|
|
|
|
\subsubsection{Sum of two numbers}
|
|
|
|
\index{2SUM problem}
|
|
|
|
Given an array of $n$ integers and an integer $x$,
|
|
our task is to find two numbers in array
|
|
whose sum is $x$ or report that there are no such numbers.
|
|
This problem is known as the \key{2SUM} problem,
|
|
and it can be solved efficiently using the
|
|
two pointers method.
|
|
|
|
First, we sort the numbers in the array in
|
|
increasing order.
|
|
After this, we iterate through the array using
|
|
two pointers that begin at both ends of the array.
|
|
The left pointer begins from the first element
|
|
and moves one step forward on each turn.
|
|
The right pointer begins from the last element
|
|
and always moves backward until the sum of the range
|
|
defined by the pointers is at most $x$.
|
|
If the sum is exactly $x$, we have found a solution.
|
|
|
|
For example, consider the following array when
|
|
our task is to find two elements whose sum is $x=12$:
|
|
\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}
|
|
|
|
The initial positions of the pointers
|
|
are as follows.
|
|
The sum of the numbers is $1+10=11$
|
|
that is smaller than $x$.
|
|
|
|
\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}
|
|
|
|
Then the left pointer moves one step forward.
|
|
The right pointer moves three steps backward,
|
|
and the sum becomes $4+7=11$.
|
|
|
|
\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}
|
|
|
|
After this, the left pointer moves one step forward again.
|
|
The right pointer doesn't move, and the solution
|
|
$5+7=12$ has been found.
|
|
|
|
\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}
|
|
|
|
At the beginning of the algorithm,
|
|
the sorting takes $O(n \log n)$ time.
|
|
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)$.
|
|
|
|
Note that it is possible to solve
|
|
in another way in $O(n \log n)$ time using binary search.
|
|
In this solution, we iterate through the array
|
|
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.
|
|
|
|
\index{3SUM-problem}
|
|
A somewhat more difficult problem is
|
|
the \key{3SUM} problem where our task is
|
|
to find \emph{three} numbers whose sum is $x$.
|
|
This problem can be solved in $O(n^2)$ time.
|
|
Can you see how it is possible?
|
|
|
|
\section{Nearest smaller elements}
|
|
|
|
\index{nearest smaller elements}
|
|
|
|
Amortized analysis is often used for
|
|
estimating the number of operations
|
|
performed for a data structure.
|
|
The operations may be distributed unevenly so
|
|
that the most operations appear during a
|
|
certain phase in the algorithm, but the total
|
|
number of the operations is limited.
|
|
|
|
As an example, let us consider a problem
|
|
where our task is to find for each element
|
|
in an array the
|
|
\key{nearest smaller element}, i.e.,
|
|
the nearest smaller element that precedes
|
|
the element in the array.
|
|
It is possible that no such element exists,
|
|
and the algorithm should notice this.
|
|
It turns out that the problem can be efficiently solved
|
|
in $O(n)$ time using a suitable data structure.
|
|
|
|
An efficient solution for the problem is to
|
|
iterate through the array from the left to the right,
|
|
and maintain a chain of elements where the
|
|
first element is the active element in the array
|
|
and each following element is the nearest smaller
|
|
element of the previous element.
|
|
If the chain only contains one element,
|
|
the active element doesn't have a nearest smaller element.
|
|
At each step, we remove elements from the chain
|
|
until the first element is smaller
|
|
than the active element, or the chain is empty.
|
|
After this, the active element becomes the first element
|
|
in the chain.
|
|
|
|
As an example, consider the following array:
|
|
\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}
|
|
|
|
First, numbers 1, 3 and 4 are added to the chain
|
|
because each element is larger than the previous element.
|
|
This means that the nearest smaller element of
|
|
number 4 is number 3 whose nearest smaller element
|
|
is number 1.
|
|
\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}
|
|
|
|
The next number 2 is smaller than two first numbers in the chain.
|
|
Thus, numbers 4 and 3 are removed, and then number 2 becomes
|
|
the first element in the chain.
|
|
Its nearest smaller element is number 1:
|
|
\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}
|
|
|
|
After this, number 5 is larger than number 2,
|
|
so it will be added to the chain and
|
|
its nearest smaller element is number 2:
|
|
\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}
|
|
|
|
Algorithm continues in a similar way
|
|
and finds out the nearest smaller element
|
|
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
|
|
element in the chain, it will only be inserted
|
|
to the beginning of the chain which is efficient.
|
|
However, sometimes the chain can contain several
|
|
larger elements and it takes time to remove them.
|
|
Still, each element is added exactly once to the chain
|
|
and removed at most once.
|
|
Thus, each element causes $O(1)$ operations
|
|
to the chain, and the total time complexity
|
|
of the algorithm is $O(n)$.
|
|
|
|
\section{Sliding window minimum}
|
|
|
|
\index{sliding window}
|
|
\index{sliding window minimum}
|
|
|
|
A \key{sliding window} is an active subarray
|
|
that moves through the array whose size is constant.
|
|
At each position of the window,
|
|
we typically want to calculate some information
|
|
about the elements inside the window.
|
|
An interesting problem is to maintain
|
|
the \key{sliding window minimum}.
|
|
This means that at each position of the window,
|
|
we should report the smallest element inside the window.
|
|
|
|
The sliding window minima can be calculated
|
|
using the same idea that we used for calculating
|
|
the nearest smaller elements.
|
|
The idea is to maintain a chain whose
|
|
first element is the last element in the window,
|
|
and each element is smaller than the previous element.
|
|
The last element in the chain is always the
|
|
smallest element inside the window.
|
|
When the sliding window moves forward and
|
|
a new element appears, we remove all elements
|
|
from the chain that are larger than the new element.
|
|
After this, we add the new number to the chain.
|
|
In addition, if the last element in the chain
|
|
doesn't belong to the window anymore, it is removed from the chain.
|
|
|
|
As an example, consider the following array
|
|
when the window size is $k=4$:
|
|
|
|
\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}
|
|
|
|
The sliding window begins from the left border of the array.
|
|
At the first window position, the smallest element is 1:
|
|
\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}
|
|
|
|
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
|
|
are removed and the number 3 is added to the chain.
|
|
The smallest element is 1 as before.
|
|
\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}
|
|
|
|
After this, the window moves again and the smallest element 1
|
|
doesn't belong to the window anymore.
|
|
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.
|
|
\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}
|
|
|
|
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:
|
|
\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}
|
|
|
|
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.
|
|
\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}
|
|
|
|
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)$.
|
|
|
|
|
|
|