Algorithmic Graph Theory
Algorithmic Graph Theory
Contents
Acknowledgments
iv
.
.
.
.
.
.
.
.
1
2
11
19
28
33
42
44
51
.
.
.
.
.
.
.
.
58
59
65
76
79
82
83
90
92
.
.
.
.
.
.
.
110
110
117
121
135
144
149
154
.
.
.
.
.
.
162
163
164
174
182
189
199
.
.
.
.
.
.
.
.
2 Graph algorithms
2.1 Representing graphs in a computer
2.2 Graph searching . . . . . . . . . . .
2.3 Weights and distances . . . . . . .
2.4 Dijkstras algorithm . . . . . . . . .
2.5 Bellman-Ford algorithm . . . . . .
2.6 Floyd-Roy-Warshall algorithm . . .
2.7 Johnsons algorithm . . . . . . . . .
2.8 Problems . . . . . . . . . . . . . . .
3 Trees and forests
3.1 Definitions and examples
3.2 Properties of trees . . . .
3.3 Minimum spanning trees
3.4 Binary trees . . . . . . .
3.5 Huffman codes . . . . .
3.6 Tree traversals . . . . . .
3.7 Problems . . . . . . . . .
4 Tree data structures
4.1 Priority queues . .
4.2 Binary heaps . . .
4.3 Binomial heaps . .
4.4 Binary search trees
4.5 AVL trees . . . . .
4.6 Problems . . . . . .
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
i
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
ii
Contents
.
.
.
.
.
.
.
.
.
206
206
211
216
219
220
222
222
227
229
.
.
.
.
233
233
233
234
234
7 Planar graphs
7.1 Planarity and Eulers Formula . . . . . . . . . . . . . . . . . . . . . . . .
7.2 Kuratowskis Theorem . . . . . . . . . . . . . . . . . . . . . . . . . . . .
7.3 Planarity algorithms . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
235
235
235
237
8 Graph coloring
8.1 Vertex coloring . . . . . . . .
8.2 Edge coloring . . . . . . . . .
8.3 The chromatic polynomial . .
8.4 Applications of graph coloring
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
238
238
241
245
247
9 Network flows
9.1 Flows and cuts . . . . . . . . . .
9.2 Chip firing games . . . . . . . . .
9.3 Ford-Fulkerson theorem . . . . .
9.4 Edmonds and Karps algorithm .
9.5 Goldberg and Tarjans algorithm
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
249
249
254
261
266
266
.
.
.
.
.
.
267
267
271
278
280
285
290
.
.
.
.
.
296
296
297
299
300
302
10 Random graphs
10.1 Network statistics . . . . . . .
10.2 Binomial random graph model
10.3 Erdos-Renyi model . . . . . .
10.4 Small-world networks . . . . .
10.5 Scale-free networks . . . . . .
10.6 Problems . . . . . . . . . . . .
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
Contents
iii
308
309
309
311
311
312
313
314
314
314
315
315
315
316
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
Bibliography
317
Index
326
Acknowledgments
Fidel Barrera-Cruz: reported typos in Chapter 3. See changeset 101. Suggested
making a note about disregarding the direction of edges in undirected graphs. See
changeset 277.
Daniel Black: reported a typo in Chapter 1. See changeset 61.
Kevin Brintnall: reported typos in the definition of iadj(v) oadj(v); see changesets 240 and 242. Solution to Example 1.14(2); see changeset 246.
John Costella: helped to clarify the idea that the adjacency matrix of a bipartite graph can be permuted to obtain a block diagonal matrix. See page 22 and
revisions 1865 and 1869.
Aaron Dutle: reported a typo in Figure 1.18. See changeset 125.
Peter L. Erdos (http://www.renyi.hu/elp) for informing us of the reference [78] on
the Havel-Hakimi theorem for directed graphs.
Noel Markham: reported a typo in Algorithm 2.5. See changeset 131 and Issue 2.
Caroline Melles: clarify definitions of various graph types (weighted graphs, multigraphs, and weighted multigraphs); clarify definitions of degree, isolated vertices,
and pendant and using the butterfly graph with 5 vertices (see Figure 1.10) to
illustrate these definitions; clarify definitions of trails, closed paths, and cycles;
see changeset 448. Some rearrangements of materials in Chapter 1 to make the
reading flow better and a few additions of missing definitions; see changeset 584.
Clarifications about unweighted and weighted degree of a vertex in a multigraph;
notational convention about a graph being simple unless otherwise stated; an example on graph minor; see changeset 617. Reported a missing edge in Figure 1.5(b);
see changeset 1945.
Pravin Paratey: simplify the sentence formation in the definition of digraphs; see
changeset 714 and Issue 7.
Henrique Renn
o: pointed out the ambiguity in the definition of weighted multigraphs; see changeset 1936. Reported typos; see changeset 1938.
The world map in Figure 2.16 was adapted from an SVG image file from Wikipedia.
The original SVG file was accessed on 2010-10-01 at http://en.wikipedia.org/wiki/
File:WorldmapwlocationwNEDw50m.svg.
iv
Chapter 1
Introduction to graph theory
Our journey into graph theory starts with a puzzle that was solved over 250 years ago by
Leonhard Euler (17071783). The Pregel River flowed through the town of Konigsberg,
which is present day Kaliningrad in Russia. Two islands protruded from the river.
On either side of the mainland, two bridges joined one side of the mainland with one
island and a third bridge joined the same side of the mainland with the other island. A
bridge connected the two islands. In total, seven bridges connected the two islands with
both sides of the mainland. A popular exercise among the citizens of Konigsberg was
determining if it was possible to cross each bridge exactly once during a single walk. For
historical perspectives on this puzzle and Eulers solution, see Gribkovskaia et al. [94]
and Hopkins and Wilson [108].
To visualize this puzzle in a slightly different way, consider Figure 1.1. Imagine that
points a and c are either sides of the mainland, with points b and d being the two islands.
Place the tip of your pencil on any of the points a, b, c, d. Can you trace all the lines
in the figure exactly once, without lifting your pencil? Known as the seven bridges of
Konigsberg puzzle, Euler solved this problem in 1735 and with his solution he laid the
foundation of what is now known as graph theory.
1.1
f (x)
0.5
0.5
0.5
1
6
0
x
0
x
(b) A scatterplot.
Everyone knows what a curve is, until he has studied enough mathematics to become confused
through the countless number of possible exceptions.
We start by calling a graph what some call an unweighted, undirected graph without
multiple edges.
Definition 1.1. A graph G = (V, E) is an ordered pair of finite sets. Elements of V are
called vertices or nodes, and elements of E V (2) are called edges or arcs. We refer to
V as the vertex set of G, with E being the edge set. The cardinality of V is called the
order of G, and |E| is called the size of G. We usually disregard any direction of the
edges and consider (u, v) and (v, u) as one and the same edge in G. In that case, G is
referred to as an undirected graph.
One can label a graph by attaching labels to its vertices. If (v1 , v2 ) E is an edge of
a graph G = (V, E), we say that v1 and v2 are adjacent vertices. For ease of notation, we
write the edge (v1 , v2 ) as v1 v2 . The edge v1 v2 is also said to be incident with the vertices
v1 and v2 .
a
(1.1)
sage : G = Graph ({ " a " :[ " b " ," e " ] , " b " :[ " a " ," c " ," e " ] , " c " :[ " b " ," d " ] ,
...
" d " :[ " c " ," e " ] , " e " :[ " a " ," b " ," d " ]})
sage : G
Graph on 5 vertices
sage : G . vertices ()
[ a , b , c , d , e ]
sage : G . edges ( labels = False )
[( a , b ) , ( a , e ) , ( b , e ) , ( c , b ) , ( c , d ) , ( e , d )]
The graph G is undirected, meaning that we do not impose direction on any edges.
Without any direction on the edges, the edge ab is the same as the edge ba. That is why
G.edges() returns six edges instead of the 12 edges listed in (1.1).
(2) Let adj(v) be the set of all vertices that are adjacent to v. Then we have
adj(a) = {b, e}
adj(b) = {a, c, e}
adj(c) = {b, d}
adj(d) = {c, e}
adj(e) = {a, b, d}.
The vertices adjacent to v are also referred to as its neighbors. We can use the function
G.neighbors() to list all the neighbors of each vertex.
sage :
[ b ,
sage :
[ a ,
sage :
[ b ,
sage :
[ c ,
sage :
[ a ,
(3) Taking the cardinalities of the above five sets, we get |adj(a)| = |adj(c)| =
|adj(d)| = 2 and |adj(b)| = |adj(e)| = 3. Thus a, c and d have the smallest number
of adjacent vertices, while b and e have the largest number of adjacent vertices.
(4) If all the edges in G are removed, the result is still a graph, although one without
any edges. By definition, the edge set of any graph is a subset of V (2) . Removing all
edges of G leaves us with the empty set , which is a subset of every set.
(5) Say we remove all of the vertices from the graph in Figure 1.3 and in the process
all edges are removed as well. The result is that both of the vertex and edge sets are
empty. This is a special graph known as the empty or null graph.
Example 1.3. Consider the illustration in Figure 1.4. Does Figure 1.4 represent a
graph? Why or why not?
Solution. If V = {a, b, c} and E = {aa, bc}, it is clear that E V (2) . Then (V, E) is a
graph. The edge aa is called a self-loop of the graph. In general, any edge of the form
vv is a self-loop.
In Figure 1.3, the edges ae and ea represent one and the same edge. If we do not
consider the direction of the edges in the graph of Figure 1.3, then the graph has six
edges. However, if the direction of each edge is taken into account, then there are 12 edges
as listed in (1.1). The following definition captures the situation where the direction of
the edges are taken into account.
A directed edge is an edge such that one vertex incident with it is designated as
the head vertex and the other incident vertex is designated as the tail vertex. In this
5
a
1.1.1
Multigraphs
This subsection presents a larger class of graphs. For simplicity of presentation, in this
book we shall assume usually that a graph is not a multigraph. In other words, when you
read a property of graphs later in the book, it will be assumed (unless stated explicitly
otherwise) that the graph is not a multigraph. However, as multigraphs and weighted
graphs are very important in many applications, we will try to keep them in the back
of our mind. When appropriate, we will add as a remark how an interesting property of
ordinary graphs extends to the multigraph or weighted graph case.
An important class of graphs consist of those graphs having multiple edges between
pairs of vertices. A multigraph is a graph in which there are multiple edges between a
pair of vertices. A multi-undirected graph is a multigraph that is undirected. Similarly,
a multidigraph is a directed multigraph.
Example 1.4. Sage can compute with and plot multigraphs, or multidigraphs, having
loops.
sage :
sage :
sage :
sage :
sage :
e4
e3
e4
e2
e5
e3
3
e2
e1
e0
e0
e1
(a)
(b)
i : E V (2) .
(1.2)
(1.3)
where h(e) i(e) for all e E. The element v = h(e) is called the head of i(e). If G has
no self-loops, then i(e) is a set having exactly two elements denoted i(e) = {h(e), t(e)}.
The element v = t(e) is called the tail of i(e). For self-loops, we set t(e) = h(e). A
multigraph with an orientation can therefore be described as the 4-tuple (V, E, i, h).
In other words, G = (V, E, i, h) is a multidigraph. Figure 1.6 illustrates a weighted
multigraph.
2
However, we always assume that E R V (2) , where the R-component is called the weight of the
edge.
7
1
v3
v4
2
3
1
1
v5
3
3
v2
v1
1
1.
eE
h(e)=v
deg (v) =
eE
vi(e)={v,v }
h(e)=v
The unweighted degree deg(v) of a vertex v of a weighted multigraph is the sum of the
unweighted indegree and the unweighted outdegree of v:
deg(v) = deg+ (v) + deg (v).
(1.4)
(1.5)
wv .
eE
h(e)=v
The weighted outdegree of a vertex v V counts the weights of edges going out of v:
wdeg (v) =
wv .
eE
vi(e)={v,v }
h(e)=v
The weighted degree of a vertex of a weighted multigraph is the sum of the weighted
indegree and the weighted outdegree of that vertex,
wdeg(v) = wdeg + (v) + wdeg (v).
In other words, it is the sum of the weights of the edges incident to that vertex, regarding
the graph as an undirected weighted graph. Unweighted degrees are a special case of
weighted degrees. For unweighted degrees, we merely set each edge weight to unity.
Definition 1.7. Let G = (V, E, h) be an unweighted multidigraph. The line graph of G,
denoted L(G), is the multidigraph whose vertices are the edges of G and whose edges are
(e, e ) where h(e) = t(e ) (for e, e E). A similar definition holds if G is undirected.
For example, the line graph of the cyclic graph is itself.
1.1.2
Simple graphs
Our life is frittered away by detail. . . . Simplify, simplify. Instead of three meals a day, if
it be necessary eat but one; instead of a hundred dishes, five; and reduce other things in
proportion.
Henry David Thoreau, Walden, 1854, Chapter 2: Where I Lived, and What I Lived For
A simple graph is a graph with no self-loops and no multiple edges. Figure 1.7 illustrates
a simple graph and its digraph version, together with a multidigraph version of the
Konigsberg graph. The edges of a digraph can be visually represented as directed arrows,
similar to the digraph in Figure 1.7(b) and the multidigraph in Figure 1.7(c). The digraph
in Figure 1.7(b) has the vertex set {a, b, c} and the edge set {ab, bc, ca}. There is an arrow
from vertex a to vertex b, hence ab is in the edge set. However, there is no arrow from
b to a, so ba is not in the edge set of the graph in Figure 1.7(b). The family Sh(n) of
Shannon multigraphs is illustrated in Figure 1.8 for integers 2 n 7. These graphs
are named after Claude E. Shannon (19162001) and are sometimes used when studying
edge colorings. Each Shannon multigraph consists of three vertices, giving rise to a total
of three distinct unordered pairs. Two of these pairs are connected by n/2 edges and
the third pair of vertices is connected by (n + 1)/2 edges.
Notational convention
remainder of this book.
Definition 1.8. For any vertex v in a graph G = (V, E), the cardinality of adj(v) (as
in 1.5) is called the degree of v and written as deg(v) = | adj(v)|. The degree of v counts
the number of vertices in G that are adjacent to v. If deg(v) = 0, then v is not incident
to any edge and we say that v is an isolated vertex. If G has no loops and deg(v) = 1,
then v is called a pendant.
(b) Digraph.
(c) Multidigraph.
(a) Sh(2)
(b) Sh(3)
(c) Sh(4)
(d) Sh(5)
(e) Sh(6)
(f) Sh(7)
10
Some examples would put the above definition in concrete terms. Consider again
the graph in Figure 1.4. Note that no vertices are isolated. Even though vertex a is
not incident to any vertex other than a itself, note that deg(a) = 2 and so by definition
a is not isolated. Furthermore, each of b and c is a pendant. For the house graph in
Figure 1.3, we have deg(b) = 3. For the graph in Figure 1.7(b), we have deg(b) = 2.
If V = and E = , then G is a graph consisting entirely of isolated vertices. From
Example 1.2 we know that the vertices a, c, d in Figure 1.3 have the smallest degree in
the graph of that figure, while b, e have the largest degree.
The minimum degree among all vertices in G is denoted (G), whereas the maximum
degree is written as (G). Thus, if G denotes the graph in Figure 1.3 then we have
(G) = 2 and (G) = 3. In the following Sage session, we construct the digraph in
Figure 1.7(b) and compute its maximum and minimum number of degrees.
sage : G = DiGraph ({ " a " : " b " , " b " : " c " , " c " : " a " })
sage : G
Digraph on 3 vertices
sage : G . degree ( " a " )
2
sage : G . degree ( " b " )
2
sage : G . degree ( " c " )
2
vV
deg(v) = 2|E|.
11
deg(v) +
vVe
deg(v) = 2|E|
vVo
As
vV
deg(v) and
vVe
vV
deg(v)
deg(v).
vVe
As E V (2) , then E can be the empty set, in which case the total degree of G =
(V, E) is zero. Where E = , then the total degree of G is greater than zero. By
Theorem 1.9, the total degree of G is nonnegative and even. This result is an immediate
consequence of Theorem 1.9 and is captured in the following corollary.
Corollary 1.11. If G is a graph, then the sum of its vertex degrees is nonnegative and
even.
If G = (V, E) is an r-regular graph with n vertices and m edges, it is clear by definition
of r-regular graphs that the total degree of G is rn. By Theorem 1.9 we have 2m = rn
and therefore m = rn/2. This result is captured in the following corollary.
Corollary 1.12. If G = (V, E) is an r-regular graph having n vertices and m edges,
then m = rn/2.
1.2
We now consider several common types of graphs. Along the way, we also present basic
properties of graphs that could be used to distinguish different types of graphs.
Let G be a multigraph as in Definition 1.6, with vertex set V (G) and edge set E(G).
Consider a graph H such that V (H) V (G) and E(H) E(G). Furthermore, if
e E(H) and i(e) = {u, v}, then u, v V (H). Under these conditions, H is called a
subgraph of G.
1.2.1
I like long walks, especially when they are taken by people who annoy me.
Noel Coward
If u and v are two vertices in a graph G, a u-v walk is an alternating sequence of vertices
and edges starting with u and ending at v. Consecutive vertices and edges are incident.
Formally, a walk W of length n 0 can be defined as
W : v0 , e1 , v1 , e2 , v2 , . . . , vn1 , en , vn
where each edge ei = vi1 vi and the length n refers to the number of (not necessarily
distinct) edges in the walk. The vertex v0 is the starting vertex of the walk and vn is
the end vertex, so we refer to W as a v0 -vn walk. The trivial walk is the walk of length
n = 0 in which the start and end vertices are one and the same vertex. If the graph has
12
no multiple edges then, for brevity, we omit the edges in a walk and usually write the
walk as the following sequence of vertices:
W : v0 , v1 , v2 , . . . , vn1 , vn .
For the graph in Figure 1.9, an example of a walk is an a-e walk: a, b, c, b, e. In other
words, we start at vertex a and travel to vertex b. From b, we go to c and then back to
b again. Then we end our journey at e. Notice that consecutive vertices in a walk are
adjacent to each other. One can think of vertices as destinations and edges as footpaths,
say. We are allowed to have repeated vertices and edges in a walk. The number of edges
in a walk is called its length. For instance, the walk a, b, c, b, e has length 4.
a
c
g
13
A walk of length n 3 whose start and end vertices are the same is called a closed
walk . A trail of length n 3 whose start and end vertices are the same is called a closed
trail . A path of length n 3 whose start and end vertices are the same is called a closed
path or a cycle (with apologies for slightly abusing terminology).3 For example, the
walk a, b, c, e, a in Figure 1.9 is a closed path. A path whose length is odd is called odd ,
otherwise it is referred to as even. Thus the walk a, b, e, a in Figure 1.9 is a cycle. It is
easy to see that if you remove any edge from a cycle, then the resulting walk contains no
closed walks. An Euler subgraph of a graph G is either a cycle or an edge-disjoint union
of cycles in G. An example of a closed walk which is not a cycle is given in Figure 1.10.
0
A cycle in a graph is sometimes also called a circuit. Since that terminology unfortunately
conflicts with the closely related notion of a circuit of a matroid, we do not use it here.
14
0 i, j n be two distinct integers with i < j such that vi = vj . Deleting the vertices
vi , vi+1 , . . . , vj1 from W results in a u-v walk W1 whose length is less than n. If W1 is
a path, then we are done. Otherwise we repeat the above process to obtain a u-v walk
shorter than W1 . As W is a finite sequence, we only need to apply the above process a
finite number of times to arrive at a u-v path.
A graph is said to be connected if for every pair of distinct vertices u, v there is a
u-v path joining them. A graph that is not connected is referred to as disconnected .
The empty graph is disconnected and so is any nonempty graph with an isolated vertex.
However, the graph in Figure 1.7 is connected. A geodesic path or shortest path between
two distinct vertices u, v of a graph is a u-v path of minimum length. A nonempty graph
may have several shortest paths between some distinct pair of vertices. For the graph
in Figure 1.9, both a, b, c and a, e, c are geodesic paths between a and c. Let H be a
connected subgraph of a graph G such that H is not a proper subgraph of any connected
subgraph of G. Then H is said to be a component of G. We also say that H is a maximal
connected subgraph of G. Any connected graph is its own component. The number of
connected components of a graph G will be denoted (G).
The following is an immediate consequence of Corollary 1.10.
Proposition 1.16. Suppose that exactly two vertices of a graph have odd degree. Then
those two vertices are connected by a path.
Proof. Let G be a graph all of whose vertices are of even degree, except for u and v. Let
C be a component of G containing u. By Corollary 1.10, C also contains v, the only
remaining vertex of odd degree. As u and v belong to the same component, they are
connected by a path.
Example 1.17. Determine whether or not the graph in Figure 1.9 is connected. Find a
shortest path from g to d.
Solution. In the following Sage session, we first construct the graph in Figure 1.9 and
use the method is_connected() to determine whether or not the graph is connected.
Finally, we use the method shortest_path() to find a geodesic path between g and d.
sage :
...
...
sage :
True
sage :
[ g ,
g = Graph ({ " a " :[ " b " ," e " ] , " b " :[ " a " ," g " ," e " ," c " ] , \
" c " :[ " b " ," e " ," d " ] , " d " :[ " c " ," f " ] , " e " :[ " f " ," a " ," b " ," c " ] , \
" f " :[ " g " ," d " ," e " ] , " g " :[ " b " ," f " ]})
g . is_connected ()
g . shortest_path ( " g " , " d " )
f , d ]
This shows that g, f, d is a shortest path from g to d. In fact, any other g-d path has
length greater than 2, so we can say that g, f, d is the shortest path between g and d.
Remark 1.18. We will explain Dijkstras algorithm in Chapter 2. Dijkstras algorithm
gives one of the best algorithms for finding shortest paths between two vertices in a
connected graph. What is very remarkable is that, at the present state of knowledge,
finding the shortest path from a vertex v to a particular (but arbitrarily given) vertex w
appears to be as hard as finding the shortest path from a vertex v to all other vertices
in the graph!
Trees are a special type of graphs that are used in modelling structures that have
some form of hierarchy. For example, the hierarchy within an organization can be drawn
as a tree structure, similar to the family tree in Figure 1.11. Formally, a tree is an
15
undirected graph that is connected and has no cycles. If one vertex of a tree is specially
designated as the root vertex , then the tree is called a rooted tree. Chapter 3 covers trees
in more details.
grandma
mum
me
sister
brother
uncle
aunt
cousin1
cousin2
1.2.2
Let G be a graph with vertex set V (G) and edge set E(G). Suppose we have a graph
H such that V (H) V (G) and E(H) E(G). Furthermore, suppose the incidence
function i of G, when restricted to E(H), has image in V (H)(2) . Then H is a subgraph
of G. In this situation, G is referred to as a supergraph of H.
Starting from G, one can obtain its subgraph H by deleting edges and/or vertices
from G. Note that when a vertex v is removed from G, then all edges incident with
v are also removed. If V (H) = V (G), then H is called a spanning subgraph of G. In
Figure 1.12, let G be the left-hand side graph and let H be the right-hand side graph.
Then it is clear that H is a spanning subgraph of G. To obtain a spanning subgraph
from a given graph, we delete edges from the given graph.
(a)
(b)
n
2
n(n 1)
.
2
(1.6)
Thus for any simple graph G with n vertices, its total number of edges |E(G)| is bounded
above by
n(n 1)
|E(G)|
.
(1.7)
2
16
Figure 1.13 shows complete graphs each of whose total number of vertices is bounded by
1 n 5. The complete graph K1 has one vertex with no edges. It is also called the
trivial graph.
(a) K5
(b) K4
(c) K3
(d) K2
(e) K1
ni
(k 1) 2
ni k .
(1.8)
(This result holds true for any nonempty but finite set of positive integers.) Note that
ni = n and by (1.7) each component i has at most 21 ni (ni 1) edges. Apply (1.8) to
get
1
ni (ni 1)
1
=
n2i
ni
2
2
2
1
1
(n2 2nk + k 2 + 2n k) n
2
2
(n k)(n k + 1)
=
2
as required.
The cycle graph on n 3 vertices, denoted Cn , is the connected 2-regular graph on n
vertices. Each vertex in Cn has degree exactly 2 and Cn is connected. Figure 1.14 shows
cycles graphs Cn where 3 n 6. The path graph on n 1 vertices is denoted Pn . For
n = 1, 2 we have P1 = K1 and P2 = K2 . Where n 3, then Pn is a spanning subgraph
of Cn obtained by deleting one edge.
A bipartite graph G is a graph with at least two vertices such that V (G) can be split
into two disjoint subsets V1 and V2 , both nonempty. Every edge uv E(G) is such that
u V1 and v V2 , or v V1 and u V2 . See Kalman [123] for an application of bipartite
graphs to the problem of allocating satellites to radio stations.
Example 1.20. The Franklin graph, shown in Figure 1.15, is named after Philip Franklin.
It is a 3-regular graph with 12 vertices and 18 edges. It is bipartite, Hamiltonian and has
radius 3, diameter 3 and girth 4. It is also a 3-vertex-connected and 3-edge-connected
perfect graph.
(a) C6
(b) C5
17
(c) C4
(d) C3
18
19
even length in order to return to the side of the bipartition from which the walk started.
Thus, any cycle in G must have even length.
Sufficiency (=): Assume G = (V, E) has order n 2 and no odd cycles. If G is
connected, choose any vertex u V and define a partition of V thus:
X = {x V | d(u, x) is even},
Y = {y V | d(u, y) is odd}
where d(u, v) denotes the distance (or length of the shortest path) from u to v. If (X, Y )
is a bipartition of G, then we are done. Otherwise, (X, Y ) is not a bipartition of G.
Then one of X and Y has two vertices v, w joined by an edge e. Let P1 be a shortest
u-v path and P2 be a shortest u-w path. By definition of X and Y , both P1 and P2 have
even lengths or both have odd lengths. From u, let x be the last vertex common to both
P1 and P2 . The subpath u-x of P1 and u-x of P2 have equal length. That is, the subpath
x-v of P1 and x-w of P2 both have even or odd lengths. Construct a cycle C from the
paths x-v and x-w, and the edge e joining v and w. Since x-v and x-w both have even
or odd lengths, the cycle C has odd length, contradicting our hypothesis that G has no
odd cycles. Hence, (X, Y ) is a bipartition of G.
Finally, if G is disconnected, each of its components has no odd cycles. Repeat the
above argument for each component to conclude that G is bipartite.
Example 1.23. The Gray graph, shown in Figure 1.17, is an undirected bipartite graph
with 54 vertices and 81 edges. It is a 3-regular graph discovered by Marion C. Gray
in 1932. The Gray graph has chromatic number 2, chromatic index 3, radius 6, and
diameter 6. It is also a 3-vertex-connected and 3-edge-connected non-planar graph. The
Gray graph is an example of a graph which is edge-transitive but not vertex-transitive.
sage :
sage :
sage :
True
sage :
False
sage :
True
sage :
6
The complete bipartite graph Km,n is the bipartite graph whose vertex set is partitioned into two nonempty disjoint sets V1 and V2 with |V1 | = m and |V2 | = n. Any
vertex in V1 is adjacent to each vertex in V2 , and any two distinct vertices in Vi are not
adjacent to each other. If m = n, then Kn,n is n-regular. Where m = 1 then K1,n is
called the star graph. Figure 1.18 shows a bipartite graph together with the complete
bipartite graphs K4,3 and K3,3 , and the star graph K1,4 .
As an example of K3,3 , suppose that there are 3 boys and 3 girls dancing in a room.
The boys and girls naturally partition the set of all people in the room. Construct a
graph having 6 vertices, each vertex corresponding to a person in the room, and draw
an edge form one vertex to another if the two people dance together. If each girl dances
three times, once with with each of the three boys, then the resulting graph is K3,3 .
1.3
20
(a) bipartite
(b) K4,3
(c) K3,3
(d) K1,4
21
yourself.
From the movie The Matrix, 1999
A=
. . . . . . . . . . . . . . . . . . . .
am1 am2 amn
The positive integers m and n are the row and column dimensions of A, respectively.
The entry in row i column j is denoted aij . Where the dimensions of A are clear from
context, A is also written as A = [aij ].
Representing a graph as a matrix is very inefficient in some cases and not so in
other cases. Imagine you walk into a large room full of people and you consider the
handshaking graph discussed in connection with Theorem 1.9. If not many people
shake hands in the room, it is a waste of time recording all the handshakes and also all
the non-handshakes. This is basically what the adjacency matrix does. In this kind
of sparse graph situation, it would be much easier to simply record the handshakes as
a Python dictionary.4 This section requires some concepts and techniques from linear
algebra, especially matrix theory. See introductory texts on linear algebra and matrix
theory [20] for coverage of such concepts and techniques.
1.3.1
Adjacency matrix
Let G be an undirected graph with vertices V = {v1 , . . . , vn } and edge set E. The
adjacency matrix of G is the n n matrix A = [aij ] defined by
aij =
1, if vi vj E,
0, otherwise.
1,
if vi vj E,
aij = 1, if vj vi E,
0,
otherwise.
Example 1.24. Compute the adjacency matrices of the graphs in Figure 1.19.
Solution. Define the graphs in Figure 1.19 using DiGraph and Graph. Then call the
method adjacency_matrix().
4
A Python dictionary is basically an indexed set. See the reference manual at http://www.python.org
for further details.
22
(a)
(b)
In general, the adjacency matrix of a digraph is not symmetric, while that of an undirected graph is symmetric.
More generally, if G is an undirected multigraph with edge eij = vi vj having multiplicity wij , or a weighted graph with edge eij = vi vj having weight wij , then we can
define the (weighted) adjacency matrix A = [aij ] by
aij =
wij , if vi vj E,
0,
otherwise.
For example, Sage allows you to easily compute a weighted adjacency matrix.
sage : G = Graph ( sparse = True , weighted = True )
sage : G . add_edges ([(0 ,1 ,1) , (1 ,2 ,2) , (0 ,2 ,3) , (0 ,3 ,4)])
sage : M = G . w e i g h t e d _ a d ja c e n c y _ m a t r i x (); M
[0 1 3 4]
[1 0 2 0]
[3 2 0 0]
[4 0 0 0]
Bipartite case
Suppose G = (V, E) is an undirected bipartite graph with n = |V | vertices. Any adjacency matrix A of G is symmetric and we assume that it is indexed from zero up to
23
n 1, inclusive. Then there exists a permutation of the index set {0, 1, . . . , n 1} such
that the matrix A = [a(i)(j) ] is also an adjacency matrix for G and has the form
A =
0 B
BT 0
(1.9)
where 0 is a zero matrix. The matrix B is called a reduced adjacency matrix or a biadjacency matrix (the literature also uses the terms transfer matrix or the ambiguous
term adjacency matrix). In fact, it is known [10, p.16] that any undirected graph is
bipartite if and only if there is a permutation on {0, 1, . . . , n 1} such that A (G) =
[a(i)(j) ] can be written as in (1.9).
Tanner graphs
If H is an m n (0, 1)-matrix, then the Tanner graph of H is the bipartite graph
G = (V, E) whose set of vertices V = V1 V2 is partitioned into two sets: V1 corresponding
to the m rows of H and V2 corresponding to the n columns of H. For any i, j with
1 i m and 1 j n, there is an edge ij E if and only if the (i, j)-th entry of
H is 1. This matrix H is sometimes called the reduced adjacency matrix or the check
matrix of the Tanner graph. Tanner graphs are used in the theory of error-correcting
codes. For example, Sage allows you to easily compute such a bipartite graph from its
matrix.
sage : H = Matrix ([(1 ,1 ,1 ,0 ,0) , (0 ,0 ,1 ,0 ,1) , (1 ,0 ,0 ,1 ,1)])
sage : B = BipartiteGraph ( H )
sage : B . r ed u c e d_ a d j ac e n c y_ m a t ri x ()
[1 1 1 0 0]
[0 0 1 0 1]
[1 0 0 1 1]
sage : B . plot ( graph_border = True )
24
entry of Ak+1 counts the number of vi -vj walks of length k + 1. Let A = [aij ], Ak = [bij ],
and Ak+1 = [cij ]. Since Ak+1 = AAk , then
p
air brj
cij =
r=1
for i, j = 1, 2, . . . , p. Note that air is the number of edges from vi to vr , and brj is the
number of vr -vj walks of length k. Any edge from vi to vr can be joined with any vr -vj
walk to create a walk vi , vr , . . . , vj of length k + 1. Then for each r = 1, 2, . . . , p, the
value air brj counts the number of vi -vj walks of length k + 1 with vr being the second
vertex in the walk. Thus cij counts the total number of vi -vj walks of length k + 1.
1.3.2
Incidence matrix
The relationship between edges and vertices provides a very strong constraint on the
data structure, much like the relationship between points and blocks in a combinatorial
design or points and lines in a finite plane geometry. This incidence structure gives rise
to another way to describe a graph using a matrix.
Let G be a digraph with edge set E = {e1 , . . . , em } and vertex set V = {v1 , . . . , vn }.
The incidence matrix of G is the n m matrix B = [bij ] defined by
bij =
1, if vi is the tail of ej ,
1,
if vi is the head of ej ,
2,
0,
(1.10)
if ej is a self-loop at vi ,
otherwise.
Each column of B corresponds to an edge and each row corresponds to a vertex. The
definition of incidence matrix of a digraph as contained in expression (1.10) is applicable
to digraphs with self-loops as well as multidigraphs.
For the undirected case, let G be an undirected graph with edge set E = {e1 , . . . , em }
and vertex set V = {v1 , . . . , vn }. The unoriented incidence matrix of G is the n m
matrix B = [bij ] defined by
1, if vi is incident to ej ,
bij = 2, if ej is a self-loop at vi ,
0, otherwise.
25
The integral cycle space of a graph is equal to the kernel of an oriented incidence
matrix, viewed as a matrix over Q. The binary cycle space is the kernel of its oriented
or unoriented incidence matrix, viewed as a matrix over GF (2).
Theorem 1.26. The incidence matrix of an undirected graph G is related to the adjacency matrix of its line graph L(G) by the following theorem:
A(L(G)) = D(G)T D(G) 2In ,
where A(L(G)) is the adjacency matrix of the line graph of G.
Proof. Let Di denote the ith column of D.
Consider the dot product of Di and Dj , i = j. The terms contributing to this
expression are associated to the vertices which are incident to the ith edge and also to
the jth edge. In other words, there is such a vertex (and only one such vertex) if and
only if this dot product is equal to 1 if and only if ith edge is incident to the jth edge
in G if and only if the vertex in L(G) associated to the ith edge in G is adjacent to the
vertex in L(G) associated to the jth edge in G. But this is exactly the condition that
the corresponding entry of A(L(G)) is equal to 1.
Consider the dot product of Di with itself. The terms contributing to this expression
are associated to the vertices which are incident to the ith edge. There are 2 such vertices
so this dot product is equal to 2. Subtracting, the 2 in 2In , gives 0, as expect for the
diagonal entries of A(L(G)).
For a directed graph, the result in the above theorem does not hold in general (except
in characteristic 2), as the following example shows.
Example 1.27. Consider the graph shown in Figure 1.21, whose line graph is shown in
Figure 1.22.
sage : G1 = DiGraph ({0:[1 ,2 ,4] , 1:[2 ,3 ,4] , 2:[3 ,4]})
sage : G1 . show ()
sage : D1 = G1 . incidence_matrix (); D1
[ -1 -1 -1 0 0 0 0 0]
[ 0 0 1 -1 -1 -1 0 0]
[ 0 1 0 0 0 1 -1 -1]
[ 0 0 0 0 1 0 0 1]
[ 1 0 0 1 0 0 1 0]
sage : A1 = G1 . adjacency_matrix ()
sage : A1
[0 1 1 0 1]
[0 0 1 1 1]
[0 0 0 1 1]
[0 0 0 0 0]
[0 0 0 0 0]
sage : G = Graph ({0:[1 ,2 ,4] , 1:[2 ,3 ,4] , 2:[3 ,4]})
sage : D = G . incidence_matrix (); D
[ -1 -1 -1 0 0 0 0 0]
[ 0 0 1 -1 -1 -1 0 0]
[ 0 1 0 0 0 1 -1 -1]
[ 0 0 0 0 1 0 0 1]
[ 1 0 0 1 0 0 1 0]
26
# # line graph
27
Figure 1.22: The line graph of a digraph example having 5 vertices and 8 edges.
Theorem 1.28. The rank (over Q) of the incidence matrix of a directed connected simple
graph having n vertices is n 1.
Since G is a simple graph, it has fewer edges than vertices.
Proof. Consider the column of D coresponding to e E. The number of entries equal
to +1 is one (corresponding to the vertex at the head of e) and the number of entries
equal to 1 is also one (corresponding to the vertex at the tail of e). All other entries
are equal to 0. Therefore, the sum of all the rows in D is the zero vector. This implies
rankQ (D) n 1.
1.3.3
Laplacian matrix
1, if i = j and vi vj E,
di , if i = j,
ij =
0,
otherwise,
ij ]
is given by
28
There are many remarkable properties of the Laplacian matrix. It shall be discussed
further in Chapter 5.
1.3.4
Distance matrix
Recall that the distance (or geodesic distance) d(v, w) between two vertices v, w V in a
connected graph G = (V, E) is the number of edges in a shortest path connecting them.
The n n matrix [d(vi , vj )] is the distance matrix of G. Sage helps you to compute the
distance matrix of a graph:
sage : G = Graph ({1:[2 ,4] , 2:[1 ,4] , 3:[2 ,6] , 4:[1 ,3] , 5:[4 ,2] , 6:[3 ,1]})
sage : d = [[ G . distance (i , j ) for i in range (1 ,7)] for j in range (1 ,7)]
sage : matrix ( d )
[0 1 2 1 2 1]
[1 0 1 1 1 2]
[2 1 0 1 2 1]
[1 1 1 0 1 2]
[2 1 2 1 0 3]
[1 2 1 2 3 0]
The distance matrix is an important quantity which allows one to better understand
the connectivity of a graph. Distance and connectivity will be discussed in more detail
in Chapters 5 and 10.
1.4
Isomorphic graphs
Determining whether or not two graphs are, in some sense, the same is a hard but
important problem. Two graphs G and H are isomorphic if there is a bijection f :
V (G) V (H) such that whenever uv E(G) then f (u)f (v) E(H). The function f
is an isomorphism between G and H. Otherwise, G and H are non-isomorphic. If G
and H are isomorphic, we write G
= H.
(a)
(b)
29
(a) C6
(b) G1
(c) G2
C6 = Graph ({ " a " :[ " b " ," c " ] , " b " :[ " a " ," d " ] , " c " :[ " a " ," e " ] , \
" d " :[ " b " ," f " ] , " e " :[ " c " ," f " ] , " f " :[ " d " ," e " ]})
G1 = Graph ({1:[2 ,4] , 2:[1 ,3] , 3:[2 ,6] , 4:[1 ,5] , \
5:[4 ,6] , 6:[3 ,5]})
G2 = Graph ({ " a " :[ " d " ," e " ] , " b " :[ " c " ," f " ] , " c " :[ " b " ," f " ] , \
" d " :[ " a " ," e " ] , " e " :[ " a " ," d " ] , " f " :[ " b " ," c " ]})
C6 . is_isomorphic ( G1 )
C6 . is_isomorphic ( G2 )
G1 . is_isomorphic ( G2 )
Thus, for the graphs C6 , G1 and G2 in Figure 1.24, C6 and G1 are isomorphic, but G1
and G2 are not isomorphic.
An important notion in graph theory is the idea of an invariant. An invariant is
an object f = f (G) associated to a graph G which has the property
G
= H = f (G) = f (H).
For example, the number of vertices of a graph, f (G) = |V (G)|, is an invariant.
1.4.1
Adjacency matrices
30
re-ordering of the rows and a corresponding re-ordering of the columns. This notion of
permutation equivalence is an equivalence relation.
To show that two undirected graphs are isomorphic depends on the following result.
Theorem 1.30. Consider two directed or undirected graphs G1 and G2 with respective
adjacency matrices A1 and A2 . Then G1 and G2 are isomorphic if and only if A1 is
permutation equivalent to A2 .
This says that the permutation equivalence class of the adjacency matrix is an invariant.
Define an ordering on the set of nn (0, 1)-matrices as follows: we say A1 < A2 if the
list of entries of A1 is less than or equal to the list of entries of A2 in the lexicographical
ordering. Here, the list of entries of a (0, 1)-matrix is obtained by concatenating the
entries of the matrix, row-by-row. For example,
1 1
1 1
<
.
0 1
1 1
Algorithm 1.1 is an immediate consequence of Theorem 1.30. The lexicographically
maximal element of the permutation equivalence class of the adjacency matrix of G is
called the canonical label of G. Thus, to check if two undirected graphs are isomorphic,
we simply check if their canonical labels are equal. This idea for graph isomorphism
checking is presented in Algorithm 1.1.
Algorithm 1.1: Computing graph isomorphism using canonical labels.
Input: Two undirected simple graphs G1 and G2 , each having n vertices.
Output: True if G1
= G2 ; False otherwise.
1
2
3
4
5
6
7
for i 1, 2 do
Ai adjacency matrix of Gi
pi permutation equivalence class of Ai
Ai lexicographically maximal element of pi
if A1 = A2 then
return True
return False
1.4.2
Degree sequence
Let G be a graph with n vertices. The degree sequence of G is the ordered n-tuple of the
vertex degrees of G arranged in non-increasing order.
The degree sequence of G may contain the same degrees, repeated as often as they
occur. For example, the degree sequence of C6 is 2, 2, 2, 2, 2, 2 and the degree sequence
of the house graph in Figure 1.3 is 3, 3, 2, 2, 2. If n 3 then the cycle graph Cn has the
degree sequence
2, 2, 2, . . . , 2 .
n copies of 2
31
For positive integer values of n and m, the complete graph Kn has the degree sequence
n 1, n 1, n 1, . . . , n 1
n copies of n1
and the complete bipartite graph Km,n has the degree sequence
n, n, n, . . . , n, m, m, m, . . . , m .
m copies of n
n copies of m
i=1
di k(k + 1) +
j=k+1
min{k, di }
(1.11)
for all 1 k n 1.
As noted above, Theorem 1.31 is an existence result showing that something exists
without providing a construction of the object under consideration. Havel [102] and
Hakimi [99, 100] independently provided an algorithmic approach that allows for constructing a simple graph with a given degree sequence. See Sierksma and Hoogeveen [184]
for a coverage of seven criteria for a sequence of integers to be graphic. See Erdos et al. [78]
for an extension of the Havel-Hakimi theorem to digraphs.
Theorem 1.32. Havel 1955 [102] & Hakimi 19623 [99, 100]. Consider the
non-increasing sequence S1 = (d1 , d2 , . . . , dn ) of nonnegative integers, where n 2 and
d1 1. Then S1 is graphical if and only if the sequence
S2 = (d2 1, d3 1, . . . , dd1 +1 1, dd1 +2 , . . . , dn )
is graphical.
Proof. Suppose S2 is graphical. Let G2 = (V2 , E2 ) be a graph of order n 1 with vertex
set V2 = {v2 , v3 , . . . , vn } such that
deg(vi ) =
di 1, if 2 i d1 + 1,
di ,
if d1 + 2 i n.
32
Construct a new graph G1 with degree sequence S1 as follows. Add another vertex v1
to V2 and add to E2 the edges v1 vi for 2 i d1 + 1. It is clear that deg(v1 ) = d1 and
deg(vi ) = di for 2 i n. Thus G1 has the degree sequence S1 .
On the other hand, suppose S1 is graphical and let G1 be a graph with degree sequence
S1 such that
(i) The graph G1 has the vertex set V (G1 ) = {v1 , v2 , . . . , vn } and deg(vi ) = di for
i = 1, . . . , n.
(ii) The degree sum of all vertices adjacent to v1 is a maximum.
To obtain a contradiction, suppose v1 is not adjacent to vertices having degrees
d2 , d3 , . . . , dd1 +1 .
Then there exist vertices vi and vj with dj > di such that v1 vi E(G1 ) but v1 vj E(G1 ).
As dj > di , there is a vertex vk such that vj vk E(G1 ) but vi vk E(G1 ). Replacing the
edges v1 vi and vj vk with v1 vj and vi vk , respectively, results in a new graph H whose degree
sequence is S1 . However, the graph H is such that the degree sum of vertices adjacent to
v1 is greater than the corresponding degree sum in G1 , contradicting property (ii) in our
choice of G1 . Consequently, v1 is adjacent to d1 other vertices of largest degree. Then
S2 is graphical because G1 v1 has degree sequence S2 .
The proof of Theorem 1.32 can be adapted into an algorithm to determine whether
or not a sequence of nonnegative integers can be realized by a simple graph. If G is
a simple graph, the degree of any vertex in V (G) cannot exceed the order of G. By
the handshaking lemma (Theorem 1.9), the sum of all terms in the sequence cannot be
odd. Once the sequence passes these two preliminary tests, we then adapt the proof of
Theorem 1.32 to successively reduce the original sequence to a smaller sequence. These
ideas are summarized in Algorithm 1.2.
Algorithm 1.2: Havel-Hakimi test for sequences realizable by simple graphs.
Input: A nonincreasing sequence S = (d1 , d2 , . . . , dn ) of nonnegative integers,
where n 2.
Output: True if S is realizable by a simple graph; False otherwise.
1
2
3
4
5
6
7
8
9
10
11
if
i di is odd then
return False
while True do
if min(S) < 0 then
return False
if max(S) = 0 then
return True
if max(S) > length(S) 1 then
return False
S (d2 1, d3 1, . . . , dd1 +1 1, dd1 +2 , . . . , dlength(S) )
sort S in nonincreasing order
We now show that Algorithm 1.2 determines whether or not a sequence of integers
is realizable by a simple graph. Our input is a sequence S = (d1 , d2 , . . . , dn ) arranged
33
in non-increasing order, where each di 0. The first test as contained in the if block,
otherwise known as a conditional, on line 1 uses the handshaking lemma (Theorem 1.9).
During the first run of the while loop, the conditional on line 4 ensures that the sequence
S only consists of nonnegative integers. At the conditional on line 6, we know that S
is arranged in non-increasing order and has nonnegative integers. If this conditional
holds true, then S is a sequence of zeros and it is realizable by a graph with only isolated
vertices. Such a graph is simple by definition. The conditional on line 8 uses the following
property of simple graphs: If G is a simple graph, then the degree of each vertex of G
is less than the order of G. By the time we reach line 10, we know that S has n terms,
max(S) > 0, and 0 di n 1 for all i = 1, 2, . . . , n. After applying line 10, S is now a
sequence of n 1 terms with max(S) > 0 and 0 di n 2 for all i = 1, 2, . . . , n 1. In
general, after k rounds of the while loop, S is a sequence of n k terms with max(S) > 0
and 0 di n k 1 for all i = 1, 2, . . . , n k. And after n 1 rounds of the while
loop, the resulting sequence has one term whose value is zero. In other words, eventually
Algorithm 1.2 produces a sequence with a negative term or a sequence of zeros.
1.4.3
Invariants revisited
In some cases, one can distinguish non-isomorphic graphs by considering graph invariants.
For instance, the graphs C6 and G1 in Figure 1.24 are isomorphic so they have the same
number of vertices and edges. Also, G1 and G2 in Figure 1.24 are non-isomorphic because
the former is connected, while the latter is not connected. To prove that two graphs
are non-isomorphic, one could show that they have different values for a given graph
invariant. The following list contains some items to check off when showing that two
graphs are non-isomorphic:
1. the number of vertices,
2. the number of edges,
3. the degree sequence,
4. the length of a geodesic path,
5. the length of the longest path,
6. the number of connected components of a graph.
1.5
This section provides a brief survey of operations on graphs to obtain new graphs from
old graphs. Such graph operations include unions, products, edge addition, edge deletion,
vertex addition, and vertex deletion. Several of these are briefly described below.
1.5.1
The disjoint union of graphs is defined as follows. For two graphs G1 = (V1 , E1 ) and
G2 = (V2 , E2 ) with disjoint vertex sets, their disjoint union is the graph
G1 G2 = (V1 V2 , E1 E2 ).
34
For example, Figure 1.25 shows the vertex disjoint union of the complete bipartite graph
K1,5 with the wheel graph W4 . The adjacency matrix A of the disjoint union of two
graphs G1 and G2 is the diagonal block matrix obtained from the adjacency matrices A1
and A2 , respectively. Namely,
A1 0
A=
.
0 A2
Sage can compute graph unions, as the following example shows.
sage : G1 = Graph ({1:[2 ,4] , 2:[1 ,3] , 3:[2 ,6] , 4:[1 ,5] , 5:[4 ,6] , 6:[3 ,5]})
sage : G2 = Graph ({7:[8 ,10] , 8:[7 ,10] , 9:[8 ,12] , 10:[7 ,9] , 11:[10 ,8] , 12:[9 ,7]})
sage : G1u2 = G1 . union ( G2 )
sage : G1u2 . adjacency_matrix ()
[0 1 0 1 0 0 0 0 0 0 0 0]
[1 0 1 0 0 0 0 0 0 0 0 0]
[0 1 0 0 0 1 0 0 0 0 0 0]
[1 0 0 0 1 0 0 0 0 0 0 0]
[0 0 0 1 0 1 0 0 0 0 0 0]
[0 0 1 0 1 0 0 0 0 0 0 0]
[0 0 0 0 0 0 0 1 0 1 0 1]
[0 0 0 0 0 0 1 0 1 1 1 0]
[0 0 0 0 0 0 0 1 0 1 0 1]
[0 0 0 0 0 0 1 1 1 0 1 0]
[0 0 0 0 0 0 0 1 0 1 0 0]
[0 0 0 0 0 0 1 0 1 0 0 0]
In the case where V1 = V2 , then G1 G2 is simply the graph consisting of all edges in G1
or in G2 . In general, the union of two graphs G1 = (V1 , E1 ) and G2 = (V2 , E2 ) is defined
as
G1 G2 = (V1 V2 , E1 E2 )
3
1
4
1
4
2
(a) G1
(b) G2
(c) G1 G2
(d) G1 G2
Figure 1.26: The union and intersection of graphs with overlapping vertex sets.
The intersection of graphs is defined as follows. For two graphs G1 = (V1 , E1 ) and
G2 = (V2 , E2 ), their intersection is the graph
G1 G2 = (V1 V2 , E1 E2 ).
35
Figure 1.26(d) illustrates the intersection of two graphs whose vertex sets overlap.
The symmetric difference of graphs is defined as follows. For two graphs G1 = (V1 , E1 )
and G2 = (V2 , E2 ), their symmetric difference is the graph
G1 G2 = (V, E)
where V = V1 V2 and the edge set is given by
E = (E1 E2 )\{uv | u V1 V2
or v V1 V2 }.
4
1
(a) G1
(b) G2
(c) G1 G2
1.5.2
36
(a) W4
(b) W5
(c) W6
(d) W7
(e) W8
(f) W9
(b) G {b}
(a) G
c
(c) G {a, b}
(d) G {a, b, e}
(e) G {a, b, c, d, e}
37
Figure 1.30 shows a sequence of graphs resulting from edge deletion. Unlike vertex
deletion, when an edge is deleted the vertices incident on that edge are left intact.
b
(a) G
(b) G {ac}
38
Proof. First, assume that e = uv is a bridge of G. Suppose for contradiction that e lies
on a cycle
C : u, v, w1 , w2 , . . . , wk , u.
Then G e contains a u-v path u, wk , . . . , w2 , w1 , v. Let u1 , v1 be any two vertices in
G e. By hypothesis, G is connected so there is a u1 -v1 path P in G. If e does not lie
on P , then P is also a path in G e so that u1 , v1 are connected, which contradicts our
assumption of e being a bridge. On the other hand, if e lies on P , then express P as
u1 , . . . , u, v, . . . , v1
or u1 , . . . , v, u, . . . , v1 .
Now
u1 , . . . , u, wk , . . . , w2 , w1 , v, . . . , v1
or u1 , . . . , v, w1 , w2 , . . . , wk , u, . . . , v1
E2 = ve w | uw E\{e} or vw E\{e} .
Let G be the wheel graph W6 in Figure 1.31(a) and consider the edge contraction G/ab,
where ab is the gray colored edge in that figure. Then the edge set E1 denotes all those
edges in G each of which is not incident on a, b, or both a and b. These are precisely
those edges that are colored red. The edge set E2 means that we consider those edges in
G each of which is incident on exactly one of a or b, but not both. The blue colored edges
in Figure 1.31(a) are precisely those edges that E2 suggests for consideration. The result
of the edge contraction G/ab is the wheel graph W5 in Figure 1.31(b). Figures 1.31(a)
to 1.31(f) present a sequence of edge contractions that starts with W6 and repeatedly
contracts it to the trivial graph K1 .
39
d
d
d
f
e
c
f
vab = g
(a) G1
vcg = h
(b) G2 = G1 /ab
(c) G3 = G2 /cg
vdh = i
(d) G4 = G3 /dh
vf i = j
(e) G5 = G4 /f i
vej
(f) G6 = G5 /ej
1.5.3
Complements
The complement of a simple graph has the same vertices, but exactly those edges that
are not in the original graph. In other words, if Gc = (V, E c ) is the complement of
G = (V, E), then two distinct vertices v, w V are adjacent in Gc if and only if they are
not adjacent in G. We also write the complement of G as G. The sum of the adjacency
matrix of G and that of Gc is the matrix with 1s everywhere, except for 0s on the
main diagonal. A simple graph that is isomorphic to its complement is called a selfcomplementary graph. Let H be a subgraph of G. The relative complement of G and H
is the edge deletion subgraph G E(H). That is, we delete from G all edges in H. Sage
can compute edge complements, as the following example shows.
sage : G = Graph ({1:[2 ,4] , 2:[1 ,4] , 3:[2 ,6] , 4:[1 ,3] , 5:[4 ,2] , 6:[3 ,1]})
sage : Gc = G . complement ()
sage : EG = Set ( G . edges ( labels = False )); EG
{(1 , 2) , (4 , 5) , (1 , 4) , (2 , 3) , (3 , 6) , (1 , 6) , (2 , 5) , (3 , 4) , (2 , 4)}
sage : EGc = Set ( Gc . edges ( labels = False )); EGc
{(1 , 5) , (2 , 6) , (4 , 6) , (1 , 3) , (5 , 6) , (3 , 5)}
sage : EG . difference ( EGc ) == EG
True
sage : EGc . difference ( EG ) == EGc
True
sage : EG . intersection ( EGc )
{}
n(n 1)
1 n(n 1)
=
.
2
2
4
40
Then n | n(n 1), with one of n and n 1 being even and the other odd. If n is even,
n 1 is odd so gcd(4, n 1) = 1, hence by [183, Theorem 1.9] we have 4 | n and so
n = 4k for some nonnegative k Z. If n 1 is even, use a similar argument to conclude
that n = 4k + 1 for some nonnegative k Z.
Theorem 1.35. A graph and its complement cannot be both disconnected.
Proof. If G is connected, then we are done. Without loss of generality, assume that G
is disconnected and let G be the complement of G. Let u, v be vertices in G. If u, v
are in different components of G, then they are adjacent in G. If both u, v belong to
some component Ci of G, let w be a vertex in a different component Cj of G. Then u, w
are adjacent in G, and similarly for v and w. That is, u and v are connected in G and
therefore G is connected.
1.5.4
Cartesian product
The Cartesian product G H of graphs G and H is a graph such that the vertex set of
G H is the Cartesian product
V (G H) = V (G) V (H).
Any two vertices (u, u ) and (v, v ) are adjacent in G H if and only if either
1. u = v and u is adjacent with v in H; or
2. u = v and u is adjacent with v in G.
The vertex set of G H is V (G H) and the edge set of G H is
E(G H) = V (G) E(H) E(G) V (H) .
n)
{0, 1}
of cardinality 2n . That is, each vertex of Qn is a bit string of length n. Two vertices
v, w V are connected by an edge if and only if v and w differ in exactly one coordinate.5
5
41
(a) K3
(b) P3
(c) K3 P3
= Qn .
(a) Q1
(b) Q2
(c) Q3
(d) Q4
Pan
where the 1-mesh is simply the path graph M (k) = Pk for some positive integer k.
Figure 1.34(a) illustrates the 2-mesh M (3, 4) = P3 P4 , while the 3-mesh M (3, 2, 3) =
P3 P2 P3 is presented in Figure 1.34(b).
42
(a) M (3, 4)
(b) M (3, 2, 3)
Figure 1.34: The 2-mesh M (3, 4) and the 3-mesh M (3, 2, 3).
1.5.5
Graph minors
1.6
Common applications
(a) 2,4,4-trimethylheptane
(b) naphthalene
43
is called a molecular graph; two such examples are illustrated in Figure 1.35. Below we
list a few common problems arising in applications of graph theory. See Foulds [84] and
Walther [201] for surveys of applications of graph theory in science, engineering, social
sciences, economics, and operation research.
If the edge weights are all nonnegative, find a cheapest closed path which contains
all the vertices. This is related to the famous traveling salesman problem and is
further discussed in Chapters 2 and 6.
Find a walk that visits each vertex, but contains as few edges as possible and
contains no cycles. This type of problem is related to spanning trees and is discussed
in further details in Chapter 3.
Determine which vertices are more central than others. This is connected with
various applications to social network analysis and is covered in more details in
Chapters 5 and 10. An example of a social network is shown in Figure 1.36, which
illustrates the marriage ties among Renaissance Florentine families [35]. Note that
one family has been removed because its inclusion would create a disconnected
graph.
Pazzi
Ginori
Salviati
Albizzi
Medici
Acciaiuol
Lambertes
Barbadori
Ridolfi
Strozzi
Castellan
Bischeri
Peruzzi
Tornabuon
Guadagni
44
(a)
(b)
1.7
45
20
50
0
20
50
40
70
20
60
90
40
50
70 $1
80 $1
60
70
90 $1
80 $1 $1
90 $1 $1
$1 $1 $1
Table 1.1: Transition table of a simple vending machine.
1.7.1
46
20
20
40
60
20
20
50
50
80
20
70
20
20
50
90
50
20
50
50
50
50
$1
50
20, 50
(1, b) = 2,
(2, a) = 2,
(2, b) = 2.
47
Example 1.42. Let A = (Q, , , Q0 , F ) be defined by Q = {1, 2}, = {a, b}, Q0 = {1},
F = {2}, and the transition function given by
(1, a) = 1,
(1, a) = 2,
(2, a) = 2,
(2, b) = 2.
Figure 1.40 shows a digraph representation of A. Note that (1, a) = 1 and (1, a) = 2.
It follows by definition that A is an NFA.
) = q.
Let q Q and let s = s1 s2 sn . In the case of the empty string, define (q,
s1 ) = (q, s1 ). For 1 < i n, define
When i = 1, we have (q,
s1 s2 si1 ), si .
s1 s2 si ) = (q,
(q,
The special language L(A) is also referred to as a regular language. Referring back to
example 1.41, any string accepted by A has zero or more a, followed by exactly one b,
and finally zero or more occurrences of a or b. We can describe this language using the
regular expression a b(a|b) .
For NFAs, we can similarly define a transition function operating on finite strings.
Each input is a string over and the transition function returns a subset of Q. Formally,
our transition function for NFAs operating on finite strings is the map
: Q 2Q .
Let q Q and let w = xa, where x and a . The input symbol a can be
interpreted as being the very last symbol in the string w. Then x is interpreted as being
the substring of w excluding the symbol a. In the case of the empty string, we have
) = {q}. For the inductive case, assume that (q,
x) = {p1 , p2 , . . . , pk } where each
(q,
(pi , a).
i=1
It may happen that for some state pi , there are no transitions from pi with input a. We
cater for this possibility by writing (pi , a) = .
48
1.7.2
Any NFA can be simulated by a DFA. One way of accomplishing this is to allow the DFA
to keep track of all the states that the NFA can be in after reading an input symbol.
The formal proof depends on this construction of an equivalent DFA and then showing
that the language of the DFA is the same as that of the NFA.
Theorem 1.43. Determinize an NFA. If A is a nondeterministic finite-state automaton, then there exists a deterministic finite-state automaton A such that L(A) =
L(A ).
({q1 , q2 , . . . , qi }, s) =
(qk , s) = {p1 , p2 , . . . , pj }.
(1.13)
k=1
For any input string w, we now show by induction on the length of w that
(q0 , w) = [q1 , q2 , . . . , qi ]
(Q0 , w) = {q1 , q2 , . . . , qi }.
(1.14)
For the basis step, let |w| = 0 so that w = . Then it is clear that
(q0 , w) = (q0 , ) = [q0 ]
(Q0 , w) = (Q0 , ) = Q0 .
Next, assume for induction that statement (1.14) holds for all strings of length less than
or equal to m > 0. Let w be a string of length m and let a so that |wa| = m + 1.
Then (q0 , wa) = (q0 , w), a . By our inductive hypothesis, we have
(q0 , w) = [p1 , p2 , . . . , pj ]
(Q0 , w) = {p1 , p2 , . . . , pj }
and applying (1.14) we get
([p1 , p2 , . . . , pj ], a) = [r1 , r2 , . . . , rk ]
({p1 , p2 , . . . , pj }, a) = {r1 , r2 , . . . , rk }.
Hence
(q0 , wa) = [r1 , r2 , . . . , rk ]
(Q0 , wa) = {r1 , r2 , . . . , rk }
which establishes that statement (1.14) holds for all finite strings over . Finally,
(q0 , w) F if and only if there is some p (Q0 , w) such that p F . Therefore
L(A) = L(A ).
49
Theorem 1.43 tells us that any NFA corresponds to some DFA that accepts the
same language. For this reason, the theorem is said to provide us with a procedure
for determinizing NFAs. The actual procedure itself is contained in the proof of the
theorem, although it must be noted that the procedure is inefficient since it potentially
yields transitions from states that are unreachable from the initial state. If q Q is
a state of A that is unreachable from q0 , then there are no input strings w such that
(q0 , w) = q. Such unreachable states are redundant insofar as they do not affect L(A ).
Another inefficiency of the procedure in the proof of Theorem 1.43 is the problem of
state space explosion. As Q = 2Q is the power set of Q, the resulting DFA can potentially
have exponentially more states than the NFA it is simulating. In the worse case, each
element of Q is a state of the resulting DFA that is reachable from q0 = [Q0 ]. The
best-case scenario is when each state of the DFA is a singleton, hence the DFA has the
same number of states as its corresponding NFA. However, according to the procedure
in the proof of Theorem 1.43, we generate all the possible 2n states of the DFA, where
n = |Q|. After considering all the transitions whose starting states are singletons, we
then consider all transitions starting from each of the remaining 2n n elements in Q .
In the best-case, none of those remaining 2n n states are reachable from q0 , hence it is
redundant to generate transitions starting at each of those 2n n states. Example 1.44
concretizes our discussion.
Example 1.44. Use the procedure in Theorem 1.43 to determinize the NFA in Figure 1.41.
a
b
a
c
3
1 {1, 2}
{2}
c
{3}
{3}
50
we apply (1.13) to construct all the possible transitions of A . These transitions are
contained in Table 1.3. Using those transitions, we obtain the digraph representation in
Figure 1.42, from which it is clear that the states [1], [2], [3], and [1, 2] are the only states
[1]
a
[1, 2]
[2]
[1, 2]
[1, 2]
[2]
[3]
[1, 3]
[1, 2]
[3]
[2, 3]
[2]
[3]
[2]
[3]
[3]
[1, 2, 3] [1, 2]
c
[3]
[2]
[3]
Table 1.3: Transition table of a deterministic version of the NFA in Figure 1.41.
reachable from the initial state q0 = [1]. The remaining states [1, 3], [2, 3], and [1, 2, 3]
are not reachable from q0 = [1]. In other words, starting at q0 = [1] there are no input
strings that would result in a transition to any of [1, 3], [2, 3], and [1, 2, 3]. Therefore these
states, and the transitions starting from them, can be deleted from Figure 1.42 without
affecting the language of A . Figure 1.43 shows an equivalent DFA with redundant states
removed.
a
a
c
1, 2
3
c
1, 2, 3
a
b
b
2, 3
1, 3
Figure 1.42: A DFA accepting the same language as the NFA in Figure 1.41.
1.8. Problems
51
a
c
a
1, 2
3
c
Figure 1.43: A DFA equivalent to that in Figure 1.42, with redundant states removed.
1.8
Problems
A problem left to itself dries up or goes rotten. But fertilize a problem with a solution
youll hatch out dozens.
N. F. Simpson, A Resounding Tinkle, 1958
Bob
Carol
od(v) = q.
vV
52
Karlsruhe
197
54
Augsburg
Mannheim
57
72
Munich
383
Kassel
149
97
Nuremberg
157
Stuttgart
Frankfurt
145
90
W
urzburg
154
Erfurt
1.8. Problems
53
1.5. If G is a simple graph of order n > 0, show that deg(v) < n for all v V (G).
1.6. Let G be a graph of order n and size m. Then G is called an overfull graph if
m > (G) n/2 . If m = (G) n/2 + 1, then G is said to be just overfull.
It can be shown that overfull graphs have odd order. Equivalently, let G be of
odd order. We can define G to be overfull if m > (G) (n 1)/2, and G is just
overfull if m = (G) (n 1)/2 + 1. Find an overfull graph and a graph that is
just overfull. Some basic results on overfull graphs are presented in [56].
1.7. Fix a positive integer n and denote by (n) the number of simple graphs on n
vertices. Show that
n
(n) = 2( 2 ) = 2n(n1)/2 .
1.8. Let G be an undirected graph whose unoriented incidence matrix is Mu and whose
oriented incidence matrix is Mo .
(a) Show that the sum of the entries in any row of Mu is the degree of the
corresponding vertex.
(b) Show that the sum of the entries in any column of Mu is equal to 2.
(c) If G has no self-loops, show that each column of Mo sums to zero.
1.9. Let G be a loopless digraph and let M be its incidence matrix.
(a) If r is a row of M , show that the number of occurrences of 1 in r counts
the outdegree of the vertex corresponding to r. Show that the number of
occurrences of 1 in r counts the indegree of the vertex corresponding to r.
(b) Show that each column of M sums to 0.
1.10. Let G be a digraph and let M be its incidence matrix. For any row r of M , let m
be the frequency of 1 in r, let p be the frequency of 1 in r, and let t be twice the
frequency of 2 in r. If v is the vertex corresponding to r, show that the degree of
v is deg(v) = m + p + t.
1.11. Let G be an undirected graph without self-loops and let M and its oriented incidence matrix. Show that the Laplacian matrix L of G satisfies L = M M T ,
where M T is the transpose of M .
1.12. Let J1 denote the incidence matrix of G1 and let J2 denote the incidence matrix of
G2 . Find matrix theoretic criteria on J1 and J2 which hold if and only if G1
= G2 .
In other words, find the analog of Theorem 1.30 for incidence matrices.
1.13. Show that the complement of an edgeless graph is a complete graph.
1.14. Let G H be the Cartesian product of two graphs G and H. Show that |E(G H)| =
|V (G)| |E(H)| + |E(G)| |V (H)|.
1.15. In 1751, Leonhard Euler posed a problem to Christian Goldbach, a problem that
now bears the name Eulers polygon division problem. Given a plane convex
polygon having n sides, how many ways are there to divide the polygon into triangles using only diagonals? For our purposes, we consider only regular polygons
having n sides for n 3 and any two diagonals must not cross each other. For
54
2n
1
.
n+1 n
Dorrie [68, pp.2127] showed that En is related to the Catalan numbers via
the equation En = Cn1 . Show that
Cn =
1
2n + 2
.
4n + 2 n + 1
For k 2, show that the Catalan numbers satisfy the recurrence relation
Ck =
4k 2
Ck1 .
k+1
1.16. A graph is said to be planar if it can be drawn on the plane in such a way that
no two edges cross each other. For example, the complete graph Kn is planar for
n = 1, 2, 3, 4, but K5 is not planar (see Figure 1.13). Draw a planar version of K4 as
presented in Figure 1.13(b). Is the graph in Figure 1.9 planar? For n = 1, 2, . . . , 5,
enumerate all simple nonisomorphic graphs on n vertices that are planar; only work
with undirected graphs.
1.8. Problems
55
1.17. If n 3, show that the join of Cn and K1 is the wheel graph Wn+1 . In other words,
show that Cn + K1 = Wn+1 .
1.18. A common technique for generating random numbers is the linear congruential
method, a generalization of the Lehmer generator [141] introduced in 1949. First,
we choose four integers:
m,
modulus,
0<m
a,
multiplier,
c,
increment,
0a<m
X0 ,
seed,
0c<m
0 X0 < m
where the value X0 is also referred to as the starting value. Then iterate the
relation
Xn+1 = (aXn + c) mod m,
n0
and halt when the relation produces the seed X0 or when it produces an integer
Xk such that Xk = Xi for some 0 i < k. The resulting sequence
S = (X0 , X1 , . . . , Xn )
is called a linear congruential sequence. Define a graph theoretic representation of
S as follows: let the vertex set be V = {X0 , X1 , . . . , Xn } and let the edge set be
E = {Xi Xi+1 | 0 i < n}. The resulting graph G = (V, E) is called the linear
congruential graph of the linear congruential sequence S. See chapter 3 of [129] for
other techniques for generating random numbers.
(a) Compute the linear congruential sequences Si with the following parameters:
(i)
(ii)
(iii)
(iv)
S1 :
S2 :
S3 :
S4 :
m = 10,
m = 10,
m = 10,
m = 10,
a = c = X0 = 7
a = 5, c = 7, X0 = 0
a = 3, c = 7, X0 = 2
a = 2, c = 5, X0 = 3
(b) Let Gi be the linear congruential graph of Si . Draw each of the graphs Gi .
Draw the graph resulting from the union
Gi .
i
56
1.20. Describe and present pseudocode to generate a random regular graph. What is the
worst-case runtime of your algorithm?
1.21. The Cantor-Schroder-Bernstein theorem states that if A, B are sets and we have an
injection f : A B and an injection g : B A, then there is a bijection between A
and B, thus proving that A and B have the same cardinality. Here we use bipartite
graphs and other graph theoretic concepts to prove the Cantor-Schroder-Bernstein
theorem. The full proof can be found in [214].
(a) Is it possible for A and B to be bipartitions of V and yet satisfy A B = ?
(b) Now assume that A B = and define a bipartite graph G = (V, E) with A
and B being the two partitions of V , where for any x A and y B we have
xy E if and only if either f (x) = y or g(y) = x. Show that deg(v) = 1 or
deg(v) = 2 for each v V .
(c) Let C be a component of G and let A A and B B contain all vertices
in the component C. Show that |A | = |B |.
1.22. Fermats little theorem states that if p is prime and a is an integer not divisible
by p, then p divides ap a. Here we cast the problem within the context of graph
theory and prove it using graph theoretic concepts. The full proof can be found
in [103, 214].
(a) Let G = (V, E) be a graph with V being the set of all sequences (a1 , a2 , . . . , ap )
of integers 1 ai a and aj = ak for some j = k. Show that G has ap a
vertices.
(b) Define the edge set of G as follows. If u, v V such that u = (u1 , u2 , . . . , up )
and v = (up , u1 , . . . , up1 ), then uv E. Show that each component of G is a
cycle of length p.
(c) Show that G has (ap a)/p components.
1.23. For the finite automaton in Figure 1.38, identify the following:
(a) The states set Q.
(b) The alphabet set .
(c) The transition function : Q Q.
1.24. The cycle graph Cn is a 2-regular graph. If 2 < r < n/2, unlike the cycle graph
there are various realizations of an r-regular graph; see Figure 1.47 for the case of
r = 3 and n = 10. The k-circulant graph on n vertices can be considered as an
intermediate graph between Cn and a k-regular graph. Let k and n be positive
integers satisfying k < n/2 with k being even. Suppose G = (V, E) is a simple
undirected graph with vertex set V = {0, 1, . . . , n 1}. Define the edge set of G
as follows. Each i V is incident with each of i + j mod n and i j mod n for
j {1, 2, . . . , k/2}. With the latter edge set, G is said to be a k-circulant graph, a
type of graphs used in constructing small-world networks (see section 10.4). Refer
to Figure 1.48 for examples of k-circulant graphs.
1.8. Problems
57
3
1
9
7
(a)
3
1
9
7
9
7
(b)
(c)
(a) k = 4
(b) k = 6
(c) k = 8
Chapter 2
Graph algorithms
Graph algorithms have many applications. Suppose you are a salesman with a product
you would like to sell in several cities. To determine the cheapest travel route from cityto-city, you must effectively search a graph having weighted edges for the cheapest
route visiting each city once. Each vertex denotes a city you must visit and each edge
has a weight indicating either the distance from one city to another or the cost to travel
from one city to another.
Shortest path algorithms are some of the most important algorithms in algorithmic
graph theory. In this chapter, we first examine several common graph traversal algorithms and some basic data structures underlying these algorithms. A data structure is
a combination of methods for structuring a collection of data (e.g. vertices and edges)
and protocols for accessing the data. We then consider a number of common shortest
path algorithms, which rely in one way or another on graph traversal techniques and
basic data structures for organizing and managing vertices and edges.
58
2.1
59
In section 1.3, we discussed how to use matrices for representing graphs and digraphs. If
A = [aij ] is an mn matrix, the adjacency matrix representation of a graph would require
representing all the mn entries of A. Alternative graph representations exist that are
much more efficient than representing all entries of a matrix. The graph representation
used can be influenced by the size of a graph or the purpose of the representation. Section 2.1.1 discusses the adjacency list representation that can result in less storage space
requirement than the adjacency matrix representation. The graph6 format discussed in
section 2.1.3 provides a compact means of storing graphs for archival purposes.
2.1.1
Adjacency lists
A list is a sequence of objects. Unlike sets, a list may contain multiple copies of the same
object. Each object in a list is referred to as an element of the list. A list L of n 0
elements is written as L = [a1 , a2 , . . . , an ], where the i-th element ai can be indexed
as L[i]. In case n = 0, the list L = [ ] is referred to as the empty list. Two lists are
equivalent if they both contain the same elements at exactly the same positions.
Define the adjacency lists of a graph as follows. Let G be a graph with vertex set
V = {v1 , v2 , . . . , vn }. Assign to each vertex vi a list Li containing all the vertices that
are adjacent to vi . The list Li associated with vi is referred to as the adjacency list of
vi . Then Li = [ ] if and only if vi is an isolated vertex. We say that Li is the adjacency
list of vi because any permutation of the elements of Li results in a list that contains
the same vertices adjacent to vi . We are mainly concerned with the neighbors of vi , but
disregard the position where each neighbor is located in Li . If each adjacency list Li
contains si elements where 0 si n, we say that Li has length si . The adjacency
list representation of the graph G requires that we represent i si = 2 |E(G)| n2
elements in a computers memory, since each edge appears twice in the adjacency list
representation. An adjacency list is explicit about which vertices are adjacent to a vertex
and implicit about which vertices are not adjacent to that same vertex. Without knowing
the graph G, given the adjacency lists L1 , L2 , . . . , Ln , we can reconstruct G. For example,
Figure 2.1 shows a graph and its adjacency list representation.
4
3
7
L1 = [2, 8]
L5 = [6, 8]
L2 = [1, 6]
L6 = [2, 5, 8]
L3 = [4]
L7 = [ ]
L4 = [3]
L8 = [1, 5, 6]
60
vertices are adjacent if their corresponding sets are disjoint. Draw the (5, 2)-Kneser
graph and find its order and adjacency lists. In general, if n and k are positive, what is
the order of the (n, k)-Kneser graph?
Solution. The (5, 2)-Kneser graph is the graph whose vertices are the 2-subsets
{1, 2}, {1, 3}, {1, 4}, {1, 5}, {2, 3}, {2, 4}, {2, 5}, {3, 4}, {3, 5}, {4, 5}
of {1, 2, 3, 4, 5}. That is, each vertex of the (5, 2)-Kneser graph is a 2-combination of the
set {1, 2, 3, 4, 5} and therefore the graph itself has order 52 = 54
= 10. The edges of
2!
this graph are
({1, 3}, {2, 4}), ({2, 4}, {1, 5}), ({2, 4}, {3, 5}), ({1, 3}, {4, 5}), ({1, 3}, {2, 5})
({3, 5}, {1, 4}), ({3, 5}, {1, 2}), ({1, 4}, {2, 3}), ({1, 4}, {2, 5}), ({4, 5}, {2, 3})
({4, 5}, {1, 2}), ({1, 5}, {2, 3}), ({1, 5}, {3, 4}), ({3, 4}, {1, 2}), ({3, 4}, {2, 5})
from which we obtain the following adjacency lists:
L{1,2} = [{3, 4}, {3, 5}, {4, 5}],
The (5, 2)-Kneser graph itself is shown in Figure 2.2. Using Sage, we have
sage : K = graphs . KneserGraph (5 , 2); K
Kneser graph with parameters 5 ,2: Graph on 10 vertices
sage : for v in K . vertices ():
...
print (v , K . neighbors ( v ))
...
({4 , 5} , [{1 , 3} , {1 , 2} , {2 , 3}])
({1 , 3} , [{2 , 4} , {2 , 5} , {4 , 5}])
({2 , 5} , [{1 , 3} , {3 , 4} , {1 , 4}])
({2 , 3} , [{1 , 5} , {1 , 4} , {4 , 5}])
({3 , 4} , [{1 , 2} , {1 , 5} , {2 , 5}])
({3 , 5} , [{2 , 4} , {1 , 2} , {1 , 4}])
({1 , 4} , [{2 , 3} , {3 , 5} , {2 , 5}])
({1 , 5} , [{2 , 4} , {3 , 4} , {2 , 3}])
({1 , 2} , [{3 , 4} , {3 , 5} , {4 , 5}])
({2 , 4} , [{1 , 3} , {1 , 5} , {3 , 5}])
If n and k are positive integers, then the (n, k)-Kneser graph has
n
k
n(n 1) (n k + 1)
k!
vertices.
We can categorize a graph G = (V, E) as dense or sparse based upon its size. A dense
graph has size |E| that is close to |V |2 , i.e. |E| = |V |2 , in which case it is feasible to
represent G as an adjacency matrix. The size of a sparse graph is much less than |V |2 ,
i.e. |E| = |V | , which renders the adjacency matrix representation as unsuitable. For
a sparse graph, an adjacency list representation can require less storage space than an
adjacency matrix representation of the same graph.
61
{3, 4}
{1, 2}
{1, 5}
{2, 5}
{2, 3}
{3, 5}
{4, 5}
{1, 4}
{2, 4}
{1, 3}
2.1.2
Edge lists
Lists can also be used to store the edges of a graph. To create an edge list L for a graph
G, if uv is an edge of G then we let uv or the ordered pair (u, v) be an element of L. In
general, let
v0 v1 , v2 v3 , . . . , vk vk+1
be all the edges of G, where k is even. Then the edge list of G is given by
L = [v0 v1 , v2 v3 , . . . , vk vk+1 ].
In some cases, it is desirable to have the edges of G be in contiguous list representation.
If the edge list L of G is as given above, the contiguous edge list representation of the
edges of G is
[v0 , v1 , v2 , v3 , . . . , vk , vk+1 ].
That is, if 0 i k is even then vi vi+1 is an edge of G.
2.1.3
The graph formats graph6 and sparse6 were developed by Brendan McKay [150] at
The Australian National University as a compact way to represent graphs. These two
formats use bit vectors and printable characters of the American Standard Code for
Information Interchange (ASCII) encoding scheme. The 64 printable ASCII characters
used in graph6 and sparse6 are those ASCII characters with decimal codes from 63 to
126, inclusive, as shown in Table 2.1. This section shall only cover the graph6 format.
For full specification on both of the graph6 and sparse6 formats, see McKay [150].
Bit vectors
Before discussing how graph6 and sparse6 represent graphs using printable ASCII characters, we first present encoding schemes used by these two formats. A bit vector is, as
62
binary decimal
0111111
63
1000000
64
1000001
65
1000010
66
1000011
67
1000100
68
1000101
69
1000110
70
1000111
71
1001000
72
1001001
73
1001010
74
1001011
75
1001100
76
1001101
77
1001110
78
1001111
79
1010000
80
1010001
81
1010010
82
1010011
83
1010100
84
1010101
85
1010110
86
1010111
87
1011000
88
1011001
89
1011010
90
1011011
91
1011100
92
1011101
93
1011110
94
glyph
?
@
A
B
C
D
E
F
G
H
I
J
K
L
M
N
O
P
Q
R
S
T
U
V
W
X
Y
Z
[
\
]
^
binary decimal
1011111
95
1100000
96
1100001
97
1100010
98
1100011
99
1100100
100
1100101
101
1100110
102
1100111
103
1101000
104
1101001
105
1101010
106
1101011
107
1101100
108
1101101
109
1101110
110
1101111
111
1110000
112
1110001
113
1110010
114
1110011
115
1110100
116
1110101
117
1110110
118
1110111
119
1111000
120
1111001
121
1111010
122
1111011
123
1111100
124
1111101
125
1111110
126
glyph
_
a
b
c
d
e
f
g
h
i
j
k
l
m
n
o
p
q
r
s
t
u
v
w
x
y
z
{
|
}
~
63
its name suggests, a vector whose elements are 1s and 0s. It can be represented as a list
of bits, e.g. E can be represented as the ASCII bit vector [1, 0, 0, 0, 1, 0, 1]. For brevity,
we write a bit vector in a compact form such as 1000101. The length of a bit vector
is its number of bits. The most significant bit of a bit vector v is the bit position with
the largest value among all the bit positions in v. Similarly, the least significant bit is
the bit position in v having the least value among all the bit positions in v. The least
significant bit of v is usually called the parity bit because when v is interpreted as an
integer the parity bit determines whether the integer is even or odd. Reading 1000101
from left to right, the first bit 1 is the most significant bit, followed by the second bit 0
which is the second most significant bit, and so on all the way down to the seventh bit
1 which is the least significant bit.
The order in which we process the bits of a bit vector
v = bn1 bn2 b0
(2.1)
p(x) =
i=0
(2.2)
at x = 2. See problem 2.2 for discussion of an efficient method to compute the integer
representation of a bit vector.
position
bit value
position value
0
1
20
1
0
21
2
0
22
3
0
23
4
1
24
5
0
25
6
1
26
64
0
1
26
1
0
25
2
0
24
3
0
23
4
1
22
5
0
21
6
1
20
n + 63,
if 0 n 62,
c2
c3
ci
cn
where ci denotes the entries a0,i a1,i ai1,i in column i of M . Then the graph6 representation of G is N (n)R(v), where R(v) and N (n) are as in (2.3) and (2.4), respectively.
That is, N (n) encodes the order of G and R(v) encodes the edges of G.
2.2
65
Graph searching
Errors, like straws, upon the surface flow;
He who would search for pearls must dive below.
John Dryden, All for Love, 1678
This section discusses two fundamental algorithms for graph traversal: breadth-first
search and depth-first search. The word search used in describing these two algorithms
is rather misleading. It would be more accurate to describe them as algorithms for
constructing trees using the adjacency information of a given graph. However, the names
breadth-first search and depth-first search are entrenched in literature on graph
theory and computer science. From hereon, we use these two names as given above,
bearing in mind their intended purposes.
2.2.1
Breadth-first search
Breadth-first search (BFS) is a strategy for running through the vertices of a graph. It
was presented by Moore [158] in 1959 within the context of traversing mazes. Lee [140]
independently discovered the same algorithm in 1961 in his work on routing wires on
circuit boards. In the physics literature, BFS is also known as a burning algorithm in
view of the analogy of a fire burning and spreading through an area, a piece of paper,
fabric, etc.
The basic BFS algorithm can be described as follows. Starting from a given vertex
v of a graph G, we first explore the neighborhood of v by visiting all vertices that are
adjacent to v. We then apply the same strategy to each of the neighbors of v. The
strategy of exploring the neighborhood of a vertex is applied to all vertices of G. The
result is a tree rooted at v and this tree is a subgraph of G. Algorithm 2.1 presents a
general template for the BFS strategy. The tree resulting from the BFS algorithm is
called a breadth-first search tree.
Algorithm 2.1: A general breadth-first search template.
Input: A directed or undirected graph G = (V, E) of order n > 0. A vertex s
from which to start the search. The vertices are numbered from 1 to
n = |V |, i.e. V = {1, 2, . . . , n}.
Output: A list D of distances of all vertices from s. A tree T rooted at s.
1
2
3
4
5
6
7
8
9
10
11
12
Q [s]
/* queue of nodes to visit */
D [, , . . . , ]
/* n copies of */
D[s] 0
T []
while length(Q) > 0 do
v dequeue(Q)
for each w adj(v) do
if D[w] = then
D[w] D[v] + 1
enqueue(Q, w)
append(T, vw)
return (D, T )
66
The breadth-first search algorithm makes use of a special type of list called a queue.
This is analogous to a queue of people waiting in line to be served. A person may enter
the queue by joining the rear of the queue. The person who is in the queue the longest
amount of time is served first, followed by the person who has waited the second longest
time, and so on. Formally, a queue Q is a list of elements. At any time, we only have
access to the first element of Q, known as the front or start of the queue. We insert
a new element into Q by appending the new element to the rear or end of the queue.
The operation of removing the front of Q is referred to as dequeue, while the operation
of appending to the rear of Q is called enqueue. That is, a queue implements a first-in
first-out (FIFO) protocol for adding and removing elements. As with lists, the length of
a queue is its total number of elements.
1
7
4
6
7
4
6
2
2
3
7
4
6
67
7
4
6
7
4
6
2
3
3
7
2
4
6
68
of edges in E(G) that make up a tree rooted at the given start vertex s. As trees are
connected graphs without cycles, we may take the vertices comprising the edges of T to
be the vertex set of the tree. It is clear that T represents a tree by means of a list of
edges, which allows us to identify the tree under consideration as the edge list T . The
list D has the same number of elements as the order of G = (V, E), i.e. length(D) = |V |.
The i-th element D[i] counts the number of edges in T between the vertices s and vi . In
other words, D[i] is the length of the s-vi path in T . It can be shown that D[i] = if
and only if G is disconnected. After one application of Algorithm 2.1, it may happen that
D[i] = for at least one vertex vi V . To traverse those vertices that are unreachable
from s, again we apply Algorithm 2.1 on G with starting vertex vi . Repeat this algorithm
as often as necessary until all vertices of G are visited. The result may be a tree that
contains all the vertices of G or a collection of trees, each of which contains a subset of
V (G). Figures 2.3 and 2.4 present BFS trees resulting from applying Algorithm 2.1 on
an undirected graph and a digraph, respectively.
Theorem 2.2. The worst-case time complexity of Algorithm 2.1 is O(|V | + |E|).
Proof. Without loss of generality, we can assume that G = (V, E) is connected. The
initialization steps in lines 1 to 4 take O(|V |) time. After initialization, all but one
vertex are labelled . Line 8 ensures that each vertex is enqueued at most once and
hence dequeued at most once. Each of enqueuing and dequeuing takes constant time.
The total time devoted to queue operations is O(|V |). The adjacency list of a vertex
is scanned after dequeuing that vertex, so each adjacency list is scanned at most once.
Summing the lengths of the adjacency lists, we have (|E|) and therefore we require
O(|E|) time to scan the adjacency lists. After the adjacency list of a vertex is scanned,
at most k edges are added to the list T , where k is the length of the adjacency list under
consideration. Like queue operations, appending to a list takes constant time, hence we
require O(|E|) time to build the list T . Therefore, BFS runs in O(|V | + |E|) time.
Theorem 2.3. For the list D resulting from Algorithm 2.1, let s be a starting vertex
and let v be a vertex such that D[v] = . Then D[v] is the length of any shortest path
from s to v.
Proof. It is clear that D[v] = if and only if there are no paths from s to v. Let v be
a vertex such that D[v] = . As v can be reached from s by a path of length D[v], the
length d(s, v) of any shortest s-v path satisfies d(s, v) D[v]. Use induction on d(s, v) to
show that equality holds. For the base case s = v, we have d(s, v) = D[v] = 0 since the
trivial path has length zero. Assume for induction that if d(s, v) = k, then d(s, v) = D[v].
Let d(s, u) = k + 1 with the corresponding shortest s-u path being (s, v1 , v2 , . . . , vk , u).
By our induction hypothesis, (s, v1 , v2 , . . . , vk ) is a shortest path from s to vk of length
d(s, vk ) = D[vk ] = k. In other words, D[vk ] < D[u] and the while loop spanning lines 5
to 11 processes vk before processing u. The graph under consideration has the edge vk u.
When examining the adjacency list of vk , BFS reaches u (if u is not reached earlier) and
so D[u] k + 1. Hence, D[u] = k + 1 and therefore d(s, u) = D[u] = k + 1.
In the proof of Theorem 2.3, we used d(u, v) to denote the length of the shortest path
from u to v. This shortest path length is also known as the distance from u to v, and
will be discussed in further details in section 2.3 and Chapter 5. The diameter diam(G)
of a graph G = (V, E) is defined as
diam(G) = max d(u, v).
u,vV
u=v
(2.5)
69
Using the above definition, to find the diameter we first determine the distance between
each pair of distinct vertices, then we compute the maximum of all such distances.
Breadth-first search is a useful technique for finding the diameter: we simply run breadthfirst search from each vertex. An interesting application of the diameter appears in the
small-world phenomenon [127, 156, 206], which contends that a certain special class of
sparse graphs have low diameter.
2.2.2
Depth-first search
70
0Z0Z0Z0Z
Z0Z0Z0Z0
0Z0Z0Z0Z
Z0Z0m0Z0
0Z0Z0Z0Z
Z0Z0Z0Z0
0Z0Z0Z0Z
Z0Z0Z0Z0
0Z0Z0Z0Z
Z0Z0Z0Z0
0Z0Z0Z0Z
Z0Z0Z0Z0
0Z0Z0Z0Z
Z0Z0Z0Z0
0Z0Z0Z0Z
Z0Z0Z0Z0
71
it might make sense to backtrack a few moves and try again, hoping we would not get
stuck. If we fail again, we try backtracking a few more moves and traverse yet another
path, hoping to make further progress. Repeat this strategy until a tour is found or until
we have exhausted all possible moves. The above strategy for finding a knights tour
is an example of depth-first search, sometimes called backtracking. Figure 2.5(b) shows
a knights tour with the starting position as shown in Figure 2.5(a); and Figure 2.5(c)
is a graph representation of this tour. The black-filled nodes indicate the endpoints
of the tour. A more interesting question is: What is the number of knights tours
on an 8 8 chessboard? Loebbing and Wegener [145] announced in 1996 that this
number is 33,439,123,484,294. The answer was later corrected by McKay [151] to be
13,267,364,410,532. See [73] for a discussion of the knights tour and its relationship to
mathematics.
Algorithm 2.2: A general depth-first search template.
Input: A directed or undirected graph G = (V, E) of order n > 0. A vertex s
from which to start the search. The vertices are numbered from 1 to
n = |V |, i.e. V = {1, 2, . . . , n}.
Output: A list D of distances of all vertices from s. A tree T rooted at s.
1
2
3
4
5
6
7
8
9
10
11
12
S [s]
/* stack of nodes to visit */
D [, , . . . , ]
/* n copies of */
D[s] 0
T []
while length(S) > 0 do
v pop(S)
for each w adj(v) do
if D[w] = then
D[w] D[v] + 1
push(S, w)
append(T, vw)
return (D, T )
Algorithm 2.2 formalizes the above description of depth-first search. The tree resulting from applying DFS on a graph is called a depth-first search tree. The general
structure of this algorithm bears close resemblance to Algorithm 2.1. A significant difference is that instead of using a queue to structure and organize vertices to be visited,
DFS uses another special type of list called a stack . To understand how elements of a
stack are organized, we use the analogy of a stack of cards. A new card is added to
the stack by placing it on top of the stack. Any time we want to remove a card, we
are only allowed to remove the top-most card that is on the top of the stack. A list
L = [a1 , a2 , . . . , ak ] of k elements is a stack when we impose the same rules for element
insertion and removal. The top and bottom of the stack are L[k] and L[1], respectively.
The operation of removing the top element of the stack is referred to as popping the
element off the stack. Inserting an element into the stack is called pushing the element
onto the stack. In other words, a stack implements a last-in first-out (LIFO) protocol
for element insertion and removal, in contrast to the FIFO policy of a queue. We also
use the term length to refer to the number of elements in the stack.
The depth-first search Algorithm 2.2 can be analyzed similar to how we analyzed
72
7
4
6
7
4
6
73
3
7
3
7
4
6
3
7
3
7
4
6
2
3
3
7
5
4
6
74
Algorithm 2.3. Just as BFS is applicable to both directed and undirected graphs, we
can also have undirected graphs and digraphs as input to DFS. For the case of an
undirected graph, line 7 of Algorithm 2.2 considers all vertices adjacent to the current
vertex v. In case the input graph is directed, we replace w adj(v) on line 7 with
w oadj(v) to signify that we only want to consider the out-neighbors of v. If any
neighbors (respectively, out-neighbors) of v are labelled as , we know that we have
not explored any paths starting from any of those vertices. So we label each of those
unexplored vertices with a positive integer and push them onto the stack S, where
they will wait for later processing. We also record the paths leading from v to each of
those unvisited neighbors, i.e. the edges vw for each vertex w adj(v) (respectively,
w oadj(v)) are appended to the list T . The test on line 8 ensures that we do not
push onto S any vertices on the path that lead to v. When we resume another round of
the while loop that starts on line 5, the previous vertex v have been popped off S and
the neighbors (respectively, out-neighbors) of v have been pushed onto S. For example,
in step 2 of Figure 2.6, vertex 5 is considered in DFS (in contrast to the vertex 2 in
step 2 of the BFS in the graph in Figure 2.3) because DFS is organized by the LIFO
protocol (in contrast to the FIFO protocol of BFS). To explore a path starting at v,
we choose any unexplored neighbors of v by popping an element off S and repeat the
for loop starting on line 7. Repeat the DFS algorithm as often as required in order to
traverse all vertices of the input graph. The output of DFS consists of two lists D and
T : T is a tree rooted at the starting vertex s; and each D[i] counts the length of the s-vi
path in T . Figures 2.6 and 2.7 show the DFS trees resulting from running Algorithm 2.2
on an undirected graph and a digraph, respectively. The worst-case time complexity of
DFS can be analyzed using an argument similar to that in Theorem 2.2. Arguing along
the same lines as in the proof of Theorem 2.3, we can also show that the list D returned
by DFS contains lengths of any shortest paths in the tree T from the starting vertex s
to any other vertex in T (but not necessarily for shortest paths in the original graph G).
0
5
1
4
6
Example 2.4. In 1898, Julius Petersen published [170] a graph that now bears his name:
the Petersen graph shown in Figure 2.8. Compare the search trees resulting from running
breadth- and depth-first searches on the Petersen graph with starting vertex 0.
Solution. The Petersen graph in Figure 2.8 can be constructed and searched as follows.
75
From the above Sage session, we see that starting from vertex 0 breadth-first search
yields the edge list
[01, 04, 05, 12, 16, 43, 49, 57, 58]
and depth-first search produces the corresponding edge list
[05, 58, 86, 69, 97, 72, 23, 34, 01].
Our results are illustrated in Figure 2.9.
0
4
6
2.2.3
Connectivity of a graph
Both BFS and DFS can be used to determine if an undirected graph is connected. Let
G = (V, E) be an undirected graph of order n > 0 and let s be an arbitrary vertex
of G. We initialize a counter c 1 to mean that we are starting our exploration at
s, hence we have already visited one vertex, i.e. s. We apply either BFS or DFS,
treating G and s as input to any of these algorithms. Each time we visit a vertex that
was previously unvisited, we increment the counter c. At the end of the algorithm, we
compare c with n. If c = n, we know that we have visited all vertices of G and conclude
that G is connected. Otherwise, we conclude that G is disconnected. This procedure is
summarized in Algorithm 2.3.
Note that Algorithm 2.3 uses the BFS template of Algorithm 2.1, with some minor
changes. Instead of initializing the list D with n = |V | copies of , we use n copies of
0. Each time we have visited a vertex w, we make the assignment D[w] 1, instead
of incrementing the value D[v] of ws parent vertex and assign that value to D[w]. At
the end of the while loop, we have the equality c =
dD d. The value of this sum
could be used in the test starting from line 12. However, the value of the counter c
is incremented immediately after we have visited an unvisited vertex. An advantage is
76
Q [s]
/* queue of nodes to visit */
D [0, 0, . . . , 0]
/* n copies of 0 */
D[s] 1
c1
while length(Q) > 0 do
v dequeue(Q)
for each w adj(v) do
if D[w] = 0 then
D[w] 1
cc+1
enqueue(Q, w)
if c = |V | then
return True
return False
that we do not need to perform a separate summation outside of the while loop. To
use the DFS template for determining graph connectivity, we simply replace the queue
implementation in Algorithm 2.3 with a stack implementation (see problem 2.20).
2.3
77
metric function. Intuitively, the distance between two physical locations is greater than
zero. When these two locations coincide, i.e. they are one and the same location, the
distance separating them is zero. Regardless of whether we are measuring the distance
from location a to b or from b to a, we would obtain the same distance. Imagine now
a third location c. The distance from a to b plus the distance from b to c is greater
than or equal to the distance from a to c. The latter principle is known as the triangle
inequality. In summary, given three vertices u, v, w in a graph G, the distance function
d on G satisfies the following property.
Lemma 2.5. Path distance as metric function. Let G = (V, E) be a graph with
weight function w : E R. Define a distance function d : V V R given by
d(u, v) =
78
D [, , . . . , ]
/* n copies of */
C list of candidate vertices to visit
while length(C) > 0 do
select v C
C remove(C, v)
for each u adj(v) do
if D[u] > D[v] + w(vu) then
D[u] D[v] + w(vu)
P [u] v
if u
/ C then
add u to C
return (D, P )
Having defined weights and distances, we are now ready to discuss shortest path
algorithms for weighted graphs. The breadth-first search Algorithm 2.1 can be applied
where each edge has unit weight. Moving on to the general case of graphs with positive
edge weights, algorithms for determining shortest paths in such graphs can be classified
as weight-setting or weight-correcting [87]. A weight-setting method traverses a graph
and assigns weights that, once assigned, remain unchanged for the duration of the algorithm. Weight-setting algorithms cannot deal with negative weights. On the other
hand, a weight-correcting method is able to change the value of a weight many times
while traversing a graph. In contrast to a weight-setting algorithm, a weight-correcting
algorithm is able to deal with negative weights, provided that the weight sum of any
cycle is nonnegative. The term negative cycle refers to the weight sum s of a cycle such
that s < 0. Some algorithms halt upon detecting a negative cycle; examples of such
algorithms include the Bellman-Ford and Johnsons algorithms.
Algorithm 2.4 is a general template for many shortest path algorithms. With a tweak
here and there, one could modify it to suit the problem at hand. Note that w(vu) is the
weight of the edge vu. If the input graph is undirected, line 6 considers all the neighbors
of v. For digraphs, we are interested in out-neighbors of v and accordingly we replace
u adj(v) in line 6 with u oadj(v). The general flow of Algorithm 2.4 follows the
same pattern as depth-first and breadth-first searches.
2.4
79
Dijkstras algorithm
Dijkstras algorithm [65], discovered by E. W. Dijkstra in 1959, is a graph search algorithm that solves the single-source shortest path problem for a graph with nonnegative
edge weights. The algorithm is a generalization of breadth-first search. Imagine that
the vertices of a weighted graph represent cities and edge weights represent distances
between pairs of cities connected by a direct road. Dijkstras algorithm can be used to
find a shortest route from a fixed city to any other city.
Let G = (V, E) be a (di)graph with nonnegative edge weights. Fix a start or source
vertex s V . Dijkstras Algorithm 2.5 performs a number of steps, basically one step
for each vertex in V . First, we initialize a list D with n copies of and then assign 0 to
D[s]. The purpose of the symbol is to denote the largest possible value. The list D is
to store the distances of all shortest paths from s to any other vertices in G, where we
take the distance of s to itself to be zero. The list P of parent vertices is initially empty
and the queue Q is initialized to all vertices in G. We now consider each vertex in Q,
removing any vertex after we have visited it. The while loop starting on line 5 runs until
we have visited all vertices. Line 6 chooses which vertex to visit, preferring a vertex v
whose distance value D[v] from s is minimal. After we have determined such a vertex v,
we remove it from the queue Q to signify that we have visited v. The for loop starting
on line 8 adjusts the distance values of each neighbor u of v such that u is also in Q. If
G is directed, we only consider out-neighbors of v that are also in Q. The conditional
starting on line 9 is where the adjustment takes place. The expression D[v] + w(vu)
sums the distance from s to v and the distance from v to u. If this total sum is less than
the distance D[u] from s to u, we assign this lesser distance to D[u] and let v be the
parent vertex of u. In this way, we are choosing a neighbor vertex that results in minimal
distance from s. Each pass through the while loop decreases the number of elements in
Q by one without adding any elements to Q. Eventually, we would exit the while loop
and the algorithm returns the lists D and P .
Example 2.7. Apply Dijkstras algorithm to the graph in Figure 2.10(a), with starting
vertex v1 .
Solution. Dijkstras Algorithm 2.5 applied to the graph in Figure 2.10(a) yields the
sequence of intermediary graphs shown in Figure 2.10, culminating in the final shortest
paths graph of Figure 2.10(f) and Table 2.4. For any column vi in the table, each 2-tuple
represents the distance and parent vertex of vi . As we move along the graph, processing
vertices according to Dijkstras algorithm, the distance and parent vertex of a column
80
D [, , . . . , ]
/* n copies of */
D[s] 0
P []
QV
/* list of nodes to visit */
while length(Q) > 0 do
find v Q such that D[v] is minimal
Q remove(Q, v)
for each u adj(v) Q do
if D[u] > D[v] + w(vu) then
D[u] D[v] + w(vu)
P [u] v
return (D, P )
2
v2
v4
v2
10
v4
1
10
v1
v3
v5
v1
v3
v2
v4
v2
10
v4
1
10
v1
v3
v5
v1
9
v3
v5
2
v2
v4
v2
10
v5
v4
7
8
4
v1
9
v3
4
v5
v1
v3
3
v5
2
81
v1
(0, )
v2
v3
(, ) (, )
(10, v1 ) (3, v1 )
(7, v3 )
v4
v5
(, ) (, )
(11, v3 ) (5, v3 )
(9, v2 )
d(v1 , v3 ) = 3
v1 -v4 : v1 , v3 , v2 , v4
d(v1 , v4 ) = 9
v1 -v5 : v1 , v3 , v5
d(v1 , v5 ) = 5
Intermediary vertices for a u-v path are obtained by starting from v and work backward
using the parent of v, then the parent of the parent, and so on.
Dijkstras algorithm is an example of a greedy algorithm. Whenever it tries to find the
next vertex, it chooses only that vertex that minimizes the total weight so far. Greedy
algorithms may not produce the best possible result. However, as the following theorem
shows, Dijkstras algorithm does indeed produce shortest paths.
Theorem 2.8. Correctness of Algorithm 2.5. Let G = (V, E) be a weighted
(di)graph with a nonnegative weight function w. When Dijkstras algorithm is applied to
G with source vertex s V , the algorithm terminates with D[u] = d(s, u) for all u V .
Furthermore, if D[v] = and v = s, then s = u1 , u2 , . . . , uk = v is a shortest s-v path
such that ui1 = P [ui ] for i = 2, 3, . . . , k.
Proof. If G is disconnected, then any v V that cannot be reached from s has distance
D[v] = upon algorithm termination. Hence, it suffices to consider the case where G
is connected. Let V = {s = v1 , v2 , . . . , vn } and use induction on i to show that after
visiting vi we have
D[v] = d(s, v)
(2.6)
For i = 1, equality holds. Assume for induction that (2.6) holds for some 1 i n 1,
so that now our task is to show that (2.6) holds for i + 1. To verify D[vi+1 ] = d(s, vi+1 ),
note that by our inductive hypothesis,
D[vi+1 ] = min {d(s, v) + w(vu) | v Vi and u adj(v) (Q\Vi )}
and respectively
D[vi+1 ] = min {d(s, v) + w(vu) | v Vi and u oadj(v) (Q\Vi )}
if G is directed. Therefore, D[vi+1 ] = d(s, vi+1 ).
Let v V such that D[v] = and v = s. We now construct an s-v path. When
Algorithm 2.5 terminates, we have D[v] = D[v1 ] + w(v1 v), where P [v] = v1 and d(s, v) =
d(s, v1 ) + w(v1 v). This means that v1 is the second-to-last vertex in a shortest s-v path.
Repeated application of this process using the parent list P , we eventually produce a
shortest s-v path s = vm , vm1 , . . . , v1 , v, where P [vi ] = vi+1 for i = 1, 2, . . . , m 1.
82
To analyze the worst case time complexity of Algorithm 2.5, note that initializing D
takes O(n + 1) and initializing Q takes O(n), for a total of O(n) devoted to initialization.
Each extraction of a vertex v with minimal D[v] requires O(n) since we search through
the entire list Q to determine the minimum value, for a total of O(n2 ). Each insertion
into D requires constant time and the same holds for insertion into P . Thus, insertion
into D and P takes O(|E| + |E|) = O(|E|), which require at most O(n) time. In the
worst case, Dijkstras Algorithm 2.5 has running time O(n2 + n) = O(n2 ).
Can we improve the run time of Dijkstras algorithm? The time complexity of Dijkstras algorithm depends on its implementation. With a simple list implementation as
presented in Algorithm 2.5, we have a worst case time complexity of O(n2 ), where n is
the order of the graph under consideration. Let m be the size of the graph. Table 2.5
presents time complexities of Dijkstras algorithm for various implementations. Out of
all the four implementations in this table, the heap implementations are much more
efficient than the list implementation presented in Algorithm 2.5. A heap is a type of
tree, a topic which will be covered in Chapter 3. Of all the heap implementations in
Table 2.5, the Fibonacci heap implementation [86] yields the best runtime. Chapter 4
discusses how to use trees for efficient implementations of priority queues via heaps.
Implementation Time complexity
list
O(n2 )
binary heap
O (n + m) ln n
k-ary heap
n
O (kn + m) ln
ln k
Fibonacci heap
O(n ln n + m)
2.5
Bellman-Ford algorithm
A disadvantage of Dijkstras Algorithm 2.5 is that it cannot handle graphs with negative
edge weights. The Bellman-Ford algorithm computes single-source shortest paths in
a weighted graph or digraph, where some of the edge weights may be negative. This
algorithm is a modification of the one published in 1957 by Richard E. Bellman [22] and
that by Lester Randolph Ford, Jr. [83] in 1956. Shimbel [181] independently discovered
the same method in 1955, and Moore [158] in 1959. In contrast to the greedy approach
that Dijkstras algorithm takes, i.e. searching for the cheapest path, the Bellman-Ford
algorithm searches over all edges and keeps track of the shortest one found as it searches.
The Bellman-Ford Algorithm 2.6 runs in time O(mn), where m and n are the size
and order of an input graph, respectively. To see this, note that the initialization on
83
D [, , . . . , ]
D[s] 0
P []
for i 1, 2, . . . , n 1 do
for each edge uv E do
if D[v] > D[u] + w(uv) then
D[v] D[u] + w(uv)
P [v] u
for each edge uv E do
if D[v] > D[u] + w(uv) then
return False
return (D, P )
/* n copies of */
lines 1 to 3 takes O(n). Each of the n 1 rounds of the for loop starting on line 4 takes
O(m), for a total of O(mn) time. Finally, the for loop starting on line 9 takes O(m).
The loop starting on line 4 performs at most n 1 updates of the distance D[v] of
each head of an edge. Many graphs have sizes that are less then n 1, resulting in
a number of redundant rounds of updates. To avoid such redundancy, we could add
an extra check in the outer loop spanning lines 4 to 8 to immediately terminate that
outer loop after any round that did not result in an update of any D[v]. Algorithm 2.7
presents a modification of the Bellman-Ford Algorithm 2.6 that avoids redundant rounds
of updates.
2.6
Floyd-Roy-Warshall algorithm
The shortest distance between two points is not a very interesting journey.
R. Goldberg
Let D be a weighted digraph of order n and size m. Dijkstras Algorithm 2.5 and
the Bellman-Ford Algorithm 2.6 can be used to determine shortest paths from a single
source vertex to all other vertices of D. To determine a shortest path between each pair
of distinct vertices in D, we repeatedly apply either of these algorithms to each vertex
of D. Such repeated application of Dijkstras and the Bellman-Ford algorithms results
in algorithms that run in time O(n3 ) and O(n2 m), respectively.
The Floyd-Roy-Warshall algorithm (FRW), or the Floyd-Warshall algorithm, is an
algorithm for finding shortest paths in a weighted, directed graph. Like the BellmanFord algorithm, it allows for negative edge weights and detects a negative weight cycle
if one exists. Assuming that there are no negative weight cycles, a single execution of
84
Algorithm 2.7: The Bellman-Ford algorithm with checks for redundant updates.
Input: An undirected or directed graph G = (V, E) that is weighted and has no
self-loops. Negative edge weights are allowed. The order of G is n > 0. A
vertex s V from which to start the search. Vertices are numbered from
1 to n, i.e. V = {1, 2, . . . , n}.
Output: A list D of distances such that D[v] is the distance of a shortest path
from s to v. A list P of vertex parents such that P [v] is the parent of v,
i.e. v is adjacent from P [v]. If G has negative-weight cycles, then return
False. Otherwise, return D and P .
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
D [, , . . . , ]
D[s] 0
P []
for i 1, 2, . . . , n 1 do
updated False
for each edge uv E do
if D[v] > D[u] + w(uv) then
D[v] D[u] + w(uv)
P [v] u
updated True
if updated = False then
exit the loop
for each edge uv E do
if D[v] > D[u] + w(uv) then
return False
return (D, P )
/* n copies of */
85
the FRW algorithm will find the shortest paths between all pairs of vertices. It was
discovered independently by Bernard Roy [176] in 1959, Robert Floyd [82] in 1962, and
by Stephen Warshall [202] in 1962.
In some sense, the FRW algorithm is an example of dynamic programming, which
allows one to break the computation into simpler steps using some sort of recursive
procedure. The rough idea is as follows. Temporarily label the vertices of a weighted
digraph G as V = {1, 2, . . . , n} with n = |V (G)|. Let W = [w(i, j)] be the weight matrix
of G where
w(ij), if ij E(G),
w(i, j) = 0,
(2.7)
if i = j,
,
otherwise.
Let Pk (i, j) be a shortest path from i to j such that its intermediate vertices are in
{1, 2, . . . , k}. Let Dk (i, j) be the weight (or distance) of Pk (i, j). If no shortest i-j
paths exist, define Pk (i, j) = and Dk (i, j) = for all k {1, 2, . . . , n}. If k = 0,
then P0 (i, j) : i, j since no intermediate vertices are allowed in the path and hence
D0 (i, j) = w(i, j). In other words, if i and j are adjacent, a shortest i-j path is the
edge ij itself and the weight of this path is simply the weight of ij. Now consider
Pk (i, j) for k > 0. Either Pk (i, j) passes through k or it does not. If k is not on the
path Pk (i, j), then the intermediate vertices of Pk (i, j) are in {1, 2, . . . , k 1}, as are
the vertices of Pk1 (i, j). In case Pk (i, j) contains the vertex k, then Pk (i, j) traverses
k exactly once by the definition of path. The i-k subpath in Pk (i, j) is a shortest i-k
path whose intermediate vertices are drawn from {1, 2, . . . , k 1}, which is also the set
of intermediate vertices for the k-j subpath in Pk (i, j). That is, to obtain Pk (i, j), we
take the union of the paths Pk1 (i, k) and Pk1 (k, j). We compute the weight Dk (i, j)
of Pk (i, j) using the expression
Dk (i, j) =
w(i, j),
if k = 0,
(2.8)
86
n |V |
P [aij ] an n n zero matrix
D[aij ] W [w(i, j)]
for k 1, 2, . . . , n do
for i 1, 2, . . . , n do
for j 1, 2, . . . , n do
if D[i, j] > D[i, k] + D[k, j] then
P [i, j] k
D[i, j] D[i, k] + D[k, j]
return (P, D)
87
The plot of this weighted digraph with four vertices appears in Figure 2.11.
1
The plot of this weighted digraph with four vertices appears in Figure 2.12.
Example 2.9. Section 1.6 briefly presented the concept of molecular graphs in chemistry. The Wiener number of a molecular graph was first published in 1947 by Harold
Wiener [209] who used it in chemistry to study properties of alkanes. Other applications [98] of the Wiener number to chemistry are now known. If G = (V, E) is a
connected graph with vertex set V = {v1 , v2 , . . . , vn }, then the Wiener number W of G is
defined by
W (G) =
d(vi , vj )
(2.9)
i<j
where d(vi , vj ) is the distance from vi to vj . What is the Wiener number of the molecular
graph in Figure 2.13?
Solution. Consider the molecular graph in Figure 2.13 as directed with unit weight.
To compute the Wiener number of this graph, use the Floyd-Roy-Warshall algorithm to
obtain a distance matrix D = [di,j ], where di,j is the distance from vi to vj , and apply the
definition (2.9). The distance matrix resulting from the Floyd-Roy-Warshall algorithm
88
0.5
1
3
0
2
M =
2
3
2
4
89
2
0
1
2
3
2
4
1
1
0
1
2
1
3
2
2
1
0
1
2
2
3
3
2
1
0
1
1
2
2
1
2
1
0
2
4
4
2
.
1
2
0
Sum all entries in the upper (or lower) triangular of M to obtain the Wiener number
W = 42. Using Sage, we have
sage :
sage :
sage :
sage :
sage :
sage :
...
...
sage :
42
G =
D =
M =
M =
W =
for
which verifies our computation above. See [98] for a survey of some results concerning
the Wiener number.
2.6.1
Transitive closure
transitive closure G answers an important question about G: If u and v are two distinct
vertices of G, are they connected by a path with length 1?
To compute the transitive closure of G, we let each edge of G be of unit weight and
apply the Floyd-Roy-Warshall Algorithm 2.8 on G. By Proposition 1.13, for any i-j path
in G we have D[i, j] < n, and if there are no paths from i to j in G, we have D[i, j] = .
This procedure for computing transitive closure runs in time O(n3 ).
Modifying the Floyd-Roy-Warshall algorithm slightly, we obtain an algorithm for
computing transitive closure that, in practice, is more efficient than Algorithm 2.8 in
terms of time and space. Instead of using the operations min and + as is the case in the
Floyd-Roy-Warshall algorithm, we replace these operations with the logical operations
(logical OR) and (logical AND), respectively. For i, j, k = 1, 2, . . . , n, define Tk (i, j) = 1
if there is an i-j path in G with all intermediate vertices belonging to {1, 2, . . . , k}, and
Tk (i, j) = 0 otherwise. Thus, the edge ij belongs to the transitive closure G if and only
if Tk (i, j) = 1. The definition of Tk (i, j) can be cast in the form of a recursive definition
as follows. For k = 0, we have
T0 (i, j) =
0, if i = j and ij
/ E,
1, if i = j or ij E
90
n |V |
T adjacency matrix of G
for k 1, 2, . . . , n do
for i 1, 2, . . . , n do
for j 1, 2, . . . , n do
T [i, j] T [i, j] T [i, k] T [k, j]
return T
2.7
Johnsons algorithm
The shortest distance between two points is under construction.
Noelie Altito
Let G = (V, E) be a sparse digraph with edge weights but no negative cycles. Johnsons
algorithm [118] finds a shortest path between each pair of vertices in G. First published
in 1977 by Donald B. Johnson, the main insight of Johnsons algorithm is to combine
the technique of edge reweighting with the Bellman-Ford and Dijkstras algorithms. The
Bellman-Ford algorithm is first used to ensure that G has no negative cycles. Next,
we reweight edges in such a manner as to preserve shortest paths. The final stage
makes use of Dijkstras algorithm for computing shortest paths between all vertex pairs.
Pseudocode for Johnsons algorithm is presented in Algorithm 2.10. With a Fibonacci
heap implementation of the minimum-priority queue, the time complexity for sparse
graphs is O(|V |2 log |V | + |V | |E|), where n = |V | is the number of vertices of the
original graph G.
To prove the correctness of Algorithm 2.10, we need to show that the new set of edge
weights produced by w must satisfy two properties:
1. The reweighted edges preserve shortest paths. That is, let p be a u-v path for
u, v V . Then p is a shortest weighted path using weight function w if and only
if p is also a shortest weighted path using weight function w.
0.
91
s vertex not in V
V V {s}
E E {sv | v V }
G digraph (V , E ) with weight w(sv) = 0 for all v V
if BellmanFord(G , w, s) = False then
return False
d distance list returned by BellmanFord(G , w, s)
for each edge uv E do
w(uv)
P [u] P
for each v V do
+ d[v] d[u]
D[u, v] [v]
return (D, P )
2. The graph G has a negative cycle using weight function w if and only if G has a
negative cycle using w.
0 for all uv E.
Proof. Write and for the shortest path weights derived from w and w,
respectively.
0 , vk ).
To prove part 1, we need to show that w(p) = (v0 , vk ) if and only if w(p)
= (v
92
w(p)
w(v
i1 vi )
i=1
k
=
i=1
k
w(vi1 vi ) +
i=1
k
=
i=1
i=1
h(vi1 ) h(vi )
= (v0 , vk ).
To prove part 2, consider any cycle c : v0 , v1 , . . . , vk where v0 = vk . Using the proof
of part 1, we have
w(c)
= w(c) + h(v0 ) h(vk )
= w(c)
thus showing that c is a negative cycle using w if and only if it is a negative cycle using
w.
2.8
Problems
I believe that a scientist looking at nonscientific problems is just as dumb as the next guy.
Richard Feynman
2.1. The Euclidean algorithm is one of the oldest known algorithms. Given two positive
integers a and b with a b, let a mod b be the remainder obtained upon dividing
a by b. The Euclidean algorithm determines the greatest common divisor gcd(a, b)
of a and b. The procedure is summarized in Algorithm 2.11. Refer to [52] for a
history of algorithms from ancient to modern times.
(a) Implement Algorithm 2.11 in Sage and use your implementation to compute
the greatest common divisors of various pairs of integers. Use the built-in
Sage command gcd to verify your answer.
2.8. Problems
93
xa
yb
while y = 0 do
r x mod y
xy
yr
return x
(b) Modify Algorithm 2.11 to compute the greatest common divisor of any pair
of integers.
2.2. Given a polynomial p(x) = an xn + an1 xn1 + + a1 x + a0 of degree n, we can use
Horners method [109] to efficiently evaluate p at a specific value x = x0 . Horners
method evaluates p(x) by expressing the polynomial as
n
p(x) =
i=0
ai xi = ( (an x + an1 )x + )x + a0
b an
for i n 1, n 2, . . . , 0 do
b bx0 + ai
return b
2.3. Let G = (V, E) be an undirected graph, let s V , and D is the list of distances
resulting from running Algorithm 2.1 with G and s as input. Show that G is
connected if and only if D[v] is defined for each v V .
2.4. Show that the worst-case time complexity of depth-first search Algorithm 2.2 is
O(|V | + |E|).
94
2.5. Let D be the list of distances returned by Algorithm 2.2, let s be a starting vertex,
and let v be a vertex such that D[v] = . Show that D[v] is the length of any
shortest path from s to v.
2.6. Consider the graph in Figure 2.10 as undirected. Run this undirected version
through Dijkstras algorithm with starting vertex v1 .
1
v3
v4
2
3
1
1
v5
3
3
v2
v1
2.8. Problems
95
for each e L do
if E = e then
return True
return False
low 0
high |L| 1
while low high do
mid low +2 high
if i = L[mid] then
return True
if i < L[mid] then
high mid 1
else
low mid + 1
return False
96
2.11. Let G be a simple undirected graph having distance matrix D = [d(vi , vj )], where
d(vi , vj ) R denotes the shortest distance from vi V (G) to vj V (G). If
vi = vj , we set d(vi , vj ) = 0. For each pair of distinct vertices (vi , vj ), we have
d(vi , vj ) = d(vj , vi ). The i-j entry of D is also written as di,j and denotes the entry
in row i and column j.
(a) The total distance td(u) of a fixed vertex u V (G) is the sum of distances
from u to each vertex in G:
td(u) =
d(u, v).
vV (G)
di,k =
k
dk,j .
(2.10)
di,j =
i<j
di,j =
i>j
1
2
d(u, v) .
(2.11)
uV vV
Hence show that the total distance of G is equal to its Wiener number:
td(G) = W (G).
2.8. Problems
97
(c) Would equations (2.10) and (2.11) hold if G is not connected or directed?
2.12. The following result is from [215]. Let G1 and G2 be graphs with orders ni =
|V (Gi )| and sizes mi = |E(Gi )|, respectively.
(a) If each of G1 and G2 is connected, show that the Wiener number of the
Cartesian product G1 G2 is
W (G1 G2 ) = n22 W (G1 ) + n21 W (G2 ).
(b) If G1 and G2 are arbitrary graphs, show that the Wiener number of the join
G1 + G2 is
W (G1 + G2 ) = n21 n1 + n22 n2 + n1 n2 m1 m2 .
2.13. The following results originally appeared in [74] and independently rediscovered
many times since.
(a) If Pn is the path graph on n 0 vertices, show that the Wiener number of
Pn is W (Pn ) = 61 n(n2 1).
(b) If Cn is the cycle graph on n 0 vertices, show that the Wiener number of
Cn is
1
n(n2 1), if n is odd,
8
W (Cn ) =
1 3
n,
if n is even.
8
(c) If Kn is the complete graph on n vertices, show that its Wiener number is
W (Kn ) = 21 n(n 1).
(d) Show that the Wiener number of the complete bipartite graph Km,n is
W (Km,n ) = mn + m(m 1) + n(n 1).
2.14. Consider the world map of major capital cities in Figure 2.16.
(a) Run breadth- and depth-first searches over the graph in Figure 2.16 and compare your results.
(b) Convert the graph in Figure 2.16 to a digraph as follows. Let 0 1 be a
fixed threshold probability and let V = {v1 , . . . , vn } be the vertex set of the
graph. For each edge vi vj , let 0 p 1 be its orientation probability and
define the directedness dir(vi , vj ) by
dir(vi , vj ) =
vi vj , if p ,
vj vi , otherwise.
That is, dir(vi , vj ) takes the endpoints of an undirected edge vi vj and returns
a directed version of this edge. The result is either the directed edge vi vj
or the directed edge vj vi . Use the above procedure to convert the graph of
Figure 2.16 to a digraph, and run breadth- and depth-first searches over the
resulting digraph.
98
2.15. Consider a finite square lattice where points on can connect to other points
in their von Neumann or Moore neighborhoods, or other points in the lattice. See
Figures 2.15(a) and 2.15(b) for illustrations of the von Neumann and Moore neighborhoods of a central node, respectively. We treat each point as a node and forbid
each node from connecting to itself. The whole lattice can be considered as a simple undirected graph . Let n be the maximum number of nodes in any connected
components. For each i {1, 2, . . . , n}, how many connected components have
exactly n nodes? In other words, what is the density of connected components in
? For example, consider the lattice in Figure 2.15(c). Denote by (i) the number
of connected components with i nodes. Then the maximum number of nodes of
any connected components is n = 6, with (1) = 2, (2) = 2, (3) = 2, (4) = 1,
(5) = 0, and (6) = 1.
Lima
Washington DC
Ottawa
Pretoria
Berlin
New Delhi
Moscow
Buenos Aires
Brasilia
Madrid
London
Bangkok
Beijing
Sydney
Tokyo
2.8. Problems
99
3154
Brasilia
Buenos Aires
Washington DC
Tokyo
Sydney
Pretoria
Ottawa
7540
1619
2103
6764
7906
3154
Lima
5915
5379
1261
929
London
8033
7288
1261
1866
Madrid
1619
5807
Moscow
7288
5796
3784
New Delhi
2908
734
5379
Ottawa
8033
7906
Pretoria
5796
Moscow
3784
1866
Madrid
New Delhi
929
London
Lima
2314
Berlin
2314
5807
Beijing
3282
Buenos Aires
2908
3282
Bangkok
Brasilia
Berlin
Beijing
Bangkok
Sydney
7540
2103
Tokyo
734
5915
6764
Washington DC
100
Chapter 2. Graph algorithms
2.8. Problems
101
2.16. Various efficient search techniques exist that cater for special situations. Some of
these are covered in chapter 6 in [130] and chapters 1418 in [178]. Investigate an
algorithm for and time complexity of trie search. Hashing techniques can result in
searches that run in O(1) time. Furthermore, hashing has important applications
outside of the searching problem, a case in point being cryptology. Investigate how
hashing can be used to speed up searches. For further information on hashing and
its application to cryptology, see [153, 187, 193].
2.17. In addition to searching, there is the related problem of sorting a list according to
an ordering relation. If the given list L = [e1 , e2 , . . . , en ] consists of real numbers,
we want to order elements in nondecreasing order. Bubble sort is a basic sorting
algorithm that can be used to sort a list of real numbers, indeed any collection of
objects that can be ordered according to an ordering relation. During each pass
through the list L from left to right, we consider ei and its right neighbor ei+1 . If
ei ei+1 , then we move on to consider ei+1 and its right neighbor ei+2 . If ei > ei+1 ,
then we swap these two values around in the list and then move on to consider ei+1
and its right neighbor ei+2 . Each successive pass pushes to the right end an element
that is the next largest in comparison to the previous largest element pushed to
the right end. Hence the name bubble sort for the algorithm. Algorithm 2.15
summarizes our discussion.
Algorithm 2.15: Bubble sort.
Input: A list L of n > 1 elements that can be ordered using the less than or
equal to relation .
Output: The same list as L, but sorted in nondecreasing order.
1
2
3
4
5
for i n, n 1, . . . , 2 do
for j 2, 3, . . . , i do
if L[j 1] > L[j] then
swap the values of L[j 1] and L[j]
return L
102
ta
ab
bt
and exchange it with L[1]. On the second scan, we find the smallest element
among L[3], . . . , L[n] and exchange that smallest element with L[2]. In general,
during the i-th scan we find the smallest element among L[i + 1], . . . , L[n] and
exchange that with L[i]. At the end of the i-th scan, the element L[i] is in its final
position and would not be processed again. When the index reaches i = n, the list
would have been sorted in nondecreasing order. The procedure is summarized in
Algorithm 2.17.
(a) Analyze the worst-case runtime of Algorithm 2.17 and compare your result to
the worst-case runtime of the bubble sort Algorithm 2.15.
(b) Modify Algorithm 2.17 to sort elements in nonincreasing order.
(c) Line 6 of Algorithm 2.17 assumes that among L[i+1], L[i+2], . . . , L[n] there is
a smallest element L[k] such that L[i] > L[k], hence we perform the swap. It is
possible that L[i] < L[k], obviating the need to carry out the value swapping.
Modify Algorithm 2.17 to take account of our discussion.
for i 1, 2, . . . , n 1 do
min i
for j i + 1, i + 2, . . . , n do
if L[j] < L[min] then
min j
swap the values of L[min] and L[i]
return L
2.19. In addition to bubble and selection sort, other algorithms exist whose runtime is
more efficient than these two basic sorting algorithms. Chapter 5 in [130] describes
various efficient sorting techniques. See also chapters 813 in [178].
(a) Investigate and provide pseudocode for insertion sort and compare its runtime
efficiency with that of selection sort. Compare the similarities and differences
between insertion and selection sort.
(b) Shellsort is a variation on insertion sort that can speed up the runtime of
insertion sort. Describe and provide pseudocode for shellsort. Compare the
time complexity of shellsort with that of insertion sort. In what ways is
shellsort different from or similar to insertion sort?
2.8. Problems
103
(c) The quicksort algorithm due to C. A. R. Hoare [104] was published in 1962.
Describe and provide pseudocode for quicksort and compare its runtime complexity with the other sorting algorithms covered in this chapter.
2.20. Algorithm 2.3 uses breadth-first search to determine the connectivity of an undirected graph. Modify this algorithm to use depth-first search. How can Algorithm 2.3 be used or modified to test the connectivity of a digraph?
2.21. The following problem is known as the river crossing problem. A man, a goat,
a wolf, and a basket of cabbage are all on one side of a river. They have a boat
that could be used to cross to the other side of the river. The boat can only hold
at most two passengers, one of whom must be able to row the boat. One of the
two passengers must be the man and the other passenger can be either the goat,
the wolf, or the basket of cabbage. When crossing the river, if the man leaves the
wolf with the goat, the wolf would prey on the goat. If he leaves the goat with the
basket of cabbage, the goat would eat the cabbage. The objective is to cross the
river in such a way that the wolf has no chance of preying on the goat, nor that
the goat eat the cabbage.
(a) Let M , G, W , and C denote the man, the goat, the wolf, and the basket of
cabbage, respectively. Initially all four are on the left side of the river and none
of them are on the right side. Denote this by the ordered pair (M GW C, w),
which is called the initial state of the problem. When they have all crossed to
the right side of the river, the final state of the problem is (w, M GW C). The
underscore w means that neither M , G, W , nor C are on the corresponding
side of the river. List a finite sequence of moves to get from (M GW C, w) to
(w, M GW C). Draw your result as a digraph.
(b) In the digraph obtained from the previous exercise, let each edge of be of
unit weight. Find a shortest path from (M GW C, w) to (w, M GW C).
(c) Rowing from one side of the river to the other side is called a crossing. What
is the minimum number of crossings needed to get from (M GW C, w) to
(w, M GW C)?
2.22. Symbolic computation systems such as Magma, Maple, Mathematica, Maxima,
and Sage are able to read in a symbolic expression such as
(a + b)^2 - (a - b)^2
and determine whether or not the brackets match. A bracket is any of the following
characters:
( ) { } [ ]
A string S of characters is said to be balanced if any left bracket in S has a corresponding right bracket that is also in S. Furthermore, if there are k occurrences
of one type of left bracket, then there must be k occurrences of the corresponding
right bracket. The balanced bracket problem is concerned with determining whether
or not the brackets in S are balanced. Algorithm 2.18 contains a procedure to determine if the brackets in S are balanced, and if so return a list of positive integers
to indicate how the brackets match.
104
L []
T empty stack
c1
n |S|
for i 0, 1, . . . , n do
if S[i + 1] is a left bracket then
append(L, c)
push (S[i + 1], c) onto T
cc+1
if S[i + 1] is a right bracket then
if T is empty then
return
(left, d) pop(T )
if left matches S[i + 1] then
append(L, d)
else
return
if T is empty then
return L
return
2.8. Problems
105
pop is assigned to a. Compute the infix expression a x b and push the result onto
E. However, if x is an operand, we push x onto E. Iterate the above process until
P is empty, at which point the top of E contains the evaluation of A. Refer to
Algorithm 2.19 for pseudocode of the above discussion.
(a) Prove the correctness of Algorithm 2.19.
(b) What is the worst-case runtime of Algorithm 2.19?
(c) Modify Algorithm 2.19 to support the exponentiation operator.
Algorithm 2.19: Evaluate arithmetic expressions in reverse Polish notation.
Input: A Polish stack P containing an arithmetic expression in reverse Polish
notation.
Output: An evaluation of the arithmetic expression represented by P .
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
E empty stack
v NULL
while P is not empty do
x pop(P )
if x is an operator then
b pop(E)
a pop(E)
if x is addition operator then
v a+b
else if x is subtraction operator then
v ab
else if x is multiplication operator then
v ab
else if x is division operator then
v a/b
else
exit algorithm with error
push(E, v)
else
push(E, x)
v pop(E)
return v
2.24. Figure 2.5 provides a knights tour for the knight piece with initial position as
in Figure 2.5(a). By rotating the chessboard in Figure 2.5(b) by 90n degrees for
positive integer values of n, we obtain another knights tour that, when represented
as a graph, is isomorphic to the graph in Figure 2.5(c).
(a) At the beginning of the 18th century, de Montmort and de Moivre provided
the following strategy [13, p.176] to solve the knights tour problem on an
8 8 chessboard. Divide the board into an inner 4 4 square and an outer
shell of two squares deep, as shown in Figure 2.17(a). Place a knight on a
square in the outer shell and move the knight piece around that shell, always
106
0Z0Z0Z0Z
Z0Z0Z0Z0
0Z0Z0Z0Z
Z0Z0Z0Z0
0Z0Z0Z0Z
Z0Z0Z0Z0
0Z0Z0Z0Z
Z0Z0Z0Z0
0Z0Z0Z0m
Z0Z0Z0Z0
0Z0Z0Z0Z
Z0Z0Z0Z0
0Z0Z0Z0Z
Z0Z0Z0Z0
0Z0Z0Z0Z
Z0Z0Z0Z0
Figure 2.17: De Montmort and de Moivres solution strategy for the 8 8 knights tour
problem.
0Z0Z0Z
Z0Z0Z0
0Z0Z0Z
Z0Z0Z0
0Z0Z0Z
m0Z0Z0
0Z0Z0Z0Z
Z0Z0Z0Z0
0Z0Z0Z0Z
Z0m0Z0Z0
0Z0Z0Z0Z
Z0Z0Z0Z0
0Z0Z0Z0Z
Z0Z0Z0Z0
(a) A 6 6 chessboard.
(b) An 8 8 chessboard.
2.8. Problems
107
0L0Z
Z0ZQ
QZ0Z
Z0L0
0ZQZ0Z0Z
Z0Z0Z0ZQ
0Z0L0Z0Z
Z0Z0Z0L0
QZ0Z0Z0Z
Z0Z0ZQZ0
0L0Z0Z0Z
Z0Z0L0Z0
(a) n = 4
(b) n = 8
108
(a)
(b)
(a) 2 2
(b) 3 3
(c) 4 4
(2.12)
Each vertex (i, j) is adjacent to any of the following vertices provided that expression (2.12) is satisfied: the vertex (i 1, j) immediately to its left, the vertex
(i + 1, j) immediately to its right, the vertex (i, j + 1) immediately above it, or
the vertex (i, j 1) immediately below it. Figure 2.21 illustrates some examples
of grid graphs. The 1 1 grid graph is the trivial graph K1 .
(a) Fix a positive integer n > 1. Describe and provide pseudocode of an algorithm
to generate all nonisomorphic n n grid graphs. What is the worst-case
runtime of your algorithm?
(b) How many n n grid graphs are there? How many of those graphs are
nonisomorphic to each other?
2.8. Problems
109
Chapter 3
Trees and forests
In section 1.2.1, we briefly touched upon trees and provided examples of how trees could
be used to model hierarchical structures. This chapter provides an in-depth study of
trees, their properties, and various applications. After defining trees and related concepts in section 3.1, we then present various basic properties of trees in section 3.2.
Each connected graph G has an underlying subgraph called a spanning tree that contains all the vertices of G. Spanning trees are discussed in section 3.3 together with
various common algorithms for finding spanning trees. We then discuss binary trees in
section 3.4, followed by an application of binary trees to coding theory in section 3.5.
Whereas breadth- and depth-first searches are general methods for traversing a graph,
trees require specialized techniques in order to visit their vertices, a topic that is taken
up in section 3.6.
3.1
Recall that a path in a graph G = (V, E) whose start and end vertices are the same is
called a cycle. We say G is acyclic, or a forest, if it has no cycles. In a forest, a vertex
of degree one is called an endpoint or a leaf . Any vertex that is not a leaf is called an
110
111
internal vertex. A connected forest is a tree. In other words, a tree is a graph without
cycles and each edge is a bridge. A forest can also be considered as a collection of trees.
A rooted tree T is a tree with a specified root vertex v0 , i.e. exactly one vertex has
been specially designated as the root of T . However, if G is a rooted tree with root
vertex v0 having degree one, then by convention we do not call v0 an endpoint or a leaf.
The depth depth(v) of a vertex v in T is its distance from the root. The height height(T )
of T is the length of a longest path starting from the root vertex, i.e. the height is the
maximum depth among all vertices of T . It follows by definition that depth(v) = 0 if and
only if v is the root of T , height(T ) = 0 if and only if T is the trivial graph, depth(v) 0
for all v V (T ), and height(T ) diam(T ).
The Unix, in particular Linux, filesystem hierarchy can be viewed as a tree (see
Figure 3.1). As shown in Figure 3.1, the root vertex is designated with the forward
slash, which is also referred to as the root directory. Other examples of trees include the
organism classification tree in Figure 3.2, the family tree in Figure 3.3, and the expression
tree in Figure 3.4.
A directed tree is a digraph which would be a tree if the directions on the edges
were ignored. A rooted tree can be regarded as a directed tree since we can imagine an
edge uv for u, v V being directed from u to v if and only if v is further away from v0
than u is. If uv is an edge in a rooted tree, then we call v a child vertex with parent u.
Directed trees are pervasive in theoretical computer science, as they are useful structures
for describing algorithms and relationships between objects in certain datasets.
/
bin
etc
home
lib
anne
sam
...
acyclic
diff
proc
opt
bin
dot
tmp
include
gc
neato
...
usr
local
share
src
...
...
112
organism
plant
animal
tree
flower
invertebrate
evergreen
deciduous
vetebrate
bird
finch
mammal
sparrow
rosella
dolphin
human
whale
Nikolaus senior
Jacob
Nicolaus
Johann
Nicolaus I
Nicolaus II
Daniel
Johann II
Johann III
Daniel II
Jakob II
113
(a)
(b)
(a) n = 1
(b) n = 2
(c) n = 3
(a)
(b)
114
(a)
(b)
(c)
(a)
(b)
(c)
(d)
(f)
(e)
115
T = DiGraph ({
" v " :[ " a " ," w " ] , " w " :[ " x " ," y " ] ,
" x " :[ " c " ," b " ] , " y " :[ " z " ," d " ] ,
" z " :[ " f " ," e " ]})
for v in T . vertex_iterator ():
print ( v ) ,
e d f w v y x z
for e in T . edge_iterator ():
print ( " % s % s " % ( e [0] , e [1])) ,
va vw yd yz xc xb ze zf
Each vertex in a binary tree has at most 2 children. Use this definition to test whether
or not a graph is a binary tree.
sage : T . is_tree ()
True
sage : def is_bintree1 ( G ):
...
for v in G . vertex_iterator ():
...
if len ( G . neighbors_out ( v )) > 2:
...
return False
...
return True
sage : is_bintree1 ( T )
True
Heres another way to test for binary trees. Let T be an undirected rooted tree. Each
vertex in a binary tree has a maximum degree of 3. If the root vertex is the only vertex
with degree 2, then T is a binary tree. (Problem 3.5 asks you to prove this result.) We
can use this test because the root vertex v of T is the only vertex with two children.
sage : def is_bintree2 ( G ):
...
if G . is_tree () and max ( G . degree ()) == 3 and G . degree (). count (2) == 1:
...
return True
...
return False
sage : is_bintree2 ( T . to_undirected ())
True
As x is the root vertex of the branch we want to cut off from T , we could use breadthor depth-first search to determine all the children of x. We then delete x and its children
from T .
sage :
sage :
sage :
[ x ,
sage :
sage :
...
a e d
sage :
...
wy va
sage :
sage :
[ x ,
sage :
sage :
...
a e d
sage :
...
wy va
T2 = copy ( T )
# using breadth - first search
V = list ( T . breadth_first_search ( " x " )); V
c , b ]
T . delete_vertices ( V )
for v in T . vertex_iterator ():
print ( v ) ,
f w v y z
for e in T . edge_iterator ():
print ( " % s % s " % ( e [0] , e [1])) ,
vw yd yz ze zf
# using depth - first search
V = list ( T2 . depth_first_search ( " x " )); V
b , c ]
T2 . delete_vertices ( V )
for v in T2 . vertex_iterator ():
print ( v ) ,
f w v y z
for e in T2 . edge_iterator ():
print ( " % s % s " % ( e [0] , e [1])) ,
vw yd yz ze zf
The resulting graph T is a binary tree because each vertex has at most two children.
sage : T
Digraph on 8 vertices
sage : is_bintree1 ( T )
True
Notice that the test defined in the function is_bintree2 can no longer be used to test
whether or not T is a binary tree, because T now has two vertices, i.e. v and w, each of
116
V (Ti )
i
{vvi } E(Ti ) .
The following game is a variant of the Shannon switching game, due to Edmonds and
Lehman. We follow the description in Oxleys survey [167]. Recall that a minimal edge
cut of a graph is also called a bond of the graph. The following two-person game is played
on a connected graph G = (V, E). Two players Alice and Bob alternately tag elements
of E. Alices goal is to tag the edges of a spanning tree, while Bobs goal is to tag the
edges of a bond. If we think of this game in terms of a communication network, then
Bobs goal is to separate the network into pieces that are no longer connected to each
other, while Alice is aiming to reinforce edges of the network to prevent their destruction.
Each move for Bob consists of destroying one edge, while each move for Alice involves
securing an edge against destruction. The next result characterizes winning strategies
on G. The full proof can be found in Oxley [167]. See Rasmussen [173] for optimization
algorithms for solving similar games.
117
v
...
T1
T2
Tn
3.2
Properties of trees
All theory, dear friend, is grey, but the golden tree of actual life springs ever green.
Johann Wolfgang von Goethe, Faust, part 1, 1808
By Theorem 1.33, each edge of a tree is a bridge. Removing any edge of a tree partitions
the tree into two components, each of which is a subtree of the original tree. The following
results provide further basic characterizations of trees.
Theorem 3.6. Any tree T = (V, E) has size |E| = |V | 1.
Proof. This follows by induction on the number of vertices. By definition, a tree has
no cycles. We need to show that any tree T = (V, E) has size |E| = |V | 1. For the
base case |V | = 1, there are no edges. Assume for induction that the result holds for
all integers less than or equal to k 2. Let T = (V, E) be a tree having k + 1 vertices.
Remove an edge from T , but not the vertices it is incident to. This disconnects T into
two components T1 = (V1 , E1 ) and T2 = (V2 , E2 ), where |E| = |E1 | + |E2 | + 1 and
|V | = |V1 | + |V2 | (and possibly one of the Ei is empty). Each Ti is a tree satisfying the
conditions of the induction hypothesis. Therefore,
|E| = |E1 | + |E2 | + 1
= |V1 | 1 + |V2 | 1 + 1
= |V | 1.
as required.
118
|E| =
i=1
|Ei |
=
i=1
|Vi | k
= |V | k.
This contradicts part (2) unless k = 1. Therefore, T is connected.
(3) = (4): If removing an edge e E leaves T = (V, E) connected then T =
(V, E ) is a tree, where E = Ee. However, this means that |E | = |E|1 = |V |11 =
|V | 2, which contradicts part (3). Therefore e is a cut set.
(4) = (1): From part (2) we know that T has no cycles and from part (3) we
know that T is connected. Conclude by the definition of trees that T is a tree.
Theorem 3.8. Let T = (V, E) be a tree and let u, v V be distinct vertices. Then T
has exactly one u-v path.
Proof. Suppose for contradiction that
P : v0 = u, v1 , v2 , . . . , vk = v
and
Q : w0 = u, w1 , w2 , . . . , w = v
are two distinct u-v paths. Then P and Q has a common vertex x, which is possibly
x = u. For some i 0 and some j 0 we have vi = x = wj , but vi+1 = wj+1 . Let
y be the first vertex after x such that y belongs to both P and Q. (It is possible that
y = v.) We now have two distinct x-y paths that have only x and y in common. Taken
together, these two x-y paths result in a cycle, contradicting our hypothesis that T is a
tree. Therefore T has only one u-v path.
Theorem 3.9. If T = (V, E) is a graph then the following are equivalent:
119
1. T is a tree.
2. For any new edge e, the join T + e has exactly one cycle.
Proof. (1) = (2): Let e = uv be a new edge connecting u, v V . Suppose that
P : v0 = w, v1 , v2 , . . . , vk = w
and
P : v0 = w, v1 , v2 , . . . , v = w
are two cycles in T + e. If either P or P does not contain e, say P does not contain e,
then P is a cycle in T . Let u = v0 and let v = v1 . The edge (v0 = w, v1 ) is a u-v path
and the sequence v = v1 , v2 , . . . , vk = w = u taken in reverse order is another u-v path.
This contradicts Theorem 3.8.
We may now suppose that P and P both contain e. Then P contains a subpath
P0 = P e (which is not closed) that is the same as P except it lacks the edge from u
to v. Likewise, P contains a subpath P0 = P e (which is not closed) that is the same
as P except it lacks the edge from u to v. By Theorem 3.8, these u-v paths P0 and P0
must be the same. This forces P and P to be the same, which proves part (2).
(2) = (1): Part (2) implies that T is acyclic. (Otherwise, it is trivial to make
two cycles by adding an extra edge.) We must show T is connected. Suppose T is
disconnected. Let u be a vertex in one component, T1 say, of T and v a vertex in another
component, T2 say, of T . Adding the edge e = uv does not create a cycle (if it did then
T1 and T2 would not be disjoint), which contradicts part (2).
Taking together the results in this section, we have the following characterizations of
trees.
Theorem 3.10. Basic characterizations of trees. If T = (V, E) is a graph with n
vertices, then the following statements are equivalent:
1. T is a tree.
2. T contains no cycles and has n 1 edges.
3. T is connected and has n 1 edges.
4. Every edge of T is a cut set.
5. For any pair of distinct vertices u, v V , there is exactly one u-v path.
6. For any new edge e, the join T + e has exactly one cycle.
Let G = (V1 , E1 ) be a graph and T = (V2 , E2 ) a subgraph of G that is a tree. As in
part (6) of Theorem 3.10, we see that adding just one edge in E1 E2 to T will create
a unique cycle in G. Such a cycle is called a fundamental cycle of G. The set of such
fundamental cycles of G depends on T .
The following result essentially says that if a tree has at least one edge, then the tree
has at least two vertices each of which has degree one. In other words, each tree of order
2 has at least two pendants.
Theorem 3.11. Every nontrivial tree has at least two leaves.
120
Proof. Let T be a nontrivial tree of order m and size n. Consider the degree sequence
d1 , d2 , . . . , dm of T where d1 d2 dm . As T is nontrivial and connected, then
m 2 and di 1 for i = 1, 2, . . . , m. If T has less than two leaves, then d1 1 and
di 2 for 2 i m, hence
m
i=1
di 1 + 2(m 1) = 2m 1.
(3.1)
i=1
di = 2n = 2(m 1) = 2m 2
which contradicts inequality (3.1). Conclude that T has at least two leaves.
Theorem 3.12. If T is a tree of order m and G is a graph with minimum degree
(G) m 1, then T is isomorphic to a subgraph of G.
Proof. Use an inductive argument on the number of vertices. The result holds for m = 1
because K1 is a subgraph of every nontrivial graph. The result also holds for m = 2
since K2 is a subgraph of any graph with at least one edge.
Let m 3, let T1 be a tree of order m 1, and let H be a graph with (H) m 2.
Assume for induction that T1 is isomorphic to a subgraph of H. We need to show that
if T is a tree of order m and G is a graph with (G) m 1, then T is isomorphic to a
subgraph of G. Towards that end, consider a leaf v of T and let u be a vertex of T such
that u is adjacent to v. Then T v is a tree of order m 1 and (G) m 1 > m 2.
Apply the inductive hypothesis to see that T v is isomorphic to a subgraph T of G.
Let u be the vertex of T that corresponds to the vertex u of T under an isomorphism.
Since deg(u ) m 1 and T has m 2 vertices distinct from u , it follows that u is
adjacent to some w V (G) such that w
/ V (T ). Therefore T is isomorphic to the
graph obtained by adding the edge u w to T .
Example 3.13. Consider a positive integer n. The Euler phi function (n) counts the
number of integers a, with 1 a n, such that gcd(a, n) = 1. The Euler phi sequence
of n is obtained by repeatedly iterating (n) with initial iteration value n. Continue
on iterating and stop when the output of (k ) is 1, for some positive integer k . The
number of terms generated by the iteration, including the initial iteration value n and
the final value of 1, is the length of (n).
(a) Let s0 = n, s1 , s2 , . . . , sk = 1 be the Euler phi sequence of n and produce a digraph G
of this sequence as follows. The vertex set of G is V = {s0 = n, s1 , s2 , . . . , sk = 1}
and the edge set of G is E = {si si+1 | 0 i < k}. Produce the digraphs of the Euler
phi sequences of 15, 22, 33, 35, 69, and 72. Construct the union of all such digraphs
and describe the resulting graph structure.
(b) For each n = 1, 2, . . . , 1000, compute the length of (n) and plot the pairs (n, (n))
on one set of axes.
Solution. The Euler phi sequence of 15 is
15,
(15) = 8,
(8) = 4,
(4) = 2,
(2) = 1.
121
The Euler phi sequences of 22, 33, 35, 69, and 72 can be similarly computed to obtain
their respective digraph representations. The union of all such digraphs is a directed tree
rooted at 1, as shown in Figure 3.11(a). Figure 3.11(b) shows a scatterplot of n versus
the length of (n).
12
4
15
10
20
33
24
44
35
22
72
length of (n)
10
8
6
4
2
0
200
400
800
1,000
69
(a)
600
(b)
3.3
122
the resulting edge-deletion subgraph connected. Thus eventually the above procedure
results in a spanning tree of G. Our discussion is summarized in Algorithm 3.1.
Algorithm 3.1: Randomized spanning tree construction.
Input: A connected graph G.
Output: A spanning tree of G.
1
2
3
4
5
6
T G
while T is not a tree do
e random edge of T
if T e is connected then
T T e
return T
3.3.1
Kruskals algorithm
In 1956, Joseph B. Kruskal published [133] a procedure for constructing a minimum spanning tree of a connected weighted graph G = (V, E). Now known as Kruskals algorithm,
with a suitable implementation the procedure runs in O |E| log |E| time. Variants
of Kruskals algorithm include the algorithm by Prim [172] and that by Loberman and
Weinberger [146].
Kruskals algorithm belongs to the class of greedy algorithms. As will be explained
below, when constructing a minimum spanning tree Kruskals algorithm considers only
the edge having minimum weight among all available edges. Given a weighted nontrivial
graph G = (V, E) that is connected, let w : E R be the weight function of G. The
first stage is creating a skeleton of the tree T that is initially set to be a graph without
edges, i.e. T = (V, ). The next stage involves sorting the edges of G by weights in
nondecreasing order. In other words, we label the edges of G as follows:
E = {e1 , e2 , . . . , en }
where n = |E| and w(e1 ) w(e2 ) w(en ). Now consider each edge ei for
i = 1, 2, . . . , n. We add ei to the edge set of T provided that ei does not result in T
having a cycle. The only way adding ei = ui vi to T would create a cycle is if both ui and
vi were endpoints of edges (not necessarily distinct) in the same connected component
of T . As long as the acyclic condition holds with the addition of a new edge to T , we
123
add that new edge. Following the acyclic test, we also test that the (updated) graph
T is a tree of G. As G is a graph of order |V |, apply Theorem 3.10 to see that if T
has size |V | 1, then it is a spanning tree of G. Algorithm 3.2 provides pseudocode of
our discussion of Kruskals algorithm. When the algorithm halts, it returns a minimum
spanning tree of G. The correctness of Algorithm 3.2 is proven in Theorem 3.14.
Algorithm 3.2: Kruskals algorithm.
Input: A connected weighted graph G = (V, E) with weight function w.
Output: A minimum spanning tree of G.
1
2
3
4
5
6
7
8
m |V |
T
sort E = {e1 , e2 , . . . , en } by weights so that w(e1 ) w(w2 ) w(en )
for i 1, 2, . . . , n do
if ei
/ E(T ) and T {ei } is acyclic then
T T {ei }
if |T | = m 1 then
return T
w(T ) =
w(ei ).
i=1
Suppose for contradiction that T is not a minimum spanning tree of G. Among all the
minimum spanning trees of G, let H be a minimum spanning tree of G such that H has
the most number of edges in common with T . As T and H are distinct subgraphs of G,
then T has at least an edge not belonging to H. Let ei E(T ) be the first edge not in
H. Construct the graph G0 = H + ei obtained by adding the edge ei to H. Note that
G0 has exactly one cycle C. Since T is acyclic, there exists an edge e0 E(C) such that
e0 is not in T . Construct the graph T0 = G0 e0 obtained by deleting the edge e0 from
G0 . Then T0 is a spanning tree of G with
w(T0 ) = w(H) + w(ei ) w(e0 )
and w(H) w(T0 ) and hence w(e0 ) w(ei ). By Kruskals algorithm 3.2, ei is an edge of
minimum weight such that {e1 , e2 , . . . , ei1 } {ei } is acyclic. Furthermore, the subgraph
{e1 , e2 , . . . , ei1 , e0 } of H is acyclic. Thus we have w(ei ) = w(e0 ) and w(T0 ) = w(H) and
so T is a minimum spanning tree of G. By construction, T0 has more edges in common
with T than H has with T , in contradiction of our hypothesis.
124
def kruskal ( G ):
"""
Implements Kruskal s algorithm to compute a MST of a graph .
INPUT :
G - a connected edge - weighted graph or digraph
whose vertices are assumed to be 0 , 1 , .... , n -1.
OUTPUT :
T - a minimum weight spanning tree .
If G is not explicitly edge - weighted then the algorithm
assumes all edge weights are 1. The tree T returned is
a weighted graph , even if G is not .
EXAMPLES :
sage : A = matrix ([[0 ,1 ,2 ,3] ,[0 ,0 ,2 ,1] ,[0 ,0 ,0 ,3] ,[0 ,0 ,0 ,0]])
sage : G = DiGraph (A , format = " adjacency_matrix " , weighted = True )
sage : TE = kruskal ( G ); TE . edges ()
[(0 , 1 , 1) , (0 , 2 , 2) , (1 , 3 , 1)]
sage : G . edges ()
[(0 , 1 , 1) , (0 , 2 , 2) , (0 , 3 , 3) , (1 , 2 , 2) , (1 , 3 , 1) , (2 , 3 , 3)]
sage : G = graphs . PetersenGraph ()
sage : TE = kruskal ( G ); TE . edges ()
[(0 , 1 , 1) , (0 , 4 , 1) , (0 , 5 , 1) , (1 , 2 , 1) , (1 , 6 , 1) , (2 , 3 , 1) ,
(2 , 7 , 1) , (3 , 8 , 1) , (4 , 9 , 1)]
TODO :
Add verbose option to make steps more transparent .
( Useful for teachers and students .)
"""
T_vertices = G . vertices () # a list of the form range ( n )
T_edges = []
E = G . edges () # a list of triples
# start ugly hack
Er = [ list ( x ) for x in E ]
E0 = []
for x in Er :
x . reverse ()
E0 . append ( x )
E0 . sort ()
E = []
for x in E0 :
x . reverse ()
E . append ( tuple ( x ))
# end ugly hack to get E is sorted by weight
for x in E : # find edges of T
TV = flatten ( T_edges )
u = x [0]
v = x [1]
if not ( u in TV and v in TV ):
T_edges . append ([ u , v ])
# find adj mat of T
if G . weighted ():
AG = G . w e i g h t e d _ a dj a c e n c y _ m a t r i x ()
else :
AG = G . adjacency_matrix ()
GV = G . vertices ()
n = len ( GV )
AT = []
for i in GV :
rw = [0]* n
for j in GV :
if [i , j ] in T_edges :
rw [ j ] = AG [ i ][ j ]
AT . append ( rw )
AT = matrix ( AT )
return Graph ( AT , format = " adjacency_matrix " , weighted = True )
Here is an example. We start with the grid graph. This is implemented in Sage such
that the vertices are given by the coordinates of the grid the graph lies on, as opposed
to 0, 1, . . . , n 1. Since the above implementation of Kruskals algorithm assumes that
the vertices are V = {0, 1, . . . , n 1}, we first redefine the graph suitable for running
Kruskals algorithm on it.
sage : G = graphs . GridGraph ([4 ,4])
125
sage : A = G . adjacency_matrix ()
sage : G = Graph (A , format = " adjacency_matrix " , weighted = True )
sage : T = kruskal ( G ); T . edges ()
[(0 , 1 , 1) , (0 , 4 , 1) , (1 , 2 , 1) , (1 , 5 , 1) , (2 , 3 , 1) , (2 , 6 , 1) , (3 ,7 , 1) ,
(4 , 8 , 1) , (5 , 9 , 1) , (6 , 10 , 1) , (7 , 11 , 1) , (8 , 12 , 1) , (9 , 13 , 1) ,
(10 , 14 , 1) , (11 , 15 , 1)]
3.3.2
Prims algorithm
Like Kruskals algorithm, Prims algorithm uses a greedy approach to computing a minimum spanning tree of a connected weighted graph G = (V, E), where n = |V | and
m = |E|. The algorithm was developed in 1930 by Czech mathematician V. Jarnk [114]
and later independently by R. C. Prim [172] and E. W. Dijkstra [65]. However, Prim
was the first to present an implementation that runs in time O(n2 ). Using 2-heaps, the
runtime can be reduced [125] to O(m log n). With a Fibonacci heap implementation [86],
the runtime can be reduced even further to O(m + n log n).
Pseudocode of Prims algorithm is given in Algorithm 3.3. For each v V , cost[v]
denotes the minimum weight among all edges connecting v to a vertex in the tree T ,
and parent[v] denotes the parent of v in T . During the algorithms execution, vertices v
that are not in T are organized in the minimum-priority queue Q, prioritized according
to cost[v]. Lines 1 to 3 set each cost[v] to a number that is larger than any weight in
the graph G, usually written . The parent of each vertex is set to NULL because we
have not yet started constructing the MST T . In lines 4 to 6, we choose an arbitrary
vertex r from V and mark that vertex as the root of T . The minimum-priority queue
is set to be all vertices from V . We set cost[r] to zero, making r the only vertex so far
with a cost that is < . During the first execution of the while loop from lines 7 to 12,
r is the first vertex to be extracted from Q and processed. Line 8 extracts a vertex u
from Q based on the key cost, thus moving u to the vertex set of T . Line 9 considers all
vertices adjacent to u. In an undirected graph, these are simply the neighbors of u. (In a
digraph, one could try to replace adj(u) with the out-neighbors oadj(u). Unfortunately,
in the digraph case the Prim algorithm in general fails to find a minimum spanning tree
with the same orientation as the original digraph.) The while loop updates the cost and
parent fields of each vertex v adjacent to u that is not in T . If parent[v] = NULL, then
cost[v] < and cost[v] is the weight of an edge connecting v to some vertex already in T .
Lines 13 to 14 construct the edge set of the minimum spanning tree and return this edge
set. The proof of correctness of Algorithm 3.3 is similar to the proof of Theorem 3.14.
126
Figure 3.13 shows the minimum spanning tree rooted at vertex 1 as a result of running
Prims algorithm over a digraph; Figure 3.14 shows the corresponding tree rooted at
vertex 5 of an undirected graph.
Algorithm 3.3: Prims algorithm.
Input: A weighted connected graph G = (V, E) with weight function w.
Output: A minimum spanning tree T of G.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
for each v V do
cost[v]
parent[v] NULL
r arbitrary vertex of V
cost[r] 0
QV
while Q = do
u extractMin(Q)
for each v adj(u) do
if v Q and w(u, v) < cost[v] then
parent[v] u
cost[v] w(u, v)
T (v, parent[v]) | v V {r}
return T
def prim ( G ):
"""
Implements Prim s algorithm to compute a MST of a graph .
INPUT :
G - a connected graph .
OUTPUT :
T - a minimum weight spanning tree .
REFERENCES :
http :// en . wikipedia . org / wiki / Prim s_algorithm
"""
T_vertices = [0] # assumes G . vertices = range ( n )
T_edges = []
E = G . edges () # a list of triples
V = G . vertices ()
# start ugly hack to sort E
Er = [ list ( x ) for x in E ]
E0 = []
for x in Er :
x . reverse ()
E0 . append ( x )
E0 . sort ()
E = []
for x in E0 :
x . reverse ()
E . append ( tuple ( x ))
# end ugly hack to get E is sorted by weight
for x in E :
u = x [0]
v = x [1]
if u in T_vertices and not ( v in T_vertices ):
T_edges . append ([ u , v ])
T_vertices . append ( v )
# found T_vertices , T_edges
# find adj mat of T
if G . weighted ():
AG = G . w e i g h t e d _ a dj a c e n c y _ m a t r i x ()
else :
AG = G . adjacency_matrix ()
127
GV = G . vertices ()
n = len ( GV )
AT = []
for i in GV :
rw = [0]* n
for j in GV :
if [i , j ] in T_edges :
rw [ j ] = AG [ i ][ j ]
AT . append ( rw )
AT = matrix ( AT )
return Graph ( AT , format = " adjacency_matrix " , weighted = True )
sage : A = matrix ([[0 ,1 ,2 ,3] , [3 ,0 ,2 ,1] , [2 ,1 ,0 ,3] , [1 ,1 ,1 ,0]])
sage : G = DiGraph (A , format = " adjacency_matrix " , weighted = True )
sage : E = G . edges (); E
[(0 , 1 , 1) , (0 , 2 , 2) , (0 , 3 , 3) , (1 , 0 , 3) , (1 , 2 , 2) , (1 , 3 , 1) , (2 , 0 , 2) ,
(2 , 1 , 1) , (2 , 3 , 3) , (3 , 0 , 1) , (3 , 1 , 1) , (3 , 2 , 1)]
sage : prim ( G )
Multi - graph on 4 vertices
sage : prim ( G ). edges ()
[(0 , 1 , 1) , (0 , 2 , 2) , (1 , 3 , 1)]
2
3
2
3
3
3
2
3
1
2
2
3
1
3
128
3.3.3
Bor
uvkas algorithm
Bor
uvkas algorithm [36, 37] is a procedure for finding a minimum spanning tree in a
weighted connected graph G = (V, E) for which all edge weights are distinct. It was first
published in 1926 by Otakar Bor
uvka but subsequently rediscovered by many others,
including Choquet [57] and Florek et al. [81]. If G has order n = |V | and size m = |E|,
it can be shown that Bor
uvkas algorithm runs in time O(m log n).
Algorithm 3.4: Bor
uvkas algorithm.
Input: A weighted connected graph G = (V, E) with weight function w. All the
edge weights of G are distinct.
Output: A minimum spanning tree T of G.
1
2
3
4
5
6
7
n |V |
T Kn
while |E(T )| < n 1 do
for each component T of T do
e edge of minimum weight that leaves T
E(T ) E(T ) e
return T
129
11
11
9
5
8
6
8
6
4
5
15
7
4
5
15
2
9
5
9
5
1
7
1
7
11
11
9
5
8
6
8
6
4
5
15
7
4
5
15
2
9
5
9
5
1
7
1
7
11
5
8
6
15
7
9
5
1
7
7
0
130
8
7
10
4
9.5
2
5
6
9
11
0
6
8
10
4
10
131
132
3.3.4
Circuit matrix
Recall, the term cycle refers to a closed path. If G is a digraph then a cycle refers to
a sequence of edges which form a path in the associated undirected graph. If repeated
vertices are allowed, it is more often called a closed walk. If the path is a simple path,
with no repeated vertices or edges other than the starting and ending vertices, it may
also be called a simple cycle or circuit. A cycle in a directed graph is called a directed
cycle.
Let Z1 , . . . , ZM denote an enumeration of the cycles (simple closed paths) of G. The
cycle matrix or circuit matrix is a M m matrix C = (cij ) whose rows are parameterized
by the cycles and whose columns are parameterized by the edges E = {e1 , . . . , em }, where
cij =
1, ei Zi ,
0, otherwise.
133
3.3.5
Cutset matrix
The term cutset (or edge cutset) refers to a sequence of edges in a connected graph
G = (V, E) which, when removed from G, creates a disconnected graph and, furthermore,
that no proper subset of these edges has this property. When a graph can be disconnected
by removing only one edge, that edge is called a bridge.
A cutset can be associated to each vertex v V as follows.
Lemma 3.18. The subset Sv E of all the edges incident to v forms forms a cutset.
This uses only the definitions and is left to the interested reader as an exercise.
Let S1 , . . . , SN denote the cutsets of G. The cutset matrix N m matrix Q = (qij )
whose rows are parameterized by the cutsets and whose columns are parameterized by
the edges E = {e1 , . . . , em }, where
qij =
1, ei Si ,
0, otherwise.
134
Proof. Let G be any connected graph (directed or not). By Lemma 3.18 above, for each
v V there is a cut-set Sv consisting of all edges incident to v. The characteristic vector
qv = (q1 , . . . , qm ) of this set (qi = 1 if ei Sv and qi = 0 otherwise) gives us a row
vector in the incidence matrix and in the cut-set matrix. Moreover, all such rows in the
incidence matrix are of this form, so
rankQ (Q) rankQ (C) = n 1.
Suppose now G is a directed graph. We show equality must hold. Let S be any
edge cutset in G and, for each v V , let Sv denote the associated cutset as above. If S
defined a partitioning
V = V1 V2
then
S=
Sv =
vV1
Sv ,
vV2
where the sum of two cutsets is simply the formal sum as oriented edges in the free
abelian group Z[E]. From this it is clear that the dimension of the row-span of Q is
equal to the dimension of the row-span of the submatrix of Q given by the Sv s.
Example 3.20. To construct the example, we will make use of the following Python
function.
def ed ge _cutset_matrix ( G ):
"""
Returns the edge cutset matrix of the connected graph G .
"""
V = G . vertices ()
E = G . edges ()
rows = []
for v1 in V :
for v2 in V :
if v1 != v2 :
S = G . edge_cut ( v1 , v2 , value_only = False )[1]
char_S = lambda e : int ( bool ( e in S ))
rows . append ([ char_S ( e ) for e in E ])
Q = matrix ( rows )
return Q
On the other hand, for the Frucht graph (see Figure 3.18):
sage : G = graphs . FruchtGraph ()
sage : G
Frucht graph : Graph on 12 vertices
sage : e dg e_cutset_matrix ( G ). rank ()
12
135
3.4
Binary trees
A binary tree is a rooted tree with at most two children per parent. Each child is
designated as either a left-child or a right-child . Thus binary trees are also 2-ary trees.
136
(a)
(b)
(c)
(d)
137
is a complete binary tree of height k and by our induction hypothesis Ti has 2k+1 1
vertices. Thus T has order
1 + (2k+1 1) + (2k+1 1) = 2k+2 1
as required.
Theorem 3.21 provides a useful upper bound on the order of a binary tree of a given
height. This upper bound is stated in the following corollary.
Corollary 3.22. A binary tree of height h has at most 2h+1 1 vertices.
We now count the number of possible binary trees on n vertices. Let bn be the number
of binary trees of order n. For n = 0, we set b0 = 1. The trivial graph is the only binary
tree with one vertex, hence b1 = 1. Suppose n > 1 and let T be a binary tree on n
vertices. Then the left subtree of T has order 0 i n 1 and the right subtree has
n 1 i vertices. As there are bi possible left subtrees and bn1i possible right subtrees,
T has a total of bi bn1i different combinations of left and right subtrees. Summing from
i = 0 to i = n 1 and we have
n1
bn =
bi bn1i .
(3.2)
i=0
Expression (3.2) is known as the Catalan recursion and the number bn is the n-th Catalan
number, which we know from problem 1.15 can be expressed in the closed form
bn =
2n
1
.
n+1 n
(3.3)
Figures 3.20 to 3.22 enumerate all the different binary trees on 2, 3, and 4 vertices,
respectively.
(a)
(b)
(a)
(b)
(c)
(d)
(e)
b1 = 1,
b2 = 2,
b3 = 5,
b4 = 14
138
(a)
(b)
(f)
(c)
(g)
(k)
(d)
(h)
(l)
(e)
(i)
(m)
(j)
(n)
3.4.1
Binary codes
What is a code?
A code is a rule for converting data in one format, or well-defined tangible representation,
into sequences of symbols in another format. The finite set of symbols used is called the
alphabet. We shall identify a code as a finite set of symbols which are the image of the
alphabet under this conversion rule. The elements of this set are referred to as codewords.
For example, using the ASCII code, the letters in the English alphabet get converted
into numbers in the set {0, 1, . . . , 255}. If these numbers are written in binary, then
each codeword of a letter has length 8, i.e. eight bits. In this way, we can reformat or
encode a string into a sequence of binary symbols, i.e. 0s and 1s. Encoding is the
conversion process one way. Decoding is the reverse process, converting these sequences
of code-symbols back into information in the original format.
139
1
2
3
4
5
6
7
8
9
10
11
12
13
Other codes are merely simpler ways to communicate information (e.g. flag semaphores,
color codes, genetic codes, braille codes, musical scores, chess notation, football diagrams,
and so on) and have little or no mathematical structure. We shall not study them.
Basic definitions
If every word in the code has the same length, the code is called a block code. If a
code is not a block code, then it is called a variable-length code. A prefix-free code is a
code (typically one of variable-length) with the property that there is no valid codeword
in the code that is a prefix or start of any other codeword.1 This is the prefix-free
condition.
1
140
01
1000
1010
100
0
0010
110
0000
00
0111
101
0100
11
N
O
P
Q
R
S
T
U
V
W
X
Y
Z
10
111
0110
1101
010
000
1
001
0001
011
1001
1011
1100
Gray codes
We begin with some history.2 Frank Gray (18871969) wrote about the so-called Gray
codes in a 1951 paper published in the Bell System Technical Journal and then in 1953
patented a device (used for television sets) based on his paper. However, the idea of
a binary Gray code appeared earlier. In fact, it appeared in an earlier patent (one by
Stibitz in 1943). It was also used in the French engineer E. Baudots telegraph machine
of 1878 and in a French booklet by L. Gros on the solution published in 1872 to the
Chinese ring puzzle.
The term Gray code is ambiguous. It is actually a large family of sequences of
n-tuples. Let Zm = {0, 1, . . . , m 1}. More precisely, an m-ary Gray code of length
n (called a binary Gray code when m = 2) is a sequence of all possible (i.e. N = mn )
n-tuples
g1 , g2 , . . . , gN
where
each gi Znm ,
2
This history comes from an unpublished section 7.2.1.1 (Generating all n-tuples) in volume 4 of
Donald Knuths The Art of Computer Programming.
141
In other words, an m-ary Gray code of length n is a particular way to order the set of
all mn n-tuples whose coordinates are taken from Zm . From the transmission/communication perspective, this sequence has two advantages:
It is easy and fast to produce the sequence, since successive entries differ in only
one coordinate.
An error is relatively easy to detect, since we can compare an n-tuple with the
previous one. If they differ in more than one coordinate, we conclude that an error
was made.
and whose edges are those line segments in Rn connecting two neighboring vertices, i.e.
two vertices that differ in exactly one coordinate. A binary Gray code of length n can
be regarded as a path on the hypercube graph Qn that visits each vertex of the cube
exactly once. In other words, a binary Gray code of length n may be identified with a
Hamiltonian path on the graph Qn . For example, Figure 3.23 illustrates a Hamiltonian
path on Q3 .
How do we efficiently compute a Gray code? Perhaps the simplest way to state the
idea of quickly constructing the reflected binary Gray code n of length n is as follows:
0 = [ ],
n = [0, n1 ], [1, rev
n1 ]
where rev
m means the Gray code in reverse order. For instance, we have
0 = [ ],
1 = [0], [1] ,
2 = [0, 0], [0, 1], [1, 1], [1, 0]
142
and so on. This is a nice procedure for creating the entire list at once, which gets very
long very fast. An implementation of the reflected Gray code using Python is given
below.
def graycode ( length , modulus ):
"""
Returns the n - tuple reflected Gray code mod m .
EXAMPLES :
sage : graycode (2 ,4)
[[0 ,
[1 ,
[2 ,
[3 ,
[3 ,
[2 ,
[1 ,
[0 ,
[0 ,
[1 ,
[2 ,
[3 ,
[3 ,
[2 ,
[1 ,
[0 ,
0] ,
0] ,
0] ,
0] ,
1] ,
1] ,
1] ,
1] ,
2] ,
2] ,
2] ,
2] ,
3] ,
3] ,
3] ,
3]]
"""
n , m = length , modulus
F = range ( m )
if n == 1:
return [[ i ] for i in F ]
L = graycode (n -1 , m )
M = []
for j in F :
M = M +[ ll +[ j ] for ll in L ]
k = len ( M )
Mr = [0]* m
for i in range (m -1):
i1 = i * int ( k / m )
# this requires Python 3.0 or Sage
i2 = ( i +1)* int ( k / m )
Mr [ i ] = M [ i1 : i2 ]
Mr [m -1] = M [( m -1)* int ( k / m ):]
for i in range ( m ):
if is_odd ( i ):
Mr [ i ]. reverse ()
M0 = []
for i in range ( m ):
M0 = M0 + Mr [ i ]
return M0
143
Consider the reflected binary code of length 8, i.e. 8 . This has 28 = 256 codewords.
Sage can easily create the list plot of the coordinates (x, y), where x is an integer j Z256
that indexes the codewords in 8 and the corresponding y is the j-th codeword in 8
converted to decimal. This will give us some idea of how the Gray code looks in some
sense. The plot is given in Figure 3.24.
200
100
0
0
50
100
150
200
250
s = bin ( m )
k = len ( s )
F = GF (2)
b = [ F (0)]* n
for i in range (2 , k ):
b [n - k + i ] = F ( int ( s [ i ]))
return vector ( b )
def graycodeword (m , n ):
144
3.5
Huffman codes
An alphabet A is a finite set whose elements are referred to as symbols. A word (or string
or message) over A is a finite sequence of symbols in A and the length of the word is
the number of symbols it contains. A word is usually written by concatenating symbols
together, e.g. a1 a2 ak (ai A) is a word of length k.
A commonly occurring alphabet in practice is the binary alphabet B = {0, 1}. A
word over the binary alphabet is a finite sequence of 0s and 1s. If A is an alphabet, let
A denote the set of all words in A. The length of a word is denoted by vertical bars.
That is, if w = a1 ak is a word over A, then define |w| : A Z by
|w| = |a1 ak | = k.
Let A and B be two alphabets. A code for A using B is an injection c : A B . By
abuse of notation, we often denote the code simply by the set
C = c(A) = {c(a) | a A}.
The elements of C are called codewords. If B is the binary alphabet, then C is called a
binary code.
3.5.1
Tree representation
145
00
01
10
11
3.5.2
146
Consider now a weighted alphabet (A, p), where p : A [0, 1] satisfies aA p(a) =
1, and a code c : A B . In other words, p is a probability distribution on A. Think of
p(a) as the probability that the symbol a arises in a typical message. The average word
length L(c) is3
L(c) =
p(a) |c(a)|
aA
where | | is the length of a codeword. Given a weighted alphabet (A, p) as above, a code
c : A B is called optimal if there is no such code with a smaller average word length.
Optimal codes satisfy the following amazing property. For a proof, which is very easy
and highly recommended for anyone who is curious to see more, refer to section 3.6 of
Biggs [29].
Lemma 3.27. Suppose c : A B is a binary optimal prefix-free code and let =
maxaA |c(a)| denote the maximum length of a codeword. The following statements
hold.
1. If |c(a )| > |c(a)|, then p(a ) p(a).
2. The subset of codewords of length , i.e.
C = {c c(A) | = |c(a)|}
contains two codewords of the form b0 and b1 for some b B .
3.5.3
Huffman coding
The Huffman code construction is based on the second property in Lemma 3.27. Using
this property, in 1952 David Huffman [111] presented an optimal prefix-free binary code,
which has since been named Huffman code.
Here is the recursive/inductive construction of a Huffman code. We shall regard the
binary Huffman code as a tree, as described above. Suppose that the weighted alphabet
(A, p) has n symbols. We assume inductively that there is an optimal prefix-free binary
code for any weighted alphabet (A , p ) having < n symbols.
Huffmans rule 1 Let a, a A be symbols with the smallest weights. Construct a new
weighted alphabet with a, a replaced by the single symbol a = aa and having
weight p(a ) = p(a) + p(a ). All other symbols and weights remain unchanged.
Huffmans rule 2 For the code (A , p ) above, if a is encoded as the binary string s,
then the encoded binary string for a is s0 and the encoded binary string for a is
s1.
The above two rules tell us how to inductively build the tree representation for the
Huffman code of (A, p) up from its leaves (associated to the low weight symbols).
Find two different symbols of lowest weight, a and a . If two such symbols do not
exist, stop. Replace the weighted alphabet with the new weighted alphabet as in
Huffmans rule 1.
3
In probability terminology, this is the expected value E(X) of the random variable X, which assigns
to a randomly selected symbol in A the length of the associated codeword in c.
147
Add two nodes (labeled with a and a , respectively) to the tree, with parent a (see
Huffmans rule 1).
If there are no remaining symbols in A, label the parent a with the empty set and
stop. Otherwise, go to the first step.
These ideas are captured in Algorithm 3.6, which outlines steps to construct a binary
tree corresponding to the Huffman code of an alphabet. Line 2 initializes a minimumpriority queue Q with the symbols in the alphabet A. Line 3 creates an empty binary
tree that will be used to represent the Huffman code corresponding to A. The for loop
from lines 4 to 10 repeatedly extracts from Q two elements a and b of minimum weights.
We then create a new vertex z for the tree T and also let a and b be vertices of T . The
weight W [z] of z is the sum of the weights of a and b. We let z be the parent of a and b,
and insert the new edges za and zb into T . The newly created vertex z is now inserted
into Q with priority W [z]. After n 1 rounds of the for loop, the priority queue has only
one element in it, namely the root r of the binary tree T . We extract r from Q (line 11)
and return it together with T (line 12).
Algorithm 3.6: Binary tree representation of Huffman codes.
Input: An alphabet A of n symbols. A weight list W of size n such that W [i] is
the weight of ai A.
Output: A binary tree T representing the Huffman code of A and the root r of T .
1
2
3
4
5
6
7
8
9
10
11
12
n |A|
QA
/* minimum priority queue */
T empty tree
for i 1, 2, . . . , n 1 do
a extractMin(Q)
b extractMin(Q)
z node with left child a and right child b
add the edges za and zb to T
W [z] W [a] + W [b]
insert z into priority queue Q
r extractMin(Q)
return (T, r)
The runtime analysis of Algorithm 3.6 depends on the implementation of the priority
queue Q. Suppose Q is a simple unsorted list. The initialization on line 2 requires O(n)
time. The for loop from line 4 to 10 is executed exactly n 1 times. Searching Q to
determine the element of minimum weight requires time at most O(n). Determining two
elements of minimum weights requires time O(2n). The for loop requires time O(2n2 ),
which is also the time requirement for the algorithm. An efficient implementation of
the priority queue Q, e.g. as a binary minimum heap, can lower the running time of
Algorithm 3.6 down to O(n log2 (n)).
Algorithm 3.6 represents the Huffman code of an alphabet as a binary tree T rooted
at r. For an illustration of the process of constructing a Huffman tree, see Figure 3.26.
To determine the actual encoding of each symbol in the alphabet, we feed T and r to
Algorithm 3.7 to obtain the encoding of each symbol. Starting from the root r whose
designated label is the empty string , the algorithm traverses the vertices of T in a
148
13
13
19
19
4
2
19
13
19
(d)
19
13
8
4
19
5
2
1
(e)
15
7
10
5
2
10
5
15
13
2
1
23
5
4
2
(f)
19
10
2
1
13
5
2
(c)
8
13
19
8
(g)
(h)
57
23
10
5
34
13
5
2
(b)
2
1
(a)
13
15
7
23
19
13
5
2
4
2
15
7
19
8
2
1
(i)
10
8
4
34
4
2
2
1
(j)
7
3
149
breadth-first search fashion. If v is an internal vertex with label e, the label of its leftchild is the concatenation e0 and for the right-child of v we assign the label e1. If v
happens to be a leaf vertex, we take its label to be its Huffman encoding. Any Huffman
encoding assigned to a symbol of an alphabet is not unique. Either of the two children of
an internal vertex can be designated as the left- (respectively, right-) child. The runtime
of Algorithm 3.7 is O(|V |), where V is the vertex set of T .
Algorithm 3.7: Huffman encoding of an alphabet.
Input: A binary tree T representing the Huffman code of an alphabet A. The
root r of T .
Output: A list H representing a Huffman code of A, where H[ai ] corresponds to
a Huffman encoding of ai A.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
H []
/* list of Huffman encodings */
Q [r]
/* queue of vertices */
while length(Q) > 0 do
root dequeue(Q)
if root is a leaf then
H[root] label of root
else
a left child of root
b right child of root
enqueue(Q, a)
enqueue(Q, b)
label of a label of root + 0
label of b label of root + 1
return H
3.6
Tree traversals
In computer science, tree traversal refers to the process of examining each vertex in a tree
data structure. Starting at the root of an ordered tree T , we can traverse the vertices of
T in one of various ways.
A level-order traversal of an ordered tree T examines the vertices in increasing order
of depth, with vertices of equal depth being examined according to their prescribed
order. One way to think about level-order traversal is to consider vertices of T having
150
j : 71
i : 49
h : 24
g:5
b:2
d : 25
e : 31
c : 40
00
a : 19
000
f :3
0000
(a)
01
10
11
001
0001
(b)
Figure 3.27: Binary tree representation of an alphabet and its Huffman encodings.
the same depth as being ordered from left to right in decreasing order of importance.
If [v1 , v2 , . . . , vn ] lists the vertices from left to right at depth k, a decreasing order of
importance can be realized by assigning each vertex a numeric label using a labelling
function L : V (T ) R such that L(v1 ) < L(v2 ) < < L(vn ). In this way, a vertex
with a lower numeric label is examined prior to a vertex with a higher numeric label. A
level-order traversal of T , whose vertices of equal depth are prioritized according to L,
is an examination of the vertices of T from top to bottom, left to right. As an example,
the level-order traversal of the tree in Figure 3.28 is
42, 4, 15, 2, 3, 5, 7, 10, 11, 12, 13, 14.
Our discussion is formalized in Algorithm 3.8, whose general structure mimics that of
breadth-first search. For this reason, level-order traversal is also known as breadth-first
traversal. Each vertex is enqueued and dequeued exactly once. The while loop is executed
n times, hence we have a runtime of O(n). Another name for level-order traversal is topdown traversal because we first visit the root node and then work our way down the
tree, increasing the depth as we move downward.
Pre-order traversal is a traversal of an ordered tree using a general strategy similar
to depth-first search. For this reason, pre-order traversal is also referred to as depth-first
traversal. Parents are visited prior to their respective children and siblings are visited
according to their prescribed order. The pseudocode for pre-order traversal is presented
in Algorithm 3.9. Note the close resemblance to Algorithm 3.8; the only significant
change is to use a stack instead of a queue. Each vertex is pushed and popped exactly
once, so the while loop is executed n times, resulting in a runtime of O(n). Using
Algorithm 3.9, a pre-order traversal of the tree in Figure 3.28 is
42, 4, 2, 3, 10, 11, 14, 5, 12, 13, 15, 7.
Whereas pre-order traversal lists a vertex v the first time we visit it, post-order
traversal lists v the last time we visit it. In other words, children are visited prior to their
respective parents, with siblings being visited in their prescribed order. The prefix pre
in pre-order traversal means before, i.e. visit parents before visiting children. On the
151
42
15
10
11
12
13
14
L []
Q empty queue
r root of T
enqueue(Q, r)
while length(Q) > 0 do
v dequeue(Q)
append(L, v)
[u1 , u2 , . . . , uk ] ordering of children of v
for i 1, 2, . . . , k do
enqueue(Q, ui )
return L
L []
S empty stack
r root of T
push(S, r)
while length(S) > 0 do
v pop(S)
append(L, v)
[u1 , u2 , . . . , uk ] ordering of children of v
for i k, k 1, . . . , 1 do
push(S, ui )
return L
152
other hand, the prefix post in post-order traversal means after, i.e. visit parents
after having visited their children. The pseudocode for post-order traversal is presented
in Algorithm 3.10, whose general structure bears close resemblance to Algorithm 3.9.
The while loop of the former is executed n times because each vertex is pushed and
popped exactly once, resulting in a runtime of O(n). The post-order traversal of the tree
in Figure 3.28 is
2, 10, 14, 11, 3, 12, 13, 5, 4, 7, 15, 42.
Algorithm 3.10: Post-order traversal.
Input: An ordered tree T on n > 0 vertices.
Output: A list of the vertices of T in post-order.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
L []
S empty stack
r root of T
push(S, r)
while length(S) > 0 do
if top(S) is unmarked then
mark top(S)
[u1 , u2 , . . . , uk ] ordering of children of top(S)
for i k, k 1, . . . , 1 do
push(S, ui )
else
v pop(S)
append(L, v)
return L
Instead of traversing a tree T from top to bottom as is the case with level-order
traversal, we can reverse the direction of our traversal by traversing a tree from bottom
to top. Called bottom-up traversal , we first visit all the leaves of T and consider the
subtree T1 obtained by vertex deletion of those leaves. We then recursively perform
bottom-up traversal of T1 by visiting all of its leaves and obtain the subtree T2 resulting
from vertex deletion of those leaves of T1 . Apply bottom-up traversal to T2 and its vertex
deletion subtrees until we have visited all vertices, including the root vertex. The result
is a procedure for bottom-up traversal as presented in Algorithm 3.11. In lines 3 to 5,
we initialize the list C to contain the number of children of vertex i. This takes O(m)
time, where m = |E(T )|. Lines 6 to 14 extract all the leaves of T and add them to the
queue Q. From lines 15 to 23, we repeatedly apply bottom-up traversal to subtrees of
T . As each vertex is enqueued and dequeued exactly once, the two loops together run
in time O(n) and therefore Algorithm 3.11 has a runtime of O(n + m). As an example,
a bottom-up traversal of the tree in Figure 3.28 is
2, 7, 10, 12, 13, 14, 15, 5, 11, 3, 4, 42.
Yet another common tree traversal technique is called in-order traversal . However, inorder traversal is only applicable to binary trees, whereas the other traversal techniques
we considered above can be applied to any tree with at least one vertex. Given a binary
tree T having at least one vertex, in-order traversal first visits the root of T and consider
Q empty queue
r root of T
C [0, 0, . . . , 0]
/* n copies of 0 */
for each edge (u, v) E(T ) do
C[u] C[u] + 1
R empty queue
enqueue(R, r)
while length(R) > 0 do
v dequeue(R)
for each w children(v) do
if C[w] = 0 then
enqueue(Q, w)
else
enqueue(R, w)
L []
while length(Q) > 0 do
v dequeue(Q)
append(L, v)
if v = r then
C[parent(v)] C[parent(v)] 1
if C[parent(v)] = 0 then
u parent(v)
enqueue(Q, u)
return L
L []
S empty stack
v root of T
while True do
if v = NULL then
push(S, v)
v left-child of v
else
if length(S) = 0 then
exit the loop
v pop(S)
append(L, v)
v right-child of v
return L
153
154
its left- and right-children. We then recursively apply in-order traversal to the left and
right subtrees of the root vertex. Notice the symmetry in our description of in-order
traversal: start at the root, then traverse the left and right subtrees in in-order. For this
reason, in-order traversal is sometimes referred to as symmetric traversal. Our discussion
is summarized in Algorithm 3.12. In the latter algorithm, if a vertex does not have a
left-child, then the operation of finding its left-child returns NULL. The same holds when
the vertex does not have a right-child. Since each vertex is pushed and popped exactly
once, it follows that in-order traversal runs in time O(n). Using Algorithm 3.12, an
in-order traversal of the tree in Figure 3.27(b) is
0000, 000, 0001, 00, 001, 0, 01, , 10, 1, 11.
3.7
Problems
When solving problems, dig at the roots instead of just hacking at the leaves.
Anthony J. DAngelo, The College Blue Book
3.7. Problems
155
(b) Explain and provide pseudocode of an algorithm for constructing all spanning
trees of the n n grid graph, where n > 0.
(d) Describe and provide pseudocode of an algorithm to generate a random spanning tree of the n n grid graph. What is the worst-case runtime of your
algorithm?
3.8. Theorem 3.4 shows how to recursively construct a new tree from a given collection
of trees, hence it can be considered as a recursive definition of trees. To prove theorems based upon recursive definitions, we use a proof technique called structural
induction. Let S(C) be a statement about the collection of structures C, each of
which is defined by a recursive definition. In the base case, prove S(C) for the
basis structure(s) C. For the inductive case, let X be a structure formed using
the recursive definition from the structures Y1 , Y2 , . . . , Yk . Assume for induction
that the statements S(Y1 ), S(Y2 ), . . . , S(Yk ) hold and use the inductive hypotheses
S(Yi ) to prove S(X). Hence conclude that S(X) is true for all X. Apply structural
induction to show that any graph constructed using Theorem 3.4 is indeed a tree.
3.9. In Kruskals Algorithm 3.2, line 5 requires that the addition of a new edge to T
does not result in T having a cycle. A tree by definition has no cycles. Suppose
line 5 is changed to:
if ei
/ E(T ) and T {ei } is a tree then
With this change, explain why Algorithm 3.2 would return a minimum spanning
tree or why the algorithm would fail to do so.
3.10. This problem is concerned with improving the runtime of Kruskals Algorithm 3.2.
Explain how to use a priority queue to obviate the need for sorting the edges by
weight. Investigate the union-find data structure. Explain how to use union-find
to ensure that the addition of each edge results in an acyclic graph.
3.11. Figure 3.29 shows a weighted version of the Chvatal graph, which has 12 vertices and 24 edges. Use this graph as input to Kruskals, Prims, and Bor
uvkas
algorithms and compare the resulting minimum spanning trees.
3.12. Algorithm 3.1 presents a randomized procedure to construct a spanning tree of a
given connected graph via repeated edge deletion.
(a) Describe and present pseudocode of a randomized algorithm to grow a spanning tree via edge addition.
(b) Would Algorithm 3.1 still work if the input graph G has self-loops or multiple
edges? Explain why or why not. If not, modify Algorithm 3.1 to handle the
case where G has self-loops and multiple edges.
(c) Repeat the previous exercise for Kruskals, Prims, and Bor
uvkas algorithms.
156
11.4
40.7
17.1
14.4
5.6
35.4
5
4
14.5
15
9
11.8
8.5
3.7
48
11
10
0.2
9.1
27.1
6.9
17
43.2
10.2
22.1
22
42.7
36.6
44.2
if n = 1 then
return K1
P random permutation of V
T null tree
for i 1, 2, . . . , n 1 do
j random element from {0, 1, . . . , i 1}
add edge (P [j], P [i]) to T
return T
3.7. Problems
157
3.13. Algorithm 3.13 constructs a random spanning tree of the complete graph Kn on
n > 0 vertices. Its runtime is dependent on efficient algorithms for obtaining a
random permutation of a set of objects, and choosing a random element from a
given set.
(a) Describe and analyze the runtime of a procedure to construct a random permutation of a set of nonnegative integers.
(b) Describe an algorithm for randomly choosing an element of a set of nonnegative integers. Analyze the runtime of this algorithm.
(c) Taking into consideration the previous two algorithms, what is the runtime of
Algorithm 3.13?
3.14. We want to generate a random undirected, connected simple graph on n vertices
and having m edges. Start by generating a random spanning tree T of Kn . Then
add random edges to T until the requirements are satisfied.
(a) Present pseudocode to realize the above procedure. What is the worst-case
runtime of your algorithm?
(b) Modify your algorithm to handle the case where m < n 1. Why must
m n 1?
(c) Modify your algorithm to handle the case where each edge has a weight within
the closed interval [, ].
You can find this on the Internet or in the literature. Part of this exercise is finding this frequency
distribution yourself.
158
(3.4)
One way to think about the Collatz conjecture is to consider the digraph G
produced by considering (ai , T (ai )) as a directed edge of G. Then the Collatz
conjecture can be rephrased to say that there is some integer k > 0 such that
(ak , T (ak )) = (2, 1) is a directed edge of G. The graph obtained in this manner is called the Collatz graph of T (n). Given a collection of positive integers
1 , 2 , . . . , k , let Gi be the Collatz graph of the function T (i ) with initial iteration value i . Then the union of the Gi is the directed tree
Gi
i
3.7. Problems
159
16
32
10
20
21
64
42
128
12
13
40
80
84
85
256
24
26
168
170
512
48
52
53
160
336
340
1024
96
104
106
320
160
3.27. The following result [157] was independently discovered in the late 1980s by Merris
and McKay, and is known as the Merris-McKay theorem. Let T be a tree of order
n and let L be its Laplacian matrix having eigenvalues 1 , 2 , . . . , n . Show that
the Wiener number of T is
n1
1
.
W (T ) = n
i
i=1
3.28. For each of the algorithms below: (i) justify whether or not it can be applied
to multigraphs or multidigraphs; (ii) if not, modify the algorithm so that it is
applicable to multigraphs or multidigraphs.
(a) Randomized spanning tree construction Algorithm 3.1.
(b) Kruskals Algorithm 3.2.
(c) Prims Algorithm 3.3.
(d) Bor
uvkas Algorithm 3.4.
3.29. Section 3.6 provides iterative algorithms for the following tree traversal techniques:
(a) Level-order traversal: Algorithm 3.8.
(b) Pre-order traversal: Algorithm 3.9.
(c) Post-order traversal: Algorithm 3.10.
(d) Bottom-up traversal: Algorithm 3.11.
(e) In-order traversal: Algorithm 3.12.
Rewrite each of the above as recursive algorithms.
3.30. In cryptography, the Merkle signature scheme [155] was introduced in 1987 as an
alternative to traditional digital signature schemes such as the Digital Signature
Algorithm or RSA. Buchmann et al. [46] and Szydlo [188] provide efficient algorithms for speeding up the Merkle signature scheme. Investigate this scheme and
how it uses binary trees to generate digital signatures.
3.31. Consider the finite alphabet A = {a1 , a2 , . . . , ar }. If C is a subset of A , then we say
that C is an r-ary code and call r the radix of the code. McMillans theorem [152],
first published in 1956, relates codeword lengths to unique decipherability. In
particular, let C = {c1 , c2 , . . . , cn } be an r-ary code where each ci has length i . If
C is uniquely decipherable, McMillans theorem states that the codeword lengths
i must satisfy Krafts inequality
n
i=1
1
1.
ri
3.7. Problems
161
i=1
1
1.
ri
2,
if n = 0,
Ln = 1,
if n = 1,
Chapter 4
Tree data structures
4.1
163
Priority queues
A priority queue is essentially a queue data structure with various accompanying rules
regarding how to access and manage elements of the queue. Recall from section 2.2.1
that an ordinary queue Q has the following basic accompanying functions for accessing
and managing its elements:
dequeue(Q) Remove the front of Q.
enqueue(Q, e) Append the element e to the end of Q.
If the above three properties hold for the relation , then we say that is a total
order on X and that X is a totally ordered set. In all, if the key of each element of
Q belongs to the same totally ordered set X, we use the total order defined on X to
compare the keys of the queue elements. For example, the set Z of integers is totally
ordered by the less than or equal to relation. If the key of each e Q is an element
of Z, we use the latter relation to compare the keys of elements of Q. In the case of an
ordinary queue, the key of each queue element is its position index.
To extract from a priority queue Q an element of lowest priority, we need to define
the notion of smallest priority or key. Let pi be the priority or key assigned to element
ei of Q. Then pmin is the lowest key if pmin p for any element key p. The element with
corresponding key pmin is the minimum priority element. Based upon the notion of key
comparison, we define two operations on a priority queue:
insert(Q, e, p) Insert into Q the element e with key p.
extractMin(Q) Extract from Q an element having the smallest priority.
164
Q []
for i 1, 2, . . . , n do
e dequeue(L)
insert(Q, e, e)
for i 1, 2, . . . , n do
e extractMin(Q)
enqueue(L, e)
4.1.1
Sequence implementation
4.2
Binary heaps
165
internal vertices in a binary tree T , with external vertices or leaves being place-holders.
The tree T satisfies two further properties:
1. A relational property specifying the relative ordering and placement of queue elements.
2. A structural property that specifies the structure of T .
The relational property of T can be expressed as follows:
Definition 4.1. Heap-order property. Let T be a binary tree and let v be a vertex of
T other than the root. If p is the parent of v and these vertices have corresponding keys
p and v , respectively, then p v .
The heap-order property is defined in terms of the total order used to compare the
keys of the internal vertices. Taking the total order to be the ordinary less than or
equal to relation, it follows from the heap-order property that the root of T is always
the vertex with a minimum key. Similarly, if the total order is the usual greater than
or equal to relation, then the root of T is always the vertex with a maximum key. In
general, if is a total order defined on the keys of T and u and v are vertices of T , we
say that u is less than or equal to v if and only if u v. Furthermore, u is said to be
a minimum vertex of T if and only if u v for all vertices of T . From our discussion
above, the root is always a minimum vertex of T and is said to be at the top of the
heap, from which we derive the name heap for this data structure.
Another consequence of the heap-order property becomes apparent when we trace
out a path from the root of T to any internal vertex. Let r be the root of T and let v be
any internal vertex of T . If r, v0 , v1 , . . . , vn , v is an r-v path with corresponding keys
r , v0 , v1 , . . . , vn , v
then we have
r v0 v1 vn v .
In other words, the keys encountered on the path from r to v are arranged in nondecreasing order.
The structural property of T is used to enforce that T be of as small a height as
possible. Before stating the structural property, we first define the level of a binary tree.
Recall that the depth of a vertex in T is its distance from the root. Level i of a binary
tree T refers to all vertices of T that have the same depth i. We are now ready to state
the heap-structure property.
Definition 4.2. Heap-structure property. Let T be a binary tree with height h.
Then T satisfies the heap-structure property if T is nearly a complete binary tree. That
is, level 0 i h 1 has 2i vertices, whereas level h has 2h vertices. The vertices at
level h are filled from left to right.
If a binary tree T satisfies both the heap-order and heap-structure properties, then
T is referred to as a binary heap. By insisting that T satisfy the heap-order property,
we are able to determine the minimum vertex of T in constant time O(1). Requiring
that T also satisfy the heap-structure property allows us to determine the last vertex
of T . The last vertex of T is identified as the right-most internal vertex of T having
the greatest depth. Figure 4.1 illustrates various examples of binary heaps. The heapstructure property together with Theorem 3.21 result in the following corollary on the
height of a binary heap.
166
10
(a)
0
17
13
19
24
10
23
(b)
1
13
10
17
(c)
167
internal vertices. On the other hand, level h 1 has at most 2h1 internal vertices.
Another application of Theorem 3.21 shows that T has at most
2h1+1 1 = 2h 1
internal vertices. Thus n is bounded by
2h1 n 2h 1.
Taking logarithms of each side in the latter bound results in
lg(n + 1) h lg n + 1
and the corollary follows.
0
0
10
17
13
19
24
23
10
(a)
(b)
10
13
17
(c)
4.2.1
Sequence representation
Any binary heap can be represented as a binary tree. Each vertex in the tree must know
about its parent and its two children. However, a more common approach is to represent
a binary heap as a sequence such as a list, array, or vector. Let T be a binary heap
consisting of n internal vertices and let L be a list of n elements. The root vertex is
represented as the list element L[0]. For each index i, the children of L[i] are L[2i + 1]
and L[2i + 2] and the parent of L[i] is
L
i1
2
168
With a sequence representation of a binary heap, each vertex needs not know about
its parent and children. Such information can be obtained via simple arithmetic on
sequence indices. For example, the binary heaps in Figure 4.1 can be represented as the
corresponding lists in Figure 4.2. Note that it is not necessary to store the leaves of T
in the sequence representation.
4.2.2
We now consider the problem of inserting a vertex v into a binary heap T . If T is empty,
inserting a vertex simply involves the creation of a new internal vertex. We let that
new internal vertex be v and let its two children be leaves. The resulting binary heap
augmented with v has exactly one internal vertex and satisfies both the heap-order and
heap-structure properties, as shown in Figure 4.3. In other words, any binary heap with
one internal vertex trivially satisfies the heap-order property.
v
(a)
(b)
169
6
17
4
13
19
8
24
2
10
23
6
17
4
13
19
8
24
(a)
17
3
4
13
19
8
24
23
2
10
6
17
4
13
19
0
24
(c)
3
4
13
19
0
24
23
2
10
6
17
4
13
19
3
24
(e)
0
4
13
19
3
24
(g)
17
23
10
(f)
1
17
23
10
(d)
1
(b)
1
23
10
23
2
10
6
17
4
13
19
3
24
(h)
23
10
8
170
in
while i > 0 do
p (i 1)/2
if T [p] v then
exit the loop
else
T [i] T [p]
ip
T [i] v
return T
4.2.3
The process for deleting the minimum vertex of a binary heap bears some resemblance
to that of inserting a new internal vertex into the heap. Having removed the minimum
vertex, we must then ensure that the resulting binary heap satisfies the heap-order
property. Let T be a binary heap. By the heap-order property, the root of T has a
key that is minimum among all keys of internal vertices in T . If the root r of T is the
only internal vertex of T , i.e. T is the trivial binary heap, we simply remove r and T now
becomes the empty binary heap or the trivial tree, for which the heap-order property
vacuously holds. Figure 4.5 illustrates the case of removing the root of a binary heap
having one internal vertex.
r
(a)
(b)
171
r that has minimum key among all of rs children. The key comparison and swapping
continue until the heap-order property holds for T . In the worst case, r would percolate
all the way down to the level that is immediately above the last level after undergoing a
number of swaps that is proportional to the height of T . Therefore, deleting the minimum
vertex of T can be achieved in time O(lg n). Figure 4.6 illustrates the deletion of the
minimum vertex of a binary heap with at least two internal vertices and the resulting
sift-down process that percolates vertices down through various levels of the heap in order
to maintain the heap-order property. Algorithm 4.3 summarizes our discussion of the
process for extracting the minimum vertex of T while also ensuring that T satisfies the
heap-order property. The pseudocode is adapted from the C implementation of binary
heaps in Howard [110]. With some minor changes, Algorithm 4.3 can be used to change
the key of the root vertex and maintain the heap-order property for the resulting binary
tree.
Algorithm 4.3: Extract the minimum vertex of a binary heap.
Input: A binary heap T , given in sequence representation, having n > 1 internal
vertices.
Output: Extract the minimum vertex of T . With one vertex removed, T must
satisfy the heap-order property.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
root T [0]
nn1
v T [n]
i0
j0
while True do
left 2i + 1
right 2i + 2
if left < n and T [left] v then
if right < n and T [right] T [left] then
j right
else
j left
else if right < n and T [right] v then
j right
else
T [i] v
exit the loop
T [i] T [j]
ij
return root
4.2.4
172
23
6
17
4
13
19
8
24
2
10
23
6
17
4
13
19
8
24
(a)
(b)
23
6
17
4
13
19
23
10
24
6
17
4
13
19
(d)
2
23
17
3
4
13
19
4
10
24
6
17
23
13
19
(f)
2
17
23
13
19
8
24
(g)
10
24
(e)
10
24
(c)
10
4
10
6
17
19
13
23
8
24
(h)
10
173
insertion requires O(lg n) time, the method of binary heap construction via successive
insertion of each of the n vertices requires O(n lg n) time. It turns out we could do a
bit better and achieve the same result in linear time.
Algorithm 4.4: Heapify a binary tree.
Input: A binary tree T , given in sequence representation, having n > 1 internal
vertices.
Output: The binary tree T heapified so that it satisfies the heap-order property.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
for i n/2 1, . . . , 0 do
v T [i]
j0
while True do
left 2i + 1
right 2i + 2
if left < n and T [left] v then
if right < n and T [right] T [left] then
j right
else
j left
else if right < n and T [right] v then
j right
else
T [i] v
exit the while loop
T [i] T [j]
ij
return T
(4.1)
174
performing a sift-down on this subtree. Once we have heapified all subtrees rooted at
T [i] for 0 i n/2 1, the resulting tree T is a binary heap. Our discussion is
summarized in Algorithm 4.4.
Earlier in this section, we claimed that Algorithm 4.4 can be used to construct a
binary heap in worst-case linear time. To prove this, let T be a binary tree satisfying the
heap-structure property and having n internal vertices. By Corollary 4.3, T has height
h = lg(n + 1) . We perform a sift-down for at most 2i vertices of depth i, where each
sift-down for a subtree rooted at a vertex of depth i takes O(h i) time. Then the total
time for Algorithm 4.4 is
O
0i<h
2i (h i)
= O 2h
2i
2hi
0i<h
= O 2h
k
2k
k>0
h+1
=O 2
= O(n)
where we used the closed form
4.3
k>0
Binomial heaps
We are given two binary heaps T1 and T2 and we want to merge them into a single heap.
We could start by choosing to insert each element of T2 into T1 , successively extracting
the minimum element from T2 and insert that minimum element into T1 . If T1 and T2
have m and n elements, respectively, we would perform n extractions from T2 totalling
O
lg k
0<kn
time and inserting all of the extracted elements from T2 into T1 requires a total runtime
of
O
lg k .
(4.2)
nk<n+m
k ln k k
+C
lg k dk =
ln 2
k=n+m
k=0
for some constant C. The above method of successive extraction and insertion therefore
has a total runtime of
(n + m) ln(n + m) n m
O
ln 2
for merging two binary heaps.
Alternatively, we could slightly improve the latter runtime for merging T1 and T2 by
successively extracting the last internal vertex of T2 . The whole process of extracting
175
all elements from T2 in this way takes O(n) time and inserting each of the extracted
elements into T1 still requires the runtime in expression (4.2). We approximate the sum
in (4.2) by
k=n+m
k=n+m
k ln k k
lg k dk =
+C
ln 2
k=n
k=n
for some constant C. Therefore the improved extraction and insertion method requires
O
(n + m) ln(n + m) n ln n m
n
ln 2
4.3.1
Binomial trees
A binomial heap can be considered as a collection of binomial trees. The binomial tree
of order k is denoted Bk and defined recursively as follows:
1. The binomial tree of order 0 is the trivial tree.
2. The binomial tree of order k > 0 is a rooted tree, where from left to right the
children of the root of Bk are roots of Bk1 , Bk2 , . . . , B0 .
Various examples of binomial trees are shown in Figure 4.7. The binomial tree Bk can
also be defined as follows. Let T1 and T2 be two copies of Bk1 with root vertices r1
and r2 , respectively. Then Bk is obtained by letting, say, r1 be the left-most child of r2 .
Lemma 4.4 lists various basic properties of binomial trees. Property (3) of Lemma 4.4
uses the binomial coefficient, from whence Bk derives its name.
Lemma 4.4. Basic properties of binomial trees. Let Bk be a binomial tree of
order k 0. Then the following properties hold:
1. The order of Bk is 2k .
2. The height of Bk is k.
3. For 0 i k, we have
k
i
vertices at depth i.
4. The root of Bk is the only vertex with maximum degree (Bk ) = k. If the children
of the root are numbered k 1, k 2, . . . , 0 from left to right, then child i is the
root of the subtree Bi .
Proof. We use induction on k. The base case for each of the above properties is B0 ,
which trivially holds.
(1) By our inductive hypothesis, Bk1 has order 2k1 . Since Bk is comprised of two
copies of Bk1 , conclude that Bk has order
2k1 + 2k1 = 2k .
176
(a) B0
(b) B1
(c) B2
(d) B3
(e) B4
(f) B5
177
(2) The binomial tree Bk is comprised of two copies of Bk1 , the root of one copy
being the left-most child of the root of the other copy. Then the height of Bk is one
greater than the height of Bk1 . By our inductive hypothesis, Bk1 has height k 1 and
therefore Bk has height (k 1) + 1 = k.
(3) Denote by D(k, i) the number of vertices of depth i in Bk . As Bk is comprised
of two copies of Bk1 , a vertex at depth i in Bk1 appears once in Bk at depth i and a
second time at depth i + 1. By our inductive hypothesis,
D(k, i) = D(k 1, i) + D(k 1, i 1)
=
k1
k1
+
i
i1
k
i
n
n
+
r1
r
4.3.2
Binomial heaps
In 1978, Jean Vuillemin [200] introduced binomial heaps as a data structure for implementing priority queues. Mark R. Brown [44, 45] subsequently extended Vuillemins
work, providing detailed analysis of binomial heaps and introducing an efficient implementation.
A binomial heap H can be considered as a collection of binomial trees. Each vertex
in H has a corresponding key and all vertex keys of H belong to a totally ordered set
having total order . The heap also satisfies the following binomial heap properties:
Heap-order property. Let Bk be a binomial tree in H. If v is a vertex of Bk
other than the root and p is the parent of v and having corresponding keys v and
p , respectively, then p v .
Root-degree property. For any integer k 0, H contains at most one binomial
tree whose root has degree k.
If H is comprised of the binomial trees Bk0 , Bk1 , . . . , Bkn for nonnegative integers ki ,
we can consider H as a forest made up of the trees Bki . We can also represent H as a tree
in the following way. List the binomial trees of H as Bk0 , Bk1 , . . . , Bkn in nondecreasing
order of root degrees, i.e. the root of Bki has order less than or equal to the root of Bkj
if and only if ki kj . The root of H is the root of Bk0 and the root of each Bki has
for its child the root of Bki+1 . Both the forest and tree representations are illustrated in
Figure 4.8 for the binomial heap comprised of the binomial trees B0 , B1 , B3 .
178
4.3.3
Let H be a binomial heap comprised of the binomial trees Bk0 , Bk1 , . . . , Bkn where the
root of Bki has order less than or equal to the root of Bkj if and only if ki kj .
Denote by rki the root of the binomial tree Bki . If v is a vertex of H, denote by
child[v] the left-most child of v and by sibling[v] we mean the sibling immediately to
the right of v. Furthermore, let parent[v] be the parent of v and let degree[v] denote
the degree of v. If v has no children, we set child[v] = NULL. If v is one of the roots
rki , we set parent[v] = NULL. And if v is the right-most child of its parent, then we set
sibling[v] = NULL.
The roots rk0 , rk1 , . . . , rkn can be organized as a linked list, called a root list, with
two functions for accessing the next root and the previous root. The root immediately
following rki is denoted next[rki ] = sibling[v] = rki+1 and the root immediately before rki
is written prev[rki ] = rki1 . For rk0 and rkn , we set next[rkn ] = sibling[v] = NULL and
prev[rk0 ] = NULL. We also define the function head[H] that simply returns rk0 whenever
H has at least one element, and head[H] = NULL otherwise.
179
Minimum vertex
To find the minimum vertex, we find the minimum among rk0 , rk1 , . . . , rkm because by
definition the root rki is the minimum vertex of the binomial tree Bki . If H has n vertices,
we need to check at most 1 + lg n vertices to find the minimum vertex of H. Therefore
determining the minimum vertex of H takes O(lg n) time. Algorithm 4.5 summarizes
our discussion.
Algorithm 4.5: Determine the minimum vertex of a binomial heap.
Input: A binomial heap H of order n > 0.
Output: The minimum vertex of H.
1
2
3
4
5
6
7
8
9
u NULL
v head[H]
min
while v = NULL do
if v < min then
min v
uv
v sibling[v]
return u
Merging heaps
Recall that Bk is constructed by linking the root of one copy of Bk1 with the root of
another copy of Bk1 . When merging two binomial heaps whose roots have the same
degree, we need to repeatedly link the respective roots. The root linking procedure runs
in constant time O(1) and is rather straightforward, as presented in Algorithm 4.6.
Algorithm 4.6: Linking the roots of binomial heaps.
Input: Two copies of Bk1 , one rooted at u and the other at v.
Output: The respective roots of two copies of Bk1 linked, with one root
becoming the parent of the other.
1
2
3
4
parent[u] v
sibling[u] child[v]
child[v] u
degree[v] degree[v] + 1
Besides linking the roots of two copies of Bk1 , we also need to merge the root lists
of two binomial heaps H1 and H2 . The resulting merged list is sorted in nondecreasing
order of degree. Let L1 be the root list of H1 and let L2 be the root list of H2 . First
we create an empty list L. As the lists Li are already sorted in nondecreasing order of
vertex degree, we use merge sort to merge the Li into a single sorted list. The whole
procedure for merging the Li takes linear time O(n), where n = |L1 | + |L2 | 1. Refer to
Algorithm 4.7 for pseudocode of the procedure just described.
Having clarified the root linking and root lists merging procedures, we are now ready
to describe a procedure for merging two nonempty binomial heaps H1 and H2 into a
180
i1
j1
L []
n |L1 | + |L2 | 1
append(L1 , )
append(L2 , )
for k 0, 1, . . . , n do
if deg(L1 [i]) deg(L2 [j]) then
append(L, L1 [i])
ii+1
else
append(L, L2 [j])
j j+1
return L
single binomial heap H. Initially there are at most two copies of B0 , one from each of
the Hi . If two copies of B0 are present, we let the root of one be the parent of the other
as per Algorithm 4.6, producing B1 as a result. From thereon, we generally have at most
three copies of Bk for some integer k > 0: one from H1 , one from H2 , and the third from
a previous merge of two copies of Bk1 . In the presence of two or more copies of Bk , we
merge two copies as per Algorithm 4.6 to produce Bk+1 . If Hi has ni vertices, then Hi
has at most 1 + lg ni binomial trees, from which it is clear that merging H1 and H2
requires
max(1 + lg n1 , 1 + lg n2 )
steps. Letting N = max(n1 , n2 ), we see that merging H1 and H2 takes logarithmic time
O(lg N ). The operation of merging two binomial heaps is presented in pseudocode as
Algorithm 4.8, which is adapted from Cormen et al. [61, p.463] and the C implementation
of binomial queues in [110]. A word of warning is order here. Algorithm 4.8 is destructive
in the sense that it modifies the input heaps Hi in-place without making copies of those
heaps.
Vertex insertion
Let v be a vertex with corresponding key v and let H1 be a binomial heap of n vertices.
The single vertex v can be considered as a binomial heap H2 comprised of exactly the
binomial tree B0 . Then inserting v into H1 is equivalent to merging the heaps Hi and
can be accomplished in O(lg n) time. Refer to Algorithm 4.9 for pseudocode of this
straightforward procedure.
10
11
12
13
14
15
16
17
18
19
20
21
22
23
181
182
4.4
A binary search tree (BST) is a rooted binary tree T = (V, E) having vertex weight
function : V R. The weight of each vertex v is referred to as its key, denoted v .
Each vertex v of T satisfies the following properties:
Left subtree property. The left subtree of v contains only vertices whose keys
are at most v . That is, if u is a vertex in the left subtree of v, then u v .
Right subtree property. The right subtree of v contains only vertices whose
keys are at least v . In other words, any vertex u in the right subtree of v satisfies
v u .
Recursion property. Both the left and right subtrees of v must also be binary
search trees.
The above are collectively called the binary search tree property. See Figure 4.10 for
an example of a binary search tree. Based on the binary search tree property, we can
use in-order traversal (see Algorithm 3.12) to obtain a listing of the vertices of a binary
search tree sorted in nondecreasing order of keys.
70
183
60
65
67
66
40
68
41
69
45
43
48
44
49
47
11
12
15
10
(a)
70
60
65
67
66
40
68
41
69
45
43
48
44
49
47
11
12
15
10
(b)
70
65
67
60
66
40
68
69
41
45
43
48
44
15
49
12
47
11
10
(c)
5
70
65
67
15
60
66
69
68
12
40
41
45
43
48
49
44
11
10
47
(d)
184
15
13
20
11
4.4.1
Searching
Given a BST T and a key k, we want to locate a vertex (if one exists) in T whose key is k.
The search procedure for a BST is reminiscent of the binary search algorithm discussed
in problem 2.10. We begin by examining the root v0 of T . If v0 = k, the search is
successful. However, if v0 = k then we have two cases to consider. In the first case, if
k < v0 then we search the left subtree of v0 . The second case occurs when k > v0 , in
which case we search the right subtree of v0 . Repeat the process until a vertex v in T
is found for which k = v or the indicated subtree is empty. Whenever the target key
is different from the key of the vertex we are currently considering, we move down one
level of T . Thus if h is the height of T , it follows that searching T takes a worst-case
runtime of O(h). The above procedure is presented in pseudocode as Algorithm 4.11.
Note that if a vertex v does not have a left subtree, the operation of locating the root
of vs left subtree should return NULL. A similar comment applies when v does not have
a right subtree. Furthermore, from the structure of Algorithm 4.11, if the input BST is
empty then NULL is returned. See Figure 4.11 for an illustration of locating vertices with
given keys in a BST.
Algorithm 4.11: Locate a key in a binary search tree.
Input: A binary search tree T and a target key k.
Output: A vertex in T with key k. If no such vertex exists, return NULL.
1
2
3
4
5
6
7
v root[T ]
while v = NULL and k = v do
if k < v then
v leftchild[v]
else
v rightchild[v]
return v
From the binary search tree property, deduce that a vertex of a BST T with minimum
key can be found by starting from the root of T and repeatedly traversing left subtrees.
When we have reached the left-most vertex v of T , querying for the left subtree of v
should return NULL. At this point, we conclude that v is a vertex with minimum key.
Each query for the left subtree moves us one level down T , resulting in a worst-case
runtime of O(h) with h being the height of T . See Algorithm 4.12 for pseudocode of the
procedure.
The procedure for finding a vertex with maximum key is analogous to that for finding
185
10
10
15
13
11
20
19
23
15
22
26
13
11
20
19
23
22
26
10
10
15
13
11
20
19
23
15
22
26
13
11
20
19
23
22
26
10
10
15
13
11
20
19
(a) Successor of 9.
15
23
22
26
13
11
20
19
23
22
26
186
one with minimum key. Starting from the root of T , we repeatedly traverse right subtrees
until we encounter the right-most vertex, which by the binary search tree property has
maximum key. This procedure has the same worst-case runtime of O(h). Figure 4.12
illustrates the process of locating the minimum and maximum vertices of a BST.
Algorithm 4.12: Finding a vertex with minimum key in a BST.
Input: A nonempty binary search tree T .
Output: A vertex of T with minimum key.
1
2
3
4
v root of T
while leftchild[v] = NULL do
v leftchild[v]
return v
Corresponding to the notions of left- and right-children, we can also define successors
and predecessors as follows. Suppose v is not a maximum vertex of a nonempty BST
T . The successor of v is a vertex in T distinct from v with the smallest key greater
than or equal to v . Similarly, for a vertex v that is not a minimum vertex of T , the
predecessor of v is a vertex in T distinct from v with the greatest key less than or equal
to v . The notions of successors and predecessors are concerned with relative key order,
not a vertexs position within the hierarchical structure of a BST. For instance, from
Figure 4.10 we see that the successor of the vertex u with key 8 is the vertex v with key
10, i.e. the root, even though v is an ancestor of u. The predecessor of the vertex a with
key 4 is the vertex b with key 3, i.e. the minimum vertex, even though b is a descendant
of a.
We now describe a method to systematically locate the successor of a given vertex.
Let T be a nonempty BST and v V (T ) not a maximum vertex of T . If v has a right
subtree, then we find a minimum vertex of vs right subtree. In case v does not have
a right subtree, we backtrack up one level to vs parent u = parent(v). If v is the root
of the right subtree of u, we backtrack up one level again to us parent, making the
assignments v u and u parent(u). Otherwise we return vs parent. Repeat the
above backtracking procedure until the required successor is found. Our discussion is
summarized in Algorithm 4.13. Each time we backtrack to a vertexs parent, we move
up one level, hence the worst-case runtime of Algorithm 4.13 is O(h) with h being the
height of T . The procedure for finding predecessors is similar. Refer to Figure 4.13 for
an illustration of locating successors and predecessors.
4.4.2
Insertion
187
into T takes O(h) time, where h is the height of T . Algorithm 4.14 presents pseudocode
of our discussion and Figure 4.14 illustrates how to insert a vertex into a BST.
Algorithm 4.14: Inserting a vertex into a binary search tree.
Input: A binary search tree T and a vertex x to be inserted into T .
Output: The same BST T but augmeneted with x.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
u NULL
v root of T
while v = NULL do
uv
if x < v then
v leftchild[v]
else
v rightchild[v]
parent[x] u
if u = NULL then
root[T ] x
else
if x < u then
leftchild[u] x
else
rightchild[u] x
4.4.3
Deletion
Whereas insertion into a BST is straightforward, removing a vertex requires much more
work. Let T be a nonempty binary search tree and suppose we want to remove v V (T )
from T . Having located the position that v occupies within T , we need to consider three
separate cases: (1) v is a leaf; (2) v has one child; (3) v has two children.
1. If v is a leaf, we simply remove v from T and the procedure is complete. The
resulting tree without v satisfies the binary search tree property.
188
10
10
15
13
11
15
20
19
23
22
26
(a)
13
11
20
19
12
23
22
26
(b)
u NULL
v NULL
if leftchild[x] = NULL or rightchild[x] = NULL then
vx
else
v successor of x
if leftchild[v] = NULL then
u leftchild[v]
else
u rightchild[v]
if u = NULL then
parent[u] parent[v]
if parent[v] = NULL then
root[T ] u
else
if v = leftchild[parent[v]] then
leftchild[parent[v]] u
else
rightchild[parent[v]] u
if v = x then
x v
copy vs auxilary data into x
189
2. Suppose v has the single child u. Removing v would disconnect T , a situation that
can be prevented by splicing out u and letting u occupy the position previously
held by v. The resulting tree with v removed as described satisfies the binary
search tree property.
3. Finally suppose v has two children and let s and p be the successor and predecessor
of v, respectively. It can be shown that s has no left-child and p has no right-child.
We can choose to either splice out s or p. Say we choose to splice out s. Then we
remove v and let s hold the position previously occupied by v. The resulting tree
with v thus removed satisfies the binary search tree property.
The above procedure is summarized in Algorithm 4.15, which is adapted from [61, p.262].
Figure 4.15 illustrates the various cases to be considered when removing a vertex from
a BST. Note that in Algorithm 4.15, the process of finding the successor dominates the
runtime of the entire algorithm. Other operations in the algorithm take at most constant
time. Therefore deleting a vertex from a binary search tree can be accomplished in worstcase O(h) time, where h is the height of the BST under consideration.
4.5
AVL trees
To motivate the need for AVL trees, note the lack of a structural property for binary
search trees similar to the structural property for binary heaps. Unlike binary heaps,
a BST is not required to have as small a height as possible. As a consequence, any
given nonempty collection C = {v0 , v1 , . . . , vk } of weighted vertices can be represented by
various BSTs with different heights; see Figure 4.16. Some BST representations of C have
heights smaller than other BST representations of C. Those BST representations with
smaller heights can result in reduced time for basic operations such as search, insertion,
and deletion and out-perform BST representations having larger heights. To achieve
logarithmic or near-logarithmic time complexity for basic operations, it is desirable to
maintain a BST with as small a height as possible.
Adelson-Velski and Landis [1] introduced in 1962 a criterion for constructing and
maintaining binary search trees having logarithmic heights. Recall that the height of a
tree is the maximum depth of the tree. Then the Adelson-Velski-Landis criterion can
be expressed as follows.
Definition 4.6. Height-balance property. Let T be a binary tree and suppose v is
an internal vertex of T . Let h be the height of the left subtree of v and let hr be the
height of vs right subtree. Then v is said to be height-balanced if |h hr | 1. For each
internal vertex u of T , if u is height-balanced then the whole tree T is height-balanced.
Binary trees having the height-balance property are called AVL trees. The structure
of such trees is such that given any internal vertex v of an AVL tree, the heights of the
left and right subtrees of v differ by at most 1. Complete binary trees are trivial examples
of AVL trees, as are nearly complete binary trees. A less trivial example of AVL trees
are what is known as Fibonacci trees, so named because the construction of Fibonacci
trees bears some resemblance to how Fibonacci numbers are produced. Fibonacci trees
can be constructed recursively in the following manner. The Fibonacci tree F0 of height
0 is the trivial tree. The Fibonacci tree F1 of height 1 is a binary tree whose left and
right subtrees are both F0 . For n > 1, the Fibonacci tree Fn of height n is a binary
190
10
10
15
13
20
11
19
23
15
22
26
13
13
26
15
23
10
19
22
10
11
20
20
12
19
23
11
15
22
26
12
20
11
19
23
22
26
10
15
10
13
20
12
18
11
17
23
19
22
16
26
16
13
20
12
11
18
17
23
19
22
26
191
20
15
13
13
10
10
10
15
13
7
5
15
13
20
10
20
20
(a)
15
(b)
(c)
(d)
(a) F0
(b) F1
(c) F2
(d) F3
(e) F4
(f) F5
Theorem 4.7. Logarithmic height. The height h of an AVL tree with n internal
vertices is bounded by
lg(n + 1) h < 2 lg n + 1.
Proof. Any binary tree of height h has at most 2i leaves. From the proof of Corollary 4.3,
we see that n is bounded by 2h1 n 2h 1 and in particular n + 1 2h . Take the
logarithm of both sides to get h lg(n + 1).
192
Figure 4.18: Fibonacci tree F6 with subtree heights for vertex labels.
Now instead of deriving an upper bound for h, we find the minimum order of an AVL
tree and from there derive the required upper bound for h. Let T be an AVL tree of
minimum order. One subtree of T has height h1. The other subtree has height h1 or
h 2. Our objective is to construct T to have as small a number of vertices as possible.
Without loss of generality, let the left and right subtrees of T have heights h 2 and
h 1, respectively. The Fibonacci tree Fh of height h fits the above requirements for T .
If N (h) denote the number of internal vertices of Fh , then N (h) = 1+N (h1)+N (h2)
is strictly increasing so
N (h) > N (h 2) + N (h 2) = 2 N (h 2).
(4.3)
(4.4)
4.5.1
Insertion
The algorithm for insertion into a BST can be modified and extended to support insertion
into an AVL tree. Let T be an AVL tree having the binary search tree property, and v
193
a vertex to be inserted into T . In the trivial case, T is the null tree so inserting v into T
is equivalent to letting T be the trivial tree rooted at v. Consider now the case where T
has at least one vertex. Apply Algorithm 4.14 to insert v into T and call the resulting
augmented tree Tv . But our problem is not yet over; Tv may violate the height-balance
property. To complete the insertion procedure, we require a technique to restore, if
necessary, the height-balance property to Tv .
To see why the augmented tree Tv may not necessarily be height-balanced, let u be
the parent of v in Tv , where previously u was a vertex T (and possibly a leaf). In the
original AVL tree T , let Pu : r = u0 , u1 , . . . , uk = u be the path from the root r of T
to u with corresponding subtree heights H(ui ) = hi for i = 0, 1, . . . , k. An effect of the
insertion is to extend the path Pu to the longer path Pv : r = u0 , u1 , . . . , uk = u, v and
possibly increase subtree heights by one. One of two cases can occur with respect to Tv .
1. Height-balanced: Tv is height-balanced so no need to do anything further. A simple
way to detect this is to consider the subtree S rooted at u, the parent of v. If S
has two children, then no height adjustment need to take place for vertices in Pu ,
hence Tv is an AVL tree (see Figure 4.19). Otherwise we perform any necessary
height adjustment for vertices in Pu , starting from uk = u and working our way
up to the root r = u0 . After adjusting the height of ui , we test to see whether
ui (with its new height) is height-balanced. If each of the ui with their new heights
are height-balanced, then Tv is height-balanced.
2. Height-unbalanced: During the height adjustment phase, it may happen that some
uj with its new height is not height-balanced. Among all such height-unbalanced
vertices, let u be the first height-unbalanced vertex detected during the process of
height adjustment starting from uk = u and going up towards r = u0 . We need to
rebalance the subtree rooted at u . Then we continue on adjusting heights of the
remaining vertices in Pu , also performing height-rebalancing where necessary.
Case 1 is relatively straightforward, but it is case 2 that involves much intricate work.
4
4
3
2
0
1
0
1
0
4
3
2
0
2
0
1
0
1
0
2
0
2
0
1
0
1
0
2
0
1
0
Figure 4.19: Augmented tree is balanced after insertion; vertex labels are heights.
We now turn to the case where inserting a vertex v into a nonempty AVL tree T
results in an augmented tree Tv that is not height-balanced. A general idea for rebalancing (and hence restoring the height-balance property to) Tv is to determine where in
Tv the height-balance property is first violated (the search phase), and then to locally
rebalance subtrees at and around the point of violation (the repair phase). A description
of the search phase follows. Let
Pv : r = u0 , u1 , . . . , uk = u, v
194
be the path from the root r of Tv (and hence of T ) to v. Traversing upward from v to r,
let z be the first height-unbalanced vertex. Among the children of z, let y be the child of
higher height and hence an ancestor of v. Similarly, among the children of y let x be the
child of higher height. In case a tie occurs, let x be the child of y that is also an ancestor
of v. As each vertex is an ancestor of itself, it is possible that x = v. Furthermore, x is
a grandchild of z because x is a child of y, which in turn is a child of z. The vertex z
is not height-balanced due to inserting v into the subtree rooted at y, hence the height
of y is 2 greater than its sibling (see Figure 4.20, where height-unbalanced vertices are
colored red). We have determined the location at which the height-balance property is
first violated.
5
4
1
0
3 z
2
1
0
2
0
0
2 y
1 x
0 v
4 z
1
0
0
2
1
0
1
0
3 y
1
0
2 x
0
1
0 v
Figure 4.20: Augmented tree is unbalanced after insertion; vertex labels are heights.
We now turn to the repair phase. The central question is: How are we to restore the height-balance property to the subtree rooted at z? By trinode restructuring is meant the process whereby the height-balance property is restored; the prefix
tri refers to the three vertices x, y, z that are central to this process. A common
name for the trinode restructuring is rotation in view of the geometric interpretation
of the process. Figure 4.21 distinguishes four rotation possibilities, two of which are
symmetrical to the other two. The single left rotation in Figure 4.21(a) occurs when
height(x) = height(root(T0 )) + 1 and detailed in Algorithm 4.16. The single right rotation in Figure 4.21(b) occurs when height(x) = height(root(T3 )) + 1; see Algorithm 4.17
for pseudocode. Figure 4.21(c) illustrates the case of a right-left double rotation and
occurs when height(root(T3 )) = height(root(T0 )); see Algorithm 4.18 for pseudocode to
handle the rotation. The fourth case is illustrated in Figure 4.21(d) and occurs when
height(root(T0 )) = height(root(T3 )); refer to Algorithm 4.19 for pseudocode to handle
this left-right double rotation. Each of the four algorithms mentioned above run in constant time O(1) and preserves the in-order traversal ordering of all vertices in Tv . In all,
the insertion procedure is summarized in Algorithm 4.20. If h is the height T , locating and inserting the vertex v takes worst-case O(h) time, which is also the worst-case
runtime for the search-and-repair phase. Thus letting n be the number of vertices in T ,
insertion takes worst-case O(lg n) time.
4.5.2
Deletion
The process of removing a vertex from an AVL tree is similar to the insertion procedure. However, instead of using the insertion algorithm for BST, we use the deletion
195
z
y
y
z
T0
T1
T2
T3
T0
T1
T2
T3
z
y
T3
T2
T0
T1
T0
T1
T2
T3
z
y
T0
T3
T1
T0
T2
T1
T2
T3
(c) Double rotation: right rotation of x over y, then left rotation over z.
z
y
x
x
T3
T0
T1
T2
T0
T1
T2
T3
(d) Double rotation: left rotation of x over y, then right rotation over z.
196
rightchild[parent[z]] y
parent[y] parent[z]
parent[z] y
leftchild[y] z
parent[root[T1 ]] z
rightchild[z] root[T1 ]
height[z] 1 + max(height[root[T0 ]], height[root[T1 ]])
height[x] 1 + max(height[root[T2 ]], height[root[T3 ]])
height[y] 1 + max(height[x], height[z])
leftchild[parent[z]] y
parent[y] parent[z]
parent[z] y
rightchild[y] z
parent[root[T2 ]] z
leftchild[z] root[T2 ]
height[x] 1 + max(height[root[T0 ]], height[root[T1 ]])
height[z] 1 + max(height[root[T2 ]], height[root[T3 ]])
height[y] 1 + max(height[x], height[z])
197
rightchild[parent[z]] x
parent[x] parent[z]
parent[z] x
leftchild[x] z
rightchild[x] y
rightchild[z] root[T1 ]
parent[root[T1 ]] z
parent[y] x
leftchild[y] root[T2 ]
parent[root[T2 ]] y
height[z] 1 + max(height[root[T0 ]], height[root[T1 ]])
height[y] 1 + max(height[root[T2 ]], height[root[T3 ]])
height[x] 1 + max(height[y], height[z])
leftchild[parent[z]] x
parent[x] parent[z]
parent[z] x
rightchild[x] z
leftchild[z] root[T2 ]
parent[T2 ] z
leftchild[x] y
parent[y] x
rightchild[y] root[T1 ]
parent[root[T1 ]] y
height[z] 1 + max(height[root[T2 ]], height[root[T3 ]])
height[y] 1 + max(height[root[T0 ]], height[root[T1 ]])
height[x] 1 + max(height[y], height[z])
198
4.6. Problems
199
Algorithm 4.15 for BST to remove the target vertex from an AVL tree. The resulting tree may violate the height-balance property, which can be restored using trinode
restructuring.
Let T be an AVL tree having vertex v and suppose we want to remove v from T . In
the trivial case, T is the trivial tree whose sole vertex is v. Deleting v is simply removing
it from T so that T becomes the null tree. On the other hand, suppose T has at least
n > 1 vertices. Apply Algorithm 4.15 to remove v from T and call the resulting tree with
v removed Tv . It is possible that Tv does not satisfy the height-balance property. To
restore the height-balance property to Tv , let u be the parent of v in T prior to deleting
v from T . Having deleted v from T , let P : r = u0 , u1 , . . . , uk = u be the path from the
root r of Tv to u. Adjust the height of u and, traversing from u up to r, perform height
adjustment to each vertex in P and where necessary carry out trinode restructuring. The
resulting algorithm is very similar to Algorithm 4.20; see Algorithm 4.21 for pseudocode.
The deletion procedure via Algorithm 4.15 requires worst-case runtime O(lg n), where
n is the number of vertices in T , and the height-adjustment process runs in worst-case
O(lg n) time as well. Thus Algorithm 4.21 has worst-case runtime of O(lg n).
4.6
Problems
No problem is so formidable that you cant walk away from it.
Charles M. Schulz
200
u parent[v]
delete v from T as per Algorithm 4.15
adjust the height of u
/* begin height adjustment */
x NULL
y NULL
z NULL
while parent[u] = NULL do
u parent[u]
if leftchild[u] = NULL and rightchild[u] = NULL then
h height[leftchild[u]]
hr height[rightchild[u]]
height[u] 1 + max(h , hr )
if |h hr | > 1 then
if height[rightchild[rightchild[u]]] = height[leftchild[u]] + 1 then
zu
y rightchild[z]
x rightchild[y]
trinode restructuring as per Algorithm 4.16
continue with next iteration of loop
if height[leftchild[leftchild[u]]] = height[rightchild[u]] + 1 then
zu
y leftchild[z]
x leftchild[y]
trinode restructuring as per Algorithm 4.17
continue with next iteration of loop
if height[rightchild[rightchild[u]]] = height[leftchild[u]] then
zu
y rightchild[z]
x leftchild[y]
trinode restructuring as per Algorithm 4.18
continue with next iteration of loop
if height[leftchild[leftchild[u]]] = height[rightchild[u]] then
zu
y leftchild[z]
x rightchild[y]
trinode restructuring as per Algorithm 4.19
continue with next iteration of loop
if leftchild[u] = NULL then
height[u] 1 + height[leftchild[u]]
continue with next iteration of loop
if rightchild[u] = NULL then
height[u] 1 + height[rightchild[u]]
continue with next iteration of loop
4.6. Problems
201
4.6. Let S be a sequence of n > 1 real numbers. How can we use algorithms described
in section 4.2 to sort S?
4.7. The binary heaps discussed in section 4.2 are properly called minimum binary
heaps because the root of the heap is always the minimum vertex. A corresponding notion is that of maximum binary heaps, where the root is always the maximum
element. Describe algorithms analogous to those in section 4.2 for managing maximum binary heaps.
4.8. What is the total time required to extract all elements from a binary heap?
4.9. Numbers of the form nr are called binomial coefficients. They also count the
number of r-combinations from a set of n objects. Algorithm 4.22 presents pseudocode to generate all the r-combinations of a set of n distinct objects. What is the
worst-case runtime of Algorithm 4.22? Prove the correctness of Algorithm 4.22.
4.10. In contrast to enumerating all the r-combinations of a set of n objects, we may
only want to generate a random r-combination. Describe and present pseudocode
of a procedure to generate a random r-combination of {1, 2, . . . , n}.
4.11. A problem related to the r-combinations of the set S = {1, 2, . . . , n} is that of
generating the permutations of S. Algorithm 4.23 presents pseudocode to generate
all the permutations of S in increasing lexicographic order. Find the worst-case
runtime of this algorithm and prove its correctness.
4.12. Provide a description and pseudocode of an algorithm to generate a random permutation of {1, 2, . . . , n}.
4.13. Takaoka [189] presents a general method for combinatorial generation that runs in
O(1) time. How can Takaokas method be applied to generating combinations and
permutations?
4.14. The proof of Lemma 4.4 relies on Pascals formula, which states that for any
positive integers n and r such that r n, the following identity holds:
n+1
r
n
n
+
.
r1
r
=
k=0
m+k
.
k
202
L []
ci i for i = 1, 2, . . . , r
append(L, c1 c2 cr )
for i 2, 3, . . . , nr do
mr
max n
while cm = max do
mm1
max max 1
cm cm + 1
cj cj1 + 1 for j = m + 1, m + 2, . . . , r
append(L, c1 c2 cr )
return L
L []
ci i for i = 1, 2, . . . , n
append(L, c1 c2 cn )
for i 2, 3, . . . , n! do
mn1
while cm > cm+1 do
mm1
kn
while cm > ck do
k k1
swap the values of cm and ck
pm+1
qn
while p < q do
swap the values of cp and cq
pp+1
q q1
append(L, c1 c2 cn )
return L
4.6. Problems
203
4.17. Let n be a positive integer. How many distinct binomial heaps having n vertices
are there?
4.18. The algorithms described in section 4.3 are formally for minimum binomial heaps
because the vertex at the top of the heap is always the minimum vertex. Describe
analogous algorithms for maximum binomial heaps.
4.19. If H is a binomial heap, what is the total time required to extract all elements
from H?
4.20. Frederickson [85] describes an O(k) time algorithm for finding the k-th smallest
element in a binary heap. Provide a description and pseudocode of Fredericksons
algorithm and prove its correctness.
4.21. Fibonacci heaps [86] allow for amortized O(1) time with respect to finding the
minimum element, inserting an element, and merging two Fibonacci heaps. Deleting the minimum element takes amortized time O(lg n), where n is the number
of vertices in the heap. Describe and provide pseudocode of the above Fibonacci
heap operations and prove the correctness of the procedures.
4.22. Takaoka [190] introduces another type of heap called a 2-3 heap. Deleting the
minimum element takes amortized O(lg n) time with n being the number of vertices
in the 2-3 heap. Inserting an element into the heap takes amortized O(1) time.
Describe and provide pseudocode of the above 2-3 heap operations. Under which
conditions would 2-3 heaps be more efficient than Fibonacci heaps?
4.23. In 2000, Chazelle [54] introduced the soft heap, which can perform common heap
operations in amortized O(1) time. He then applied [53] the soft heap to realize a
very efficient implementation of an algorithm for finding minimum spanning trees.
In 2009, Kaplan and Zwick [124] provided a simple implementation and analysis of
Chazelles soft heap. Describe soft heaps and provide pseudocode of common heap
operations. Prove the correctness of the algorithms and provide runtime analyses.
Describe how to use soft heap to realize an efficient implementation of an algorithm
to produce minimum spanning trees.
4.24. Explain any differences between the binary heap-order property, the binomial heaporder property, and the binary search tree property. Can in-order traversal be used
to list the vertices of a binary heap in sorted order? Explain why or why not.
4.25. Present pseudocode of an algorithm to find a vertex with maximum key in a binary
search tree.
4.26. Compare and contrast algorithms for locating minimum and maximum elements
in a list with their counterparts for a binary search tree.
4.27. Let T be a nonempty BST and suppose v V (T ) is not a minimum vertex of T .
If h is the height of T , describe and present pseudocode of an algorithm to find the
predecessor of v in worst-case time O(h).
4.28. Let L = [v0 , v1 , . . . , vn ] be the in-order listing of a BST T . Present an algorithm
to find the successor of v V (T ) in constant time O(1). How can we find the
predecessor of v in constant time as well?
204
4.29. Modify Algorithm 4.15 to extract a minimum vertex of a binary search tree. Now
do the same to extract a maximum vertex. How can Algorithm 4.15 be modified
to extract a vertex from a binary search tree?
4.30. Let v be a vertex of a BST and suppose v has two children. If s and p are the
successor and predecessor of v, respectively, show that s has no left-child and p has
no right-child.
4.31. Let L = [e0 , e1 , . . . , en ] be a list of n + 1 elements from a totally ordered set X with
total order . How can binary search trees be used to sort L?
4.32. Describe and present pseudocode of a recursive algorithm for each of the following
operations on a BST.
(a) Find a vertex with a given key.
(b) Locate a minimum vertex.
(c) Locate a maximum vertex.
(d) Insert a vertex.
4.33. Are the algorithms presented in section 4.4 able to handle a BST having duplicate
keys? If not, modify the relevant algorithm(s) to account for the case where two
vertices in a BST have the same key.
4.34. The notion of vertex level for binary trees can be extended to general rooted trees
as follows. Let T be a rooted tree with n > 0 vertices and height h. Then level
0 i h of T consists of all those vertices in T that have the same depth i. If
each vertex at level i has i + m children for some fixed integer m > 0, what is the
number of vertices at each level of T ?
4.35. Compare the search, insertion, and deletion times of AVL trees and random binary
search trees. Provide empirical results of your comparative study.
4.36. Describe and present pseudocode of an algorithm to construct a Fibonacci tree of
height n for some integer n 0. Analyze the worst-case runtime of your algorithm.
4.37. The upper bound in Theorem 4.7 can be improved as follows. From the proof of
the theorem, we have the recurrence relation N (h) > N (h 1) + N (h 2).
(a) If h 2, show that there exists some c > 0 such that N (h) ch .
(b) Assume for induction that
N (h) > N (h 1) + N (h 2) ch1 + ch2
for some h > 2. If c > 0, show that c2 c 1 = 0 is a solution to the
recurrence relation ch1 + ch2 and that
N (h) >
1+ 5
2
4.6. Problems
205
1
lg n
lg
where = (1 + 5)/2 is the golden ratio and n counts the number of internal
vertices of an AVL tree of height h.
4.38. The Fibonacci sequence Fn is defined as follows. We have initial values F0 = 0
and F1 = 1. For n > 1, the n-th term in the sequence can be obtained via the
recurrence relation Fn = Fn1 + Fn2 . Show that
n (1/)n
Fn =
5
(4.5)
where is the golden ratio. The closed form solution (4.5) to the Fibonacci sequence is known as Binets formula, named after Jacques Philippe Marie Binet,
even though Abraham de Moivre knew about this formula long before Binet did.
Chapter 5
Distance and connectivity
5.1
5.1.1
W (P ) =
W (ei ).
i=1
206
(5.1)
207
otherwise, where the minimum is taken over all paths P from v1 to v2 . By hypothesis, G
has no negative weight cycles so the minimum in (5.1) exists. It follows by definition of
the distance function that d(u, v) = if and only if there is no path between u and v.
How we interpret the distance function d depends on the meaning of the weight
function W . In practical applications, vertices can represent physical locations such as
cities, sea ports, or landmarks. An edge weight could be interpreted as the physical
distance in kilometers between two cities, the monetary cost of shipping goods from one
sea port to another, or the time required to travel from one landmark to another. Then
d(u, v) could mean the shortest route in kilometers between two cities, the lowest cost
incurred in transporting goods from one sea port to another, or the least time required
to travel from one landmark to another.
The distance function d is not in general a metric, i.e. the triangle inequality does
not in general hold for d. However, when the distance function is a metric then G is
called a metric graph. The theory of metric graphs, due to their close connection with
tropical curves, is an active area of research. For more information on metric graphs, see
Baker and Faber [12].
5.1.2
A new hospital is to be built in a large city. Construction has not yet started and a
number of urban planners are discussing the future location of the new hospital. What
is a possible location for the new hospital and how are we to determine this location?
This is an example of a class of problems known as facility location problems. Suppose
our objective in selecting a location for the hospital is to minimize the maximum response
time between the new hospital and the site of an emergency. To help with our decision
making, we could use the notion of the center of a graph.
The center of a graph G = (V, E) is defined in terms of the eccentricity of the graph
under consideration. The eccentricity : V R is defined as follows. For any vertex
v, the eccentricity (v) is the greatest distance between v and any other vertex in G. In
symbols, the eccentricity is expressible as
(v) = max d(u, v).
uV
For example, in a tree T with root r the eccentricity of r is the height of T . In the graph
of Figure 5.1, the eccentricity of 2 is 5 and the shortest paths that yield (2) are
P1 : 2, 3, 4, 14, 15, 16
P2 : 2, 3, 4, 14, 15, 17.
The eccentricity of a vertex v can be thought of as an upper bound on the distance from
v to any other vertex in G. Furthermore, we have at least one vertex in G whose distance
from v is (v).
v 1
(v) 6
2
5
3
4
4
4
5
5
6
6
7
7
8
7
9
5
10
6
11
7
12
7
13
6
14
5
15 16
6 7
17
7
208
16
11
12
15
10
13
14
17
(v)
4
0
10
15
Figure 5.2: Eccentricity distribution of the graph in Figure 5.1. The horizontal axis
represents the vertex name, while the vertical axis is the corresponding eccentricity.
209
is shown in Table 5.1. Among the eccentricities in the latter table, the minimum eccentricity is (3) = (4) = 4. An intuitive interpretation is that both of the vertices 3 and
4 have the shortest distance to any other vertices in G. We can invoke an analogy with
plane geometry as follows. If a circle has radius r, then the distance from the center
of the circle to any point within the circle is at most r. The minimum eccentricity in
graph theory plays a role similar to the radius of a circle. If an object is strategically
positionede.g. a vertex with minimum eccentricity or the center of a circlethen its
greatest distance to any other object is guaranteed to be minimum. With the above
analogy in mind, we define the radius of a graph G = (V, E), written rad(G), to be the
minimum eccentricity among the eccentricity distribution of G. In symbols,
rad(G) = min (v).
vV
The center of G, written C(G), is the set of vertices with minimum eccentricity. Thus
the graph in Figure 5.1 has radius 4 and center {3, 4}. As should be clear from the latter
example, the radius is a number whereas the center is a set. Refer to the beginning of
the section where we mentioned the problem of selecting a location for a new hospital.
We could use a graph to represent the geography of the city wherein the hospital is to
be situated and select a location that is in the center of the graph.
Consider now the maximum eccentricity of a graph. In (2.5) we defined the diameter
of a graph G = (V, E) by
diam(G) = max d(u, v).
u,vV
u=v
The diameter of G can also be defined as the maximum eccentricity of any vertex in G:
diam(G) = max (v).
vV
210
5.1.3
Center of trees
Given a tree T of order 3, we want to derive a bound on the number of vertices that
comprise the center of T . A graph in general can have one, two, or more number of
vertices for its center. Indeed, for any integer n > 0 we can construct a graph whose
center has cardinality n. The cases for n = 1, 2, 3 are illustrated in Figure 5.3. But can
we do the same for trees? That is, given any positive integer n does there exist a tree
whose center has n vertices? It turns out that the center of a tree cannot have more
than two vertices, a result first discovered [120] by Camille Jordan in 1869.
(a) |C(G)| = 1
(b) |C(G)| = 2
(c) |C(G)| = 3
5.1.4
Distance matrix
In sections 1.3.4 and 2.3, the distance matrix D of a graph G was defined to be D = [dij ],
where dij = d(vi , vj ) and the vertices of G are indexed by V = {v0 , v1 , . . . , vk }. The
matrix D is square where we set dij = 0 for entries along the main diagonal. If there is
no path from vi to vj , then we set dij = . If G is undirected, then D is symmetric and
is equal to its transpose, i.e. DT = D. To compute the distance matrix D, apply the
Floyd-Roy-Warshall algorithm to determine the distances between all pairs of vertices.
Refer to Figure 5.4 for examples of distance matrices of directed and undirected graphs.
In the remainder of this section, graph refers to an undirected graph unless otherwise
specified.
Instead of one distance matrix, we can define several distance matrices on G. Consider
an edge-weighted graph G = (V, E) without negative weight cycles and let
d : V V R {}
be a distance function of G. Let = diam(G) be the diameter of G and index the
vertices of G in some arbitrary but fixed manner, say V = {v0 , v1 , . . . , vn }. The sequence
211
3
5
(a)
1
0
1
2
2
1
0
1
1
0
1
2
2
3
2
1
0
1
3
2
2
0
1
1
0
3
5
1
0
(b)
0
1
1
2
3
2
1
0
2
1
1
2
3
2
0
1
2
3
1
0
5.2
212
vertex connectivity v (G) is also written as (G). The vertex connectivity of the graph in
Figure 5.5 is v (G) = 1 because we only need to remove vertex 0 in order to disconnect
the graph. The vertex connectivity of a connected graph G is thus the vertex-cut of
minimum cardinality. And G is said to be k-connected if v (G) k. From the latter
definition, it immediately follows that if G has at least 3 vertices and is k-connected then
any vertex-cut of G has at least cardinality k. For instance, the graph in Figure 5.5 is
1-connected. In other words, G is k-connected if the graph remains connected even after
removing any k 1 or fewer vertices from G.
0
G = graphs . PetersenGraph ()
len ( G . vertices ())
G . vertex_connectivity ()
G . delete_vertex (0)
len ( G . vertices ())
G . vertex_connectivity ()
The notions of edge-cut and cut-edge are similarly defined. Let G = (V, E) be a
graph and D E an edge set such that the edge deletion subgraph G D has more
components than G. Then D is called an edge-cut. An edge-cut D is said to be minimal
213
G = graphs . PetersenGraph ()
len ( G . vertices ())
E = G . edges (); len ( E )
G . edge_connectivity ()
G . delete_edge ( E [0])
len ( G . edges ())
G . edge_connectivity ()
Vertex and edge connectivity are intimately related to the reliability and survivability
of computer networks. If a computer network G (which is a connected graph) is kconnected, then it would remain connected despite the failure of at most k 1 network
nodes. Similarly, G is k-edge-connected if the network remains connected after the failure
of at most k 1 network links. In practical terms, a network with redundant nodes
and/or links can afford to endure the failure of a number of nodes and/or links and
still be connected, whereas a network with very few redundant nodes and/or links (e.g.
something close to a spanning tree) is more prone to be disconnected. A k-connected or
k-edge-connected network is more robust (i.e. can withstand) against node and/or link
failures than is a j-connected or j-edge-connected network, where j < k.
Proposition 5.5. If (G) is the minimum degree of an undirected connected graph G =
(V, E), then the edge connectivity of G satisfies (G) (G).
Proof. Choose a vertex v V whose degree is deg(v) = (G). Deleting the (G) edges
incident on v suffices to disconnect G as v is now an isolated vertex. It is possible that
G has an edge-cut whose cardinality is smaller than (G). Hence the result follows.
Let G = (V, E) be a graph and suppose X1 and X2 comprise a partition of V . A
partition-cut of G, denoted X1 , X2 , is the set of all edges of G with one endpoint in
X1 and the other endpoint in X2 . If G is a bipartite graph with bipartition X1 and X2 ,
then X1 , X2 is a partition-cut of G. It follows that a partition-cut is also an edge-cut.
214
215
216
Note that d(w, wk1 ) < k and apply the induction hypothesis to see that we have two
internally disjoint w-wk1 paths in G; call these paths P and Q. As G is 2-connected,
we have a w-x path R in G wk1 and hence R is also a w-x path in G. Let z be the
vertex on R that immediately precedes x and assume without loss of generality that z
is on P . We claim that G has two internally disjoint w-x paths. One of these paths is
the concatenation of the subpath of P from w to z with the subpath of R from z to x.
If x is not on Q, then construct a second w-x path, internally disjoint from the first one,
as follows: concatenate the path Q with the edge wk1 w. In case x is on Q, take the
subpath of Q from w to x as the required second path.
From Theorem 5.11, an undirected connected graph G is 2-connected if and only if any
two distinct vertices of G are connected by two internally disjoint paths. In particular,
let u and v be any two distinct vertices of G and let P and Q be two internally disjoint
u-v paths as guaranteed by Theorem 5.11. Starting from u, travel along the path P
to arrive at v. Then start from v and travel along the path Q to arrive at u. The
concatenation of the internally disjoint paths P and Q is hence a cycle passing through
u and v. We have proved the following corollary to Theorem 5.11.
Corollary 5.12. Let G be an undirected connected graph having at least 3 vertices. Then
G is 2-connected if and only if any two distinct vertices of G lie on a common cycle.
The following theorem provides further characterizations of 2-connected graphs, in
addition to Whitneys characterization.
Theorem 5.13. Characterizations of 2-connected graphs. Let G = (V, E) be an
undirected connected graph having at least 3 vertices. Then the following are equivalent.
1. G is 2-connected.
2. If u, v V are distinct vertices of G, then u and v lie on a common cycle.
3. If v V and e E, then v and e lie on a common cycle.
4. If e1 , e2 E are distinct edges of G, then e1 and e2 lie on a common cycle.
5. If u, v V are distinct vertices and e E, then they lie on a common path.
6. If u, v, w V are distinct vertices, then they lie on a common path.
7. If u, v, w V are distinct vertices, then there is a path containing any two of these
vertices but excluding the third.
5.3
Mengers theorem
217
deletion subgraph G S. The vertices u and v are positioned such that after removing
vertices in S from G and the corresponding edges, u and v are no longer connected nor
strongly connected to each other. It is clear by definition that u, v
/ S. We also say
that S separates u and v, or S is a vertex separating set. Similarly an edge set T E is
u-v separating (or separates u and v) if u and v lie in different components of the edge
deletion subgraph G T . But unlike the case of vertex separating sets, it is possible for
u and v to be endpoints of edges in T because the removal of edges does not result in
deleting the corresponding endpoints. The set T is also called an edge separating set. In
other words, S is a vertex cut and T is an edge cut. When it is clear from context, we
simply refer to a separating set. See Figure 5.7 for illustrations of separating sets.
Figure 5.7: Vertex and edge separating sets. Blue-colored vertices are those we want
to separate. The red-colored vertices form a vertex separating set or vertex cut; the
red-colored edges constitute an edge separating set or edge cut.
(5.2)
Proof. Each u-v path in Puv must include at least one vertex from Suv because Suv is
a vertex cut of G. Any two distinct paths in Puv cannot contain the same vertex from
Suv . Thus the number of internally disjoint u-v paths is at most |Suv |.
The bound (5.2) holds for any u-v separating set Suv of vertices in G. In particular,
we can choose Suv to be of minimum cardinality among all u-v separating sets of vertices
in G. Thus we have the following corollary. Mengers Theorem 5.18 provides a much
stronger statement of Corollary 5.15, saying in effect that the two quantities max(|Puv |)
and min(|Suv |) are equal.
Corollary 5.15. Consider any two distinct, non-adjacent vertices u, v in a connected
graph G. Let max(|Puv |) be the maximum number of internally disjoint u-v paths in G
and denote by min(|Suv |) the minimum cardinality of a u-v separating set of vertices in
G. Then we have max(|Puv |) min(|Suv |).
218
219
so that k . Let e E and let G/e be the contraction graph having edges E {e}
and vertices the same as those of G, except that the endpoints of e have been identified.
Suppose that k < and G does not have independent u-v paths. The contraction
graph G/e does not have independent u-v paths either (where now, if e contains u or
v then we must appropriately redefine u or v, if needed). However, by the induction
hypothesis G/e does have the property that the maximum number of internally disjoint
u-v paths equals the minimum number of vertices needed to separate u and v. Therefore,
#{independent u v paths in G/e}
< #{min. number of vertices needed to separate u and v in G}.
By induction,
#{independent u v paths in G/e}
= #{min. number of vertices needed to separate u and v in G/e}.
Now, we claim we can pick e such that e does contain u or v and in such a way that
#{minimum number of vertices needed to separate u and v in G}
#{minimum number of vertices needed to separate u and v in G/e}.
Proof: Indeed, since n > 3 any separating set realizing the minimum number of vertices
needed to separate u and v in G cannot contain both a vertex in G adjacent to u and a
vertex in G adjacent to v. Therefore, we may pick e accordingly. (Q.E.D. claim)
The result follows from the claim and the above inequalities.
The following statement is the undirected, edge-connectivity version of Mengers theorem.
Theorem 5.19. Mengers theorem (edge-connectivity form). Let G be an undirected graph, and let s and t be vertices in G. Then, the maximum number of edgedisjoint (s, t)-paths in G equals the minimum number of edges from E(G) whose deletion
separates s and t.
This is proven the same way as the previous version but using the generalized mincut/max-flow theorem (see Remark 9.16 above).
Theorem 5.20. Diracs theorem. Let G = (V, E) be an undirected k-connected graph
with |V | k + 1 vertices for k 3. If S V is any set of k vertices, then G has a cycle
containing the vertices of S.
Proof.
5.4
Whitneys Theorem
220
Solution. ...
Solution. ...
Theorem 5.23. Whitneys Theorem. Let G = (V, E) be a connected graph such that
|V | 3. Then G is 2-connected if and only if any pair u, v V has two internally
disjoint paths between them.
5.5
Centrality of a vertex
Louis, I think this is the beginning of a beautiful friendship.
Rick from the 1942 film Casablanca
degree centrality
betweenness centrality; for efficient algorithms, see [39, 213]
closeness centrality
eigenvector centrality
The degree centrality of a graph G = (V, E) is the list parameterized by the vertex set
V of G whose v-th entry is the fraction of vertices connected to v V . The centrality of
a vertex within a graph determines the relative importance of that vertex to its graph.
Degree centrality measures the number of edges incident upon a vertex.
sage : G = graphs . R a n d o m N e w m an W a t t s S t r o g a t z (6 ,2 ,1/2)
sage : G
Graph on 6 vertices
sage : D = G . degree_sequence ()
sage : D
[5 , 4 , 3 , 3 , 3 , 2]
sage : VG = G . vertices ()
sage : VG
[0 , 1 , 2 , 3 , 4 , 5]
sage : DC = [ QQ ( x )/ len ( VG ) for x in D ]
sage : DC
[5/6 , 2/3 , 1/2 , 1/2 , 1/2 , 1/3]
221
Figure 5.8: A randomly choosen graph whose central-most vertex is 0, with degree
centrality equal to 5/6.
if n = 1 then
return C3
G null graph
N 2n + 1
for i 0, 1, . . . , N 3 do
if i is odd then
add edges (i, i + 1) and (i, N 1) to G
else
add edge (i, N 1) to G
add edges (N 2, 0) and (N 2, N 1) to G
return E
222
5.6
Network reliability
Whitney synthesis
Tuttes synthesis of 3-connected graphs
Harary graphs
constructing an optimal k-connected n-vertex graph
5.7
We use the notes The spectrum of a graph by Andries Brouwer [43] as a basic reference.
Spectrum of a graph
Laplacian spectrum of a graph
Applications
Examples from Boolean functions
5.7.1
Recall from section 1.3.3 that, given a simple graph G with n vertices V = {v1 , . . . , vn },
its (vertex) Laplacian matrix L = ( i,j )nn is defined as:
i,j
deg(vi ), if i = j,
= 1,
if i = j and vi is adjacent to vj ,
0,
otherwise.
The Laplacian spectrum is by definition the spectrum of the vertex Laplacian of G, that
is, its multi-set of eigenvalues together with their multiplicities.
For a graph G and its Laplacian matrix L with eigenvalues n n1 1 :
For all i, i 0 and n = 0.
The number of times 0 appears as an eigenvalue in the Laplacian is the number of
connected components in the graph.
n = 0 because every Laplacian matrix has an eigenvector of [1, 1, . . . , 1],
223
If we define a signed edge adjacency matrix M with element me,v for edge e E
(connecting vertex vi and vj , with i < j) and vertex v V given by
Mev
if v = vi ,
1,
= 1, if v = vj ,
0,
otherwise
5.7.2
5.7.3
224
where b(k) is the binary representation of the integer k. Note f is a regular graph of
degree wt(f ), where wt denotes the Hamming weight of f when regarded as a vector of
values (of length 2n ).
Recall that, given a graph and its adjacency matrix A, the spectrum Spec() is
the multi-set of eigenvalues of A.
The Walsh transform of a Boolean function f is an integer-valued function over
GF (2)n that can be defined as
(1)f (x)+ u,x .
Wf (u) =
xinGF (2)n
A Boolean function f is bent if |Wf (a)| = 2n/2 (this only makes sense if n is even). This
property says, roughly speaking, that f is as non-linear as possible. The Hadamard
transform of a integer-valued function f is an integer-valued function over GF (2)n that
can be defined as
f (x)(1) u,x .
Hf (u) =
xinGF (2)n
It turns out that the spectrum of f is equal to the Hadamard transform of f when
regarded as a vector of (integer) 0, 1-values. (This nice fact seems to have first appeared
in [24], [25].)
Recall that a graph is regular of degree r (or r-regular) if every vertex has degree r.
We say that an r-regular graph is a strongly regular graph with parameters (v, r, d, e)
(for nonnegative integers e, d) provided, for all vertices u, v the number of vertices
adjacent to both u, v is equal to
e, if u, v are adjacent,
d, if u, v are nonadjacent.
It turns out tht f is bent if and only if f is strongly regular and e = d (see [25], [8]).
The following Sage computations illustrate these and other theorems in [186], [24],
[25], [8].
First, consider the Boolean function f : GF (2)4 GF (2) given by f (x0 , x1 , x2 ) =
x0 x1 + x2 x3 .
sage : V = GF (2)^4
sage : f = lambda x : x [0]* x [1]+ x [2]* x [3]
sage : CartesianProduct ( range (16) , range (16))
Cartesian product of [0 , 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 , 11 , 12 , 13 , 14 , 15] ,
[0 , 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 , 11 , 12 , 13 , 14 , 15]
sage : C = CartesianProduct ( range (16) , range (16))
sage : Vlist = V . list ()
sage : E = [( x [0] , x [1]) for x in C if f ( Vlist [ x [0]]+ Vlist [ x [1]])==1]
sage : len ( E )
96
sage : E = Set ([ Set ( s ) for s in E ])
sage : E = [ tuple ( s ) for s in E ]
sage : Gamma = Graph ( E )
sage : Gamma
Graph on 16 vertices
sage : VG = Gamma . vertices ()
sage : L1 = []
sage : L2 = []
sage : for v1 in VG :
....:
for v2 in VG :
....:
N1 = Gamma . neighbors ( v1 )
....:
N2 = Gamma . neighbors ( v2 )
....:
if v1 in N2 :
225
2,
2,
2,
2,
2,
2,
2,
2,
2,
2,
2,
2,
2,
2,
2,
2,
2,
2,
2,
2,
2,
2,
2,
2,
2,
2,
2,
2,
2,
2,
2,
2,
2,
2,
2,
2,
2,
2,
2,
2,
2,
2,
2,
2,
2,
2,
2,
2,
2,
2,
2,
2,
2,
2,
2,
2,
2,
2,
2,
2,
2,
2,
2,
2,
2,
2,
2,
2,
2,
2,
2,
2,
2,
2,
2,
2,
2,
2,
2,
2,
2,
2,
2,
2,
2,
2,
2,
2,
2,
2,
2,
2,
2,
2,
2,
2,
2,
2,
2,
2,
2,
2,
2,
2,
2,
2,
2,
2,
2,
2,
2,
2,
2,
2,
2,
2,
2,
2,
2,
2,
2,
2,
2,
2,
2,
2,
2,
2,
2,
2,
2,
2,
2,
2,
2,
2,
2]
2,
2,
2,
2,
2,
2,
2,
2,
2,
2,
2,
2,
2,
2,
2,
2,
2,
2,
2,
2,
2,
2,
2,
2,
2,
2,
2,
2,
2,
2,
2,
2,
2,
2,
2,
2,
2,
2,
2,
2,
2,
2,
2,
2,
2,
2,
2,
2,
2,
2,
2,
This implies the graph is strongly regular with d = e = 2. Let us use Sage to
determine some properties of this graph.
sage : Gamma . spectrum ()
[6 , 2 , 2 , 2 , 2 , 2 , 2 , -2 , -2 , -2 , -2 , -2 , -2 , -2 , -2 , -2]
sage : [ walsh_transform (f , a ) for a in V ]
[4 , 4 , 4 , -4 , 4 , 4 , 4 , -4 , 4 , 4 , 4 , -4 , -4 , -4 , -4 , 4]
sage : Omega_f = [ v for v in V if f ( v )==1]
sage : len ( Omega_f )
6
sage : Gamma . is_bipartite ()
False
sage : Gamma . is_hamiltonian ()
True
sage : Gamma . is_planar ()
False
sage : Gamma . is_regular ()
True
sage : Gamma . is_eulerian ()
True
sage : Gamma . is_connected ()
True
sage : Gamma . is_triangle_free ()
False
sage : Gamma . diameter ()
2
sage : Gamma . degree_sequence ()
[6 , 6 , 6 , 6 , 6 , 6 , 6 , 6 , 6 , 6 , 6 , 6 , 6 , 6 , 6 , 6]
The Hadamard transform of the Boolean function does indeed determine the spec-
226
This implies that the graph is not strongly regular, therefore f is not bent. (There
are other reasons why f cannot be bent as well.) Again, let us use Sage to determine
some properties of this graph.
sage : Gamma . spectrum ()
[4 , 2 , 0 , 0 , 0 , -2 , -2 , -2]
227
sage :
sage : Gamma . is_bipartite ()
False
sage : Gamma . is_hamiltonian ()
True
sage : Gamma . is_planar ()
False
sage : Gamma . is_regular ()
True
sage : Gamma . is_eulerian ()
True
sage : Gamma . is_connected ()
True
sage : Gamma . is_triangle_free ()
False
sage : Gamma . diameter ()
2
sage : Gamma . degree_sequence ()
[4 , 4 , 4 , 4 , 4 , 4 , 4 , 4]
sage : H = matrix ( QQ , 8 , 8 , [( -1)^( Vlist [ x [0]]). dot_product ( Vlist [ x [1]]) for x in C ])
sage : H
[ 1 1 1 1 1 1 1 1]
[ 1 -1 1 -1 1 -1 1 -1]
[ 1 1 -1 -1 1 1 -1 -1]
[ 1 -1 -1 1 1 -1 -1 1]
[ 1 1 1 1 -1 -1 -1 -1]
[ 1 -1 1 -1 -1 1 -1 1]
[ 1 1 -1 -1 -1 -1 1 1]
[ 1 -1 -1 1 -1 1 1 -1]
sage : flist = vector ( QQ , [ int ( f ( v )) for v in V ])
sage : H * flist
(4 , 0 , 0 , 0 , -2 , -2 , -2 , 2)
sage : Gamma . spectrum ()
[4 , 2 , 0 , 0 , 0 , -2 , -2 , -2]
sage : A = matrix ( QQ , 8 , 8 , [ f ( Vlist [ x [0]]+ Vlist [ x [1]]) for x in C ])
sage : A . eigenvalues ()
[4 , 2 , 0 , 0 , 0 , -2 , -2 , -2]
Again, we see the Hadamard transform does indeed determine the graph spectrum.
The picture of the graph is given in Figure 5.10.
5.8
228
min
|V |
0<|S| 2
|(S)|
,
|S|
where the minimum is over all nonempty sets S of at most |V |/2 vertices and (S) is
the edge boundary of S, i.e., the set of edges with exactly one endpoint in S.
The vertex expansion (or vertex isoperimetric number) hout (G) of a graph G is defined
as
hout (G) =
min
0<|S|
|V |
2
|out (S)|
,
|S|
where out (S) is the outer boundary of S, i.e., the set of vertices in V (G) \ S with at
least one neighbor in S.
sage : G = PSL (2 , 5)
sage : X = G . cayley_graph ()
sage : V = X . vertices ()
sage : S = [ V [1] , V [3] , V [7] , V [10] , V [13] , V [ 14] , V [23]]
sage : delS = X . edge_boundary ( S )
sage : edge_expan_XS = len ( delS )/ len ( S ); RR ( edge_expan_XS )
1.00000000000000
sage : S = [ V [1] , V [3] , V [7] , V [12] , V [24] , V [37]]
sage : delS = X . edge_boundary ( S )
sage : edge_expan_XS = len ( delS )/ len ( S ); RR ( edge_expan_XS )
1.50000000000000
sage : S = [ V [2] , V [8] , V [13] , V [27] , V [32] , V [44] , V [57]]
sage : delS = X . edge_boundary ( S )
sage : edge_expan_XS = len ( delS )/ len ( S ); RR ( edge_expan_XS )
1.42857142857143
sage : S = [ V [0] , V [6] , V [11] , V [16] , V [21] , V [29] , V [35] , V [45] , V [53]]
sage : delS = X . edge_boundary ( S )
sage : edge_expan_XS = len ( delS )/ len ( S ); RR ( edge_expan_XS )
1.77777777777778
sage : n = len ( X . vertices ())
sage : J = range ( n )
sage : J30 = Subsets (J , int ( n /2))
sage : K = J30 . random_element ()
sage : K
{0 , 2 , 3 , 4 , 5 , 6 , 8 , 9 , 11 , 13 , 16 , 18 , 19 , 21 , 24 , 25 , 26 , 28 , 29 ,
30 , 36 , 37 , 38 , 40 , 42 , 45 , 46 , 49 , 53 , 57}
sage : S = [ V [ i ] for i in K ]
# 30 vertices , randomly selected
sage : delS = [ v for v in V if min ([ X . distance (a , v ) for a in S ]) == 1]
sage : RR ( len ( delS ))/ RR ( len ( S ))
0.8 00000000000000
5.8.1
Ramanujan graphs
5.9. Problems
229
Let q be a prime power such that q 1 (mod 4). Note that this implies that the
finite field GF (q) contains a square root of 1.
Now let V = GF (q) and E = {{a, b} GF (q) GF (q) | (a b) GF (q) )2 }. This
set is well defined since a b = (1) (b a), and since 1 is a square, it follows that
ab is a square if and only if b a is a square.
By definition G = (V, E) is the Paley graph of order q.
The following facts are known about Paley graphs.
The eigenvalues of Paley graphs are
multiplicity q1
).
2
q1
2
1 q
2
(both with
Below is an example.
sage : X = Paley (13)
sage : X . vertices ()
[0 , 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 , 11 , 12]
sage : X . i s_vertex_transitive ()
True
sage : X . degree_sequence ()
[6 , 6 , 6 , 6 , 6 , 6 , 6 , 6 , 6 , 6 , 6 , 6 , 6]
sage : X . spectrum ()
[6 , 1.302775637731995? , 1.302775637731995? , 1.302775637731995? ,
1.302775637731995? , 1.302775637731995? , 1.302775637731995? ,
-2.302775637731995? , -2.302775637731995? , -2.302775637731995? ,
-2.302775637731995? , -2.302775637731995? , -2.302775637731995?]
sage : G = X . automorphism_group ()
sage : G . cardinality ()
78
sage : 13*12/2
78
5.9
Problems
When you dont share your problems, you resent hearing the problems of other people.
Chuck Palahniuk, Invisible Monsters, 1999
5.1. Let G = (V, E) be an undirected, unweighted simple graph. Show that V and the
distance function on G form a metric space if and only if G is connected.
5.2. Let u and v be two distinct vertices in the same connected component of G. If P
is a u-v path such that d(u, v) = (u), we say that P is an eccentricity path for u.
(a) If r is the root of a tree, show that the end-vertex of an eccentricity path for
r is a leaf.
1
230
5.3. If u and v are vertices of a connected graph G such that d(u, v) = diam(G), show
that u and v are mutually eccentric.
5.4. If uv is an edge of a tree T and w is a vertex of T distinct from u and v, show that
|d(u, w) d(w, v)| = W (uv) with W (uv) being the weight of uv.
5.5. If u and v are vertices of a tree T such that d(u, v) = diam(T ), show that u and v
are leaves.
5.6. Let v1 , v2 , . . . , vk be the leaves of a tree T . Show that per(T ) = {v1 , v2 , . . . , vk }.
5.7. Show that all the eccentric vertices of a tree are leaves.
5.8. If G is a connected graph, show that rad(G) diam(G) 2 rad(G).
5.9. Let T be a tree of order 3. If the center of T has one vertex, show that diam(T ) =
2 rad(T ). If the center of T has two vertices, show that diam(T ) = 2 rad(T ) 1.
5.10. Let G = (V, E) be a simple undirected, connected graph. Define the distance of a
vertex v V by
d(v) =
d(v, x)
xV
1
d(v).
2 vV
For any vertex v V , show that d(G) d(v) + d(G v) with G v being a vertex
deletion subgraph of G. This result appeared in Entringer et al. [74, p.284].
5.11. Determine the sequence of distance matrices for the graphs in Figure 5.4.
5.12. If G = (V, E) is an undirected connected graph and v V , prove the following
vertex connectivity inequality:
(G) 1 (G v) (G).
5.13. If G = (V, E) is an undirected connected graph and e E, prove the following
edge connectivity inequality:
(G) 1 (G e) (G).
5.9. Problems
231
17
39
38
42
49
36
40
11
26
18
51
12
37
47
30
24
48
33
45
41
14
20
43
15
52
13
19
27
32
44
23
10
6
46
25
29
34
31
53
16
35
1
50
21
22
28
232
name
Alicante Bouschet
Cabernet Franc
Chardonnay
Donzillinho
Flora
Gr
uner Veltliner
Meslier-Saint-Francois
Muscat Hamburg
Ortega
Perle
Petit Manseng
Reichensteiner
Roter Veltliner
Ruby Cabernet
Semillon
Taminga
Traminer
Trousseau
code
1
4
7
10
13
16
19
22
25
28
31
34
37
40
43
46
49
52
name
Aramon
Cabernet Sauvignon
Chenin Blanc
Ehrenfelser
Gamay
Kemer
M
uller-Thurgau
Muscat of Alexandria
Osteiner
Perle de Csaba
Petite Bouschet
Riesling
Rotgipfler
Sauvignon Blanc
Siegerrebe
Teinturier du Cher
Trincadeiro
Verdelho
code
2
5
8
11
14
17
20
23
26
29
32
35
38
41
44
47
50
53
name
Bequignol
Carignan
Colombard
Fer Servadou
Gelber Ortlieber
Merlot
Muscat Blanc
Optima
Peagudo
Perlriesling
Pinot Noir
Rotberger
Royalty
Schonburger
Sylvaner
Tinta Madeira
Trollinger
Wittberger
Table 5.2: Numeric code and actual name of common grape cultivars.
5.14. Figure 5.11 depicts how common grape cultivars are related to one another; the
graph is adapted from Myles et al. [159]. The numeric code of each vertex can
be interpreted according to Table 5.2. Compute various distance and connectivity
measures for the graph in Figure 5.11.
5.15. Prove the characterizations of 2-connected graphs as stated in Theorem 5.13.
5.16. Let G = (V, E) be an undirected connected graph of order n and suppose that
deg(v) (n + k 2)/2 for all v V and some fixed positive integer k. Show that
G is k-connected.
5.17. A vertex (or edge) separating set S of a connected graph G is minimum if S has
the smallest cardinality among all vertex (respectively edge) separating sets in G.
Similarly S is said to be maximum if it has the greatest cardinality among all
vertex (respectively edge) separating sets in G. For the graph in Figure 5.7(a),
determine the following:
(a) A minimum vertex separating set.
(b) A minimum edge separating set.
(c) A maximum vertex separating set.
(d) A maximum edge separating set.
(e) The number of minimum vertex separating sets.
(f) The number of minimum edge separating sets.
Chapter 6
Optimal graph traversals
6.1
Eulerian graphs
Motivation: tracing out all the edges of a graph without lifting your pencil.
multigraphs and simple graphs
Eulerian tours
Eulerian trails
6.2
Hamiltonian graphs
Theorem 6.1. Ore 1960. Let G be a simple graph with n 3 vertices. If deg(u) +
deg(v) n for each pair of non-adjacent vertices u, v V (G), then G is Hamiltonian.
Corollary 6.2. Dirac 1952. Let G be a simple graph with n 3 vertices. If deg(v)
n/2 for all v V (G), then G is Hamiltonian.
233
234
6.3
6.4
Chapter 7
Planar graphs
A planar graph is a graph that can be drawn on a sheet of paper without any overlapping
between its edges.
It is a property of many natural graphs drawn on the earths surface, like for
instance the graph of roads, or the graph of internet fibers. It is also a necessary property
of graphs we want to build, like VLSI layouts.
Of course, the property of being planar does not prevent one from finding a drawing
with many overlapping between edges, as this property only asserts that there exists a
drawing (or embedding) of the graph avoiding it. Planarity can be characterized in many
different ways, one of the most satiating being Kuratowskis theorem.
See chapter 9 of Gross and Yellen [95].
7.1
7.2
Kuratowskis Theorem
Kuratowski graphs
236
237
It can easily be seen that if a graph G is planar, any of its subgraph is also planar.
Besides, planarity is still preserved under edge contraction. These two facts mean together that any minor of a planar graph is still planar graph, which makes of planarity
a minor-closed property. If we let P denote the poset of all non-planar graph, ordered
with the minor partial order, we can now consider the set Pmin of its minimal elements
which, by the Graph Minor Theorem, is a finite set.
Actually, Kuratowskis theorem asserts that Pmin = {K5 , K3,3 }.
7.3
Planarity algorithms
Chapter 8
Graph coloring
Graph coloring problems originated with the coloring of maps. For example, regard
each state in the United States as a vertex, and connect two vertices by an edge if and
only if they share a boundary, i.e., are neighbors. If you can color the United States
map using k colors in such a way that no two neighboring states have the same color
then we say the map has a k-coloring. While a student in London in the mid-1800s, the
South African mathematician Francis Guthrie conjectured to his mathematics professor
Augustus de Morgan that four colors suffice to color any map. It was an open problem
for over 100 years (only proven by Appel and Haken in 1976).
8.1
Vertex coloring
239
240
v (G) (G).
It is also not hard to give a non-trivial upper bound.
Theorem 8.3. Every simple graph can be colored with one more color than the maximum
vertex degree,
v (G) (G) + 1.
We give two proofs.
Proof. (proof 1) We prove this by induction on the number n of vertices.
It is obvious in the case n = 1.
Assume the result is true for all graphs with k 1 vertices and let G = (V, E) be a
graph with k vertices. We want to chow that G has a coloring with 1 + (G) colors.
Let v V and consider the graph G v. By hypothesis, this graph has a coloring with
1 + (G v) colors. Since there are at most (G) neighbors of v, no more than (G)
colors could be used for these adjacent vertices. There are two cases.
Case 1 + (G v) < 1 + (G). In this case, we create a new color for v to obtain a
coloring for G with 2 + (G v) 1 + (G) colors.
Case 1 + (G v) = 1 + (G). In this case, the vertices adjacent to v have been
colored with at most (G) colors, leaving us with at least one unused color. We can use
that color for v.
Proof. (proof 2) The fact that the graph coloring (decision) problem is NP-complete
must not prevent one from trying to color it greedily. One such method would be to
iteratively pick, in a graph G, an uncolored vertex v, and to color it with the smallest
color available which is not yet used by one of its neighbors. Such a coloring algorithm
will never use more than (G) + 1 different colors (where (G) is the maximal degree
of G), as no vertex in the procedure will ever exclude more than (G) colors.
Such a greedy algorithm can be written in Sage in a few lines:
sage :
sage :
sage :
sage :
...
...
Example 8.4. A Frucht graph, shown in Figure 8.2, has 12 nodes and 18 edges. It is
3-regular, planar and Hamiltonian. It is named after Robert Frucht. The Frucht graph
has no nontrivial symmetries. It has chromatic number 3, chromatic index 3, radius 3,
diameter 4 and girth 3. It is 3-vertex-connected and 3-edge-connected graph.
sage :
sage :
sage :
sage :
3
sage :
sage :
sage :
1
G = graphs . FruchtGraph ()
G . show ( dpi =300)
vc = G . coloring ()
G . chromatic_number ()
d = { blue : vc [0] , red : vc [1] , green : vc [2]}
G . show ( vertex_colors = d )
G . a utomorphism_group (). order ()
Brooks Theorem
241
Theorem 8.5. (Brooks inequality) If G is not a complete graph and is not an odd cycle
graph then
v (G) (G).
8.2
Edge coloring
Edge coloring is the direct application of vertex coloring to the line graph of a graph G.
(Recall, L(G) is the graph whose vertices are the edges of G, two vertices being adjacent
if and only if their corresponding edges share an endpoint). We write v (L(G)) = e (G)
for the chromatic index of G. (This is also called the edge chromatic number. An edge
coloring of a graph is an assignment of colors to edges so that no vertex is incident to
two edges of the same color. An edge coloring with k colors is called a k-edge-coloring.
Example 8.6. The Heawood graph, shown in Figure 8.4, is named after Percy John
Heawood. It is an undirected graph with 14 vertices and 21 edges. It is a 3-regular,
distance-transitive, distance-regular graph. It has chromatic number 2 and chromatic
index 3. An edge-coloring is shown in Figure 8.3. A vertex-coloring is shown in Figure 8.4.
Recall that a graph is vertex-transitive if its automorphism group acts transitively
upon its vertices. The automorphism group of the Heawood graph is isomorphic to the
projective linear group PGL 2(7), a group of order 336. It acts transitively on the vertices
and on the edges of the graph. Therefore, this graph is vertex-transitive.
242
sage :
sage :
sage :
sage :
sage :
3
Example 8.7. The Icosahedral graph, shown in Figure 8.5, is a particular projection of
the edges of the solid icosahedron onto the plane. It is a 4-regular graph with 30 edges
and 12 vertices. It has chromatic number 4 and chromatic index 5. An edge-coloring is
shown in Figure 8.6. A vertex-coloring is shown in Figure 8.5.
sage :
sage :
4
sage :
sage :
sage :
G = graphs . IcosahedralGraph ()
G . chromatic_number ()
vc = G . coloring ()
d = { blue : vc [0] , red : vc [1] , green : vc [2] , orange : vc [3]}
G . show ( vertex_colors = d )
sage : G . is_hamiltonian ()
True
sage : G . is_regular ()
True
sage : G . i s_vertex_transitive ()
True
243
244
sage :
False
sage :
True
sage :
False
sage :
False
G . is_perfect ()
sage :
5
sage :
sage :
sage :
G . is_planar ()
G . is_clique ()
G . is_bipartite ()
ec = edge_coloring ( G )
d = { blue : ec [0] , red : ec [1] , green : ec [2] , orange : ec [3] , yellow : ec [4]}
G . show ( edge_colors = d )
Example 8.9. The Pappus graph, shown in Figure 8.7, is named after Pappus of Alexandria. It is 3-regular, symmetric, and distance-regular with 18 vertices and 27 edges. The
Pappus graph has girth 6, diameter 4, radius 4, chromatic number 2 (i.e. is bipartite),
chromatic index 3 and is both 3-vertex-connected and 3-edge-connected.
sage : G = graphs . PappusGraph ()
sage : G . coloring ()
[[1 , 3 , 5 , 6 , 8 , 10 , 12 , 14 , 16] , [0 , 2 , 4 , 7 , 9 , 11 , 13 , 15 , 17]]
sage : G . is_regular ()
245
G . is_planar ()
G . i s_vertex_transitive ()
G . is_hamiltonian ()
G . girth ()
G . is_bipartite ()
G . show ( dpi =300)
G . line_graph (). chromatic_number ()
ec = edge_coloring ( G )
ec
1) , (2 , 3) , (4 , 5) , (6 , 17) , (7 , 14) , (8 , 13) , (9 , 16) , (10 , 15) , (11 , 12)] ,
5) , (1 , 2) , (3 , 4) , (6 , 13) , (7 , 12) , (8 , 15) , (9 , 14) , (10 , 17) , (11 , 16)] ,
6) , (1 , 7) , (2 , 8) , (3 , 9) , (4 , 10) , (5 , 11) , (12 , 15) , (13 , 16) , (14 , 17)]]
d = { blue : ec [0] , red : ec [1] , green : ec [2]}
G . plot ( edge_colors = d ). show ()
8.3
George David Birkhoff introduced the chromatic polynomial in 1912. The chromatic
polynomial is defined as the unique interpolating polynomial of degree n through the
points (k, PG (k)) for k = 0, 1, . . . n, where n is the number of vertices in G. For natural
246
numbers t, the chromatic polynomial is the function that counts the number of t-colorings
of G.
As the name indicates, for a given G the function is indeed a polynomial in t.
For a complex number x, let x(k) = k1
i=0 (x i).
Lemma 8.10. If mk (G) denotes the number of distinct partitions of V into k different
color-classes then
n
PG (t) =
mk (G)t(k) .
k=1
Proof. Given a partition of V into k color-classes, we can assign t colors to the colorclasses in t(t 1) . . . (t k + 1) = t(k) ways. For each k with 1 k n, there are mk (G)
distinct partitions of V into k such color-classes. Therefore, the number of colorings with
t colors is nk=1 mk (G)t(k) , as desired.
Theorem 8.11. A graph G with n vertices is a tree if and only if P (G, t) = t(t 1)n1 .
Proof. We prove this by induction on n. The statement is clearly true when n = 1.
Assume the statement holds for any tree of n 1 vertices and let G be a tree with n
vertices. There is a vertex v of G having degree 1. The graph G v is a tree having n 1
vertices, so has t(t 1)n2 t-colorings. The number of t-colorings of G is the number of
t-colorings of G v times the number of ways to color v. Since we may color v using
any color not used for its one neighbor, there are t 1 colorings of v. Therefore, G has
t(t 1)n1 t-colorings.
The theorem above gives examples of non-isomorphic graphs which have the same
chromatic polynomial.
Two graphs are said to be chromatically equivalent if they have the same chromatic
polynomial. For example, two trees having the same number of vertices are chromatically
equivalent.
It is also an open problem to find necessary and sufficient conditions for two arbitrary
graphs to be chromatically equivalent.
Theorem 8.12. If G has n vertices, m edges, and k components G1 , G2 ,. . . ,Gk , then
P (G, t) = P (G1 , t)P (G2 , t) P (Gk , t).
This is proven by induction on k and the proof is left to the interested reader.
Fix a pair of vertices u, v V . Recall that the (edge contraction) graph G/uv is
obtained by merging the two vertices and removing any edges between them. Recall
that the (edge deletion) graph G uv is obtained by merging the the edge uv but not
the two vertices u, v.
Theorem 8.13. (Fundamental Reduction Theorem) The chromatic polynomial satisfies
the recurrence relation
P (G, t) = P (G uv, t) P (G/uv, t).
247
8.4
assignment problems
scheduling problems
matching problems
map coloring and the Four Color Problem
8.4.1
Application to scheduling
Assume that we have a finite set of different people (for example, students) and a finite
set of distinct one-hour meetings they could attend (for example, classes). Construct the
graph G as follows. The vertices V of G are given by the set of meetings. We connect
two meetings with an edge if some person needs to attend both meetings.
Example 8.16. Suppose the in the table below indicated that the student in that
column is attending the class in that row.
Bob
Calculus
Chemistry
English
History
Physics
Carol
Ted
Zoe
248
8.4.2
Map coloring
Theorem 8.18. (Four Color Theorem) Every planar graph can be 4-colored.
Chapter 9
Network flows
See Jungnickel [121], and chapter 12 of Gross and Yellen [95].
9.1
C 1 (G, F ) = {f : E F },
f (x)g(x),
(X = V, resp. X = E),
(9.1)
xX
and
dim C 0 (G, F ) = |V |,
If you index the sets V and E in some arbitrary but fixed way and define, for 1 i |V |
and 1 j |E|,
fi (v) =
1, v = vi ,
0, otherwise,
gj (e) =
1, e = ej ,
0, otherwise,
250
ai = ai (E ) =
In particular, this defines a mapping
Vi = ,
V1 V2 = ,
satisfying3
uV, (u,v)E
f (u, v) =
wV, (v,w)E
f (v, w).
(Note: this is simply the condition that f belongs to the kernel of D.)
Suppose G has a subgraph H and f is a circulation of G such that f is a constant
function on H and 0 elsewhere. We call such a circulation a characteristic function of
H. For example, if G has a cycle C and if f is the characteristic function on C, then f
is a circulation.
The circulation space C is the F -vector space of circulation functions. The cycle space
clearly may be identified with a subspace of the circulation space, since the F -vector
1
Also called an edge cut subgraph or disconnecting set or seg or edge cutset.
Jungnickel and Vanstone in [122] call this the even graphical code of G.
3
Note: In addition, some authors add the condition f (e) 0 - see e.g., Chung [58].
251
space spanned by the characteristic functions of cycles may be identified with the cycle
space of G. In fact, these spaces are isomorphic. Under the inner product (9.1), i.e.,
(f, g) =
f (e)g(e),
(9.2)
eE
dx , if (x, y) E,
0, otherwise,
where dx denotes the out-degree. The Perron-Frobenius Theorem states that there exists
a unique left eigenvector such that (when regarded as a function : V R) (v) > 0,
for all v V and P = , where is the spectral radius of G. We scale so that
vV (v) = 1. (This vector is sometimes called the Perron vector.) Let F (u, v) =
(v)P (u, v). Fact: F is a circulation. For a proof, see F. Chung [58].
If the edges of E are indexed in some arbitrary but fixed way then a circulation
function restricted to a subgraph H of G may be identified with a vector representation
of H, as described above. Thefore, the circulation functions gives a coordinate-free
version of the cycle space.
The F -vector space spanned by the vector representations of all the segs is called the
cocycle space (or the cut space) of G, denoted Z (G) = Z (G, F ). This is the column
space of the transpose of the incidence matrix of G (14.1 in Godsil and Royle [91]). It
may be regarded as a subspace of C 1 (G, F ) of dimension the rank of G, r(G). When F
is a finite field, sometimes the cocycle space is called the cocycle code of G.
Lemma 9.2. Under the inner product (9.1) on C 1 (G, F ), the cycle space is orthogonal
to the cocycle space.
Solution. One proof follows from Theorem 8.3.1 in Godsil and Royle [91].
Here is another proof. By Theorem 2.3 in Bondy and Murty [34, p.27], an edge of G is
an edge cut if and only if it is contained in no cycle. Therefore, the vector representation
of any cocycle is supported on a set of indices which is disjoint from the support of the
vector representation of any cycle. Therefore, there is a basis of the cycle space which is
orthogonal to a basis of the cocycle space.
Proposition 9.3. Let F = GF (2). The cycle code of a graph G = (V, E) is a linear
binary block code of length |E|, dimension equal to the nullity of the graph, n(G),
and minimum distance equal to the girth of G. If C GF (2)|E| is the cycle code
associated to G and C is the cocycle code associated to G then C is the dual code of
C. In particular, the cocycle code of G is a linear binary block code of length |E|, and
dimension r(G) = |E| n(G).
This follows from Hakimi-Bredeson [101] (see also Jungnickel-Vanstone [122]) in the
binary case4 .
4
It is likely true in the non-binary case as well, but no proof seems to be in the literature.
252
Solution. Let d denote the minimum distance of the code C. Let denote the girth of
G, i.e., the smallest cardinality of a cycle in G. If K is a cycle in G then the vector
vec(K) GF (2)|E| is an element of the cycle code C GF (2)|E| . This implies d .
In the other direction, suppose K1 and K2 are cycles in G with associated support
vectors v1 = vec(K1 ), v2 = vec(K2 ). Assume that at least one of these cycles is a cycle
of minimum length, say K1 , so the weight of its corresponding support vector is equal to
the girth . The only way that wt(v1 + v2 ) < min{wt(v1 ), wt(v2 )} can occur is if K1 and
K2 have some edges in common. In this case, the vector v1 + v2 represents a subgraph
which is either a cycle or it is a union of disjoint cycles. In either case, by minimality of
K1 , these new cycles must be at least as long. Therefore, d , as desired.
Consider a spanning tree T of a graph G and its complementary subgraph T . For
each edge e of T the graph T e contains a unique cycle. The cycles which arise in this
way are called the fundamental cycles of G, denoted cyc(T, e).
Example 9.4. Consider the graph below, with edges labeled as indicated, together with
a spanning tree, depicted to its right, in Figure 9.4.
7
0
2
3
10
5
Figure 9.1: A graph and a spanning tree for it.
g1 = (1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0),
by adding edge 6 to the tree, you get a cycle 4, 5, 6 with vector representation
g2 = (0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0),
by adding edge 10 to the tree, you get a cycle 8, 9, 10 with vector representation
g3 = (0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1).
The vectors {g1 , g2 , g3 } form a basis of the cycle space of G over GF (2).
The cocycle space of a graph G (also known as the bond space of G or the cut-set
space of G) is the F -vector space spanned by the characteristic functions of bonds.
Example 9.5. Consider the graph below, with edges labeled as indicated, together with
an example of a bond, depicted to its right, in Figure 9.5.
You can see from Figure 9.5 that:
253
7
0
2
3
10
by removing edge 3 from the graph, you get a bond with vector representation
b1 = (0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0),
by removing edge 7 from the graph, you get a bond with vector representation
b2 = (0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0),
by removing edges 0, 1 from the graph, you get a bond with vector representation
b3 = (1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0),
by removing edges 1, 2 from the graph, you get a bond with vector representation
b4 = (0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0),
by removing edges 4, 5 from the graph, you get a bond with vector representation
b5 = (0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0),
by removing edges 4, 6 from the graph, you get a bond with vector representation
b6 = (0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0),
by removing edges 8, 9 from the graph, you get a bond with vector representation
b7 = (0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0),
by removing edges 9, 10 from the graph, you get a bond with vector representation
b8 = (0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1).
The vectors {b1 , b2 , b3 , b4 , b5 , b6 , b7 , b8 } form a basis of the cocycle space of G over GF (2).
Note that these vectors are orthogonal to the basis vectors of the cycle space in
Example 9.4. Note also that the xor sum of two cuts is not a cut.For example, if you xor
the bond 4, 5 with the bond 4, 6 then you get the subgraph foormed by the edges 5, 6
and that is not a disconnecting cut of G.
254
9.1.1
Electrical networks
Ai = 0,
(9.3)
where A is the incidence matrix and i is regarded as a column vector. This equation
comes from the fact that the algebraic sum of the currents going into a node is
zero.
Kirchhoff s voltage law,
Cv = 0,
(9.4)
where C is the circuit matrix and v is regarded as a column vector. This equation
comes from the fact that the algebraic sum of the voltage drops around a closed
loop is zero.
In a network with resistors but no inductors or capacitors, there is a relationship
between i and v given by
Ri = v + b,
where b is a vector of external battery sources and R is a resistor matrix.
9.2
Chip firing games on graphs (which are just pure fun) relate to abelian sandpile models
from physics to rotor-routing models from theoretical computer scientists (designing
efficient computer multiprocessor circuits) to self-organized criticality (a subdiscipline
of dynamical systems) to algebraic potential theory on a graph [26] to cryptography
(via the Biggs cryptosystem). Moreover, it relates the concepts of the Laplacian of the
graph to the tree number to the circulation space of the graph to the incidence matrix,
as well as many other ideas. Some good references are [69], [168], [169], [105] and [27].
255
Basic set-up
A chip firing game always starts with a directed multigraph G having no loops. A configuration is a vertex-weighting, i.e., a function s : V R. The players are represented
by the vertices of G and the vertex-weights represent the number of chips each player
(represented by that vertex) has. The initial vertex-weighting is called the starting configuration of G. Let vertex v have outgoing degree d+ (v). If the weight of vertex v is
d+ (v) (so that player can afford to give away all their chips) then that vertex is called
active.
Here is some SAGE/Python code for determining the active vertices.
SAGE
def active_vertices(G, s):
"""
Returns the list of active vertices.
INPUT:
G - a graph
s - a configuration (implemented as a list
or a dictionary keyed on
the vertices of the graph)
EXAMPLES:
sage: A = matrix([[0,1,1,0,0],[1,0,1,0,0],[1,1,0,1,0],[0,0,0,0,1],[0,0,0,0,0]])
sage: G = Graph(A, format = "adjacency_matrix", weighted = True)
sage: s = {0: 3, 1: 1, 2: 0, 3: 1, 4: 1}
sage: active_vertices(G, s)
[0, 4]
"""
V = G.vertices()
degs = [G.degree(v) for v in V]
active = [v for v in V if degs[V.index(v)]<=s[v]]
return active
If v is active then when you fire v you must also change the configuration. The new
configuration s will satisfy s (v) = s(v) d+ (v) and s (v ) = s(v ) + 1 for each neighbor
v of v. In other words, v will give away one chip to each of its d+ (v) neighbors. If
x : V {0, 1}|V | R|V | is the representation vector (characteristic function) of a
vertex then this change can be expressed more compactly as
s = s L x,
(9.5)
where L is the vertex Laplacian. It turns out that the column sums of L are all 0, so
this operation does not change the total number of chips. We use the notation
v
ss,
1 1 0
0
0
0 1 1 0
0
0 1 1 0
D= 1
0
0
0 1 1
0
0
0
0
1
256
L = D tD =
2 1 0 1 0
0
1 2
1
0
0
1 1
3
1 0
0
0
1
2 1
0
0
0
1 1
Notice player 0 is active. If we fire 0 then we get the new configuration s = (1, 2, 1, 1, 1).
Indeed, if we compute s = s Lx(0), we get:
s =
3
1
0
1
1
2 1 1 0
0
1 2 1 0
0
1 1 3 1 0
0
0 1 2 1
0
0
0 1 1
1
0
0
0
0
3
1
0
1
1
2
1
1
0
0
1
2
1
1
1
257
and s(v) 0 for all v V with v = q. A vertex v = q can be fired if and only
if deg(v) s(v) (i.e., it has enough chips). The equation (9.5) describes the new
configuration after firing a vertex.
Here is some SAGE/Python code for determining the configuration after firing an
active vertex.
SAGE
def fire(G, s, v0):
"""
Returns the configuration after firing the active vertex v.
INPUT:
G - a graph
s - a configuration (implemented as a list
or a dictionary keyed on
the vertices of the graph)
v - a vertex of the graph
EXAMPLES:
sage: A = matrix([[0,1,1,0,0],[1,0,1,0,0],[1,1,0,1,0],[0,0,0,0,1],[0,0,0,0,0]])
sage: G = Graph(A, format = "adjacency_matrix", weighted = True)
sage: s = {0: 3, 1: 1, 2: 0, 3: 1, 4: 1}
sage: fire(G, s, 0)
{0: 1, 1: 2, 2: 1, 3: 1, 4: 1}
"""
V = G.vertices()
j = V.index(v0)
s1 = copy(s)
if not(v0 in V):
raise ValueError, "the last argument must be a vertex of the graph."
if not(v0 in active_vertices(G, s)):
raise ValueError, "the last argument must be an active vertex of the graph."
degs = [G.degree(w) for w in V]
for w in V:
if w == v0:
s1[v0] = s[v0] - degs[j]
if w in G.neighbors(v0):
s1[w] = s[w]+1
return s1
258
A = matrix([[0,1,1,0,0],[1,0,1,0,0],[1,1,0,1,0],[0,0,0,0,1],[0,0,0,0,0]])
G = Graph(A, format = "adjacency_matrix", weighted = True)
s = {0: 3, 1: 1, 2: 0, 3: 1, 4: 1}
stable_vertices(G, s)
"""
V = G.vertices()
degs = [G.degree(v) for v in V]
if source==None:
stable = [v for v in V if degs[V.index(v)]>s[v]]
else:
stable = [v for v in V if degs[V.index(v)]>s[v] and v!=source]
return stable
259
and
f (v).
vV
260
1 1 0
0
0
0 1 1 0
0
0 1 1 0
D= 1
0
0
0 1 1
0
0
0
0
1
and Laplacian
L=D D=
2 1 0 1 0
0
1 2
1
0
0
1 1
3
1 0
0
0
1
2 1
0
0
0
1 1
The legal sequence (0, 1, 0, 2, 1, 0, 3, 2, 1, 0) leads to the stable configuration (0, 1, 2, 1, 4).
If q is fired then the configuration (0, 1, 2, 2, 5) is achieved. This is recurrent since it is
contained in the cyclic legal sequence
3
261
s(q)
vV,v=q
9.3
Ford-Fulkerson theorem
uV, (u,v)E
f (u, v) =
uV, (v,u)E
The reduced Laplacian matrix is obtained from the Laplacian matrix by removing the row and
column associated to the source vertex.
262
vV
f (s, v)
f (v, s),
vV
where s is the source. It represents the amount of flow passing from the source to the
sink. The maximum flow problem is to maximize |f |, that is, to route as much flow as
possible from s to t.
Example 9.11. Consider the digraph having adjacency
0
1
1
0
0 0
1 0 1 1
0 1
1 1
0
0
1 0
0 1 0
0
0 1
0
0 1 0
0 1
0 1 0 1 1 0
matrix
sage : B = matrix ([[0 ,1 ,1 ,0 ,0 ,0] ,[0 ,0 ,0 ,1 ,0 ,1] ,[0 ,1 ,0 ,0 ,1 ,0] ,[0 ,0 ,0 ,0 ,0 ,1] ,[0 ,0 ,0 ,0 ,0 ,1] ,[0 ,0 ,
sage : H = DiGraph (B , format = " adjacency_matrix " , weighted = True )
Type H.show(edgewlabels=True) if you want to see the graph with the capacities labeling the edges.
263
Given a capacitated digraph with capacity c and flow f , we define the residual digraph
Gf = (V, E) to be the digraph with capacity cf (u, v) = c(u, v) f (u, v) and no flow. In
other words, Gf is the same graph but it has a different capacity cf and flow 0. This is
also called a residual network.
Define an s t cut in our capacitated digraph G to be a partition C = (S, T ) of V
such that s S and t T . Recall the cut-set of C is the set
{(u, v) E | u S, v T }.
Lemma 9.12. Let G = (V, E) be a capacitated digraph with capacity c : E R, and
let s and t denote the source and the sink of G, respectively. If C is an s t cut and if
the edges in the cut-set of C are removed, then |f | = 0.
Exercise 9.13. Prove Lemma 9.12.
The capacity of an s t cut C = (S, T ) is defined by
c(S, T ) =
c(u, v).
(s,t)(S,T )
wV
f (w, v) c(v),
for each v V {s, t}. Define an s t cut to be the set of vertices and edges such
that for any path from s to t, the path contains a member of the cut. In this case, the
264
capacity of the cut is the sum the capacity of each edge and vertex in it. In this new
definition, the generalized max-flow min-cut theorem states that the maximum value of
an s t flow is equal to the minimum capacity of an s t cut..
The idea behind the Ford-Fulkerson algorithm is very simple: As long as there is a
path from the source to the sink, with available capacity on all edges in the path, we
send as much flow as we can alone along each of these paths. This is done inductively,
one path at a time.
Algorithm 9.1: Ford-Fulkerson algorithm.
Input: Graph G = (V, E) with flow capacity c, source s, and sink t.
Output: A flow f from s to t which is a maximum for all edges in E.
1
2
3
4
5
6
265
Here is some Python code7 which implements this. The class FlowNetwork is basically
a Sage Graph class with edge weights and an extra data structure representing the flow
on the graph.
class Edge :
def __init__ ( self ,U ,V , w ):
self . source = U
self . to = V
self . capacity = w
def __repr__ ( self ):
return str ( self . source ) + " ->" + str ( self . to ) + " : " + str ( self . capacity )
class FlowNetwork ( object ):
"""
This is a graph structure with edge capacities .
EXAMPLES :
g = FlowNetwork ()
map ( g . add_vertex , [ s ,o ,p ,q ,r ,t ])
g . add_edge ( s ,o ,3)
g . add_edge ( s ,p ,3)
g . add_edge ( o ,p ,2)
g . add_edge ( o ,q ,3)
g . add_edge ( p ,r ,2)
g . add_edge ( r ,t ,3)
g . add_edge ( q ,r ,4)
g . add_edge ( q ,t ,2)
print g . max_flow ( s ,t )
"""
def __init__ ( self ):
self . adj , self . flow , = {} ,{}
def add_vertex ( self , vertex ):
self . adj [ vertex ] = []
def get_edges ( self , v ):
return self . adj [ v ]
def add_edge ( self , u ,v , w =0):
assert ( u != v )
edge = Edge (u ,v , w )
redge = Edge (v ,u ,0)
edge . redge = redge
redge . redge = edge
self . adj [ u ]. append ( edge )
self . adj [ v ]. append ( redge )
self . flow [ edge ] = self . flow [ redge ] = 0
def find_path ( self , source , sink , path ):
if source == sink :
return path
for edge in self . get_edges ( source ):
residual = edge . capacity - self . flow [ edge ]
if residual > 0 and not ( edge , residual ) in path :
result = self . find_path ( edge . to , sink , path + [ ( edge , residual ) ])
if result != None :
return result
def max_flow ( self , source , sink ):
path = self . find_path ( source , sink , [])
while path != None :
flow = min ( res for edge , res in path )
for edge , res in path :
self . flow [ edge ] += flow
self . flow [ edge . redge ] -= flow
path = self . find_path ( source , sink , [])
return sum ( self . flow [ edge ] for edge in self . get_edges ( source ))
266
9.4
The objective of this section is to prove Edmond and Karps algorithm for the maximum
flow-minimum cut problem with polynomial complexity.
9.5
The objective of this section is to prove Goldberg and Tarjans algorithm for finding
maximal flows with polynomial complexity.
Chapter 10
Random graphs
A random graph can be thought of as being a member from a collection of graphs having
some common properties. Recall that Algorithm 3.5 allows for generating a random
binary tree having at least one vertex. Fix a positive integer n and let T be a collection
of all binary trees on n vertices. It can be infeasible to generate all members of T , so for
most purposes we are only interested in randomly generating a member of T . A binary
tree of order n generated in this manner is said to be a random graph.
This chapter is a digression into the world of random graphs and various models
for generating different types of random graphs. Unlike other chapters in this book, our
approach is rather informal and not as rigorous as in other chapters. We will discuss some
common models of random graphs and a number of their properties without being bogged
down in details of proofs. Along the way, we will demonstrate that random graphs can
be used to model diverse real-world networks such as social, biological, technological,
and information networks. The edited volume [160] provides some historical context
for the new science of networks. Bollobas [31] and Kolchin [131] provide standard
references on the theory of random graphs with rigorous proofs. For comprehensive
surveys of random graphs and networks that do not go into too much technical details,
see [14, 71, 204, 205]. On the other hand, surveys that cover diverse applications of
random graphs and networks and are geared toward the technical aspects of the subject
include [5, 30, 50, 62, 63, 67, 164].
10.1
Network statistics
Numerous real-world networks are large, having from thousands up to millions of vertices
and edges. Network statistics provide a way to describe properties of networks without
concerning ourselves with individual vertices and edges. A network statistic should
describe essential properties of the network under consideration, provide a means to
differentiate between different classes of networks, and be useful in network algorithms
and applications [41]. In this section, we discuss various common network statistics that
can be used to describe graphs underlying large networks.
10.1.1
Degree distribution
(10.1)
268
As indicated by the notation, we can think of (10.1) as the probability that a vertex v V
chosen uniformly at random has degree k. The degree distribution of G is consequently a
histogram of the degrees of vertices in G. Figure 10.1 illustrates the degree distribution
of the Zachary [216] karate club network. The degree distributions of many real-world
networks have the same general curve as depicted in Figure 10.1(b), i.e. a peak at low
degrees followed by a tail at higher degrees. See for example the degree distribution of
the neural network in Figure 10.2, that of a power grid network in Figure 10.3, and the
degree distribution of a scientific co-authorship network in Figure 10.4.
0.3
0.25
0.2
0.15
0.1
5 102
2
10
12
14
16
100.6
100.8
101
101.2
101.4
100
100.2
100.4
100.6
100.8
101
101.2
Figure 10.1: The friendship network within a 34-person karate club. This is more commonly known as the Zachary [216] karate club network. The network is an undirected,
connected, unweighted graph having 34 vertices and 78 edges. The horizontal axis represents degree; the vertical axis represents the probability that a vertex from the network
has the corresponding degree.
10.1.2
Distance statistics
In chapter 5 we discussed various distance metrics such as radius, diameter, and eccentricity. To that distance statistics collection we add the average or characteristic distance
d, defined as the arithmetic mean of all distances in a graph. Let G = (V, E) be a simple
graph with n = |V | and m = |E|, where G can be either directed or undirected. Then
269
102
6
101.5
4
102
2
20
40
60
80
100
120
100
101
102
Figure 10.2: Degree distribution of the neural network of the Caenorhabditis elegans.
The network is a directed, not strongly connected, weighted graph with 297 vertices
and 2,359 edges. The horizontal axis represents degree; the vertical axis represents the
probability that a vertex from the network has the corresponding degree. The degree
distribution is derived from dataset by Watts and Strogatz [206] and White et al. [207].
0.3
101
0.2
102
0.1
103
10
15
100
100.2
100.4
100.6
100.8
101
101.2
Figure 10.3: Degree distribution of the Western States Power Grid of the United States.
The network is an undirected, connected, unweighted graph with 4,941 vertices and 6,594
edges. The horizontal axis represents degree; the vertical axis represents the probability
that a vertex from the network has the corresponding degree. The degree distribution is
derived from dataset by Watts and Strogatz [206].
270
0.12
0.1
102
8 102
6 102
103
4 102
104
2 102
0
50
100
150
200
250
100
101
102
d(u, v) = 0,
k,
d is given by
if there is no path from u to v,
if u = v,
where k is the length of a shortest u-v path.
If G is strongly connected (respectively, connected for the undirected case) then our
distance function is of the form d : V V Z+ {0}, where the codomain is the
set of nonnegative integers. The case where G is not strongly connected (respectively,
disconnected for the undirected version) requires special care. One way is to compute
the characteristic distance for each component and then find the average of all such
characteristic distances. Call the resulting characteristic distance dc , where c means
component. Another way is to assign a large number as the distance of non-existing
shortest paths. If there is no u-v path, we let d(u, v) = n because n = |V | is larger than
the length of any shortest path between connected vertices. The resulting characteristic
distance is denoted db , where b means big number. Furthermore denote by d the number
of pairs (u, v) such that v is not reachable from u. For example, the Zachary [216] karate
club network has d = 2.4082 and d = 0; the C. elegans neural network [206, 207]
has db = 71.544533, dc = 3.991884, and d = 20, 268; the Western States Power Grid
network [206] has d = 18.989185 and d = 0; and the condensed matter co-authorship
network [162] has db = 7541.74656, dc = 5.499329, and d = 152, 328, 281.
271
We can also define the concept of distance distribution similar to how the degree
distribution was defined in section 10.1.1. If is a positive integer with u and v being
connected vertices in a graph G = (V, E), denote by
p = Pr[d(u, v) = ]
(10.2)
0.3
0.2
0.1
0.1
10
12
14
102
0.3
5
4
0.2
3
2
0.1
1
0
10
20
30
40
(d) Condensed
work [162].
matter
10
12
14
16
co-authorship
18
net-
Figure 10.5: Distance distributions for various real-world networks. The horizontal axis
represents distance and the vertical axis represents the probability that a uniformly
chosen pair of distinct vertices from the network has the corresponding distance between
them.
10.2
In 1959, Gilbert [89] introduced a random graph model that now bears the name binomial (or Bernoulli ) random graph model. First, we fix a positive integer n, a probability
272
G Kn
V {0, 1, . . . , n 1}
E {2-combinations of V }
for each e E do
r draw uniformly at random from interval (0, 1)
if r < p then
add edge e to G
return G
p, and a vertex set V = {0, 1, . . . , n 1}. By G(n, p) we mean a probability space over
the set of undirected simple graphs on n vertices. If G is any element of the probability
space G(n, p) and ij is any edge for distinct i, j V , then ij occurs as an edge of G
independently with probability p. In symbols, for any distinct pair i, j V we have
Pr[ij E(G)] = p
where all such events are mutually independent. Any graph G drawn uniformly at
random from G(n, p) is a subgraph of the complete graph Kn and it follows from (1.6)
that G has at most n2 edges. Then the probability that G has m edges is given by
n
pm (1 p)( 2 )m .
(10.3)
n
2
pn(n 1)
2
n
2
= pn(n 1).
Then the expected degree of each edge is p(n 1). From problem 1.7 we know that the
number of undirected simple graphs on n vertices is given by
2n(n1)/2
273
where (10.3) is the probability of any of these graphs being the output of the above
procedure. Let (n, m) be the number of graphs from G(n, p) that are connected and
have size m, and by Pr[G ] is meant the probability that G G(n, p) is connected. Apply
expression (10.3) to see that
(n2 )
Pr[G ] =
i=n1
n
(n, i) pi (1 p)( 2 )i
where n 1 is the least number of edges of any undirected connected graph on n vertices,
i.e. the size of any spanning tree of a connected graph in G(n, p). Similarly define
Pr[ij ] to be the probability that two distinct vertices i, j of G G(n, p) are connected.
Gilbert [89] showed that as n , then we have
Pr[G ] 1 n(1 p)n1
and
Pr[ij ] 1 2(1 p)n1 .
Algorithm 10.2: Random oriented graph via G(n, p).
Input: Positive integer n and probability 0 < p < 1.
Output: A random oriented graph on n vertices.
1
2
3
4
5
6
7
8
9
10
11
274
275
108
0.2
0.4
0.6
0.8
Figure 10.7: Comparison of expected and experimental values of the number of edges
and total degree of random simple undirected graphs in G(n, p). The horizontal axis
represents probability points; the vertical axis represents the size and total degree (expected or experimental). Fix n = 20, 000 and consider r = 50 probability points chosen
as follows. Let pmin = 0.000001, pmax = 0.999999, and F = (pmax /pmin )1/(r1) . For
i = 1, 2, . . . , r = 50 the i-th probability point pi is defined by pi = pmin F i1 . Each
experiment consists in generating M = 500 random graphs from G(n, pi ). For each
Gi G(n, pi ), where i = 1, 2, . . . , 500, compute its actual size i and actual total degree
i . Then take the mean
of the i and the mean of the i .
Let n > 0 be the number of vertices in D and let 0 < p < 1. Generate a random
oriented graph as follows. First we generate a binomial random graph G G(n, p) where
G is simple and undirected. Then we consider the digraph version of G and proceed to
randomly prune either uv or vu from G, for each distinct pair of vertices u, v. Refer to
Algorithm 10.2 for pseudocode of our discussion. A Sage implementation follows:
sage :
sage :
sage :
sage :
sage :
...
...
...
...
...
276
Figure 10.8: A random oriented graph generated using a graph in G(20, 0.1) and cutoff
probability 0.5.
of G, we can consider the events leading up to the choice of e as
e1 , e2 , . . . , ek
where in the i-th trial the event ei is a failure, for 1 i < k, but the event ek is the
first success after k 1 successive failures. In probabilistic terms, we perform a series
of independent trials each having success probability p and stop when the first success
occurs. Letting X be the number of trials required until the first success occurs, then X
is a geometric random variable with parameter p and probability mass function
Pr[X = k] = p(1 p)k1
for integers k 1, where
(10.4)
k=1
p(1 p)k1 = 1.
k = 1, 2, 3, . . .
Note that
k=1
277
k>
ln(1 r)
ln(1 p)
ln(1 r)
.
ln(1 p)
ln(1 r)
ln(1 p)
which is used as a basis of Algorithm 10.3. In the latter algorithm, note that the vertex
set is V = {0, 1, . . . , n 1} and candidate edges are generated in lexicographic order.
The Batagelj-Brandes Algorithm 10.3 has worst-case runtime O(n + m), where n and m
are the order and size, respectively, of the resulting graph.
Algorithm 10.3: Linear generation of a random sparse graph in G(n, p).
Input: Positive integer n and a probability 0 < p < 1.
Output: A random sparse graph from G(n, p).
1
2
3
4
5
6
7
8
9
10
11
12
G Kn
u1
v 1
while u < n do
r draw uniformly at random from interval (0, 1)
v v + 1 + ln(1 r)/ ln(1 p)
while v u and u < n do
v vu
uu+1
if u < n then
add edge uv to G
return G
Degree distribution
Consider a random graph G G(n, p) and let v be a vertex of G. With probability p, the
vertex v is incident with each of the remaining n 1 vertices in G. Then the probability
that v has degree k is given by the binomial distribution
Pr[deg(v) = k] =
n1 k
p (1 p)n1k
k
(10.5)
278
and the expected degree of v is E[deg(v)] = p(n 1). Setting z = p(n 1), we can
express (10.5) as
Pr[deg(v) = k] =
n1
k
z
n1z
z
1
n1
n1
and thus
zk
Pr[deg(v) = k]
exp(z)
k!
as n . In the limit of large n, the probability that vertex v has degree k approaches
the Poisson distribution. That is, as n gets larger and larger any random graph in G(n, p)
has a Poisson degree distribution.
10.3
Erd
os-R
enyi model
Let N be a fixed nonnegative integer. The Erdos-Renyi [76, 77] (or uniform) random
graph model, denoted G(n, N ), is a probability space over the set of undirected simple
graphs on n vertices and exactly N edges. Hence G(n, N ) can be considered as a collection
n
of ( 2 ) undirected simple graphs on exactly N edges, each such graph being selected with
N
equal probability. A note of caution is in order here. Numerous papers on random graphs
refer to G(n, p) as the Erdos-Renyi random graph model, where in fact this binomial
random graph model should be called the Gilbert model in honor of E. N. Gilbert who
introduced [89] it in 1959. Whenever a paper makes a reference to the Erdos-Renyi
model, one should question whether the paper is referring to G(n, p) or G(n, N ).
To generate a graph in G(n, N ), start with G being a graph on n vertices but no
edges. Then choose N of the possible n2 edges independently and uniformly at random
and let the chosen edges be the edge set of G. Each graph G G(n, N ) is associated
with a probability
n
2
of being the graph resulting from the above procedure. Furthermore each of the
edges has a probability
n
1
2
n
2
We repeatedly choose an edge uniformly at random from the collection of all possible
edges, until we come across the first edge that is not already in the graph. The number
of trials required until the first new edge is chosen can be modeled using the geometric
distribution with probability mass function (10.4). Given a geometric random variable
279
1
2
n
2
G Kn
E e0 , e1 , . . . , e(n)1
2
3
4
5
6
7
8
for i 0, 1, . . . , N 1 do
r draw uniformly at random from 0, 1, . . . , n2 1
while er is an edge of G do
r draw uniformly at random from 0, 1, . . . , n2 1
add edge er to G
return G
E[X] =
1
n p(1 p)n1 = .
p
n=1
i=1
n
2
n
2
n
2
n
2
n
ln
2
dx
n
2
n
2
+C
for some constant C. The denominator in the latter fraction becomes zero when n2 = N ,
which can be prevented by adding one to the denominator. Then we have the expected
total runtime
N
n
n
n
2
2
ln n
n
2
i
N +1
2
i=1 2
which is O(N ) when N n2 /2, and O(N ln N ) when N = n2 . In other words,
Algorithm 10.4 has expected linear runtime when the number N of required edges satisfies
N n2 /2. But for N > n2 /2, we obtain expected linear runtime by generating the
complete graph Kn and randomly delete n2 N edges from the latter graph. Our
discussion is summarized in Algorithm 10.5.
280
1
2
3
4
5
6
7
n
2
if N n2 /2 then
return result of Algorithm 10.4
G Kn
for i 1, 2, . . . , n2 N do
e draw uniformly at random from E(G)
remove edge e from G
return G
10.4
Small-world networks
Many real-world networks exhibit the small-world effect: that most pairs of distinct
vertices in the network are connected by relatively short path lengths. The small-world
effect was empirically demonstrated [156] in a famous 1960s experiment by Stanley Milgram, who distributed a number of letters to a random selection of people. Recipients
were instructed to deliver the letters to the addressees on condition that letters must be
passed to people whom the recipients knew on a first-name basis. Milgram found that
on average six steps were required for a letter to reach its target recipient, a number now
immortalized in the phrase six degrees of separation [97]. Figure 10.9 plots results of
an experimental study of the small-world problem as reported in [194]. The small-world
effect has been studied and verified for many real-world networks including
social: collaboration network of actors in feature films [7,206], scientific publication
authorship [51, 96, 161, 162];
information: citation network [174], Rogets Thesaurus [128], word co-occurrence [66,
80];
technological: internet [55, 79], power grid [206], train routes [180], software [163,
197];
biological: metabolic network [117], protein interactions [116], food web [112, 149],
neural network [206, 207].
281
frequency
15
10
10
number of intermediaries
Figure 10.9: Frequency distribution of the number of intermediaries required for letters
to reach their intended addressees. The distribution has a mean of 5.3, interpreted as the
average number of intermediaries required for a letter to reach its intended destination.
The plot is derived from data reported in [194].
Watts and Strogatz [203, 204, 206] proposed a network model that produces graphs
exhibiting the small-world effect. We will use the notation to mean much greater
than. Let n and k be positive integers such that n
k
ln n
1 (in particular,
0 < k < n/2) with k being even. Consider a probability 0 < p < 1. Starting from
an undirected k-circulant graph G = (V, E) on n vertices, the Watts-Strogatz model
proceeds to rewire each edge with probability p. The rewiring procedure, also called
edge swapping, works as follows. Let V be uniformly distributed. For each v V , let
e E be an edge having v as an endpoint. Choose another u V different from v. With
probability p, delete the edge e and add the edge vu. The rewiring must produce a simple
graph with the same order and size as G. As p 1, the graph G goes from k-circulant
to exhibiting properties of graphs drawn uniformly from G(n, p). Small-world networks
are intermediate between k-circulant and binomial random graphs (see Figure 10.10).
The Watts-Strogatz model is said to provide a procedure for interpolating between the
latter two types of graphs.
(a) p = 0, k-circulant
(c) p = 1, random
Figure 10.10: With increasing randomness, k-circulant graphs evolve to exhibit properties of random graphs in G(n, p). Small-world networks are intermediate between
k-circulant graphs and random graphs in G(n, p).
282
The last paragraph contains an algorithm for rewiring edges of a graph. While the
algorithm is simple, in practice it potentially skips over a number of vertices to be
considered for rewiring. If G = (V, E) is a k-circulant graph on n vertices and p is the
rewiring probability, the candidate vertices to be rewired follow a geometric distribution
with parameter p. This geometric trick, essentially the same speed-up technique used by
the Batagelj-Brandes Algorithm 10.3, can be used to speed up the rewiring algorithm.
To elaborate, suppose G has vertex set V = {0, 1, . . . , n 1}. If r is chosen uniformly at
random from the interval (0, 1), the index of the vertex to be rewired can be obtained
from
ln(1 r)
.
1+
ln(1 p)
The above geometric method is incorporated into Algorithm 10.6 to generate a WattsStrogatz network in worst-case runtime O(nk +m), where n and k are as per the input of
the algorithm and m is the size of the k-circulant graph on n vertices. Note that lines 7
to 12 are where we avoid self-loops and multiple edges.
Algorithm 10.6: Watts-Strogatz network model.
Input: Positive integer n denoting the number of vertices. Positive even integer k
for the degree of each vertex, where n
k
ln n
1. In particular, k
should satisfy 0 < k < n/2. Rewiring probability 0 < p 1.
Output: A Watts-Strogatz network on n vertices.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
M nk
/* sum of all vertex degrees = twice number of edges */
r draw uniformly at random from interval (0, 1)
v 1 + ln(1 r)/ ln(1 p)
E contiguous edge list of k-circulant graph on n vertices
while v M do
u draw uniformly at random from [0, 1, . . . , n 1]
if v 1 is even then
while E[v] = u or (u, E[v]) E do
u draw uniformly at random from [0, 1, . . . , n 1]
else
while E[v 2] = u or (E[v 2], u) E do
u draw uniformly at random from [0, 1, . . . , n 1]
E[v 1] u
r draw uniformly at random from interval (0, 1)
v v + 1 + ln(1 r)/ ln(1 p)
G Kn
add edges in E to G
return G
283
dij = 0, if i = j,
1
1
n(n 1)/2 2
1
=
n(n 1)
dij
i=j
(10.6)
dij
i=j
which is averaged over all possible pairs of distinct vertices, i.e. the number of edges in
the complete graph Kn .
It is inefficient to compute the characteristic path length via equation (10.6) because
we would effectively sum n(n 1) distance values. As G is undirected, note that
1
2
dij =
dij =
i<j
i=j
dij .
i>j
The latter equation holds for the following reason. Let D = [dij ] be a matrix of distances
for G, where i is the row index, j is the column index, and dij is the distance from i to j.
The required sum of distances can be obtained by summing all entries above (or below)
the main diagonal of D. Therefore the characteristic path length can be expressed as
(G) =
=
2
n(n 1)
2
n(n 1)
dij
i<j
dij
i>j
=
.
4k
2k
In the special case p = 0, we have
=
However as p 1, we have
ln n
.
ln k
n(n + k 2)
.
2k(n 1)
284
Clustering coefficient
The clustering coefficient of a simple graph G quantifies the cliquishness of vertices
in G = (V, E). This quantity is thus said to be a local property of G. Watts and
Strogatz [206] defined the clustering coefficient as follows. Suppose n = |V | > 0 and let ni
count the number of neighbors of vertex i V , a quantity that is equivalent to the degree
of i, i.e. deg(i) = ni . The complete graph Kni on the ni neighbors of i has ni (ni 1)/2
edges. The neighbor graph Ni of i is a subgraph of G, consisting of all vertices (= i) that
are adjacent to i and preserving the adjacency relation among those vertices as found in
the supergraph G. For example, given the graph in Figure 10.11(a) the neighbor graph
of vertex 10 is shown in Figure 10.11(b). The local clustering coefficient Ci of i is the
ratio
Ni
Ci =
ni (ni 1)/2
where Ni counts the number of edges in Ni . In case i has degree deg(i) < 2, we set the
local clustering coefficient of i to be zero. Then the clustering coefficient of G is defined
by
1
1
Ni
C(G) =
Ci =
.
n iV
n iV ni (ni 1)/2
3
1
10
9
7
6
7
(b) N10
285
Degree distribution
For a Watts-Strogatz network without rewiring, each vertex has the same degree k. It
easily follows that for each vertex v, we have the degree distribution
1, if i = k,
Pr[deg(v) = i] =
0, otherwise.
A rewiring probability p > 0 introduces disorder in the network and broadens the
degree distribution, while the expected degree is k. A k-circulant graph on n vertices
has nk/2 edges. With the rewiring probability p > 0, a total of pnk/2 edges would
be rewired. However note that only one endpoint of an edge is rewired, thus after the
rewiring process the degree of any vertex v is deg(v) k/2. Therefore with k > 2, a
Watts-Strogatz network has no isolated vertices.
For p > 0, Barrat and Weigt [18] showed that the degree of a vertex v can be written
as deg(v) = k/2 + ni with ni 0, where ni can be divided into two parts and as
follows. First k/2 edges are left intact after the rewiring process, the probability of
this occurring is 1 p for each edge. Second = ni edges have been rewired towards
i, each with probability 1/n. The probability distribution of is
k/2
(1 p) pk/2
P1 () =
and the probability distribution of is
P2 () =
pnk/2
1
n
1
1
n
pnk/2
where
(pk/2)
exp(pk/2)
!
for large n. Combine the above two factors to obtain the degree distribution
P2 ()
min{k/2, k/2}
Pr[deg(v) = ] =
i=0
k/2
(pk/2)k/2i
exp(pk/2)
(1 p)i pk/2i
i
( k/2 i)!
for k/2.
10.5
Scale-free networks
The networks covered so farGilbert G(n, p) model, Erdos-Renyi G(n, N ) model, WattsStrogatz small-world modelare static. Once a network is generated from any of these
models, the corresponding model does not specify any means for the network to evolve
over time. Barabasi and Albert [15] proposed a network model based on two ingredients:
1. Growth: at each time step, a new vertex is added to the network and connected
to a pre-determined number of existing vertices.
2. Preferential attachment: the newly added vertex is connected to an existing vertex
in proportion to the latters existing degree.
286
Preferential attachment also goes by the colloquial name of the rich-get-richer effect
due to the work of Herbert Simon [185]. In sociology, preferential attachment is known
as the Matthew effect due to the following verse from the Book of Matthew, chapter 25
verse 29, in the Bible: For to every one that hath shall be given but from him that
hath not, that also which he seemeth to have shall be taken away. Barabasi and Albert
observed that many real-world networks exhibit statistical properties of their proposed
model. One particularly significant property is that of power-law scaling, hence the
Barabasi-Albert model is also called a model of scale-free networks. Note that it is only
the degree distributions of scale-free networks that are scale-free. In their empirical study
of the World Wide Web (WWW) and other real-world networks, Barabasi and Albert
noted that the probability that a web page increases in popularity is directly proportional
to the pages current popularity. Thinking of a web page as a vertex and the degree of a
page as the number of other pages that the current page links to, the degree distribution
of the WWW follows a power law function. Power-law scaling has been confirmed for
many real-world networks:
actor collaboration network [15]
citation [64, 174, 179] and co-authorship networks [162]
human sexual contacts network [119, 144]
the Internet [55, 79, 198] and the WWW [6, 17, 42]
metabolic networks [116, 117]
telephone call graphs [3, 4]
Figure 10.12 illustrates the degree distributions of various real-world networks, plotted
on log-log scales. Corresponding distributions for various simulated Barabasi-Albert
networks are illustrated in Figure 10.13.
But how do we generate a scale-free graph as per the description in Barabasi and
Albert [15]? The original description of the Barabasi-Albert model as contained in [15]
is rather ambiguous with respect to certain details. First, the whole process is supposed
to begin with a small number of vertices. But as the degree of each of these vertices
is zero, it is unclear how the network is to grow via preferential attachment from the
initial pool of vertices. Second, Barabasi and Albert neglected to clearly specify how to
select the neighbors for the newly added vertex. The above ambiguities are resolved by
Bollobas et al. [33], who gave a precise statement of a random graph process that realizes
the Barabasi-Albert model. Fix a sequence of vertices v1 , v2 , . . . and consider the case
where each newly added vertex is to be connected to m = 1 vertex already in a graph.
Inductively define a random graph process (Gt1 )t0 as follows, where Gt1 is a digraph on
{vi | 1 i t}. Start with the null graph G01 or the graph G11 with one vertex and one
self-loop. Denote by degG (v) the total (in and out) degree of vertex v in the graph G.
by adding the vertex vt and a directed edge from vt to
For t > 1 construct Gt1 from Gt1
1
vi , where i is randomly chosen with probability
Pr[i = s] =
degGt1
(vs )/(2t 1), if 1 s t 1,
1
1/(2t 1),
if s = t.
287
101
101
102
102
3
10
103
104
104
105
105
106
100
101
102
100
101
102
103
101
102
102
103
103
104
104
105
105
106
100
101
102
103
104
100
101
102
103
Figure 10.12: Degree distributions of various real-world networks on log-log scales. The
horizontal axis represents degree and the vertical axis is the corresponding probability of
a vertex having that degree. The US patent citation network [142] is a directed graph on
3, 774, 768 vertices and 16, 518, 948 edges. It covers all citations made by patents granted
between 1975 and 1999. The Google web graph [143] is a digraph having 875, 713 vertices
and 5, 105, 039 edges. This dataset was released in 2002 by Google as part of the Google
Programming Contest. The LiveJournal friendship network [11, 143] is a directed graph
on 4, 847, 571 vertices and 68, 993, 773 edges. The actor collaboration network [15], based
on the Internet Movie Database (IMDb) at http://www.imdb.com, is an undirected graph
on 383, 640 vertices and 16, 557, 920 edges. Two actors are connected to each other if
they have starred in the same movie. In all of the above degree distributions, self-loops
are not taken into account and, where a graph is directed, we only consider the in-degree
distribution.
288
101
101
102
102
103
103
104
104
105
105 0
10
101
102
103
106 0
10
104
101
103
104
105
101
101
102
102
103
103
104
104
105
105
106
106
107 0
10
102
107
10
10
10
10
10
10
100
101
102
103
104
105
106
289
The latter process generates a forest. For m > 1 the graph evolves as per the case
m = 1; i.e. we add m edges from vt one at a time. This process can result in selfn
for the collection of all graphs on n vertices
loops and multiple edges. We write Gm
n
is
and minimal degree m in the Barabasi-Albert model, where a random graph from Gm
n
n
denoted Gm Gm .
Now consider the problem of translating the above procedure into pseudocode. Fix a
positive integer n > 1 for the number of vertices in the scale-free graph to be generated
via preferential attachment. Let m 1 be the number of vertices that each newly added
vertex is to be connected to; this is equivalent to the minimum degree that any new vertex
will end up possessing. At any time step, let M be the contiguous edge list of all edges
created thus far in the above random graph process. It is clear that the frequency (or
number of occurrences) of a vertex is equivalent to the vertexs degree. We can thus use
M as a pool to sample in constant time from the degree-skewed distribution. Batagelj and
Brandes [19] used the latter observation to construct an algorithm for generating scalefree networks via preferential attachment; pseudocode is presented in Algorithm 10.7.
Note that the algorithm has linear runtime O(n + m), where n is the order and m the
size of the graph generated by the algorithm.
Algorithm 10.7: Scale-free network via preferential attachment.
Input: Positive integer n > 1 and minimum degree d 1.
Output: Scale-free network on n vertices.
1
2
3
4
5
6
7
8
9
G Kn
/* vertex set is V = {0, 1, . . . , n 1} */
M list of length 2nd
for v 0, 1, . . . , n 1 do
for i 0, 1, . . . , d 1 do
M [2(vd + i)] v
r draw uniformly at random from {0, 1, . . . , 2(vd + i)}
M [2(vd + i) + 1] M [r]
add edge (M [2i], M [2i + 1]) to G for i 0, 1, . . . , nd 1
return G
1
2t 1
2t
E[degGt1
(vs )].
1
2t 1
Taking the above two equations together, for 1 s n we have
E[degGt1 (vs )] =
n
E[degGn1 (vs )] =
i=s
2i
4ns+1 n!2 (2s 2)!
=
.
2i 1
(2n)!(s 1)!2
290
2m(m + 1)n
(k + m)(k + m + 1)(k + m + 2)
uniformly in k.
As regards the diameter, with n as per Algorithm 10.7, computer simulation by
Barabasi, Albert, and Jeong [6, 17] and heuristic arguments by Newman et al. [165]
suggest that a graph generated by the Barabasi-Albert model has diameter approximately
ln n. As noted by Bollobas and Riordan [32], the approximation diam(Gnm ) ln n holds
for the case m = 1, but for m 2 they showed that as n then diam(Gnm )
ln / ln ln n.
10.6
Problems
Where should I start? Start from the statement of the problem. What can I do? Visualize
the problem as a whole as clearly and as vividly as you can.
G. Polya, from page 33 of [171]
10.1. Algorithm 10.8 presents a procedure to construct a random graph that is simple
and undirected; the procedure is adapted from pages 47 of Lau [139]. Analyze the
time complexity of Algorithm 10.8. Compare and contrast your results with that
for Algorithm 10.5.
10.2. Modify Algorithm 10.8 to generate the following random graphs.
(a) Simple weighted, undirected graph.
(b) Simple digraph.
(c) Simple weighted digraph.
10.3. Algorithm 10.1 can be considered as a template for generating random graphs in
G(n, p). The procedure does not specify how to generate all the 2-combinations of
a set of n > 1 objects. Here we discuss how to construct all such 2-combinations
and derive a quadratic time algorithm for generating random graphs in G(n, p).
(a) Consider a vertex set V = {0, 1, . . . , n 1} with at least two elements and let
E be the set of all 2-combinations of V , where each 2-combination is written
ij. Show that ij E if and only if i < j.
(b) From the previous exercise, we know that if 0 i < n 1 then there are
n (i + 1) pairs jk where either i = j or i = k. Show that
n2
n2 n
(n i 1) =
2
i=0
and conclude that Algorithm 10.9 has worst-case runtime O((n2 n)/2).
10.4. Modify the Batagelj-Brandes Algorithm 10.3 to generate the following types of
graphs.
(a) Directed simple graphs.
10.6. Problems
if n = 1 then
return K1
max n(n 1)/2
if m > max then
return Kn
G null graph
A n n adjacency matrix with entries aij
aij False for 0 i, j < n
i0
while i < m do
u draw uniformly at random from {0, 1, . . . , n 1}
v draw uniformly at random from {0, 1, . . . , n 1}
if u = v then
continue with next iteration of loop
if u > v then
swap values of u and v
if auv = False then
add edge uv to G
auv True
ii+1
return G
G Kn
V {0, 1, . . . , n 1}
for i 0, 1, . . . , n 2 do
for j i + 1, i + 2, . . . , n 1 do
r draw uniformly at random from interval (0, 1)
if r < p then
add edge ij to G
return G
291
292
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
n
2
max n2
if n = 1 or N = max then
return Kn
G Kn
u0
v1
t0
/* number of candidates processed so far */
k0
/* number of edges selected so far */
while True do
r draw uniformly at random from {0, 1, . . . , max t}
if r < N k then
add edge uv to G
k k+1
if k = N then
return G
tt+1
v v+1
if v = n then
uu+1
v u+1
10.6. Problems
293
d0 (t)
t
294
dk1 (t)
dk (t)
2
.
t
t
As t , show that the probability that a vertex be chosen twice decreases as
t2 . If v is a vertex chosen uniformly at random, show that
dk (t + 1) = dk (t) + 2
Pr[deg(v) = k] =
(2)k
(1 + 2)k+1
and conclude that the CHKNS model has an exponential degree distribution. The
size of a component counts the number of vertices in the component itself. Let
Nk (t) be the expected number of components of size k at time t. Show that
N1 (t + 1) = N1 (t) + 1 2
N1 (t)
t
Nk (t + 1) = Nk (t) +
i=1
t
t
kNk (t)
.
t
10.12. Algorithm 10.7 can easily be modified to generate other types of scale-free networks. Based upon the latter algorithm, Batagelj and Brandes [19] presented
a procedure for generating bipartite scale-free networks; see Algorithm 10.11 for
pseudocode. Analyze the runtime efficiency of Algorithm 10.11. Fix positive integer values for n and d, say n = 10, 000 and d = 4. Use Algorithm 10.11 to generate
a bipartite graph with your chosen values for n and d. Plot the degree distribution
of the resulting graph using a log-log scale and confirm that the generated graph
is scale-free.
10.13. Find the degree and distance distributions, average path lengths, and clustering
coefficients of the following network datasets:
(a) actor collaboration [15]
(b) co-authorship of condensed matter preprints [162]
(c) Google web graph [143]
(d) LiveJournal friendship [11, 143]
(e) neural network of the C. elegans [206, 207]
(f) US patent citation [142]
(g) Western States Power Grid of the US [206]
(h) Zachary karate club [216]
10.14. Consider the plots of degree distributions in Figures 10.12 and 10.13. Note the
noise in the tail of each plot. To smooth the tail, we can use the cumulative degree
distribution
P c (k) =
Pr[deg(v) = i].
i=k
Given a graph with scale-free degree distribution P (k) k and > 1, the
cumulative degree distribution follows P c (k) k 1 . Plot the cumulative degree
distribution of each network dataset in Problem 10.13.
10.6. Problems
295
G K2n
/* vertex set is {0, 1, . . . , 2n 1} */
M1 list of length 2nd
M2 list of length 2nd
for v = 0, 1, . . . , n 1 do
for i = 0, 1, . . . , d 1 do
M1 [2(vd + i)] v
M2 [2(vd + i)] n + v
r draw uniformly at random from {0, 1, . . . , 2(vd + i)}
if r is even then
M1 [2(vd + i) + 1] M2 [r]
else
M1 [2(vd + i) + 1] M1 [r]
r draw uniformly at random from {0, 1, . . . , 2(vd + i)}
if r is even then
M2 [2(vd + i) + 1] M1 [r]
else
M2 [2(vd + i) + 1] M2 [r]
add edges (M1 [2i], M1 [2i + 1]) and (M2 [2i], M2 [2i + 1]) to G for i = 0, 1, . . . , nd 1
return G
Chapter 11
Graph problems and their LP
formulations
This document is meant as an explanation of several graph theoretical functions defined
in Sages Graph Library (http://www.sagemath.org/), which use Linear Programming
to solve optimization of existence problems.
11.1
Even though such a formulation does not show it, this quantity can be computed in
polynomial time through Linear Programming. Indeed, we can think of this as a simple
flow problem defined on a bipartite graph. Let D be a directed graph whose vertex set
we first define as the disjoint union of E(G) and V (G). We add in D an edge between
(e, v) E(G) V (G) if and only if v is one of es endpoints. Each edge will then have a
flow of 2 (through the addition in D of a source and the necessary edges) to distribute
among its two endpoints. We then write in our linear program the constraint that each
vertex can absorb a flow of at most z (add to D the necessary sink and the edges with
capacity z).
Clearly, if H G is the densest subgraph in G, its |E(H)| edges will send a flow
. An
of 2|E(H)| to their |V (H)| vertices, such a flow being feasible only if z 2|E(H)|
|V (H)|
elementary application of the max-flow/min-cut theorem, or of Halls bipartite matching
theorem shows that such a value for z is also sufficient. This LP can thus let us compute
the Maximum Average Degree of the graph.
Sage method : Graph.maximum_average_degree()
LP Formulation :
Minimize : z
Such that :
296
297
eE(G)
ev
xe,v z
REMARK : In many if not all the other LP formulations, this Linear Program
is used as a constraint. In those problems, we are always at some point looking for a
subgraph H of G such that H does not contain any cycle. The edges of G are in this
case variables, whose value can be equal to 0 or 1 depending on whether they belong
to such a graph H. Based on the observation that the Maximum Average Degree of a
tree on n vertices is exactly its average degree (= 2 2/n < 1), and that any cycles
in a graph ensures its average degree is larger than 2, we can then set the constraint
2
. This is a handy way to write in LP the constraint that the set of
that z 2 |V (G)|
edges belonging to H is acyclic. For this to work, though, we need to ensure that the
variables corresponding to our edges are binary variables.
11.2
298
w(e)be
eE(G)
Such that :
be = 2
eE(G)
ev
v V (G v ),
eE(G)
ev
xe,v 2
2
|V (G)|
Variables
299
p . get_values ( f )
= Graph ()
e in g . edges ( labels = False ):
if f [ R ( e [0] , e [1])] == 1:
tsp . add_edge ( e )
#
#
#
#
#
11.3
optional
optional
optional
optional
optional
This problem is polynomial by a result from Edmonds. Obviously, nothing ensures the
following formulation is a polynomial algorithm as it contains many integer variables,
but it is still a short practical way to solve it.
This problem amounts to finding, given a graph G and an integer k, edge-disjoint
spanning trees T1 , . . . , Tk which are subgraphs of G. In this case, we will chose to define
a spanning tree as an acyclic set of |V (G)| 1 edges.
Sage method : Graph.edge_disjoint_spanning_trees()
LP Formulation :
Maximize : nothing
Such that :
i[1,...,k]
be,k 1
eE(G)
be,k = |V (G)| 1
No cycles
In each set, each edge sends a flow of 2 if it is taken
eE(G)
ev
xe,k,v 2
2
|V (G)|
Variables
300
#
#
#
#
11.4
optional
optional
optional
optional
Steiner tree
See Trietsch [195] for a relationship between Steiner trees and Eulers problem of polygon
division. Finding a spanning tree in a Graph G can be done in linear time, whereas
computing a Steiner Tree is NP-hard. The goal is in this case, given a graph, a weight
function w : E(G) R and a set S of vertices, to find the tree of minimum cost
connecting them all together. Equivalently, we will be looking for an acyclic subgraph
Hof G containing |V (H)| vertices and |E(H)| = |V (H)| 1 edges, which contains each
vertex from S
301
LP Formulation :
Minimize :
w(e)be
eE(G)
Such that :
eE(G)
ev
be 1
cv
be = 1
eE(G)
No Cycles
Each edge sends a flow of 2 if it is taken
v V (G),
eE(G)
ev
xe,v 2
2
|V (G)|
Variables :
302
11.5
Linear arboricity
The linear arboricity of a graph G is the least number k such that the edges of G can
be partitioned into k classes, each of them being a forest of paths (the disjoints union
of paths trees of maximal degree 2). The corresponding LP is very similar to the one
giving edge-disjoint spanning trees
LP Formulation :
Maximize : nothing
Such that :
be,k = 1
i[1,...,k]
303
eE(G)
ev
be,k 2
No cycles
In each set, each edge sends a flow of 2 if it is taken
eE(G)
ev
xe,k,v 2
2
|V (G)|
Variables
gg = g . copy ()
gg . delete_edges ( g . edges ())
answer = [ gg . copy () for i in range ( k )]
add = lambda (u , v ) , i : answer [ i ]. add_edge (( u , v ))
304
11.6
H-minor
11.6. H-minor
305
LP Formulation :
Maximize : nothing
Such that :
hV (H)
rsh,g 1
An edge e can only belong to the tree of h if both its endpoints represent h
e = g1 g2 E(G), te,h rsh,g1 and te,h rsh,g2
In each representative set, the number of vertices is one more than the number
of edges in the corresponding tree
h,
gV (G)
rsh,g
te,h = 1
eE(G)
te,h
hV (H)
v V (G),
eE(G)
ev
xe,k,v 2
2
|V (G)|
arc(g1 ,g2 ),(h1 ,h2 ) can only be equal to 1 if g1 g2 is leaving the representative set
of h1 to enter the one of h2 . (note that this constraints has to be written both
for g1 , g2 , and then for g2 , g1 )
g1 , g2 V (G), g1 = g2 , h1 h2 E(H)
arc(g1 ,g2 ),(h1 ,h2 ) rsh1 ,g1 and arc(g1 ,g2 ),(h1 ,h2 ) rsh2 ,g2
We have the necessary edges between the representative sets
h1 h2 E(H)
g1 ,g2 V (G),g1 =g2
Variables
306
sage : for v in g :
...
p . add_constraint ( sum ([ rs [ h ][ v ] for h in H ]) , max = 1)
sage : # We ensure that the set of representatives of a
sage : # vertex h contains a tree , and thus is connected
sage : # edges represents the edges of the tree
sage : edges = p . new_variable ( dim = 2)
sage : # there can be a edge for h between two vertices
sage : # only if those vertices represent h
sage : for u , v in g . edges ( labels = None ):
...
for h in H :
...
p . add_constraint ( edges [ h ][ S (( u , v ))] - rs [ h ][ u ] , max = 0 )
...
p . add_constraint ( edges [ h ][ S (( u , v ))] - rs [ h ][ v ] , max = 0 )
sage : # The number of edges of the tree in h is exactly the cardinal
sage : # of its representative set minus 1
sage : for h in H :
...
p . add_constraint (
...
sum ([ edges [ h ][ S ( e )] for e in g . edges ( labels = None )])
...
- sum ([ rs [ h ][ v ] for v in g ])
...
==1 )
sage : # a tree has no cycle
sage : epsilon = 1/(5* Integer ( g . order ()))
sage : r_edges = p . new_variable ( dim =2)
sage : for h in H :
...
for u , v in g . edges ( labels = None ):
...
p . add_constraint (
...
r_edges [ h ][( u , v )] + r_edges [ h ][( v , u )] >= edges [ h ][ S (( u , v ))])
...
for v in g :
...
p . add_constraint (
...
sum ([ r_edges [ h ][( u , v )] for u in g . neighbors ( v )]) <= 1 - epsilon )
sage : # Once the representative sets are described , we must ensure
sage : # there are arcs corresponding to those of H between them
sage : h_edges = p . new_variable ( dim =2)
sage : for h1 , h2 in H . edges ( labels = None ):
...
for v1 , v2 in g . edges ( labels = None ):
...
p . add_constraint ( h_edges [( h1 , h2 )][ S (( v1 , v2 ))]
...
p . add_constraint ( h_edges [( h1 , h2 )][ S (( v1 , v2 ))]
...
p . add_constraint ( h_edges [( h2 , h1 )][ S (( v1 , v2 ))]
...
p . add_constraint ( h_edges [( h2 , h1 )][ S (( v1 , v2 ))]
sage :
sage :
sage :
sage :
0.0
p . set_binary ( rs )
p . set_binary ( edges )
p . set_objective ( None )
p . solve ()
rs [ h2 ][ v2 ] ,
rs [ h1 ][ v1 ] ,
rs [ h1 ][ v2 ] ,
rs [ h2 ][ v1 ] ,
max
max
max
max
=
=
=
=
0)
0)
0)
0)
11.6. H-minor
sage : # the corresponding set of vertices in G
sage : rs = p . get_values ( rs )
sage : from sage . sets . set import Set
sage : rs_dict = {}
sage : for h in H :
...
rs_dict [ h ] = [ v for v in g if rs [ h ][ v ]==1]
307
Appendix A
Asymptotic growth
Name
theta
Standard notation
f (n) = (g(n))
big oh
f (n) = O(g(n))
omega
f (n) = (g(n))
little oh
little omega
tilde
f (n) c g(n)
f (n) = o(g(n))
f (n)
f (n)
g(n)
f (n) = (g(n))
f (n) = (g(n))
f (n)
f (n)
g(n)
f (n) (g(n))
o(g(n))
(g(n))
f (n) (g(n))
f (n) c g(n)
Class
lim f (n)/g(n) =
Equivalent definition
f (n) = (g(n))
a constant
f (n) = o(g(n))
zero
f (n) = (g(n))
308
Appendix B
GNU Free Documentation License
Version 1.3, 3 November 2008
Copyright 2000, 2001, 2002, 2007, 2008 Free Software Foundation, Inc.
http://www.fsf.org
Everyone is permitted to copy and distribute verbatim copies of this license document,
but changing it is not allowed.
Preamble
The purpose of this License is to make a manual, textbook, or other functional
and useful document free in the sense of freedom: to assure everyone the effective
freedom to copy and redistribute it, with or without modifying it, either commercially
or noncommercially. Secondarily, this License preserves for the author and publisher a
way to get credit for their work, while not being considered responsible for modifications
made by others.
This License is a kind of copyleft, which means that derivative works of the document must themselves be free in the same sense. It complements the GNU General
Public License, which is a copyleft license designed for free software.
We have designed this License in order to use it for manuals for free software, because free software needs free documentation: a free program should come with manuals
providing the same freedoms that the software does. But this License is not limited to
software manuals; it can be used for any textual work, regardless of subject matter or
whether it is published as a printed book. We recommend this License principally for
works whose purpose is instruction or reference.
310
A Modified Version of the Document means any work containing the Document
or a portion of it, either copied verbatim, or with modifications and/or translated into
another language.
A Secondary Section is a named appendix or a front-matter section of the Document that deals exclusively with the relationship of the publishers or authors of the
Document to the Documents overall subject (or to related matters) and contains nothing that could fall directly within that overall subject. (Thus, if the Document is in part
a textbook of mathematics, a Secondary Section may not explain any mathematics.) The
relationship could be a matter of historical connection with the subject or with related
matters, or of legal, commercial, philosophical, ethical or political position regarding
them.
The Invariant Sections are certain Secondary Sections whose titles are designated, as being those of Invariant Sections, in the notice that says that the Document is
released under this License. If a section does not fit the above definition of Secondary
then it is not allowed to be designated as Invariant. The Document may contain zero
Invariant Sections. If the Document does not identify any Invariant Sections then there
are none.
The Cover Texts are certain short passages of text that are listed, as Front-Cover
Texts or Back-Cover Texts, in the notice that says that the Document is released under
this License. A Front-Cover Text may be at most 5 words, and a Back-Cover Text may
be at most 25 words.
A Transparent copy of the Document means a machine-readable copy, represented in a format whose specification is available to the general public, that is suitable
for revising the document straightforwardly with generic text editors or (for images composed of pixels) generic paint programs or (for drawings) some widely available drawing
editor, and that is suitable for input to text formatters or for automatic translation to
a variety of formats suitable for input to text formatters. A copy made in an otherwise Transparent file format whose markup, or absence of markup, has been arranged to
thwart or discourage subsequent modification by readers is not Transparent. An image
format is not Transparent if used for any substantial amount of text. A copy that is not
Transparent is called Opaque.
Examples of suitable formats for Transparent copies include plain ASCII without
markup, Texinfo input format, LaTeX input format, SGML or XML using a publicly
available DTD, and standard-conforming simple HTML, PostScript or PDF designed
for human modification. Examples of transparent image formats include PNG, XCF
and JPG. Opaque formats include proprietary formats that can be read and edited only
by proprietary word processors, SGML or XML for which the DTD and/or processing
tools are not generally available, and the machine-generated HTML, PostScript or PDF
produced by some word processors for output purposes only.
The Title Page means, for a printed book, the title page itself, plus such following
pages as are needed to hold, legibly, the material this License requires to appear in the
title page. For works in formats which do not have any title page as such, Title Page
means the text near the most prominent appearance of the works title, preceding the
beginning of the body of the text.
The publisher means any person or entity that distributes copies of the Document
to the public.
A section Entitled XYZ means a named subunit of the Document whose title
either is precisely XYZ or contains XYZ in parentheses following text that translates XYZ
311
in another language. (Here XYZ stands for a specific section name mentioned below,
such as Acknowledgements, Dedications, Endorsements, or History.)
To Preserve the Title of such a section when you modify the Document means that
it remains a section Entitled XYZ according to this definition.
The Document may include Warranty Disclaimers next to the notice which states
that this License applies to the Document. These Warranty Disclaimers are considered
to be included by reference in this License, but only as regards disclaiming warranties:
any other implication that these Warranty Disclaimers may have is void and has no effect
on the meaning of this License.
2. VERBATIM COPYING
You may copy and distribute the Document in any medium, either commercially
or noncommercially, provided that this License, the copyright notices, and the license
notice saying this License applies to the Document are reproduced in all copies, and
that you add no other conditions whatsoever to those of this License. You may not use
technical measures to obstruct or control the reading or further copying of the copies
you make or distribute. However, you may accept compensation in exchange for copies.
If you distribute a large enough number of copies you must also follow the conditions in
section 3.
You may also lend copies, under the same conditions stated above, and you may
publicly display copies.
3. COPYING IN QUANTITY
If you publish printed copies (or copies in media that commonly have printed covers)
of the Document, numbering more than 100, and the Documents license notice requires
Cover Texts, you must enclose the copies in covers that carry, clearly and legibly, all
these Cover Texts: Front-Cover Texts on the front cover, and Back-Cover Texts on the
back cover. Both covers must also clearly and legibly identify you as the publisher of
these copies. The front cover must present the full title with all words of the title equally
prominent and visible. You may add other material on the covers in addition. Copying
with changes limited to the covers, as long as they preserve the title of the Document
and satisfy these conditions, can be treated as verbatim copying in other respects.
If the required texts for either cover are too voluminous to fit legibly, you should put
the first ones listed (as many as fit reasonably) on the actual cover, and continue the
rest onto adjacent pages.
If you publish or distribute Opaque copies of the Document numbering more than 100,
you must either include a machine-readable Transparent copy along with each Opaque
copy, or state in or with each Opaque copy a computer-network location from which
the general network-using public has access to download using public-standard network
protocols a complete Transparent copy of the Document, free of added material. If
you use the latter option, you must take reasonably prudent steps, when you begin
distribution of Opaque copies in quantity, to ensure that this Transparent copy will
remain thus accessible at the stated location until at least one year after the last time
you distribute an Opaque copy (directly or through your agents or retailers) of that
edition to the public.
312
It is requested, but not required, that you contact the authors of the Document well
before redistributing any large number of copies, to give them a chance to provide you
with an updated version of the Document.
4. MODIFICATIONS
You may copy and distribute a Modified Version of the Document under the conditions of sections 2 and 3 above, provided that you release the Modified Version under
precisely this License, with the Modified Version filling the role of the Document, thus
licensing distribution and modification of the Modified Version to whoever possesses a
copy of it. In addition, you must do these things in the Modified Version:
A. Use in the Title Page (and on the covers, if any) a title distinct from that of the
Document, and from those of previous versions (which should, if there were any,
be listed in the History section of the Document). You may use the same title as
a previous version if the original publisher of that version gives permission.
B. List on the Title Page, as authors, one or more persons or entities responsible for
authorship of the modifications in the Modified Version, together with at least five
of the principal authors of the Document (all of its principal authors, if it has fewer
than five), unless they release you from this requirement.
C. State on the Title page the name of the publisher of the Modified Version, as the
publisher.
D. Preserve all the copyright notices of the Document.
E. Add an appropriate copyright notice for your modifications adjacent to the other
copyright notices.
F. Include, immediately after the copyright notices, a license notice giving the public
permission to use the Modified Version under the terms of this License, in the form
shown in the Addendum below.
G. Preserve in that license notice the full lists of Invariant Sections and required Cover
Texts given in the Documents license notice.
H. Include an unaltered copy of this License.
I. Preserve the section Entitled History, Preserve its Title, and add to it an item
stating at least the title, year, new authors, and publisher of the Modified Version as
given on the Title Page. If there is no section Entitled History in the Document,
create one stating the title, year, authors, and publisher of the Document as given
on its Title Page, then add an item describing the Modified Version as stated in
the previous sentence.
J. Preserve the network location, if any, given in the Document for public access to
a Transparent copy of the Document, and likewise the network locations given in
the Document for previous versions it was based on. These may be placed in the
History section. You may omit a network location for a work that was published
at least four years before the Document itself, or if the original publisher of the
version it refers to gives permission.
313
K. For any section Entitled Acknowledgements or Dedications, Preserve the Title
of the section, and preserve in the section all the substance and tone of each of the
contributor acknowledgements and/or dedications given therein.
L. Preserve all the Invariant Sections of the Document, unaltered in their text and
in their titles. Section numbers or the equivalent are not considered part of the
section titles.
M. Delete any section Entitled Endorsements. Such a section may not be included
in the Modified Version.
N. Do not retitle any existing section to be Entitled Endorsements or to conflict in
title with any Invariant Section.
O. Preserve any Warranty Disclaimers.
If the Modified Version includes new front-matter sections or appendices that qualify
as Secondary Sections and contain no material copied from the Document, you may at
your option designate some or all of these sections as invariant. To do this, add their
titles to the list of Invariant Sections in the Modified Versions license notice. These
titles must be distinct from any other section titles.
You may add a section Entitled Endorsements, provided it contains nothing but
endorsements of your Modified Version by various partiesfor example, statements of
peer review or that the text has been approved by an organization as the authoritative
definition of a standard.
You may add a passage of up to five words as a Front-Cover Text, and a passage of up
to 25 words as a Back-Cover Text, to the end of the list of Cover Texts in the Modified
Version. Only one passage of Front-Cover Text and one of Back-Cover Text may be
added by (or through arrangements made by) any one entity. If the Document already
includes a cover text for the same cover, previously added by you or by arrangement
made by the same entity you are acting on behalf of, you may not add another; but you
may replace the old one, on explicit permission from the previous publisher that added
the old one.
The author(s) and publisher(s) of the Document do not by this License give permission to use their names for publicity for or to assert or imply endorsement of any
Modified Version.
5. COMBINING DOCUMENTS
You may combine the Document with other documents released under this License,
under the terms defined in section 4 above for modified versions, provided that you
include in the combination all of the Invariant Sections of all of the original documents,
unmodified, and list them all as Invariant Sections of your combined work in its license
notice, and that you preserve all their Warranty Disclaimers.
The combined work need only contain one copy of this License, and multiple identical
Invariant Sections may be replaced with a single copy. If there are multiple Invariant
Sections with the same name but different contents, make the title of each such section
unique by adding at the end of it, in parentheses, the name of the original author or
publisher of that section if known, or else a unique number. Make the same adjustment
314
to the section titles in the list of Invariant Sections in the license notice of the combined
work.
In the combination, you must combine any sections Entitled History in the various
original documents, forming one section Entitled History; likewise combine any sections Entitled Acknowledgements, and any sections Entitled Dedications. You must
delete all sections Entitled Endorsements.
6. COLLECTIONS OF DOCUMENTS
You may make a collection consisting of the Document and other documents released
under this License, and replace the individual copies of this License in the various documents with a single copy that is included in the collection, provided that you follow the
rules of this License for verbatim copying of each of the documents in all other respects.
You may extract a single document from such a collection, and distribute it individually under this License, provided you insert a copy of this License into the extracted
document, and follow this License in all other respects regarding verbatim copying of
that document.
8. TRANSLATION
Translation is considered a kind of modification, so you may distribute translations
of the Document under the terms of section 4. Replacing Invariant Sections with translations requires special permission from their copyright holders, but you may include
translations of some or all Invariant Sections in addition to the original versions of these
Invariant Sections. You may include a translation of this License, and all the license
notices in the Document, and any Warranty Disclaimers, provided that you also include
the original English version of this License and the original versions of those notices and
disclaimers. In case of a disagreement between the translation and the original version
of this License or a notice or disclaimer, the original version will prevail.
If a section in the Document is Entitled Acknowledgements, Dedications, or
History, the requirement (section 4) to Preserve its Title (section 1) will typically
require changing the actual title.
315
9. TERMINATION
You may not copy, modify, sublicense, or distribute the Document except as expressly
provided under this License. Any attempt otherwise to copy, modify, sublicense, or
distribute it is void, and will automatically terminate your rights under this License.
However, if you cease all violation of this License, then your license from a particular
copyright holder is reinstated (a) provisionally, unless and until the copyright holder
explicitly and finally terminates your license, and (b) permanently, if the copyright holder
fails to notify you of the violation by some reasonable means prior to 60 days after the
cessation.
Moreover, your license from a particular copyright holder is reinstated permanently
if the copyright holder notifies you of the violation by some reasonable means, this is the
first time you have received notice of violation of this License (for any work) from that
copyright holder, and you cure the violation prior to 30 days after your receipt of the
notice.
Termination of your rights under this section does not terminate the licenses of parties
who have received copies or rights from you under this License. If your rights have been
terminated and not permanently reinstated, receipt of a copy of some or all of the same
material does not give you any rights to use it.
11. RELICENSING
Massive Multiauthor Collaboration Site (or MMC Site) means any World Wide
Web server that publishes copyrightable works and also provides prominent facilities for
anybody to edit those works. A public wiki that anybody can edit is an example of
such a server. A Massive Multiauthor Collaboration (or MMC) contained in the
site means any set of copyrightable works thus published on the MMC site.
CC-BY-SA means the Creative Commons Attribution-Share Alike 3.0 license published by Creative Commons Corporation, a not-for-profit corporation with a principal
place of business in San Francisco, California, as well as future copyleft versions of that
license published by that same organization.
316
Bibliography
[1] G. M. Adelson-Velski and E. M. Landis. An algorithm for the organization of information. Soviet Math. Doklady, 3:12591263, 1962.
[2] A. V. Aho, J. E. Hopcroft, and J. D. Ullman. The Design and Analysis of Computer
Algorithms. Addison-Wesley Publishing Company, 1974.
[3] W. Aiello, F. Chung, and L. Lu. A random graph model for massive graphs. In TOC,
pages 171180. ACM, 2000.
[4] W. Aiello, F. Chung, and L. Lu. Handbook of Massive Data Sets, chapter Random
evolution of massive graphs, pages 97122. Kluwer Academic Publishers, 2002.
[5] R. Albert and A.-L. Barab
asi. Statistical mechanics of complex networks. Rev. Mod.
Phys., 74:4797, 2002.
[6] R. Albert, H. Jeong, and A.-L. Barabasi. Diameter of the World-Wide Web. Nature,
401:130131, 1999.
[7] L. A. N. Amaral, A. Scala, M. Barthelemy, and H. E. Stanley. Classes of small-world
networks. PNAS, 97:1114911152, 2000.
[8] J. V. Anna Bernasconi, Bruno Codenotti. A characterization of bent functions in terms
of strongly regular graphs. IEEE Trans. Comp., 50:984985, 2001.
[9] V. Arlazarov, E. Dinic, M. Kronrod, and I. Faradzev. On economical construction of the
transitive closure of a directed graph. Soviet Math. Doklady, 11:12091210, 1970.
[10] A. S. Asratian, T. M. J. Denley, and R. Haggkvist. Bipartite Graphs and their Applications. Cambridge University Press, 1998.
[11] L. Backstrom, D. P. Huttenlocher, J. M. Kleinberg, and X. Lan. Group formation in large
social networks: membership, growth, and evolution. In T. Eliassi-Rad, L. H. Ungar,
M. Craven, and D. Gunopulos, editors, KDD, pages 4454. ACM, 2006.
[12] M. Baker and X. Faber. Quantum Graphs and Their Applications, chapter Metrized
Graphs, Laplacian Operators, and Electrical Networks, pages 1533. AMS, 2006.
[13] W. W. R. Ball and H. S. M. Coxeter. Mathematical Recreations and Essays. Dover
Publications, 13th edition, 1987.
[14] A.-L. Barab
asi. Linked: The New Science of Networks. Basic Books, 2002.
[15] A.-L. Barab
asi and R. Albert. Emergence of scaling in random networks. Science,
286:509512, 1999.
[16] A.-L. Barab
asi, R. Albert, and H. Jeong. Mean-field theory for scale-free random networks. Phys. A, 272:173187, 1999.
[17] A.-L. Barab
asi, R. Albert, and H. Jeong. Scale-free characteristics of random networks:
The topology of the world wide web. Phys. A, 281:6977, 2000.
[18] A. Barrat and M. Weigt. On the properties of small-world network models. Eur. Phys.
J. B, 13:547560, 2000.
[19] V. Batagelj and U. Brandes. Efficient generation of large random networks. Phys. Rev.
E, 71:036113, 2005.
[20] R. A. Beezer. A First Course in Linear Algebra. Robert A. Beezer, University of Puget
Sound, USA, 2009. http://linear.ups.edu.
[21] J. Bell and B. Stevens. A survey of known results and research areas for n-queens. Disc.
Math., 309:131, 2009.
317
318
Bibliography
Bibliography
319
[50] C. Castellano, S. Fortunato, and V. Loreto. Statistical physics of social dynamics. Rev.
Mod. Phys., 81:591646, 2009.
[51] R. D. Castro and J. W. Grossman. Famous trails to Paul Erdos. Math. Intel., 21:5153,
1999.
[52] J.-L. Chabert, editor. A History of Algorithms: From the Pebble to the Microchip.
Springer, 1999.
[53] B. Chazelle. A minimum spanning tree algorithm with inverse-Ackermann type complexity. J. ACM, 47:10281047, 2000.
[54] B. Chazelle. The soft heap: An approximate priority queue with optimal error rate. J.
ACM, 47:10121027, 2000.
[55] Q. Chen, H. Chang, R. Govindan, S. Jamin, S. Shenker, and W. Willinger. The origin of
power-laws in internet topologies revisited. In INFOCOM, pages 608617. IEEE, 2002.
[56] A. G. Chetwynd and A. J. W. Hilton. Star multigraphs with three vertices of maximum
degree. Math. Proc. Camb. Phil. Soc., 100:303317, 1986.
320
Bibliography
[76] P. Erd
os and A. Renyi. On random graphs. Pub. Math., 6:290297, 1959.
[77] P. Erd
os and A. Renyi. On the evolution of random graphs. Magyar Tud. Akad. Mat.
Kutat
o Int. K
ozl., 5:1761, 1960.
[78] P. L. Erd
os, I. Mikl
os, and Z. Toroczkai. A simple Havel-Hakimi type algorithm to realize
graphical degree sequences of directed graphs. Elec. J. Comb., 17:R66, 2010.
[79] M. Faloutsos, P. Faloutsos, and C. Faloutsos. On power-law relationships of the Internet
topology. Comp. Comm. Rev., 29:251262, 1999.
[80] R. Ferrer i Cancho and R. V. Sole. The small world of human language. Proc. R. Soc.
Lond. B, 268:22612265, 2001.
[81] K. Florek, J. Lukaszewicz, J. Perkal, H. Steinhaus, and S. Zubrzycki. Sur la liaison et la
division des points dun ensemble fini. Colloquium Mathematicum, 2:282285, 1951.
[82] R. W. Floyd. Algorithm 97: Shortest path. Comm. ACM, 5:345, 1962.
[83] L. R. Ford Jr. Network flow theory. Technical Report P-923, The Rand Corporation,
USA, 1956.
[84] L. R. Foulds. Graph Theory Applications. Springer, 1992.
[85] G. N. Frederickson. An optimal algorithm for selection in a min-heap. Inf. Comp.,
104:197214, 1993.
[86] M. L. Fredman and R. E. Tarjan. Fibonacci heaps and their uses in improved network
optimization algorithms. J. ACM, 34:596615, 1987.
[87] G. Gallo and S. Pallottino. Netflow at Pisa, chapter Shortest path methods: A unifying
approach, pages 3864. Springer, 1986.
[88] D. Garlaschelli. The weighted random graph model. New J. Phys., 11:073005, 2009.
[89] E. N. Gilbert. Random graphs. Ann. Math. Stat., 30:11411144, 1959.
[90] C. Godsil and R. Beezer. Explorations in algebraic graph theory with Sage, 2012. in
preparation.
[91] C. Godsil and G. Royle. Algebraic Graph Theory. Springer, 2004.
[92] R. L. Graham and P. Hell. On the history of the minimum spanning tree problem. Ann.
Hist. Comp., 7:4357, 1985.
[93] R. L. Graham and H. O. Pollak. On the addressing problem for loop switching. Bell
Sys. Tech. J., 50:24952519, 1971.
[94] I. Gribkovskaia, . Halskau Sr., and G. Laporte. The bridges of Konigsberga historical
perspective. Networks, 49:199203, 2007.
[95] J. Gross and J. Yellen. Graph Theory and Its Applications. CRC Press, 1999.
[96] J. W. Grossman and P. D. F. Ion. On a portion of the well-known collaboration graph.
Congressus Numerantium, 108:129131, 1995.
[97] J. Guare. Six Degrees of Separation: A Play. Vintage, 1990.
[98] I. Gutman, Y.-N. Yeh, S.-L. Lee, and Y.-L. Luo. Some recent results in the theory of
the Wiener number. Indian J. Chem., 32A:651661, 1993.
[99] S. L. Hakimi. On realizability of a set of integers as degrees of the vertices of a linear
graph I. SIAM J. App. Math., 10:496506, 1962.
[100] S. L. Hakimi. On realizability of a set of integers as degrees of the vertices of a linear
graph II: Uniqueness. SIAM J. App. Math., 11:135147, 1963.
[101] S. L. Hakimi and J. Bredeson. Graph theoretic error-correcting codes. IEEE Trans. Inf.
Theory, 14:584591, 1968.
Bibliography
321
[107] J. E. Hopcroft and R. E. Tarjan. Efficient planarity testing. J. ACM, 21:549568, 1974.
[108] B. Hopkins and R. Wilson. The truth about Konigsberg. College Math. J., 35:198207,
2004.
[109] W. G. Horner. A new method of solving numerical equations of all orders, by continuous
approximation. Phil. Trans. R. Soc. Lond., 109:308335, 1819.
[110] S. Howard. C algorithms (version 1.2.0), accessed 20th December 2010. http://
c-algorithms.sourceforge.net.
[111] D. A. Huffman. A method for the construction of minimum-redundancy codes. In
Proceedings of the I.R.E, volume 40, pages 10981102, 1952.
[112] M. Huxham, S. Beaney, and D. Raffaelli. Do parasites reduce the chances of triangulation
in a real food web? Oikos, 76:284300, 1996.
[113] F. Jaeger, D. L. Vertigan, and D. J. A. Welsh. On the computational complexity of the
Jones and Tutte polynomials. Mathematical Proceedings of the Cambridge Philosophical
Society, 108:3553, 1990.
[114] V. Jarnk. O jistem problemu minimalnm (Z dopisu panu O. Bor
uvkovi) (Czech). Pr
ace
Moravske Prrodovedecke Spolecnosti Brno, 6:5763, 1930.
[115] T. R. Jensen and B. Toft. Graph Coloring Problems. John Wiley & Sons, 1995.
[116] H. Jeong, S. Mason, A.-L. Barabasi, and Z. N. Oltvai. Lethality and centrality in protein
networks. Nature, 411:4142, 2001.
[117] H. Jeong, B. Tombor, R. Albert, Z. N. Oltvai, and A.-L. Barabasi. The large-scale
organization of metabolic networks. Nature, 407:651654, 2000.
[118] D. B. Johnson. Efficient algorithms for shortest paths in sparse networks. J. ACM,
24:113, 1977.
[119] J. H. Jones and M. S. Handcock. An assessment of preferential attachment as a mechanism for human sexual network formation. Proc. R. Soc. Lond. B, 270:11231128, 2003.
[120] C. Jordan. Sur les assemblages de lignes. Journal f
ur die reine und angewandte Mathematik, 70:185190, 1869.
[121] D. Jungnickel. Graphs, Networks and Algorithms. Springer, 3rd edition, 2008.
[122] D. Jungnickel and S. A. Vanstone. Graphical codes revisited. IEEE Trans. Inf. Theory,
43:136146, 1997.
[123] D. Kalman. Marriages made in the heavens: A practical application of existence. Math.
Mag., 72:94103, 1999.
[124] H. Kaplan and U. Zwick. A simpler implementation and analysis of Chazelles soft heaps.
In C. Mathieu, editor, SODA, pages 477485. SIAM, 2009.
[125] A. Kershenbaum and R. Van Slyke. Computing minimum spanning trees efficiently. In
Proceedings of the ACM Annual Conference 25, pages 518527. ACM, 1972.
[126] S. C. Kleene. Automata Studies, chapter Representation of Events in Nerve Nets and
Finite Automata, pages 341. Princeton University Press, 1956.
[127] J. Kleinberg. The small-world phenomenon: An algorithmic perspective. In STOC, pages
163170. ACM, 2000.
[128] D. E. Knuth. The Stanford GraphBase: A Platform for Combinatorial Computing.
Addison-Wesley, 1993.
[129] D. E. Knuth. Seminumerical Algorithms, volume 2 of The Art of Computer Programming.
Addison-Wesley, 3rd edition, 1998.
[130] D. E. Knuth. Sorting and Searching, volume 3 of The Art of Computer Programming.
Addison-Wesley, 2nd edition, 1998.
[131] V. F. Kolchin. Random Graphs. Cambridge University Press, 1999.
[132] L. G. Kraft. A device for quantizing, grouping, and coding amplitude-modulated pulses.
Masters thesis, Massachusetts Institute of Technology, USA, 1949.
[133] J. B. Kruskal. On the shortest spanning subtree of a graph and the traveling salesman
problem. Proc. AMS, 7:4850, 1956.
322
Bibliography
[134] K. Kuratowski. Sur le probl`eme des courbes gauches en topologie. Fundamenta Mathematicae, 15:271283, 1930.
[135] J. C. Lagarias. The 3x + 1 problem and its generalizations. Am. Math. Mont., 92:323,
1985.
[136] J. C. Lagarias. The 3x+1 problem: An annotated bibliography (19631999), 03rd August
2009. arXiv:math/0309224, http://arxiv.org/abs/math.NT/0309224.
[137] J. C. Lagarias. The 3x+1 problem: An annotated bibliography, II (2000-2009), 27th August 2009. arXiv:math/0608208, http://arxiv.org/abs/math.NT/0608208.
[138] V. Latora and M. Marchiori. Economic small-world behavior in weighted networks. Eur.
Phys. J. B, 32:249263, 2003.
[139] H. T. Lau. A Java Library of Graph Algorithms and Optimization. Chapman & Hall/CRC, 2007.
[140] C. Y. Lee. An algorithm for path connections and its applications. IRE Transactions on
Electronic Computers, EC-10:346365, 1961.
[141] D. H. Lehmer. Mathematical methods in large-scale computing units. In Proceedings of
the Second Symposium on Large-Scale Digital Calculating Machinery, 1949.
[142] J. Leskovec, J. M. Kleinberg, and C. Faloutsos. Graphs over time: Densification laws,
shrinking diameters and possible explanations. In R. Grossman, R. J. Bayardo, and K. P.
Bennett, editors, KDD, pages 177187. ACM, 2005.
[143] J. Leskovec, K. J. Lang, A. Dasgupta, and M. W. Mahoney. Statistical properties of
community structure in large social and information networks. In J. Huai, R. Chen, H.W. Hon, Y. Liu, W.-Y. Ma, A. Tomkins, and X. Zhang, editors, WWW, pages 695704.
ACM, 2008.
[144] F. Liljeros, C. R. Edling, L. A. N. Amaral, H. E. Stanley, and Y.
Aberg. The web of
human sexual contacts. Nature, 411:907908, 2001.
[145] M. Lobbing and I. Wegener. The number of knights tours equals 33,439,123,484,294
counting with binary decision diagrams. Elec. J. Comb., 3:R5, 1996.
[146] H. Loberman and A. Weinberger. Formal procedures for connecting terminals with a
minimum total wire length. J. ACM, 4:428437, 1957.
[147] M. E. Lucas. Recreations Mathematiques. 4 volumes, Gauthier-Villars, Paris, 188294.
[148] M. Mares. The saga of minimum spanning trees. Comp. Sci. Rev., 2:165221, 2008.
[149] N. D. Martinez. Artifacts or attributes? Effects of resolution on the Little Rock Lake
food web. Ecological Monographs, 61:367392, 1991.
[150] B. McKay. Description of graph6 and sparse6 encodings, accessed 05th April 2010.
http://cs.anu.edu.au/bdm/data/formats.txt.
[151] B. D. McKay. Knights tours of an 8 8 chessboard. Technical Report TR-CS-97-03,
Department of Computer Science, Australian National University, Australia, February
1997.
[152] B. McMillan. Two inequalities implied by unique decipherability. IRE Transactions on
Information Theory, 2:115116, 1956.
[153] A. J. Menezes, P. C. van Oorschot, and S. A. Vanstone. Handbook of Applied Cryptography. CRC Press, 1996.
[154] C. Merino. Matroids, the tuttle polynomial, and the chip firing game. 1999. http://
calli.matem.unam.mx/merino/ewpublications.html#2, http://www.dmtcs.org/dmtcs-ojs/
index.php/proceedings/article/viewArticle/dmAA0118, (The first link is for the thesis (in
ps format); the second is to a paper.).
[155] R. C. Merkle. A digital signature based on a conventional encryption function. In
C. Pomerance, editor, CRYPTO, pages 369378. Springer, 1988.
[156] S. Milgram. The small world problem. Psychology Today, 1:6067, 1967.
[157] B. Mohar, D. Babic, and N. Trinajstic. A novel definition of the Wiener index for trees.
J. Chem. Inf. Comp. Sci., 33:153154, 1993.
Bibliography
323
[158] E. F. Moore. The shortest path through a maze. In Proceedings of the International
Symposium on the Theory of Switching, pages 285292, 1959.
[159] S. Myles, A. R. Boyko, C. L. Owens, P. J. Brown, F. Grassi, M. K. Aradhya, B. Prins,
A. Reynolds, J.-M. Chia, D. Ware, C. D. Bustamante, and E. S. Buckler. Genetic
structure and domestication history of the grape. PNAS, 108:35303535, 2011.
[160] M. Newman, A.-L. Barab
asi, and D. J. Watts, editors. The Structure and Dynamics of
Networks. Princeton University Press, 2006.
[161] M. E. J. Newman. Scientific collaboration networks: I. Network construction and fundamental results. Phys. Rev. E, 64:016131, 2001.
[162] M. E. J. Newman. The structure of scientific collaboration networks. PNAS, 98:404409,
2001.
[163] M. E. J. Newman. Mixing patterns in networks. Phys. Rev. E, 67:026126, 2003.
[164] M. E. J. Newman. The structure and function of complex networks. SIAM Rev., 45:167
256, 2003.
[165] M. E. J. Newman, S. H. Strogatz, and D. J. Watts. Random graphs with arbitrary degree
distribution and their applications. Phys. Rev. E, 64:026118, 2001.
[166] E. Nuutila. Efficient Transitive Closure Computation in Large Digraphs. Finnish
Academy of Technology, 1995. http://www.cs.hut.fi/enu/thesis.html.
[167] J. Oxley. What is a matroid? Cubo Matem
atica Educacional, 5:179218, 2003.
[168] D. Perkinson, J. Perlman, and J. Wilmes. Primer on the algebraic geometry of sandpiles.
2009. http://people.reed.edu/davidp/412/handouts/primer091810.pdf.
[169] J. G. Perlman. Sandpiles: a bridge between graphs and toric ideals. 2009. Thesis, Reed
College, http://people.reed.edu/davidp/homepage/seniors/perlman.pdf.
[170] J. Petersen. Sur le theor`eme de tait. LIntermediaire des Mathematiciens, 5:225227,
1898.
[171] G. Polya. How To Solve It: A New Aspect of Mathematical Method. Princeton University
Press, 2nd edition, 1957.
[172] R. C. Prim. Shortest connection networks and some generalizations. Bell Sys. Tech. J.,
36:13891401, 1957.
[173] R. Rasmussen. Algorithmic Approaches for Playing and Solving Shannon Games. PhD
thesis, Queensland University of Technology, Australia, 2007. http://eprints.qut.edu.au/
18616/.
[174] S. Redner. How popular is your paper? An empirical study of the citation distribution.
Eur. Phys. J. B, 4:131134, 1998.
[175] K. H. Rosen. Elementary Number Theory and Its Applications. Addison Wesley Longman, 4th edition, 2000.
[176] B. Roy. Transitivite et connexite. Comptes Rendus des Seances de lAcademie des
Sciences, 249:216218, 1959.
[177] V. Runde. A Taste of Topology. Springer, 2005.
[178] R. Sedgewick. Algorithms in C. Addison-Wesley Publishing Company, 1990.
[179] P. O. Seglen. The skewness of science. J. Am. Soc. Inf. Sci., 43:628638, 1992.
[180] P. Sen, S. Dasgupta, A. Chatterjee, P. A. Sreeram, G. Mukherjee, and S. S. Manna.
Small-world properties of the Indian railway network. Phys. Rev. E, 67:036106, 2003.
[181] A. Shimbel. Structure in communications nets. In Proceedings of the Symposium on
Information Networks, pages 199203, 1955.
[182] S. Shirali and H. L. Vasudeva. Metric Spaces. Springer, 2006.
[183] V. Shoup. A Computational Introduction to Number Theory and Algebra. Cambridge
University Press, 2nd edition, 2008. http://www.shoup.net/ntb.
[184] G. Sierksma and H. Hoogeveen. Seven criteria for integer sequences being graphic. J.
Graph Theory, 15:223231, 1991.
[185] H. A. Simon. On a class of skew distribution functions. Biometrika, 42:425440, 1955.
324
Bibliography
[186] P. Stanica. Graph eigenvalues and Walsh spectrum of boolean functions. Integers, 7,
2007.
[187] D. R. Stinson. Cryptography: Theory and Practice. Chapman & Hall/CRC, 2nd edition,
2002.
[188] M. Szydlo. Merkle tree traversal in log space and time. In C. Cachin and J. Camenisch,
editors, EUROCRYPT, pages 541554. Springer, 2004.
[189] T. Takaoka. O(1) time algorithms for combinatorial generation by tree traversal. Comp.
J., 42:400408, 1999.
[190] T. Takaoka. Theory of 2-3 heaps. In T. Asano, H. Imai, D. T. Lee, S.-I. Nakano, and
T. Tokuyama, editors, COCOON. Springer, 1999.
[191] R. E. Tarjan. Depth-first search and linear graph algorithms. SIAM J. Comp., 1:146160,
1972.
[192] G. Tarry. Le probl`eme des labyrinthes. Nouvelles Annales de Mathematique, 14:187190,
1895.
[193] W. Trappe and L. C. Washington. Introduction to Cryptography with Coding Theory.
Pearson Education, 2nd edition, 2006.
[194] J. Travers and S. Milgram. An experimental study of the small world problem. Sociometry, 32:425443, 1969.
[195] D. Trietsch. Eulers problem of polygon division and full steiner topologiesa duality. Technical Report 625, Center for Mathematical Studies in Economics and Management Science, Northwestern University, USA, October 1984. http://econpapers.repec.
org/paper/nwucmsems/625.htm.
[196] A. Tripathi and S. Vijay. A note on a theorem of Erdos & Gallai. Disc. Math., 265:417
420, 2003.
[197] S. Valverde, R. F. Cancho, and R. V. Sole. Scale-free networks from optimal design.
Euro. Lett., 60:512517, 2002.
[198] A. Vazquez, R. Pastor-Satorras, and A. Vespignani. Large-scale topological and dynamical properties of the Internet. Phys. Rev. E, 65:066130, 2002.
[199] J. S. Vitter. Random sampling with a reservoir. ACM Tran. Math. Soft., 11:3757, 1985.
[200] J. Vuillemin. A data structure for manipulating priority queues. Comm. ACM, 21:309
315, 1978.
[201] H. Walther. Ten Applications of Graph Theory. Kluwer Academic Publishers, 1984.
[202] S. Warshall. A theorem on boolean matrices. J. ACM, 9:1112, 1962.
[203] D. J. Watts. Networks, dynamics, and the small-world phenomenon. Am. J. Soc.,
105:493527, 1999.
[204] D. J. Watts. Small Worlds. Princeton University Press, 1999.
[205] D. J. Watts. Six Degrees: The Science of a Connected Age. W. W. Norton & Company,
2004.
[206] D. J. Watts and S. H. Strogatz. Collective dynamics of small-world networks. Nature,
393:440442, 1998.
[207] J. G. White, E. Southgate, J. N. Thompson, and S. Brenner. The structure of the
nervous system of the nematode Caenorhabditis elegans. Phil. Trans. R. Soc. Lond. B,
314:1340, 1986.
[208] H. Whitney. Congruent graphs and the connectivity of graphs. Am. J. Math., 54:150168,
1932.
[209] H. Wiener. Structural determination of paraffin boiling points. J. Am. Chem. Soc.,
69:1720, 1947.
[210] J. W. J. Williams. Algorithm 232: Heapsort. Comm. ACM, 7:347348, 1964.
[211] T. Yamada, S. Kataoka, and K. Watanabe. Listing all the minimum spanning trees in
an undirected graph. Int. J. Comp. Math., 87:31753185, 2010.
[212] T. Yamada and H. Kinoshita. Finding all the negative cycles in a directed graph. Disc.
App. Math., 118:279291, 2002.
Bibliography
325
[213] J. Yang and Y. Chen. Fast computing betweenness centrality with virtual nodes on large
sparse networks. PLoS ONE, 6:e22557, 2011.
[214] V. Yegnanarayanan. Graph theory to pure mathematics: Some illustrative examples.
Resonance, 10:5059, 2005.
[215] Y.-N. Yeh and I. Gutman. On the sum of all distances in composite graphs. Disc. Math.,
135:359365, 1994.
[216] W. W. Zachary. An information flow model for conflict and fission in small groups. J.
Anth. Res., 33:452473, 1977.
Index
A(G), 21
C 0 (G, F ), 249
C 1 (G, F ), 249
Cn , 16
En , 54
Gc , 39
Kn , 15
Km,n , 19
Ln , 40
Pn , 16, 40
Qn , 40
Wn , 35
, 35
(G), 10
adj, 4
L, 27, 47
L(G), 8
Ni , 284
v (G), 239
=, 28
deg, 5, 8
deg+ , 7
deg , 7
(G), 10
depth(v), 111
diam(G), 209
dir, 97
, 207
, 280
height(T ), 111
iadj, 5
id, 5
(G), 212
e (G), 213
v (G), 211
(G), 213
lg, 167
oadj, 5
od, 5
, 14
(G), 239
G, 39
per(G), 209
rad(G), 209
, 40
td, 96
, 144, 147
(n), 120
f -augmenting, 262
f -saturated, 262
f -unsaturated, 262
f -zero, 262
k-coloring, 238
k-connected, 212
k-edge-connected, 213
n-queens problem, 106
n-space, 141
graph6, 59, 61, 63, 64
sparse6, 61, 63
Lukaszewicz, J., 128
edge expander family, 228
active vertex, 255
acyclic, 110, 122, 155
Adelson-Velski, G. M., 189
adjacency matrix, 21
reduced, 23
signed edge, 223
algorithm
greedy, 81, 82, 122, 125
optimization, 116
random, 55, 56, 109, 155157, 272, 273,
277, 279, 280, 282, 289, 291, 292,
295
recursive, 160
alphabet, 45, 46, 138, 139, 144
binary, 144, 145
English, 138, 157
weighted, 139, 146
Altito, Noelie, 90
arcs, 3
326
Index
327
Binet
formula, 205
Jacques Philippe Marie, 205
binomial
coefficient, 175, 201
distribution, 272
random graph, 274
tree, 175
binomial heap, 162, 174, 175, 177, 203
backtrack, 71
maximum, 203
algorithm, 107
minimum, 203
Baker, Matthew, 207
order property, 177, 178, 203
balanced bracket problem, 103, 104
properties, 177
Bangkok, 99, 100
root-degree property, 177, 178
Barabasi-Albert model, 286
biology, 206
Batagelj, Vladimir, 275, 294
bipartite graph, 16, 20, 55, 56, 294, 295
Batagelj-Brandes algorithm, 275, 277
complete, 19, 20
Baudot, E., 140
Birkhoff-Lewis Conjecture, 247
Beijing, 99, 100
bit, 63, 138, 144
Bellman, Richard E., 82
least significant, 63
Bellman-Ford algorithm, 78, 8285, 90, 91,
most significant, 63
98
parity, 63
Benjamin, Arthur T., 161
bit vector, 61, 63, 64
bent, 224
length, 63
Berlin, 99, 100
bond,
37, 116, 250
Bernoulli family, 112
Bor
uvka
BFS, 6569, 71, 75
algorithm, 122, 128, 130, 154, 155, 160
big-endian, 63, 64
Otakar, 122, 128
Biggs, Norman, 146
bowtie
graph, 13
binary heap, 162, 164, 189
braille, 139
heap-structure property, 189
branch cut, 113, 115
maximum, 201
Brandes, Ulrik, 275, 294
minimum, 201
Brasilia, 99, 100
order property, 165, 203
Brazil, 99, 100
sift-down, 170
breadth-first search, 6569, 74, 75, 7779,
sift-up, 168
94, 97, 103, 115, 149, 150
structure property, 165
tree, 65, 68
binary search, 94, 96, 184
binary search tree, 162, 182, 184, 189, 191, bridge, 37, 111, 117, 128, 133, 213
bridgeless, 213
192
Briggs
left subtree property, 182
algorithm, 293
property, 182, 192, 203
Keith M., 293
recursion property, 182
Brooks
inequality, 241
right subtree property, 182
BST, 182
binary tree, 113, 115, 135138, 157, 162
bubble sort, 101, 102
complete, 136, 137, 189
Buenos Aires, 99, 100
nearly complete, 165, 189
butterfly graph, 13
random, 139, 157
Argentina, 99, 100
ASCII, 61, 63, 138, 140
augmenting path, 262
Australia, 99, 100
Australian National University, 61
automata theory, 44, 90
AVL tree, 189, 205
height-balance property, 189
328
Caenorhabditis elegans, 269
Canada, 99, 100
canonical label, 30
Cantor-Schroder-Bernstein theorem, 56
capacity, 261
cut, 263
card, 71
cardinality, 8
Carroll, Lewis, 2
Cartesian product, 40, 41
Catalan
number, 54, 137
recursion, 137
Cayley graph of Boolean function, 223
characteristic polynomial, 222
Chazelle, Bernard, 203
check matrix, 23
chemistry, 87, 206
chess, 69, 106, 139
chessboard, 69
knight, 69
knight piece, 69
knights tour, 6971, 106
queen, 106, 107
child
left, 135, 136, 149, 154
right, 135, 149, 154
China, 99, 100
Chinese ring puzzle, 140, 141
chip firing game, 255
chip-firing game, 255
chip-firing games, 254
CHKNS model, 293
Choquet, G., 128
chromatic
index, 241
number, 239
polynomial, 246
root, 247
chromatically equivalent, 246
Chu Shi-Chieh, 201
Chvatal graph, 155, 156
circuit, 13
board, 65
electronic, 121
circuit matrix, 132
circulation, 250
circulation space, 250, 251
Index
classification tree, 111, 112, 116
claw graph, 212
clique, 239
maximal, 239
number, 239
closed form, 137
closeness centrality, 220
cocycle code, 251
cocycle space, 251
cocyle, 250
code, 138, 144
r-ary, 160
binary, 138, 144
block, 139
economy, 139
error-correcting, 23, 139
linear, 141
optimal, 146
prefix, 139
prefix-free, 139, 140, 145, 157
radix, 160
reliability, 139
security, 139
tree representation, 144, 145
uniquely decodable, 145
variable-length, 139
codeword, 138, 144
length, 146
coding function, 139
Cohen, Danny, 63
Collatz
conjecture, 158
graph, 158, 159
length, 158
sequence, 158
tree, 158, 159
color code, 139
color-class, 239
coloring
edge, 43
vertex, 43, 44
combinatorial generation, 201
combinatorial graphs, 2
combinatorics, 141
communications network, 215
complement, 39
complete graph, 15, 16, 156, 157, 272, 277,
279, 282, 283, 291, 292
Index
329
330
deletion subgraph, 37
directed, 4
endpoint, 122
expansion, 228
head, 6
incident, 3
multigraph, 6
multiple, 3
tagging game, 116
tail, 6
weight, 6
edge chromatic number, 241
edge coloring, 241
edge cut subgraph, 250
edge cutset, 250
edge-cut, 212
Edmonds, Jack, 116
eigenvalue, 160
electrical network, 254
element
random, 138
Elkies, Noam D., 71
encode, 138
endianness, 63
England, 99, 100, 107
entropy
encoding, 139
function, 139
Erdos, Paul, 31
error rate, 139
Euclidean algorithm, 93
Euler
Leonhard, 1, 10, 53, 54, 120
phi function, 120, 158
phi sequence, 120, 121
polygon division problem, 53, 54
subgraph, 13
Euler subgraph, 250
Eulerian trail, 1
expander graph, 228
Faber, Xander, 207
family tree, 14, 15, 111, 112
fault-tolerant, 215
Fermats little theorem, 56
Fibonacci
number, 189
sequence, 205
Index
tree, 189, 191193, 204
FIFO, 66, 71
filesystem, 111
hierarchy, 111
finite automaton, 44, 45, 56
deterministic, 45, 46
nondeterministic, 46, 47
first in, first out, 66
flag semaphore, 139
Florek, K., 128
Florentine families, 43
flow, 250, 261
value, 262
flow chart, 58
flow space, 250
Floyd, Robert, 85
Floyd-Roy-Warshall algorithm, 83, 8587,
89, 90, 98, 210, 211
football, 139
forbidden minor, 42
Ford, Lester Randolph, Jr., 82
forest, 110, 111
Foulds, L. R., 43
Franklin graph, 28
Frederickson, Greg N., 203
FreeBSD, 58
frequency distribution, 281
friendship graph, 221
FRW, 83, 85
function plot, 2
fundamental cycles, 252
Gallai, Tibor, 31
Garlaschelli, Diego, 293
genetic code, 139
Germany, 99, 100
Gilbert, E. N., 278
girth, 13
Goldbach, Christian, 53
Goldberg, R., 83
golden ratio, 161, 205
Graham, Ronald L., 211
graph, 3
applications, 42
connected, 14, 75, 76
cube, 135
dense, 60, 85
Desargues, 135, 136
Index
directed, 5
disconnected, 14
Dyck, 239
Foster, 18
Franklin, 17
Frucht, 241
Gray, 20
Heawood, 243
icosahedral, 244
intersection, 34
join, 35
line, 8
nonisomorphic, 54
Pappas, 245
simple, 8
sparse, 21, 60, 85, 90, 91
traversal, 65
trivial, 120, 128
undirected, 3
union, 34
unweighted, 3
weighted, 6, 76, 122, 125
graph isomorphism, 28, 30
graph minor, 42
graphical sequence, 31, 32
Gray code, 140, 141
m-ary, 140
binary, 140, 141
reflected, 141, 143
Gray, Frank, 140
Gribkovskaia, Irina, 1
grid, 41
graph, 108, 109, 111, 113, 124, 125, 155,
157
Gros, L., 140
group theory
computational, 141
Gullivers Travels, 63
Hadamard transform, 224
Hakimi, S. L., 31
Halskau Sr., yvind, 1
Hamming distance, 40
Hampton Court Palace, 107
handshaking lemma, 10
Havel, Vaclav, 31
Havel-Hakimi
test, 32
331
theorem, 31
heap
2-heap, 125
k-ary, 82
binary, 82
binary minimum, 147
Fibonacci, 82, 90, 125
heapsort, 164
Heawood graph, 241
hierarchical structure, 14, 110, 111
Hoare, C. A. R., 103
Hopcroft, John E., 69
Hopkins, Brian, 1
Horner
method, 93
W. G., 93
house graph, 3
Huffman
David, 146
tree, 162
Huffman code, 144, 146148, 150, 157
binary, 146
encoding, 149
tree construction, 146
tree representation, 147, 148, 150
Humpty Dumpty, 2
hypercube graph, 40, 41, 141, 142
in-neighbor, 5
incidence
function, 6
matrix, 24
incidence matrix
oriented, 24
unoriented, 24
indegree, 5
unweighted, 7
independent set, 239
India, 99, 100
induction, 117, 120, 136, 145, 146, 155
structural, 155
infix notation, 104
information channel, 139
insertion sort, 102
Internet, 286
topology, 287
interpolation search, 96
invariant, 29, 33
332
isomorphism, 120
isoperimetric number, 228
Japan, 99, 100
Jarnk, V., 125
Johnson
algorithm, 78, 90, 91, 98
Donald B., 90
join, 119
Jordan, Camille, 210
Konigsberg, 1
graph, 2, 6
seven bridges puzzle, 1, 10
Kaliningrad, 1
Kaplan, Haim, 203
Kataoka, Seiji, 154
Kinoshita, Harunobu, 109
Kirchhoffs current law, 254
Kirchhoffs voltage law, 254
Kleene
algorithm, 90
Stephen, 90
Klein, Felix, 2
Kneser graph, 5961
Knuth
Algorithm S, 293
Donald E., 140, 293
Kraft
inequality, 160, 161
Leon Gordon, 161
theorem, 161
Kruskal
algorithm, 122125, 154, 155, 160
Joseph B., 122
ladder graph, 40
Lagarias, Jeffrey C., 158
Landis, E. M., 189
language, 47
regular, 47
Laplacian, 259
reduced, 261
Laplacian matrix, 27, 160, 222
Laporte, Gilbert, 1
last in, first out, 71
Latora, V., 293
Latora-Marchiori model, 293
lattice, 41
Index
Lee, C. Y., 65
legal firing sequence, 258
Lehman, A., 116
Lehmer, D. H., 55
level
binary tree, 165
tree, 204
LIFO, 71
Lima, 99, 100
line graph, 8
linear search, 94
Linux, 111
list, 59, 66, 68, 71, 82, 147
adjacency, 27, 59, 60, 68
contiguous edge, 61, 282
edge, 61
element, 59
empty, 59
length, 59
little-endian, 63
Loberman, H., 122
Loebbing, Martin, 71
London, 99, 100
Lucas
M. Edouard,
69, 161
number, 161
Madrid, 99, 100
Marchiori, M., 293
marriage ties, 43
matrix, 21
adjacency, 22, 59, 60, 64
bi-adjacency, 23
cutset, 135
distance, 211
incidence, 26
main diagonal, 64
transpose, 53
upper triangle, 64
Matthew effect, 286
max-flow min-cut theorem, 263
generalized, 264
maximum flow problem, 262
maze, 65, 69, 107
McKay, Brendan D., 61, 71
McMillan
Brockway, 160
theorem, 160
Index
Mengers theorem, 216218
merge sort, 179
Merkle, Ralph C., 160
Merris-McKay theorem, 160
mesh, 41, 42
message, 144
metabolic network, 286
metric, 77, 207
function, 76
metric graph, 207
metric space, 77
finite, 77
Milgram, Stanley, 280
minimum cut problem, 263
minimum spanning tree problem, 122
molecular graph, 42, 43, 87
Montmort-Moivre strategy, 106
Moore, Edward F., 65, 82
Morse code, 140, 145, 157
Moscow, 99, 100
MST, 122
multi-undirected graph, 5
multidigraph, 5, 45
multigraph, 5
adjacency, 7
in-neighbor, 7
out-neighbor, 7
Munroe, Randall, 58, 69, 79, 82, 110, 162,
233, 234
musical score, 139
neighbor graph, 284
network, 44, 261
biological, 269, 280
citation, 287
collaboration, 287
communication, 116
flow, 44
information, 280
social, 270, 280, 287
technological, 269, 280
Zachary karate club, 268
New Delhi, 99, 100
NFA, 46, 47
node, 3
noisy channel, 139
null graph, 4, 286
Nuutila, Esko, 90
333
operations research, 44
order, 3
organism, 112, 116
orientation, 6, 24
probability, 97
oriented graph, 273
random, 273, 276
Ottawa, 99, 100
out-neighbor, 5, 66, 74, 78, 79
outdegree, 5
unweighted, 7, 8
outer boundary, 228
overfull graph, 53
Oxley, James, 116
Paley graph, 229
parallel forest-merging, 128
parallelization, 128
partition, 55
Pascal
formula, 177, 201
path, 12, 13, 110, 111
closed, 13
distance, 76
even, 13
geodesic, 14
graph, 40
Hamiltonian, 141, 142
internally disjoint, 215
length, 76, 77, 111
odd, 13
shortest, 58, 7679, 81, 83, 85, 90
tree, 118, 119
weighted, 90
path graph, 16
pendant, 8, 119
perfect square, 112
Perkal, J., 128
permutation
equivalent, 29
random, 157
Peru, 99, 100
Petersen
graph, 43, 44, 74, 75, 212, 213
Julius, 74
planar graph, 43, 54
plane, 108
Pollak, O., 211
334
Index
remainder, 63
postfix notation, 104
power grid, 269
Renaissance, 43
preferential attachment, 285, 286, 289
reservoir sampling, 293
prefix-free condition, 139
residual digraph, 263
Pregel River, 1
residual network, 263
Pretoria, 99, 100
reverse Polish notation, 104
Prim
rich-get-richer effect, 286
algorithm, 122, 125127, 129, 154, 155, river crossing problem, 103
160, 162
Robertson, Neil, 42
R. C., 122, 125
Robertson-Seymour theorem, 42
priority queue, 162, 163
Rogets Thesaurus, 280
probability, 146
root directory, 111
expectation, 146
root list, 178
sample space, 138
rotor-routing model, 254
space, 272
Roy, Bernard, 85
pseudorandom number, 55, 276
RSA, 160
Python, 21
Runde, Volker, 77
Russia, 1, 99, 100
queue, 66, 68, 71, 75, 79, 150
dequeue, 66, 68, 150, 152
sandpile model, 257
end, 66
saturated edge, 262
enqueue, 66, 68, 150, 152
scale-free network, 289, 294, 295
front, 66
scatterplot, 2, 121, 143
length, 66
Schulz, Charles M., 199
minimum-priority, 90, 125, 147
scientific collaboration, 270
priority, 147
seg, 250
rear, 66
selection sort, 101, 102
start, 66
self-complementary graph, 39
quicksort, 103
self-loop, 4
separating set, 37, 217
Ramanujan graph, 228
set, 59
random graph, 267
n-set, 2
Bernoulli, 271
totally ordered, 163
binomial, 271, 281
Seymour
Erdos-Renyi, 278
Paul, 42, 213
uniform, 278
Shannon
weighted, 293
Claude E., 8, 116
random variable
multigraphs, 8, 9
geometric, 276
switching game, 116, 117
Rasmussen, Rune, 116
shellsort, 102
recurrence relation, 204, 205
recursion, 85, 116, 117, 128, 130, 146, 152, Shimbel, A., 82
Shirali, Satish, 77
154, 155
shortest path, 14
reduced Laplacian, 261
Simon, Herbert, 286
regular expression, 47
simple graph, 8, 157, 281
regular graph, 10, 56
random, 272, 277, 279, 280, 291, 292
k-circulant, 56, 57, 281, 282
single-source shortest path, 79, 82
r-regular, 10, 57
six degrees of separation, 280
relative complement, 39
Index
size, 3
component, 294
tree, 117119
small-world, 56, 69, 282
algorithm, 282
characteristic path length, 282
clustering coefficient, 282, 284
effect, 280
experimental results, 281
network, 282
social network, 206
social network analysis, 43
South Africa, 99, 100
Spain, 99, 100
spanning forest, 128
spanning subgraph, 15
spanning tree, 43, 111, 113, 116, 121123,
155, 157, 161
maximum, 154
minimum, 121123, 125130, 155
randomized construction, 155, 156, 160
sparse graph, 275
spectrum, 222
Laplacian, 222
stack, 71, 74, 75, 150
length, 71
pop, 71, 74, 150, 152, 154
push, 71, 74, 150, 152, 154
Stanley, Richard P., 71
star graph, 19, 20
state, 45, 46
accepting, 45, 46
diagram, 45, 46
final, 45, 46, 103
initial, 45, 46, 103
Steinhaus, H., 128
string, 45, 138, 144
accepted, 47
empty, 147
Strogatz, Steven H., 280
strongly regular graph, 224
subgraph, 11, 15
edge-deletion, 121, 122, 160
subtree, 116, 152
left, 136, 154
right, 136, 154
supergraph, 15
Swift, Jonathan, 63
335
Sydney, 99, 100
Sylvesters Law of Nullity, 132
symbol, 144
symbolic computation, 103
symmetric difference, 35
Szekeres, G., 213
Takaoka, Tadao, 201, 203
Tanner graph, 23
Tarjan, Robert Endre, 69
Tarry, Gaston, 69
telegraph, 140
Thailand, 99, 100
The Brain puzzle, 141
Thoreau, Henry David, 8
threshold
probability, 97
Through the Looking Glass, 2
Tokyo, 99, 100
topology, 77
total order, 163
Tower of Hanoi puzzle, 141
trail, 12, 13
closed, 13
transition
function, 45, 46
table, 45
transition probability matrix, 251
transitive closure, 89, 90
trapdoor function, 139
traveling salesman problem, 43, 58
traversal
bottom-up, 152, 153, 160
in-order, 152154, 160, 182
level-order, 149152, 160
post-order, 150, 152, 160
pre-order, 150, 151, 160
treasure map, 1
tree, 14, 65, 110, 111, 119
2-ary, 135
n-ary, 111
binary, 111, 144, 147, 152
complete, 111, 116
depth, 111
directed, 111
expression, 111, 112
height, 111
nonisomorphic, 111, 113, 114
336
ordered, 111, 149
recursive definition, 116, 117, 155
rooted, 15, 65, 74, 111, 115, 135
subtree, 117
traversal, 149, 151
triangle inequality, 77, 92, 207
Tripathi, Amitabha, 31
trivial graph, 16, 111, 293
tuple, 2
union
digraph, 120, 121
union-find, 155
Unix, 111
unweighted degree, 7
USA, 99, 100, 269, 287
value of flow, 262
Vandermonde
Alexandre-Theophile, 201
convolution, 201
Vasudeva, Harkrishan L., 77
vending machine, 4446
vertex, 3
adjacent, 3
child, 111, 115
cut, 37, 217
degree, 10
deletion, 36, 152
deletion subgraph, 35, 211
endpoint, 110, 111
expansion, 228
head, 4
internal, 111, 215
isolated, 8, 59, 116
isoperimetric number, 228
leaf, 110, 111, 144, 152
multigraph, 6
parent, 111
root, 15, 111, 113, 115, 144
set, 3
source, 79, 92
tail, 4
union, 34
vertex connectivity, 211
vertex-cut, 211
Vijay, Sujith, 31
Vitter
algorithm, 293
Index
Jeffrey Scott, 293
Vizings theorem, 244
voltage, 254
Vuillemin, Jean, 177
Wagner
conjecture, 42
Klaus, 42
walk, 11, 13
closed, 13
length, 11, 12
trivial, 11
Walsh transform, 224
Walther, Hansjoachim, 43
Warshall, Stephen, 85, 90
Washington DC, 99, 100
Watanabe, Kohtaro, 154
Watts, Duncan J., 280
Watts-Strogatz model, 281, 282, 293
Wegener, Ingo, 71
weight, 76, 77, 85, 122
correcting, 77, 78
function, 77, 91, 122
graph, 6
minimum, 81, 122, 125, 128
multigraph, 6
negative, 78, 82, 83
nonnegative, 7679, 81, 90
path, 206
positive, 77
reweight, 90, 91
setting, 77, 78
unit, 76, 77
Weinberger, A., 122
wheel graph, 35, 43, 161
Whitney
Hassler, 215
inequality, 214
theorem, 219
Wiener
Harold, 87, 158
number, 87, 160
Williams, J. W. J., 164
Wilson, Robin, 1
wine, 221, 226, 227, 231, 232
word, 45, 144
World Wide Web, 286
Yamada, Takeo, 109, 154
Index
Yerger, Carl R., 161
Zachary, Wayne W., 268
zero padding, 63
Zubrzycki, S., 128
Zwick, Uri, 203
337