Skip to content
Snippets Groups Projects
Commit d26d8184 authored by Martin Mareš's avatar Martin Mareš
Browse files

Intro: Potentials

This is mostly a translation of the section on potentials in the
Guide to the Labyrinth of Algorithms.
parent de29b85a
No related branches found
No related tags found
No related merge requests found
......@@ -298,6 +298,103 @@ the data --- in our example, with \|1|~bits. (This can be considered a~special c
the accounting method, because we redistribute time from operations which save coins to
those which spend the saved coins later.)
\subsection{Potentials}
\subsection{The potential method}
In the above examples, the total complexity of a~sequence of operations was much better
that the sum of the worst-case complexities. The proof usually involved increasing the
cost of ``easy'' operations to compensate for the occassional ``hard'' ones. We are going
to generalize this approach now.
Let us have a~data structure supporting several types of operations. Let us consider
an~arbitrary sequence of operations $O_1,\ldots,O_m$ on the structure. Each operation
is characterized by its \em{real cost}~$R_i$ --- this is the time spent by the machine
on performing the operation.\foot{We prefer to speak of an~abstract cost instead of time,
since the same technique can be used to analyze other resources besides time.}
The cost depends on the current state of the data structure, but we often bound it
by the worst-case time for simplicity.
We will often simplify calculations by clever choices of the unit of time, which is
correct as long as the real time is linear in the chosen cost.
We further assign an~\em{amortized cost}~$A_i$ to each operation.
This cost expresses our opinion on how much does this operation contribute to the
total execution time. We can choose it arbitrarily as long as the sum of all amortized
costs is an upper bound for the sum of all real costs.
Therefore, an~outside observer who does not see the internals of the computation
and observes only the total time cannot distinguish between the real and amortized costs.
Execution of the first~$n$ operations takes real cost $R_1 + \ldots + R_n$, but we
claimed that it will take $A_1 + \ldots + A_n$. We can view the difference of these
two costs as a~balance of a~``bank account'' used to save time for the future.
This balance is usually called the \df{potential} of the data structure.
It is denoted by $\Phi_i = (\sum_{j=1}^i A_j) - (\sum_{j=1}^i R_j)$.
Before we perform the first operation, we naturally have $\Phi_0=0$.
At every moment, we have $\Phi_i \ge 0$ as we can never owe time.
The \df{potential difference} $\Delta\Phi_i = \Phi_i - \Phi_{i-1}$ is then equal
to $A_i - R_i$. It tells us if the $i$-th operation saves time ($\Delta\Phi_i > 0$)
or it spends time saved in the past ($\Delta\Phi_i < 0$).
\examples
\list{o}
\:In the binary counter, the potential corresponds to the number of coins saved,
that is to the number of \|1|~bits. The amortized cost of each operation is~2.
The real cost is $1+o$ where~$o$ is the number of trailing \|1|s. We calculate
that the potential difference si $2 - (1+o) = 1-o$, which matches that one~\|1|
was created the $o$~of them disappeared.
\:When we analyze the stretchable and shrinkable array, the potential counts the
number of operations since the last reallocation. All operations are assigned
amortized cost~2. Cost~1 is spent on the append/removal of the element, the
other~1 is saved in the potential. When a~reallocation comes, the potential
drops to~0 and we can observe that its decrease offsets the cost of the
reallocation.
\endlist
In both cases, there is a~simple correspondence between the potential and the
state or history of the data structure. This suggests an~inverse approach: first
we choose the potential according to the state of the structure, then we use it
to calculate the amortized complexity. Indeed, the formula $A_i - R_i = \Delta\Phi_i$
can be re-written as $A_i = R_i + \Delta\Phi_i$. This says that \em{the amortized
cost is the sum of the real cost and the potential difference.}
The sum of all amortized costs is then
$$
\sum_{i=1}^m A_i =
\sum_{i=1}^m (R_i + \Phi_i - \Phi_{i-1}) =
\left( \sum_{i=1}^m R_i \right) + \Phi_m - \Phi_0.
$$
The second sum \em{telescopes} --- each~$\Phi_i$ except for the first one and the
last one is added once and subtracted once, so it disappears.
As long as $\Phi_m \ge \Phi_0$, the sum of real costs is majorized by the the sum
of amortized costs, as we wanted.
Let us summarize how the potential method works:
\list{o}
\:We choose an~appropriate potential function~$\Phi$, which depends on the current
state of the structure and possibly also on its history. There is no universal
recipe for choosing it, but we usually want the potential to increase when we
get closer to a~costly operation.
\:We prove that $\Phi_0 \le \Phi_m$ (we ``do not owe time''). This is easy if our
potential counts occurrences of something in the structure --- this makes it
always non-negative and zero at the start.
\:We establish the amortized costs of operations from their real costs and the
potential differences: $A_i = R_i + \Phi_i - \Phi_{i-1}$.
\:If we are unable to calculate the costs exactly, we use an~upper bound.
It also helps to choose units of cost to simplify multiplicative constants.
\endlist
Amortized analysis is helpful if we use the data structure inside an~algorithm
and we are interested only in the total time spent by the algorithm. However,
programs interacting with the environment in real time still require data structures
efficient in the worst case --- imagine that your program controls a~space rocket.
We should also pay attention to the difference between amortized and average-case
complexity. Averages can be computed over all possible inputs or over all possible
random bits generated in an~randomized algorithm, but they do not promise anything
for a~concrete input. On the other hand, amortized complexity guarantees an~upper
bound on the total execution time, but it does not reveal anything about distribution
of this time betwen individual operations.
\endchapter
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment