\ifx\endpart\undefined
\input macros.tex
\fi

\chapter{Fine Details of Computation}

\section{Models and machines}

Traditionally, computer scientists use a~variety of computational models
for a~formalism in which their algorithms are stated. If we were studying
NP-completeness, we could safely assume that all the models are equivalent,
possibly up to polynomial slowdown which is negligible. In our case, the
differences between good and not-so-good algorithms are on a~much smaller
scale. In this chapter, we will replace the usual ``yardsticks'' by a~micrometer,
state our computation models carefully and develop a repertoire of basic
data structures taking advantage of the fine details of the models.

We would like to keep the formalism close enough to the reality of the contemporary
computers. This rules out Turing machines and similar sequentially addressed
models, but even the remaining models are subtly different. For example, some of them
allow indexing of arrays in constant time, while the others have no such operation
and arrays have to be emulated with pointer structures, requiring $\Omega(\log n)$
time to access a~single element of an~$n$-element array. It is hard to say which
way is superior --- while most ``real'' computers have instructions for constant-time
indexing, it seems to be physically impossible to fulfil this promise regardless of
the size of memory. Indeed, at the level of logical gates, the depth of the
actual indexing circuits is logarithmic.

In recent decades, most researchers in the area of combinatorial algorithms
have been considering two computational models: the Random Access Machine and the Pointer
Machine. The former one is closer to the programmer's view of a~real computer,
the latter one is slightly more restricted and ``asymptotically safe.''
We will follow this practice and study our algorithms in both models.

\para
The \df{Random Access Machine (RAM)} is not a~single model, but rather a~family
of closely related models, sharing the following properties.
(See Cook and Reckhow \cite{cook:ram} for one of the usual formal definitions
and Hagerup \cite{hagerup:wordram} for a~thorough description of the differences
between the RAM variants.)

The \df{memory} of the model is represented by an~array of \df{memory cells}
addressed by non-negative integers, each of them containing a~single non-negative integer.
The \df{program} is a~sequence of \df{instructions} of two basic kinds: calculation
instructions and control instructions.

\df{Calculation instructions} have two source arguments and one destination
argument, each \df{argument} being either an~immediate constant (not available
as destination), a~directly addressed memory cell (specified by its number)
or an~indirectly addressed memory cell (its address is stored in a~directly
addressed memory cell).

\df{Control instructions} include branches (to a~specific instruction in
the program), conditional branches (e.g., jump if two arguments specified as
in the calculation instructions are equal) and an~instruction to halt the program.

At the beginning of the computation, the memory contains the input data
in specified memory cells and arbitrary values in all other cells.
Then the program is executed one instruction at a~time. When it halts,
specified memory cells are interpreted as the program's output.

\para\id{wordsize}%
In the description of the RAM family, we have omitted several properties
on~purpose, because different members of the family define them differently.
These are: the size of the available integers, the time complexity of a~single
instruction, the space complexity of a~single memory cell and the repertoire
of operations available in calculation instructions.

If we impose no limits on the magnitude of the numbers and we assume that
arithmetic and logical operations work on them in constant time, we get
a~very powerful parallel computer --- we can emulate an~exponential number
of parallel processors using arithmetics and suddenly almost everything can be
computed in constant time, modulo encoding and decoding of input and output.
Such models are unrealistic and there are two basic possibilities how to
avoid this behavior:

\numlist\ndotted
\:Keep unbounded numbers, but increase costs of instructions: each instruction
  consumes time proportional to the number of bits of the numbers it processes,
  including memory addresses. Similarly, space usage is measured in bits,
  counting not only the values, but also the addresses of the respective memory
  cells.
\:Place a~limit on the size of the numbers ---define the \df{word size~$W$,}
  the number of bits available in the memory cells--- and keep the cost of
  of instructions and memory cells constant. The word size must not be constant,
  since we can address only~$2^W$ cells of memory. If the input of the algorithm
  is stored in~$N$ cells, we need~$W\ge\log N$ just to be able to read the input.
  On the other hand, we are interested in polynomial-time algorithms only, so $\Theta(\log N)$-bit
  numbers should be sufficient. In practice, we pick~$w$ to be the larger of
  $\Theta(\log N)$ and the size of integers used in the algorithm's input and output.
\endlist

Both restrictions easily avoid the problems of unbounded parallelism. The first
choice is theoretically cleaner and Cook et al.~show nice correspondences to the
standard complexity classes, but the calculations of time and space complexity tend
to be somewhat tedious. What more, when compared with the RAM with restricted
word size, the complexities are usually exactly $\Theta(w)$ times higher.
This does not hold in general (consider a~program which uses many small numbers
and $\O(1)$ large ones), but it is true for the algorithms we are interested in.
Therefore we will always assume that the operations have unit cost and we make
sure that all numbers are limited by the available word size.

\para
As for the choice of RAM operations, the following three instruction sets are often used:

\itemize\ibull
\:\df{Word-RAM} --- allows the ``C-language operators'', i.e., addition,
  subtraction, multiplication, division, remainder, bitwise {\sc and,} {\sc or,} exclusive
  {\sc or} ({\sc xor}), negation ({\sc not}) and bitwise shifts ($\ll$ and~$\gg$).
\:\df{${\rm AC}^0$-RAM} --- allows all operations from the class ${\rm AC}^0$, i.e.,
  those computable by constant-depth polynomial-size boolean circuits with unlimited
  fan-in and fan-out. This includes all operations of the Word-RAM except for multiplication,
  division and remainders, and also many other operations like computing the Hamming
  weight (number of bits set in a~given number).
\:Both restrictions at once.
\endlist

Thorup discusses the usual techniques employed by RAM algorithms in~\cite{thorup:aczero}
and he shows that they work on both Word-RAM and ${\rm AC}^0$-RAM, but the combination
of the two restrictions is too weak. On the other hand, the intersection of~${\rm AC}^0$
with the instruction set of modern processors (e.g., adding some floating-point
operations and multimedia instructions available on the Intel's Pentium~4~\cite{intel:pentium}) is already strong enough.

We will therefore use the Word-RAM instruction set, mentioning differences from the
${\rm AC}^0$-RAM where necessary.

\nota
When speaking of the \df{RAM,} we implicitly mean the version with numbers limited
by a~specified word size of $W$~bits, unit cost of operations and memory cells and the instruction
set of the Word-RAM. This corresponds to the usage in recent algorithmic literature,
although the authors rarely mention the details. In some cases, a~non-uniform variant
of the Word-RAM is considered as well (e.g., in~\cite{hagerup:dd}):

\defn
A~Word-RAM is called \df{weakly non-uniform,} if it is equipped with $\O(1)$-time
access to a~constant number of word-sized constants, which depend only on the word
size. These are called \df{native constants} and they are available in fixed memory
cells when the program starts. (By analogy with the high-level programming languages,
these constants can be thought of as computed at ``compile time.'')

\para
The \df{Pointer Machine (PM)} also does not have any well established definition. The
various kinds of pointer machines are mapped by Ben-Amram in~\cite{benamram:pm},
but unlike the RAM's they turn out to be equivalent up to constant slowdown.
Our definition will be closely related to the \em{linking automaton} proposed
by Knuth in~\cite{knuth:fundalg}, we will only adapt it to use RAM-like
instructions instead of an~opaque control unit.

The PM works with two different types of data: \df{symbols} from a~finite alphabet
and \df{pointers}. The memory of the machine consists of a~fixed amount of \df{registers}
(some of them capable of storing a~single symbol, each of the others holds a~single pointer)
and an~arbitrary amount of \df{cells}. The structure of all cells is the same: each of them
again contains a~fixed number of fields for symbols and pointers. Registers can be addressed
directly, the cells only via pointers --- either by using a~pointer stored in a~register,
or in a~cell pointed to by a~register (longer chains of pointers cannot be followed in
constant time).

We can therefore view the whole memory as a~directed graph, whose vertices
correspond to the cells (the registers are stored in a~single special cell).
The outgoing edges of each vertex correspond to pointer fields and they are
labelled with distinct labels drawn from a~finite set. In addition to that,
each vertex contains a~fixed amount of symbols. The program can directly access
vertices within distance~2 from the register vertex.

The program is a~sequence of instructions of the following kinds:

\itemize\ibull
\:\df{symbol instructions,} which read a~pair of symbols, apply an~arbitrary
  function on them and write the result to a~symbol register or field;
\:\df{pointer instructions} for assignment of pointers to pointer registers/fields
  and for creation of new memory cells (a~pointer to the new cell is assigned
  immediately);
\:\df{control instructions} --- similarly to the RAM; conditional jumps can decide
  on~arbitrary unary relations on symbols and compare pointers for equality.
\endlist

Time and space complexity are defined in the straightforward way: all instructions
have unit cost and so do all memory cells.

Both input and output of the machine are passed in the form of a~linked structure
pointed to by a~designated register. For example, we can pass graphs back and forth
without having to encode them as strings of numbers or symbols. This is important,
because with the finite alphabet of the~PM, all symbolic representations of graphs
require super-linear space and therefore also time.

\para
Compared to the RAM, the PM lacks two important capabilities: indexing of arrays
and arithmetic instructions. We can emulate both with poly-logarithmic slowdown,
but it will turn out that they are rarely needed in graph algorithms. We are
also going to prove that the RAM is strictly stronger, so we will prefer to
formulate our algorithms in the PM model and use RAM only when necessary.

\thm
Every program for the Word-RAM with word size~$W$ can be translated to a~program
computing the same\foot{Given a~suitable encoding of inputs and outputs, of course.}
on the~PM with $\O(W^2)$ slowdown. If the RAM program does not
use multiplication, division and remainder operations, $\O(W)$~slowdown
is sufficient.

\proofsketch
Represent the memory of the RAM by a~balanced binary search tree or by a~radix
trie of depth~$\O(W)$. Values are encoded as~linked lists of symbols pointed
to by the nodes of the tree. Both direct and indirect accesses to the memory
can therefore be done in~$\O(W)$ time. Use standard algorithms for arithmetic
on big numbers: $\O(W)$ per operation except for multiplication, division and
remainders which take $\O(W^2)$.\foot{We could use more efficient arithmetic
algorithms, but the quadratic bound it good enough for our purposes.}
\qed

\FIXME{Add references.}

\thm
Every program for the PM running in polynomial time can be translated to a~program
computing the same on the Word-RAM with only $\O(1)$ slowdown.

\proofsketch
Encode each cell of the PM's memory to $\O(1)$ integers. Store the encoded cells to
memory of the RAM sequentially and use memory addresses as pointers. As the symbols
are finite and there is only a~polynomial number of cells allocated during execution
of the program, $\O(\log N)$-bit integers suffice.
\qed

\para
There are also \df{randomized} versions of both machines. These are equipped
with an~additional instruction for generating a~single random bit. The standard
techniques of design and analysis of randomized algorithms then apply (see for
example Motwani and Raghavan~\cite{motwani:randalg}).

\FIXME{Consult sources. Does it make more sense to generate random words at once on the RAM?}

\rem
There is one more interesting machine: the \df{Immutable Pointer Machine} (see
the description of LISP machines in \cite{benamram:pm}). It differs from the
ordinary PM by the inability to modify existing memory cells. Only the contents
of the registers are allowed to change. All cell modifications thus have to
be performed by creating a~copy of the particular cell with some fields changed.
This in turn requires the pointers to the cell to be updated, possibly triggering
a~cascade of cell copies. For example, when a~node of a~binary search tree is
updated, all nodes on the path from that node to the root have to be copied.

One of the advantages of this model is that the states of the machine are
persistent --- it is possible to return to a~previously visited state by recalling
the $\O(1)$ values of the registers (everything else could not have changed
since that time) and ``fork'' the computations. This corresponds to the semantics
of pure functional languages, e.g., Haskell.

Unless we are willing to accept a~logarithmic penalty in execution time and space
(in fact, our emulation of the Word-RAM on the PM can be easily made immutable),
the design of efficient algorithms for the immutable PM requires very different
techniques. Therefore, we will concentrate on the imperative models instead
and refer the interested reader to the thorough treatment of purely functional
data structures in the Okasaki's monograph~\cite{okasaki:funcds}.

%--------------------------------------------------------------------------------

\section{Bucket sorting and contractions}\id{bucketsort}%

The Contractive Bor\o{u}vka's algorithm (\ref{contbor}) needed to contract a~given
set of edges in the current graph and flatten it afterwards, all in time $\O(m)$.
We have spared the technical details for this section and they will be useful
in further algorithms, too.

As already suggested, the contractions can be performed by building an~auxiliary
graph and finding its connected components, so we will take care of the flattening
only.

\para
On the RAM, it is sufficient to sort the edges lexicographically (each edge viewed
as an~ordered pair of vertex identifiers with the smaller of the identifiers placed
first). We can do that by a two-pass bucket sort with~$n$ buckets corresponding
to the vertex identifiers.

However, there is a~catch in this. Suppose that we use the standard representation
of graphs as adjacency lists whose heads are stored in an array indexed by vertex
identifiers. When we contract and flatten the graph, the number of vertices decreases,
but if we inherit the original vertex identifiers, the arrays will still have the
same size. Hence we spend a~super-linear amount of time on scanning the arrays,
most of the time skipping unused entries.

To avoid this, we just renumber the vertices after each contraction to component
identifiers from the auxiliary graph and we create a~new vertex array. This way,
the representation of the graph will be linear with respect to the size of the
current graph.

\para
The pointer representation of graphs does not suffer from sparsity as the vertices
are always identified by pointers to per-vertex structures. Each such structure
then contains all attributes associated with the vertex, including the head of the
adjacency list. However, we have to find a~way how to perform bucket sorting
without arrays.

We will keep a~list of the per-vertex structures which defines the order on~vertices.
Each such structure will contain a~pointer to the head of the corresponding bucket,
again stored as a~list. Putting an~edge to a~bucket can be done in constant time then,
scanning all~$n$ buckets takes $\O(n+m)$ time.

\FIXME{Add an example of locally determined orders, e.g., tree isomorphism?}

%--------------------------------------------------------------------------------

\section{Data structures on the RAM}

There is a~lot of data structures designed specifically for the RAM, taking
advantage of both indexing and arithmetic. In many cases, they surpass the known
lower bounds for the same problem on the~PM and often achieve constant time
per operation, at least when either the magnitude of the values or the size of
the data structure are suitably bounded.

A~classical result of this type are the trees of van Emde Boas~\cite{boas:vebt},
which represent a~subset of the integers $\{0,\ldots,U-1\}$, allowing insertion,
deletion and order operations (minimum, maximum, successor etc.) in time $\O(\log\log U)$,
regardless of the size of the subset. If we plug this structure in the Jarn\'\i{}k's
algorithm (\ref{jarnik}), replacing the heap, we immediately get an~algorithm
for finding the MST in integer-weighted graphs in time $\O(m\log\log w_{max})$,
where $w_{max}$ is the maximum weight. We will show later that it is even
possible to achieve linear time complexity for arbitrary integer weights.

A~real breakthrough has been made by Fredman and Willard, who introduced
the Fusion trees~\cite{fw:fusion} which again perform membership and predecessor
operation on a~set of integers, but this time with complexity $\O(\log_W n)$
per operation on a~Word-RAM with $W$-bit words. This of course assumes that
each element of the set fits in a~single word. As $W$ is at least~$\log n$,
the operations take $\O(\log n/\log\log n)$ and we are able to sort integers
in time~$o(n\log n)$. This was a~beginning of a~long sequence of faster and
faster integer sorting algorithms, culminating with the work by Thorup and Han.
They have improved the time complexity to $\O(n\log\log n)$ deterministically~\cite{han:detsort}
and expected $\O(n\sqrt{\log\log n})$ for randomized algorithms~\cite{hanthor:randsort},
both in linear space.

Despite the recent progress, the corner-stone of most RAM data structures
is still the representation of data structures by integers introduced by Fredman
and Willard and it will also form a~basis for the rest of this chapter.

\FIXME{Add more history.}

%--------------------------------------------------------------------------------

\section{Bits and vectors}

\endpart
