Compl
Compl
Peter J. Cameron
ii
Preface
These notes have been developed for the first part of the course MAS223, Com-
plexity and Optimisation in Operations Research, at Queen Mary, University of
London. The description for this part of the course reads:
The O and o notation; arranging functions in order of value for large argu-
ment.
Input data representation and simple algorithms for arithmetic, matrix, and
graph problems.
Turing machines; ability to translate instructions into the action of the ma-
chine.
[Link] ˜ped/teachadmin/algor/[Link]
and some applets demonstrating various heuristics for the Travelling Salesman
Problem by Stephan Mertens (University of Magdeburg) at
[Link]
Peter J. Cameron
March 2001
iv
Contents
1 Introduction 1
1.1 Minimal connector, travelling salesman . . . . . . . . . . . . . . 1
1.2 Graphs, trees and circuits . . . . . . . . . . . . . . . . . . . . . . 6
1.3 Proofs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
v
Chapter 1
Introduction
The travelling salesman problem: a salesman has to travel to all the towns,
visiting each town once and returning to his starting point. We want the
1
2 CHAPTER 1. INTRODUCTION
John o’Groats
Inverness
Kyle of Lochalsh
Fort William Aberdeen
Glasgow
Birmingham
Harwich
London
Cardiff Dover
Exeter
Although these two problems look quite similar, we will see that the minimal
connector problem is “easy”, but the travelling salesman problem is “hard”. There
is no difficulty in principle in solving the travelling salesman problem – we could
simply look at all possible cyclic tours through the towns – but the number of
possibilities to check grows very rapidly, and for even a moderate number of towns
it is not practicable to check all possibilities.
First we attack the minimal connector problem in a simple-minded way. We
first choose the shortest possible link between any two towns. We continue doing
this until all the towns are connected, except that, if two towns already have an
indirect connection, we do not need to link them again. Thus, for example, if we
have already chosen edges AB, BC and CD, there is no need to include AD.
1.1. MINIMAL CONNECTOR, TRAVELLING SALESMAN 3
B C D E F G H I J K L
A 676 813 947 916 240 233 861 169 373 304 832
B 166 312 253 631 470 269 737 924 758 188
C 383 195 781 620 396 884 1094 908 253
D 399 959 786 201 1001 1201 1080 114
E 901 723 449 995 1197 1011 291
F 163 874 106 314 127 821
G 695 267 475 288 639
H 916 1116 983 122
I 208 135 885
J 304 1067
K 943
More formally, the procedure (known as the greedy algorithm for minimal
connector) works as follows:
Repeat the following step until the edges in T connect all the towns:
What is the output of this algorithm? Clearly the final list T of edges connects
all the towns. The solution contains no cycles, since to create a cycle we would
have to add an edge joining two towns already connected. Thus, the solution is a
tree. It is not obvious that it is a minimal connector, but in fact this is the case,
as we prove later. It is also clear that this is an “efficient” algorithm (we will also
make this more precise later).
We see that this algorithm begins by producing a number of disconnected
pieces, which later coalesce. We can avoid this by a small modification to the
algorithm, which also can be shown to produce a minimal connector. This is
Prim’s algorithm:
Let L be the list of all pairs of towns (sorted by increasing distance), and T
the empty list.
Repeat the following step until the edges in T connect all the towns:
– Take the first pair in the list L having the property that one of its towns
lies on an edge in T and the other does not; say t1 t2 . Add t1 t2 to
T.
Exercise 1.1.1 Work through this algorithm in the example, and show that it finds
the same minimal connector as the greedy algorithm (though the edges are chosen
in a different order).
Let us try the same technique for the travelling salesman. Adapting the above,
we have the greedy algorithm for travelling salesman. We assume that the number
n of towns is greater than 2, else there is not much choice about the travelling
salesman’s itinerary.
Let L be the list of all pairs of towns (sorted by increasing distance), and T
the empty list.
Take the first pair in L; add it to T . At this and all subsequent stages except
the last, the edges in T will form a path, so we can talk about the ends of
the path.
Repeat the following step until the edges in T connect all the towns:
– Take the first pair in the list L having the property that one of its towns
is an end of the path T and the other is not on the path; say t1 t2 .
Add t1 t2 to T .
Add to T the edge joining its two endpoints, creating a cycle. Return the
list T .
Although this looks superficially similar to Prim’s algorithm, and it does produce
an itinerary for the travelling salesman, it does not produce a tour of smallest
length.
In our example, the edges are chosen in the order
FI, FK, AI, AG, KJ, BG, BC, CE, EL, DL, DH, HJ,
it guarantees to find a solution which is not too far from the optimal;
it makes some random choices, and guarantees to find the optimal with not-
too-small probability;
Find a minimal connector (e.g. using the greedy algorithm or Prim’s algo-
rithm).
Find a tour visiting all the towns and returning to its starting point, using
each edge of the tree twice. (We will discuss later how this is done.)
Take this tour, and modify as follows: At each stage, go directly to the next
town on the tour which has not yet been visited. Return the result.
In our example, the tour in the second stage of the algorithm can be chosen to
be AIJIFKFGBCECBLDLHLBGFIA, and the final travelling salesman’s tour is
then AIJFKGBCELDHA, with length 3404, not too far from optimal!
A list of distances between pairs of towns is said to satisfy the triangle in-
equality if, for any three towns x y z, we have
d x y d y z d x z ;
............
.... ...
... ...
... ...
...................................................... .....
......................
.....................................................
set E by the ordered pair V E . Figure 1.2 shows a graph, in the usual pictorial
representation, with vertices represented by dots and edges by lines.
Note that our definition allows two features which are sometimes disallowed:
There may be several edges incident with the same pair of vertices. We call
such edges multiple edges. We saw in the twice-round-the-tree algorithm
that multiple edges have their uses!
There may be an edge with the property that the two vertices incident with
it are equal. Such an edge is called a loop.
A graph without loops and multiple edges is called a simple graph. (A graph
which may contain them is sometimes referred to as a general graph or multi-
graph.) An important example of a simple graph is the complete graph Kn , the
graph with n vertices and one edge incident with each pair of vertices. (That is,
the edge set E consists of all 2-element subsets of the vertex set V .)
An edge-weighted graph is a graph with a number d e associated with each
edge e. The total weight of an edge-weighted graph is the sum of the weights of
the edges. The weights d e are non-negative real numbers, which may represent
distances, capacities of pipelines, costs of building communication links, etc.
A walk in a graph is a sequence
v0 e1 v1 e2 v2 vn 1 en vn
where v0 v1 vn are vertices, e1 en are edges, and ei is incident with vi 1
and vi for i 1 n. We say that it is a walk from v0 to vn . Two classes of walks
are particularly important:
If all the vertices are different, the walk is called a path.
If all the vertices are different except that vn v0 (and also, if n 2, the
two edges are different), then the walk is called a circuit.
If two vertices are joined by a walk, then they are joined by a path. For, if the
vertex v occurs more than once on the walk, we can delete the part of the walk
between the first and last occurrence of v and obtain a shorter walk. After doing
this finitely many times, we will obtain a path.
8 CHAPTER 1. INTRODUCTION
Initialise by setting S E.
While the graph V S contains a circuit, delete from E any edge which lies
in at least one circuit.
A tree with the properties given in this proposition is called a spanning tree
of the graph G. If G is a weighted graph, then a spanning tree of G with smallest
possible total weight is a minimal connector.
If a graph consists of a circuit, then removing any edge gives rise to a spanning
tree.
Let G V E be a graph. A Hamiltonian circuit in G is a circuit containing
all the vertices of V (each exactly once). Clearly a graph containing a Hamiltonian
circuit is connected. The converse is false, and there is no simple test known for
recognising Hamiltonian graphs (those containing Hamiltonian circuits). As we
will see, this is a hard problem.
Solution Any circuit passing through all vertices in any order is Hamilto-
nian, since each pair of vertices is joined by an edge.
Exercise 1.2.2 Is the graph shown in Figure 1.3 (the so-called Petersen graph)
Hamiltonian?
10 CHAPTER 1. INTRODUCTION
$ # #
$ #
$ #
$ #
$
$ %
" % #
%$* % * " % )
#
) "
% ** ## " % $$ ) ) "
% # " $% "
% " # $ #$ % "
% & " "
% & $ #
( "
%'& ( "
( "
Solution The Petersen graph does not have a Hamiltonian cycle. You should
follow the argument below on the drawing of the graph.
Consider the five edges joining the outer pentagon to the inner pentagram. Any
Hamiltonian circuit must start and end either in the outer or in the inner cycle, and
so must use an even number of these edges.
Suppose that a cycle uses two of the crossing edges. Then it must follow the
outer cycle between their outer ends and the inner cycle between their inner ends.
But if the outer ends are four steps apart, then the inner ends are not more than
three steps apart. So no cycle can be formed using two crossing edges.
Suppose that a cycle uses four crossing edges. We can suppose that the one
not used is the vertical edge in the figure. Then the two vertices on this edge must
be reached by two edges of the outer and inner cycles. This gives us eight of the
ten edges of the cycle, and clearly there is no way to join them up to form a cycle.
1.3 Proofs
In this section we give the proofs of two results from the first section: the greedy
algorithm always finds a minimal connector; and, if the triangle inequality holds,
12 CHAPTER 1. INTRODUCTION
Theorem 1.3.2 Suppose that the edge weights in a complete graph satisfy the
triangle inequality. Then the twice-round-the-tree algorithm always finds a trav-
elling salesman tour whose length is less than twice the minimum.
d v x1 d x1 x2 9...: d xm w - d v w
so these short cuts don’t increase the weight of the tour. So if the final weight is
L , we have L , 2l 1 2L, and we are done.
d v x1 d x1 x2 9...: d xm w - d v w
d v x1 d x1 x2 9... d xm 1 xm 5 d v xm
and by hypothesis,
d v xm d xm w - d v w
The result follows.
Solution Let M be the weight of the minimal connector. Then the algorithm
produces a travelling salesman tour of weight at most 2M.
Now removing any edge from a travelling salesman tour gives a spanning
tree, whose weight is thus not smaller than M. Suppose we remove the edge of
largest weight x in the tour. Then at least n edges of the complete graph have
weight smaller than x, so x t, where t is the nth smallest edge weight. Thus
M , L x , L t.
Similarly, if we pick a vertex v and remove the edge of the travelling salesman
tour containing v and of larger weight x, then x is at least the second smallest
weight of an edge through v, and the argument proceeds as before.
This chapter ends with something a bit different. We often use the principle
that, if one of N possibilities can be determined uniquely as a result of n binary
choices, then N , 2n . (This is sometimes called the “Twenty Questions” principle,
after the panel game in which the panellists are allowed to ask twenty questions
with “yes” or “no” answers and have to identify some object. Since 220 is a
little greater than a million, in theory one of a million objects can be identified.
The following exercise shows that there is a ternary version as well, where each
question is allowed to have one of three possible answers.
Exercise 1.3.3 (a) I have twelve coins, which are identical except that one of the
coins is either lighter or heavier than the others. I have a balance which can
compare the weight of two sets of coins. Show that, in three weighings, I
can determine which coin is different, and whether it is lighter or heavier
than the others.
(b) Each weighing can have three results (left-hand side heavier, right-hand side
heavier, or exact balance). So in three weighings I can distinguish 33 27
possibilities. If I had 13 coins C1 C13 , I might expect to be able to
determine which of the possible cases “Ci light”, “Ci heavy” (for 1 , i , 13)
or “ all coins the same”, since there are 2 . 13 1 27 possibilities. (This
argument shows that we certainly can’t deal with more than 13 coins in just
three weighings.)
Is there a scheme for determining which coin out of 13 is different in only
three weighings?
(b) If we have 13 coins, then there are 27 possibilities (each coin could be
either light or heavy, or they might all be the same) to be determined by three
weighings each with three possible outcomes. Since 33 27, this would only be
possible if the first weighing reduced the number of possibilities to 9, the second
weighing to 3, and the third weighing to just one. But consider the first weighing,
and suppose that we put m coins in each pan. If the left-hand pan is heavier, then
we have 2m possibilities (a coin in the left-hand pan may be heavy, or a coin in the
right-hand pan may be light). There is no integer m satisfying 2m 9, however,
so the weighing is not possible.
16 CHAPTER 1. INTRODUCTION
Chapter 2
In this section, we make precise the notion that finding a minimal connector is easy
while finding a minimal travelling salesman tour is hard. As said earlier, there is
no difficulty in principle in either case, but we know how to find a solution quickly
in the first case, and we don’t know how (and suspect that it is not possible) in the
second.
Our measure of the complexity of a problem will be the amount of computa-
tion resources required to solve it, or (more precisely) how this grows as a function
of the amount of data required to specify the problem. Various resources can be
considered; for example:
time, the number of computational steps required to solve the problem on
an idealised model of a computer;
processors, the number of processors used (in the case of a parallel algo-
rithm);
17
18 CHAPTER 2. PROBLEMS, ALGORITHMS, COMPUTATIONS
Note: There is one computational model to which this remark does not apply.
This is a quantum computer. Although a quantum computer cannot compute any-
thing which could not be computed on a Turing machine, it can perform exponen-
tially many Turing machine steps in a single cycle. However, quantum computers
have not yet been built!
the first is a decision problem, “Is the graph Hamiltonian?”, to which the
answer is simply “yes” or “no” (one bit of information);
These three types of problems are more closely related than they appear. A
decision problem is often a special case of a counting problem. (To decide whether
G is Hamiltonian, we could count the number of Hamiltonian circuits in G and see
whether the number is zero or not.) A decision problem may also be a special case
of an optimization problem. (As we noted, to decide whether G is Hamiltonian or
not, we give weight 1 to each edge of G and weight 2 to each non-edge; then G is
Hamiltonian if and only if the shortest travelling salesman tour has length n.)
In the other direction, suppose that we have an optimization or counting prob-
lem, to which the solution is known to be a non-negative integer strictly smaller
than N, for some number N. (The solution to a counting problem is always a
non-negative integer; and, for example, the number of Hamiltonian circuits in a
graph on n vertices will certainly not be greater than n! . In the travelling salesman
problem, if all edge weights are non-negative integers not exceeding M, then the
length of the shortest tour is an integer at most nM.)
In this situation, we can solve the optimization problem by solving at most
log2 N decision problems of the form “Is the answer at least K?”, for various
integers K. For, when written in base 2, the solution has (at most) log2 N binary
digits, and these can be determined one at a time (from largest to smallest) by a
sequence of questions of the above type.
2.2. INPUT AND OUTPUT 19
For example, if the answer is known to be at most 64 and is actually 27, we ask
“Is the answer at least K?” for K 32, 16, 24, 28, 26, 27, receiving the answers
No, Yes, Yes, No, Yes,Yes; these answers reveal that the number is 011011 in
base 2.
We will usually assume that the solution to any optimization problem is an
integer. Usually there will be an a priori upper bound which is at most the expo-
nential of a polynomial in the size of the input data, so we can reduce the problem
to a polynomial number of decision problems.
We could give a list of n lists, the ith list consisting of all numbers j for
which v j is adjacent to vi . This is slightly less efficient in general, since
each number j lies in the interval = 0 n 1> and therefore takes log2 n bits to
write down in base 2 notation, so we might require as many as n2 log2 n bits
in [Link], if the graph has only a few edges, then this method is
better. For example, if each vertex is joined to exactly three others, then the
number of bits required is 3n log2 n.
20 CHAPTER 2. PROBLEMS, ALGORITHMS, COMPUTATIONS
? B@ ? E@
? @ ? @
? @ ? @
? @ ? @
? @ ? @
? @ D? @ G
A@ ? @ ?
@ ? @ ?
@ ? @ ?
@ ? @ ?
@ ? @ ?
@ ? @ ?
C F
A: BC
B: ACDE
C: ABDF
D: BCEF
E: BDFG
F: CDEG
G: EF
You can see an example of a moderately large graph on the Web. This is a
fragment of the mathematical collaboration graph, whose vertices are all math-
ematicians, two vertices adjacent if they have written a joint article. One very
prolific mathematician was Paul Erdős, who died in 1996. He had over 500 co-
authors. At the web site
2.3. ORDERS OF MAGNITUDE 21
[Link] ˜grossman/[Link]
you can find the vertices of the collaboration graph at most two steps from Erdős
(these number over 5000), with all edges involving at least one mathematician
adjacent to Erdős. The graph is given by the “lists of neighbours” method. (A
similar database for the Kevin Bacon game, at
[Link]
does not make the lists available but simply looks up shortest paths to Kevin Ba-
con.)
All that we require in the representation of input data is that it is not too in-
efficient. For example, an important problem (related to cryptography) is that of
deciding whether a given positive integer n is prime. The input is the number n. It
could be given as a string of n ones, but this is very inefficient; we could instead
write n in base 2, needing only about log2 n 1 bits. (It is simple to test in n
steps whether n is prime; to do it in log n k steps, for any fixed k, is much more
challenging!)
Solution We are asked to prove that the inequality n! , Acn is false for large
enough n, for any constants A and c. We observe that, if c1 1 c2 , then A1 cn1 1 A2 cn2
holds for large enough n for any positive constants A1 and A2 . Taking logarithms,
we require that
n log c1 log A1 1 n log c2 log A2
and this is true as long as
log A1 log A2
n3
log c2 log c1
So it is enough to prove that the inequality fails for given c and some value of A.
We may assume that c is an integer.
But clearly, for any integer c, we have n! 3 c!cn c M c! < cc cn for n 3 c, since
all factors in the product apart from 1 c are greater than c.
For the last part, note that
n! 1 . 2 ... n , n . n ... n nn
so certainly n! O nn holds.
Exercise 2.3.2 A function g on the natural numbers is said to grow faster than
another function f if g n +3 f n for all sufficiently large n (that is, all n 3 n0 , for
some number n0 ). Arrange the following functions in increasing speed of growth:
10 10 10
1010 n 10n10 10n log n n N n 10 10 10 log n n
10 n 10 log n n 10 n!
Solution We use the fact that log n grows slower than any power of n, and an
exponential function of n grows faster than any power of n. Also, n! grows faster
than any exponential function of n. But n! is smaller than nn en log n , so grows
α
slower than ecn for any α 3 1.
For posers of n, we can ignore any constants, and order them by the exponent:
10 10
thus, 1010 n comes before 1010 n10 , which comes before 10n10 .
How about a function like n N n ? This is equal to e N n log n , so grows slower than
ecn , since the exponent O n log n grows slower than cn (because log n grows slower
than c O n).
So finally the order is
10 10 10
1010 log n 1010 n 10n log n 1010 n10 10n10 nlog n n N n n
n! 10
2.4 Examples
In this section, we give a few examples in an informal style. These can of course
be done more formally.
2.4. EXAMPLES 23
At the conclusion of this loop, we have i n, and there are no more digits
to add. If c ! 0, then put zn : c.
Although xi yi c appears to involve two additions, it is easy to see that c
is either 0 or 1. So the operation in this step can be done by looking up tables of
“addition carrying zero” and “addition carrying one”. Only n such lookups and
assignments are required.
zi x0 yi x1 yi 1 9... xi y0 carry
x u1 2n P 2
u0 y v1 2nP 2
v0
then
xy w2 2n w1 2nP 2
w0
where
w2 u1 v1
w1 u1 v0 u0 v1 w2 w0 8 u1 u0 Q v1 v0
w0 u0 v0
Thus multiplication of two n-bit numbers requires three multiplications of n < 2-bit
numbers together with O n additions and subtractions of digits.
24 CHAPTER 2. PROBLEMS, ALGORITHMS, COMPUTATIONS
T n R Anlog 3 P log 2
O n log n
where Sn is the set of all permutations of 1 : n and sign is the sign of the per-
mutation. This is clearly very bad: there are n! terms to be calculated, each term
involving n 1 multiplications, so the number of steps is more than exponential
in n.
For example, let
1 2 3
A LX 4 5 6 X
X X
X7 8 9X
X X
X X
Equation 2.1 gives X X
det A + 1 5 9 2 6 7 3 4 8 1 6 8 2 4 9 3 5 7 0
5 6 4 6 4 5
det A R 1 . 2. 3.
X8 9X X7 9X X7 8X
X X X X X X
X X X X X X
which involves calculating threeX smaller
X determinants.
X X X X
2.4. EXAMPLES 25
1 2 3 1 2 3
det A X0 3
6 X X 0 3 6 X 0
X X X X
X 0 6 12 X X 0 0 0 X
X X X X
X X X X
Of course, the advantage of n over n! is not clear for n 3.X
X 3 X X
The O n3 can be further improved using a trick somewhat like that for multi-
plication.
The permanent of a matrix is the function worked out by the formula for the
determinant in Equation (2.1), but leaving out the sign factor:
per A 1 5 9 2 6 7 3 4 8 1 6 8 2 4 9 3 5 7 450
Exercise 2.4.1 Show that any algorithm for the determinant of an n ; n matrix (or
even for deciding whether the matrix is non-singular) requires at least n2 steps.
– Set f : \cedgfhb .
– Run through the vertices which have been assigned the number i.
Whenever such a vertex has an out-neighbour which has not yet been
assigned a number, assign i 1 to it and set f : 7[^]`_ a:b .
– Set i : i 1.
When we reach this stage, no new assignments have been made at the last
pass. Terminate the algorithm and return the vertex assignments.
The number assigned to each vertex v is, as claimed, the least number of steps
from s to v. We prove this as follows. First, there is a path from s to v of length i.
This is clear if i 0, since s is the only vertex assigned 0. If i 3 0, then v is an
out-neighbour of a vertex u to which i 1 was assigned. By induction, we can
reach u in i 1 steps, whence we can reach v in i steps.
We must also show that there is no shorter path. Suppose that this is false, and
let v be chosen so that the shortest path from s to v is smaller than the number i
assigned to v, and (subject to this) that i is minimal. Clearly i 3 0. But if u is the
penultimate vertex on a path from v to w, then the number assigned to u is equal to
its distance from s (which is less than i 1), and the algorithm assigns a number
less than i to v, a contradiction.
The argument above shows that from the assignments we can find a shortest
path from s to v by backtracking. If i is assigned to v, then choose any in-neighbour
of v to which i 1 is assigned, and work back in this manner until s is reached.
Exercise 2.4.2 Apply this algorithm to the graph shown in Figure 2.1, with each
edge oriented in both directions.
2.4. EXAMPLES 27
Solution The table shows assignments and vertex labels after each pass of
the algorithm:
i f A B C D E F G
0 [^]`_ a:b 0
1 [^]`_ a:b 0 1 1
2 [^]`_ a:b 0 1 1 2 2 2
3 [^]`_ a:b 0 1 1 2 2 2 3
4 cedgfhb 0 1 1 2 2 2 3
∑ f x v R ∑ f v y
V x i v WjU E V vi y WjU E
In other words, the flow in each edge cannot exceed its capacity, and the flow into
and out of any vertex other than the source or target must balance (so that the net
flow out of such a vertex is zero).
It is easily checked that, for any flow, the net flow out of s is equal to the net
flow into t; this number is called the value of the flow. We are interested in finding
a flow whose value is as large as possible.
If all the edge capacities are positive integers (and are not too large), then there
is an efficient algorithm to solve the problem. It works as follows.
We define a flow-augmenting path to be a directed path which uses the follow-
ing two types of edges:
Type 1: any edge e k x y for which the flow in e is less than the capacity;
Type 2: any edge e in which the flow is non-zero, but used in the reverse direction
– that is, if e x y has non-zero flow, then the path is allowed to use the
“edge” y x .
Now suppose that we have a flow f with the property that each f e is an integer.
(We call such a flow integral.) The following step attempts to augment the flow
(that is, to increase its value).
Calculate the set S of all vertices x for which there exists a flow-augmenting
path from s to x. (That is, find the vertices which can be reached from s using the
above two types of edges.) There are two possibilities:
28 CHAPTER 2. PROBLEMS, ALGORITHMS, COMPUTATIONS
(a) t l S. In this case, take a flow-augmenting path from s to t, and modify the
flow by increasing by one the flow in each edge of the first type on the path,
and decreasing by one the flow in each edge of the second type. It can be
checked that we obtain a new flow f whose value is one greater than the
value of f .
(b) t l < S. In this case, let T be the complementary set to S. Then s l S and t l T .
Moreover, if C is the set of edges from S to T , then each edge in C carries its
full capacity in the flow f . The set C is called a cut, since its removal leaves
no path from s to t. Now no flow can have value larger than the capacity of
any cut (see below). So the existence of the cut C whose capacity is equal
to the value of f demonstrates that no flow with larger value is possible.
Suppose that C is any cut, consisting of the edges from S to the complementary
set T , and f any flow, with value v. The flow out of s is equal to v; since all other
vertices of S have equal flow in and out, the net flow out of S is equal to v. That
is, the flow out of S (which must use the edges of C) minus the flow into S must
be equal to v. So the capacity of C must be at least v.
Now the algorithm can be given.
Start with the zero flow.
Repeatedly attempt to augment the flow until no further augmentation is
possible.
At this point, return the flow f and the cut C defined in case (b) above.
Because the value of f is equal to the capacity of C, there cannot be a flow with
larger value.
The number of times that we can augment the flow is not greater than the sum
of the capacities of all the edges, so is at most n1 n2 , where n1 is the number of
edges and n2 the largest capacity of an edge. Searching for the flow-augmenting
path takes at most n1 steps. So the whole procedure runs in at most n21 n2 steps.
So we have given an algorithmic proof of the following two important theo-
rems.
Theorem 2.4.1 (Max-Flow Min-Cut Theorem) In any network with positive in-
teger capacities, the maximum value of a flow is equal to the minimum capacity of
a cut.
Theorem 2.4.2 (Integrity Theorem) In any network with positive integer capac-
ities, there is an integral flow with maximum value. Such a flow can be found in a
number of steps polynomial in the number of vertices and the maximum capacity
of an edge.
2.4. EXAMPLES 29
Example Consider the network shown in Figure 2.2, in which all edges have
capacity 1.
m m`n o o m `qn o o
a c
m o o m m o o
m
m o m o o m m o `o p
m m o m`n t
s o
o o m m o o
m m m
o o`p m m o o `p m m
b d
In the first pass of the algorithm, we might choose the augmenting path sact,
and introduce a flow of 1 in each of the edges s a , a c , c t . At the next stage,
there is an augmenting path sbcadt (note that we use the edge a c in the wrong
direction, since this edge carries a positive flow). So we introduce a flow of 1 in
s b , b c , a d and d t , and reduce to zero the flow in the edge a c . Now
there are no augmenting paths leaving s at all; so we have a flow of maximum
value (namely 2), and the edges s a and s c form a cut with capacity 2.
In practice, rather than implementing the algorithm for Max-Flow as given,
it is more efficient to begin by guessing a flow, reducing the capacities of edges
accordingly, and then implementing the algorithm. The larger the value of the
flow we guess, the fewer iterations of the algorithm are needed.
Exercise 2.4.3 Find a maximal flow and a minimal cut in the network in Fig-
ure 2.3. (The numbers written on the edges represent capacities, and arrows give
directions.)
8 3 2 4 2 1 6 0 1 2 2 9
to the edges
sa sb ab ad ac cb be dc ce de dt et
respectively, we have a flow of value 11. In searching for an augmenting path, we
observe that sacet consists of edges all in the positive direction and all carrying
less than capacity. So we increase the flow in these edges obtaining
9 3 2 4 3 1 6 0 2 2 2 10
30 CHAPTER 2. PROBLEMS, ALGORITHMS, COMPUTATIONS
@a 4 ? q d@
? ?r @ ? @
? @ ? @
? @3 2? @3
? 10 @ @s ?u ? @
? @ s
s @ 2 c? @ 2 ? ?r
t
@ ? @
@ ? @ ?
4 @ ? 1 5 @ ? 12
@ @s t ?u ? @ ?
@ s t?
6 q
b e
with value 12. Now, in searching for an augmenting path, we find that we can use
the edge sa, but all outlets from a are blocked; we can use sb, then bc (which is
carrying a flow of 1 in the reverse direction), then cet. This gives a new flow
9 4 2 4 3 0 6 0 3 2 2 11
with value 13. This time in searching for an augmenting path we find that we can-
not leave the set s a b , so the edges leading out of this set, namely, ad ac be ,
form a cut with capacity 13, demonstrating that we have the maximum possible
flow.
where s is the source and t the target; the edges are as follows:
an edge s ri for 1 , i , n;
2.4. EXAMPLES 31
an edge c j t for 1 , j , n.
Each edge has capacity 1.
Proof Considering the edges out of s, we see that the value of a flow cannot be
greater than n, and is equal to n if and only if it uses every edge out of s.
Suppose that the maximum value of a flow is equal to n. By the Integrity
Theorem, the flow realising this value can be taken to be integral; and it uses
every edge out of s and every edge into t. Now the flow has value 1 from each
ri to some c j , and clearly the map σ taking i to j is a permutation of 1 n .
Thus, aiσ V i W ! 0 for i 1 n, and we get a non-zero term in the expression for
the permanent. Since all entries are non-negative, the permanent is non-zero.
Conversely, if the permanent is non-zero, then at least one term, say the term
a1σ V 1W a2σ V 2 W ... anσ V nW , is non-zero. Then there is a flow of value n, using the edges
s ri , ri cσ V i W , and ci t for 1 , i , n.
Theorem 2.4.4 Let A1 : An be subsets of a set S. For any set I of indices (that
is, for any subset I of 1 n ), let
A I w/x Ai
iU I
32 CHAPTER 2. PROBLEMS, ALGORITHMS, COMPUTATIONS
This is a very important and useful theorem. However, since there are 2n
subsets of 1 : n , checking the conditions directly would be very slow! But
we can do better. We restrict to the case where S has exactly n elements (that is,
in the application, there are as many boys as girls).
Define an n ; n matrix M mi j , where
1 if i l A j ,
mi j {z
0 if not.
This is sometimes called the incidence matrix of the family of sets. Then the
permanent of M is non-zero if and only if there exists a SRD for the family; that
is, if and only if compatible marriages can be arranged. By Proposition 2.4.3,
there is an efficient algorithm to decide whether this holds or not. More generally,
any SDR corresponds to a non-zero term in the permanent; so the permanent is
equal to the number of [Link] next example illustrates.
Solution The network is as follows. All edges are directed from left to right
and have capacity 1.
A #
m? ~ W
$ $ @o o@ o m m? ~ #
$ m ?o ~o X # #
$
$
| Bm m @? ~ o } }
s$|#} |} |
| |
? ~@ @ } } #} # t
}
# } } } } C ? ~ @ Y | | | |$ $|
# ~ | $
# ~ $
# # ~ $
D Z$
2.4. EXAMPLES 33
So the permanent is equal to 3, and there are three compatible matchings. These
are exactly given by the three terms shown, namely
A W , B X , C Y and D Z
A X , B W , C Y and D Z
A Y , B X , C W and D Z .
Sorting One of the commonest jobs that computers do is sorting data: given a
list of data items, in an unknown permutation of the correct order, the task is to
restore the list to the correct order. We assume that the items in the list are integers
and we are required to sort them into increasing order, but similar remarks apply
to any sorting task. (Indeed, in discussing the Greedy Algorithm for the Minimal
Connector and Travelling Salesman problems in Chapter 1, we saw the advantage
of sorting the list of pairs of towns in order of increasing distance.)
Each comparison or movement of data can be broken down into more elemen-
tary machine steps; the number of such steps which is at most some polynomial in
the number of digits of the numbers compared. So we will simply count the num-
ber of comparisons required by an algorithm in order to estimate the complexity
of the task. Let n be the number of items in the list to be sorted.
There are n! possible orderings of the list. By the end of a successful sort,we
have effectively identified which one of these possibilities actually occurred. So
34 CHAPTER 2. PROBLEMS, ALGORITHMS, COMPUTATIONS
the number of comparisons required is not more than log2 n! n log2 n. (As a
result of k yes-no questions, we can identify one of at most 2k possibilities.)
The simplest sorting algorithm is Bubblesort: we make repeated passes through
the list, and whenever we find two elements out of order, we interchange them.
Note that each element can move at most one place in each pass. So, if the
largest element occurs in position i, we will need at least n i passes, requiring
n i Q n 1 comparisons. We see that
there are many orderings that require at least n 1 2 comparisons;
On average, at least n n 1 < 2 comparisons are required.
Clearly, this is not very good compared to our lower bound!
Several better sorting algorithms are known. One of the simplest to describe
is Quicksort, which works as follows:
Let x be the first element of the list, and split the remainder of the list into
sublists L R consisting of elements less than and greater than x respectively
(this requires n 1 comparisons).
Recursively sort L and R.
Sort L1 and L2 .
Merge the sorted lists, by starting with an empty list M and repeating the
following operation:
2.4. EXAMPLES 35
(b) A new algorithm for the problem has been devised which only involves look-
ing through 1000n3 possible permutations instead of all n!. These con-
figurations are more complicated to generate; each takes one microsecond
( 10 6 second). They can still be tested in four nanoseconds. If I use the
new algorithm, how large an instance can I solve?
(a) Generating and testing one permutation takes 5 ; 10 9 seconds, so the num-
ber of permutations we can check is 604800 <J 5 ; 10 9 1 2 ; 1014 . So
the number n must satisfy
n! , 1 2 ; 1014
or n , 16.
(b) This time, generating and testing one permutation takes 1 004 ; 10 6 sec-
onds, so the number we can check is 604800 <J 1 004 ; 10 6 6 02 ; 1011 .
So the number n must satisfy
1000n3 , 6 02 ; 1011
or n , 844.
Chapter 3
Complexity: and
37
38 CHAPTER 3. COMPLEXITY: AND
β β x1 x2 x3 x4 x5 x6 ...
x j l a1 an si
The head moves over the tape, so that at any given clock cycle it is scanning
one square of the tape. It also can be in any one of a finite set of internal states
S { s1 : sr . Usually we assume that there is a distinguished starting state;
there is no harm in also assuming a stopping state, as we shall see.
The action of the machine is specified by giving a list of instructions. Each
instruction has the form
for each a new quadruple si a j a j sH (that is, “leave the symbol scanned as it is and
move into state sH ”). Since no quadruple begins sH , the new machine will now
halt in this state.
For a NDTM, the difference is that the machine may have a choice of in-
structions at some stage. In this case, in a particular computation, it chooses one
instruction to obey. So we have a branching tree of possible computation paths.
Some paths may lead to the machine halting, while others lead to it continuing
forever. Again, we are only interested in computation paths which halt; again we
may assume that the machine halts in a distinguished halting state.
Example The following list of quadruples defines a Turing machine which does
the following job. If we write a number n on the tape in base 2, and put the head
in an initial state s0 scanning the blank square immediately to the right of the
number, it replaces n by n 1 and returns to its starting square before halting in
state sH . Check this by tracking its operation on a number of your choice.
s0 βLs1
s1 10s2
s2 0Ls1
s1 01s3
s1 β1s3
s3 0Rs3
s3 1Rs3
s3 ββsH
A couple of observations are in order. First, we have not specified how the
machine should act if it is not set up according to the specification. Indeed, if it
starts in state s0 not scanning a blank, it does nothing; and if it starts scanning a
blank not immediately to the right of a binary string, it changes the blank to its
left to a 1 and then halts.
Second, not all state-symbol pairs occur in quadruples. If the machine is in
state s2 , it expects to be scanning a zero: the only way state s2 arises in normal
operation is when the machine has changed a 1 to a 0 and is about to move left.
Exercise 3.1.1 Consider the Turing machine defined by the following seventeen
quadruples. The states are s0 s1 s2 sH t1 t7, and the tape symbols are β (blank),
40 CHAPTER 3. COMPLEXITY: AND
0 and 1.
s0 βLt1 s1 βRt2 s2 βLt6
t1β0s1 t2 βLsH t6 10t7
s1 0Rs1 t2 11t3 t7 0Lt6
s1 1Rs1 t31Rt3 t601s1
t3βLt4 t6 β1s1
t41βt5
t5 βLs2
s2 1Ls2
(a) Show that, if the machine is in state s0 scanning a blank square with a blank
square to its left, then it writes 0 in the square to the left and returns to the
starting square in state s1 in three moves.
(b) Show that, if the machine is in state s1 scanning a blank square with a blank
square to its right, then it halts on the starting square in state sH in two
moves.
(c) Show that, if the machine is in state s1 scanning a blank square with a string of
1s of length n (followed by a blank) to its right, then it erases the rightmost
1 and returns to its starting square in state s2 in O n moves.
(d) Show that, if the machine is in state s2 scanning a blank square with the
number n written in base 2 immediately to its left, then it replaces n with
n 1 and returns to its starting square in state s1 in O log n steps. (You
may wish to compare the triples in the third column above with an example
from lectures.)
(e) Now suppose that the machine starts in state s0 scanning a blank square with
n ones immediately to its right (and the rest of the tape blank). Show that
it terminates on its starting square in state sH in O n2 steps. Describe the
configuration on the tape when the machine halts.
Solution For simplicity I will write a1 a2 = si > a3 to denote that the tape
has the symbols a1 a2 a3 written on it and the machine is in state si scanning
the square with a2 written.
(a) The first three instructions show that
ββ = s0 > I β = t1 > β I 0 = s1 > β I 0β = s1 >
(b) The first two instructions in the second column show that
β = s1 > 111 11β I β1 = t2 > 11 11β I β1 = t3 > 11 11β
Now while the machine is in state t3 scanning a 1, it moves right. This happens n
times until we have
β111 11β = t3 >
The next steps are
β111 11 = t4 > β I β111 1β = t5 > β I β111 1 = s2 > ββ
At this point there are n 1 ones on the tape, and the machine moves right for
n 1 stages to reach
β = s2 > 111 1ββ
in 2n 4 steps altogether.
(d) Consider the number n written in base 2. Suppose that the longest run of
ones starting at the right (the units digit) is k (this includes the possibility that
k 0, if the number has units digit zero. Then either the number in base 2 is
11 1 (k ones) which is equal to
2k 1
2k 2
9... 2 1 2k 1
011 11 = t6 > β I 011 10 = t7 > β I 011 1 = t6 > oβ
In other words, in two steps the machine changes a 1 to a 0 and moves left. So
after 2k steps we have 0 = t6 > 0 00β . Next
following which the machine moves right for k steps to reach 10 00β = s1 >
The binary number 10 00 is equal to N 2k n 1, and the total number of
steps taken is 1 2k 1 k 0 log2 n .
In the other case, where n 2k 1, the operation is the same except that
instead of changing a 0 to a 1 the machine changes a β to a 1 giving the number
10 00 2k n 1. The number of steps is the same.
(e) Let us denote by n the number n in base 2 written on the tape. Starting
with ββ = s0 > 1 1β (with n ones in the string), the machine first writes a 0 to
42 CHAPTER 3. COMPLEXITY: AND
the left (after three steps). We can write this configuration as 0 β = s1 > 1 1β .
Now, after O n log2 n steps, it removes a 1 from the string to the right and
increases 0 to 1 , resulting in 1 β = s1 > 1 1β (with n 1 ones in the string).
After a similar time, it becomes 2 β = s1 > 1 1β (with n 2 ones). That is, it
repeatedly removes 1 from the string and increases the number written to the left
by one. After doing this n times (which takes time nO n log2 n O n2 ), we
reach n β = s1 > β . Then by (b), two more steps take us to n β = sH > β and the
machine halts. So the operation of the machine can be described as follows:
If started in state s0 on a blank square with a string of ones to the right
and the rest of the tape blank, it counts the ones (and erases them) and
writes the number of ones in base 2 to the left, then halts; all this in
time O n2 .
Exercise 3.1.2 Outline the construction of a Turing machine which, when started
on a blank square with the number n written in base 10 to its left on the tape,
decides whether n is divisible by 3. [Hint: How would you decide whether n is
divisible by 3? How would you do this if you could only remember a very small
amount of information while you do the sum?] A detailed list of quadruples is not
required, but you should explain the principles that your machine uses.
We could allow it to move several squares left or right, instead of just one. (How-
ever, there are good reasons why the length of a jump should be bounded. The
finite number of state-symbol pairs cannot encode infinitely many jump lengths,
and physically the distance moved in one clock cycle is bounded because the head
cannot move faster than the speed of light.) We could replace the one-dimensional
tape with a multi-dimensional array; we could equip the head with its own mem-
ory in the form of a stack. We could even allow several communicating heads
(with restrictions as for jumps). It can be shown that none of these improve-
ments enlarges the class of computations which can be performed; and, although
they speed up computations somewhat, they do not change the definitions of the
classes and , to which we turn next.
Note that this definition refers to the “worst case” of the problem. It may
be that a typical problem instance can be solved very quickly, but there are a
few recalcitrant instances which take much longer. (Some people argue that an
“average case” complexity is more meaningful. It is certainly true that there are
many important problems where the average case is much easier than the worst
case.)
Note also that the definition says “there exists a Turing machine”, that is,
“there exists an algorithm”. So, to show that a problem is polynomial-time
solvable, all we have to do is to exhibit an algorithm which will solve the polyno-
mial in a polynomial number of steps. Our earlier arguments show that it is not
even necessary to translate the algorithm into a Turing machine; we can be quite
informal about the definition of steps. So all the problems in the final section of
the preceding chapter are in . However, to show that a problem is not in is
usually much more difficult: we have to show that there is no possible algorithm
which can guarantee to solve the problem in a polynomial number of steps.
Analogously, we say that a problem is non-deterministic polynomial-time solv-
able, or belongs to , if there is a non-deterministic Turing machine which has
an accepting calculation for any positive instance of the problem and takes at most
nk steps for some k, where n is the size of the input.
Since non-deterministic computations are quite hard to think about, we give
another interpretation. To specify a computation path of a non-deterministic Tur-
ing machine, we have to give some additional information which tells the machine
which instruction to execute at each point where an ambiguity arises. We can turn
this into a deterministic computation as follows. We give all the required infor-
mation in advance, so that the machine is presented with both the data for the
problem and some additional data forming a “certificate”. Now we require that
the machine can perform a deterministic computation, using information from the
certificate as well as the problem data, and terminate with the answer “yes” pre-
cisely in the case where the solution to the problem is “yes”.
For example, the problem “Given a graph, does it have a Hamiltonian circuit?”
is in . The certificate is just the Hamiltonian circuit. You can think about it like
this:
a problem is in if it can be solved quickly;
3.2. AND 45
a problem is in if a proposed solution can be checked quickly, using the
certificate to do the checking. For example, I can quickly convince you that
a graph is Hamiltonian, just by showing you a Hamiltonian circuit.
A feature of this definition is that, whereas the negation of a problem in
is also in (since we can just perform the calculation for the original problem
and then negate the final answer in one more step), the negation of a problem in
is not necessarily in . Although I can quickly convince you that a graph
is Hamiltonian, I will have a much harder job convincing you that a graph is not
Hamiltonian!
Exercise 3.2.1 Show that the following decision problem in in . You may
argue informally; you are not required to construct a Turing machine to solve the
problem.
Composite number
Instance: A positive integer n in base 2 notation.
Problem: Is n composite?
Solution To show that the problem is in , we have to show that there is a
certificate for any positive instance of the problem, such that given the certificate,
the correctness of the positive answer can be verified in a polynomial number of
steps.
Given that the number n is composite, we take the certificate to be a number m
such that 1 1 m 1 n and m divides n. The size of the input data is the number of
bits necessary to write n in base 2, which is log2 n 1 . The size of the certificate
m is smaller than this, and the division sum can be done in a polynomial number
of steps.
which is in is in : just use the empty certificate. So
Any problem
. Since contains many problems (such as the Hamiltonian circuit problem)
which are regarded as “hard” (and where no polynomial-time algorithm has ever
been found, despite a lot of effort), it is widely believed that k
! . This is the
outstanding open problem of complexity theory.
On 24 May 2000, the Clay Mathematical Institute announced seven prizes,
each worth one million U.S. dollars, for the solution of seven of the major prob-
lems in contemporary mathematics. The first problem on the list is that of deciding
whether ! . See the Web page at
[Link] problems/p vs [Link]
for more information.
46 CHAPTER 3. COMPLEXITY: AND
HC (Hamiltonian circuit)
Instance: A graph G.
Instance: A weighted complete graph, where weights are positive integers, and a
positive integer L.
Proof We are given that there is a Turing machine T1 which transforms an in-
stance X of of length n into an instance Y of with the same answer in time
at most p n , where p is a polynomial. The size of Y is at most q n , where q is
polynomial, as we remarked above.
If is in , then we are also given that there is a Turing machine T2 which
solves Y in time polynomial in its size, that is, r q n , where r is a polynomial.
Now let T be the Turing machine which simulates the operation of T1 until it
halts, and then the operation of T2 . (Take T1 and T2 to have disjoint sets of states,
and then identify the halting state of T1 with the initial state of T2 .) The resulting
machine solves X correctly in time p n r q n , which is polynomial in n.
The argument for is similar, using a non-deterministic Turing machine in
place of T2 to solve Y .
and each literal ui j is either a variable xmi j or a negated variable ¢ xmi j (which
we write for short as xmi j ). It is a theorem of Boolean logic that any formula is
equivalent to one in conjunctive normal form.
An assignment of values to the Boolean variables is said to be a satisfying
assignment for a formula F if the truth value of F with this assignment is true.
If F is in CNF, then in a satisfying assignment, each clause Ci must get the value
true; so at least one literal ui j in each clause must get the value true (which means
that xmi j takes the value true if ui j xmi j , or the value false if ui j xmi j ).
3.4. COOK’S THEOREM; -COMPLETENESS 49
x1 x2 x3 £ x2 x4
Exercise 3.4.1 For each of the following Boolean formulae, (i) is it in conjunctive
normal form, (b) is it satisfiable? [Recall that x means the negation of x.]
(a) F x1 x3 x26 x1 .
(b) G k x1 x2 x3 ¥ x1 x2 x3 ¡ x1 x2 x3 ¡ x1 x2 x4 ¡ x3 x4 ¦
x3 x4 £ x3 x4 .
Solution
(a) This formula is in conjunctive normal form. (Note that each clause contains
just one literal; this is permitted.) It is not satisfiable since no assignment of
truth values can give both x1 and x1 the value ced¤fhb .
(b) This formula is in conjunctive normal form; and it is not satisfiable. For
the only way of satisfying the last three clauses is to put x3 [^]`_ a:b and
x4 7ced¤fhb ; then to satisfy the first four clauses requires that each of the four
combinations x1 x2 , x1 x2 , x1 x2 and x1 x2 must be satisfied,
which is clearly not possible.
Theorem 3.4.2 (Cook’s Theorem) The problem SAT is -complete.
50 CHAPTER 3. COMPLEXITY: AND
z i j ak will indicate that at time i, square j has symbol ak written in it, for
0 , i , 10, 10 , j , 10 and k 0 1 2 (where a0 β, a1 0, and a2 1.
The clauses of the formula reflect the correct action of the Turing machine, to-
gether with its initial and final configuration. They can be divided into six clauses,
as follows.
First group: These express the fact that each time the head is in exactly one state.
They are of two types:
x i s0 x i s1 ¡...: x i s5
x i s j x i sk
for k ! j, is true if the head is not both in state s j and state sk .We require
these for 0 , i , 10 and, for the second type, 0 , j k , 5.
Second group: These express the fact that, at each time, the head is scanning
exactly one square. They are constructed like the first group but using the y
variables.
Third group: These express the fact that each square contains only one symbol
at any given time. Again similar, using the z variables.
Fourth group: These describe the initial configuration. Each clause consists of
only a single literal. We include x 0 s0 , y 0 0 , z 0 i 1 for i 1 2 3 4,
and z 0 i β for the other values of i.
Fifth group: These describe the operation of the machine. Note that a clause
x y z
x y +I z
which holds unless x and y are true and z false. Now we translate each
machine instruction into several types of clauses. Thus, si a j ak sl becomes
x t si y t u z t u a j p;
52 CHAPTER 3. COMPLEXITY: AND
there are three such clauses, one having p z t 1 u ak (this will say
that the machine writes ak ), one with p x t 1 sl (this will say that
the state changes to sl ), and one with p y t 1 u (to say that the head
does not move). For the instruction si a j Lsl , we have the above clauses with
p y t 1 u 1 in place of the last value, and the first one involving ak
deleted; for si a j Rsl , use p y t 1 u 1 instead. There are two or three
clauses for each instruction, each value of t with 0 , t , 9, and each value
of u with 9 , u , 10. Unfortunately we are not finished yet: we need to
say that squares not being scanned don’t change their content. This can be
done by clauses of the form
y t u z t u ak zt 1 u ak
Sixth group: These assert that the head terminates scanning a square bearing the
symbol 1 and in the halt state s5 . We can take a clause
y 10 j z 10 j 1
It is interesting to stop and think about what has been done here. We have
shown that, given the description of a Turing machine and its input (all written
on a tape), there is another Turing machine which takes this tape as input and
produces as output a logical formula which is satisfiable if and only if the first
Turing machine accepts its input!
3.5 Examples
In order to show that a problem is -complete, we have to show two things:
is in ; and
there is a polynomial transformation from a known -complete problem
to .
Usually the first step is easy. To begin with, the only example of an -complete
problem which we can use in the second step is SAT, by Cook’s Theorem. But,
as we progress, we increase our stock of -complete problems, and this step
becomes easier. In this section, we give a few examples of such proofs.
3.5. EXAMPLES 53
3-SAT The problem 3-SAT is a special case of SAT, where we consider only
formulae in which every clause contains exactly three literals. Clearly a special
case is no more difficult than the general case; we show that it is no easier either!
u1 u2 z 1 u3 z 1 z 2 u4 z 2 z 3
uk 2 z k 4 z k 3 uk 1 uk zk 3
Exercise 3.5.1 Check that all these clauses are satisfied by an assignment if and
only if the original clause is satisfied by the assignment of values to the us.
Solution In the general case, the parts of clauses involving the new variables
can be written as
z1 z1 I z2 : zk 4 I zk 3 zk 3
54 CHAPTER 3. COMPLEXITY: AND
In this form it is clear that no assignment can make all these subformulae true.
But, by setting z1 zi to be true and zi 1 zk 3 to be false, we satisfy all of
them except zi I zi 1 . Similarly, we can satisfy all but the first (by putting all
variables false), or all but the last (by putting them all true).
Now suppose we have an assignment of truth values satisfying the k-literal
clause. Then some ui is satisfied. We can now assign values to the zs to satisfy
all the fragments except the one in the 3-literal clause containing ui . So all the
3-literal clauses are satisfied.
Conversely, suppose that all the 3-literal clauses are satisfied. Then as we
noted, not all the fragments involving the zs can be true, so at least one ui must be
true, and so the original k-literal clause is satisfied.
Why do we take 3-SAT here? It can be shown that the problem 2-SAT (sat-
isfiability of Boolean formulae in CNF with two literals in each clause) is in ,
that is, it can be solved efficiently. You may wish to try to prove this – it is not
completely straightforward, and not immediately relevant, so we will not give the
proof.
Exercise 3.5.2 What is the smallest size of a vertex-cover of the Petersen graph
(Figure 1.3)?
Solution A vertex cover for a pentagon must contain at least three vertices. So
we require at least three vertices from both the outer pentagon and the inner pen-
tagram; so no smaller vertex cover than 6 is possible. But there is a vertex cover
of size 6, given by the circled vertices in Figure 3.2.
VC (vertex cover)
Instance: A graph G, and a positive integer k.
Problem: Does G have a vertex cover of size at most k?
This problem is in , since the list of vertices in a vertex cover is a certificate
for a positive instance.
$ ¨# #
$ #
$ #
$ #
$ ¨ " % #
$
%$* % * "
%
% )
#
) "
¨ ¨ "
% ** ## " % $$ ) )
"
% # " $%
% " #$ # $ % "
% & " "
% & $ #
( "
%& ( "
¨ (¨ "
Truth-setting vertices
ª x% % 1 x@ 1 x% 2 ª x2 x3 ª x3
#%# x4 $ ª x4
% @ % # $ $ $ $
% @ % # $
% @ $% $ # $
$
ª $© @ % #
% ~ # $© ©
% x 3~ © @ % # ~~ x4
% ~ © % @ ~# ©
% ~ © % @ ~ #
~ ©ª @ª ~ # © ©
# ª
x1 x2 x1 x2
Satisfaction-testing vertices
Needless to say, it is believed that these two classes are unequal but nobody
¡·¬«H0-¯-°
can prove it! We will prove that and treat more informally.
¸kH«¬0-¯-°
The reason that is simply that, if a Turing machine runs for a
polynomial number of steps, it obviously cannot move further than a polynomial
57
58 CHAPTER 4. OTHER COMPLEXITY CLASSES
distance from its starting square. In general, the space complexity of a problem
cannot be larger than the time complexity.
For , we argue as follows. Suppose that there is a nondeterministic Turing
machine which solves an instance of the problem in polynomial time. Remember
that there is at least one computational path which leads to the successful result.
We can simulate this Turing machine by a deterministic machine which tries all
the possible computational paths; we do not need any more memory space, except
for enough to save a copy of the input data. (There is a difficulty, since there might
be a computational path which doesn’t terminate, or which takes a very long time,
which the machine tries before reaching the successful path. So we have to equip
the machine with a “clock” which tells it to abandon a particular attempt if it has
not succeeded in a fixed polynomial number of steps.)
° ±²H³2´¶µ¥°
A problem is in the class if an instance of the problem of size n
k
can be solved by some Turing machine in time O 2n for some k. That is,
° ±¹³º´µ¥°
really means “time which is the exponential of a polynomial”.
H«¬0-¯-°»·°¦±¹H³2´µ¥°
Theorem 4.1.2 .
H«¬0-¯°
To prove this, suppose that is a problem in the class , so that an
instance of size n can be solved by a Turing machine whose head moves no more
than p n steps from its original position. Thus the only tape squares which are
used are those in the range from p n to p n . Suppose that the number of tape
symbols is k and the number of machine states is r.
We claim that the total number of possible configurations of machine and tape
is at most
2p n 1 rk2p V n W¼ 1
For there are at most 2p n ½ 1 positions for the head, and at most r states; then
each tape square has one of k symbols written in it, so the total number of strings
that could be written on the tape is at most k2p V n W¼ 1 .
Now we claim that the time taken by the computation is not greater than this
number. For otherwise, some configuration of position and state of the head and
string written on the tape must occur twice. But then, the second time, the compu-
tation will proceed exactly as it did on the first occasion, so the machine is stuck in
a loop and will never terminate. This contradicts the assumption that the machine
really does solve the problem!
Now 2p n 1 rk2p V nW¼ 1 is certainly bounded by the exponential of a poly-
nomial: we have 2p n 1 1 22p V n W¼ 1 , and so
2p n 1 rk2p V nW¼ 1
1 2V 1 log2 k WgV 2p V n W¼ 1 W' log2 r
° ±¹³º´µ¥°
So the class belongs to , as claimed.
4.2. COUNTING PROBLEMS 59
SAT (Satisfiability)
Instance: A Boolean formula, in conjunctive normal form
Decision problem: Is there an assignment of truth values to the variables which
satisfies the formula?
Counting problem: How many such satisfying assignments are there?
HC (Hamiltonian circuit)
Instance: A graph G.
Decision problem: Does G have a Hamiltonian circuit?
Counting problem: How many Hamiltonian circuits does G have?
In each case, the counting problem is harder than the decision problem. If we
could solve the counting problem, we could immediately solve the decision prob-
lem by just testing whether the answer is zero or non-zero.
Sometimes, an easy decision problem is associated with an easy counting
problem. We have seen that a graph G has a spanning tree if and only if G is
connected (and this can be decided quickly). The number of spanning trees can
be computed by evaluating a determinant (which can also be done quickly), ac-
cording to Kirchhoff’s Matrix-Tree Theorem, which we state without proof:
Theorem 4.2.1 Let A be the adjacency matrix of a simple graph G. Let D be the
diagonal matrix whose i i entry is the valency of the ith vertex (the number of
edges on which it lies. Let X be the matrix obtained from D A by deleting the
first row and column. Then det X is equal to the number of spanning trees of G.
For example, let G K3 , the complete graph on 3 vertices. Then
A D
2 1 1
D A C 1 2 1G
1 1 2
and
2 1
det X det ¾ 3
1 2 ¿
so G has three spanning trees.
60 CHAPTER 4. OTHER COMPLEXITY CLASSES
for any positive instance of , there is at least one computation path which
accepts the instance;
for any positive instance of , at least half of all the computation paths
accept the instance;
There is nothing special about the probability 1 < 2 in the definition: any posi-
tive constant would do, for the following reason:
Let us suppose that we have a problem with input data of size n, where we
are required to compute some numerical function (such as a counting problem or
the Travelling Salesman Problem). Let K be the true answer to the problem. We
say that there is a polynomial-time approximation algorithm for the problem if,
given any positive number ε, there is a Turing machine which computes a number
k satisfying
K <J 1 ε 1 k 1 K 1 ε;
the number of steps is bounded by a polynomial in n and log 1 < ε .
We say that the algorithm estimates K to within ε if the first condition holds.
(This agrees with the usage “to within 1%”, for example.)
We have to explain why we use log 1 < ε here. Suppose that we have computed
the answer, and we are required to improve our accuracy by one decimal place.
That means, we have to reduce the possible error to one-tenth of its previous
value, so log 1 < ε increases by a constant amount. If, for example, the time was a
linear function of log 1 < ε , then this would increase the time taken by a constant
amount. If the time was proportional to 1 < ε instead, then to get one extra decimal
place would take ten times as long! In brief, we require that the time taken grows
as a polynomial in the size of the input data and in the number of significant figures
required in the answer.
We can combine the last two ideas and define a randomised approximation
algorithm. Here, we use a randomised algorithm (one which makes choices be-
tween computation paths based on random bits). We prescribe both the accuracy
of the computed answer (the number ε above) and the probability that the algo-
rithm fails to meet the requirements (another positive number δ). If the running
time is bounded by a polynomial in n (the size of the input data), log 1 < ε , and
log 1 < δ , we call the algorithm fully polynomial.
If there is an randomised algorithm which estimates K to within ε with prob-
ability at least 3 < 4, say, then we can estimate K to within ε with arbitrarily high
probability 1 δ, by the following simple trick: repeat the algorithm N times,
where N 1 12 log 1 < δ : , and take the median of the resulting N estimates.
This depends on the following result about probability theory, of which we omit
the proof (which just involves estimates for binomial coefficients):
Proposition 4.5.1 Let X1 XN be independent random variables having the
same distribution, where N is odd. Let a and b be real numbers. Suppose that
3
P a , Xi , b -
4
for all i. Then, if X denotes the median of X1 XN , we have
P a , X , b 1 e N P 12
64 CHAPTER 4. OTHER COMPLEXITY CLASSES
Now, in our case, to make this probability greater than 1 δ, we just require
e N P 12 1 δ, or N 12 log 1 < δ . The added 1 is to make N odd, so that the median
is defined.
So we can give a simpler definition: a randomised approximation algorithm
for a number K is fully polynomial if, for any positive number ε, it approximates
K to within ε with probability at least 3 < 4 in time polynomial in n and log 1 < ε .
For example, consider the counting problem associated with SAT: we are
given a Boolean formula F in conjunctive normal form, and we are asked to count
the number of satisfying assignments. If F is a formula in n variables, then there
are altogether 2n assignments of truth values, so it will take exponentially long to
try them all and count the successes.
We could proceed by sampling. In other words, choose a large enough integer
N, and then choose N assignments of values at random; count the proportion M
of these assignments which make F true. Provided that M is not too small, we
would guess that about M < N of all assignments are satisfying assignments, and
estimate the total number of satisfying assignments as M < N 2n . It can be shown
that, if the actual number of satisfying assignments is µ . 2n, and if we choose N
large enough (precisely, N 4 log 2 < δ < µε2), then we will succeed in estimating
the number to within ε with probability at least 1 δ.
But if the number of satisfying assignments is rather small, we are likely to
find none in our sample. (Remember that there are 2n assignments and we can
only look at a small proportion of them.) Then we will not be able to give an
accurate estimate.
There are more advanced sampling methods to get around this problem, but
we don’t consider them here.
been constructed.
Further information on quantum computation can be obtained from the Web
page
[Link]