This action might not be possible to undo. Are you sure you want to continue?

BooksAudiobooksComicsSheet Music### Categories

### Categories

### Categories

Editors' Picks Books

Hand-picked favorites from

our editors

our editors

Editors' Picks Audiobooks

Hand-picked favorites from

our editors

our editors

Editors' Picks Comics

Hand-picked favorites from

our editors

our editors

Editors' Picks Sheet Music

Hand-picked favorites from

our editors

our editors

Top Books

What's trending, bestsellers,

award-winners & more

award-winners & more

Top Audiobooks

What's trending, bestsellers,

award-winners & more

award-winners & more

Top Comics

What's trending, bestsellers,

award-winners & more

award-winners & more

Top Sheet Music

What's trending, bestsellers,

award-winners & more

award-winners & more

Welcome to Scribd! Start your free trial and access books, documents and more.Find out more

**David Joyner, Minh Van Nguyen, Nathann Cohen
**

Version 0.3

Copyright c 2009–2010

David Joyner <wdjoyner@gmail.com>

Minh Van Nguyen <nguyenminh2@gmail.com>

Nathann Cohen <nathann.cohen@gmail.com>

Permission is granted to copy, distribute and/or modify this document under the terms

of the GNU Free Documentation License, Version 1.3 or any later version published by

the Free Software Foundation; with no Invariant Sections, no Front-Cover Texts, and no

Back-Cover Texts. A copy of the license is included in the section entitled “GNU Free

Documentation License”.

Edition

Version 0.3

19 March 2010

Contents

Acknowledgements iii

List of Algorithms v

List of Figures vii

List of Tables ix

1 Introduction to Graph Theory 1

1.1 Graphs and digraphs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1

1.2 Subgraphs and other graph types . . . . . . . . . . . . . . . . . . . . . . 5

1.3 Representing graphs using matrices . . . . . . . . . . . . . . . . . . . . . 10

1.4 Isomorphic graphs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14

1.5 New graphs from old . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18

1.6 Common applications . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23

2 Graph Algorithms 25

2.1 Graph searching . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25

2.2 Shortest path algorithms . . . . . . . . . . . . . . . . . . . . . . . . . . . 28

3 Trees and Forests 37

3.1 Properties of trees . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38

3.2 Minimum spanning trees . . . . . . . . . . . . . . . . . . . . . . . . . . . 40

3.3 Binary trees . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48

3.4 Applications to computer science . . . . . . . . . . . . . . . . . . . . . . 57

4 Distance and Connectivity 61

4.1 Paths and distance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61

4.2 Vertex and edge connectivity . . . . . . . . . . . . . . . . . . . . . . . . . 61

4.3 Centrality of a vertex . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61

4.4 Network reliability . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 62

5 Optimal Graph Traversals 63

5.1 Eulerian graphs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63

5.2 Hamiltonian graphs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63

5.3 The Chinese Postman Problem . . . . . . . . . . . . . . . . . . . . . . . 63

5.4 The Traveling Salesman Problem . . . . . . . . . . . . . . . . . . . . . . 64

i

ii CONTENTS

6 Planar Graphs 65

6.1 Planarity and Euler’s Formula . . . . . . . . . . . . . . . . . . . . . . . . 65

6.2 Kuratowski’s Theorem . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65

6.3 Planarity algorithms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65

7 Graph Coloring 67

7.1 Vertex coloring . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67

7.2 Edge coloring . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67

7.3 Applications of graph coloring . . . . . . . . . . . . . . . . . . . . . . . . 67

8 Network Flows 69

8.1 Flows and cuts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69

8.2 Ford and Fulkerson’s theorem . . . . . . . . . . . . . . . . . . . . . . . . 69

8.3 Edmonds and Karp’s algorithm . . . . . . . . . . . . . . . . . . . . . . . 69

8.4 Goldberg and Tarjan’s algorithm . . . . . . . . . . . . . . . . . . . . . . 69

9 Random Graphs 71

9.1 Erd¨ os-R´enyi graphs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 71

9.2 Small-world networks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 71

9.3 Scale-free networks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 71

9.4 Evolving networks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 71

10 Graph Problems and Their LP Formulations 73

10.1 Maximum average degree . . . . . . . . . . . . . . . . . . . . . . . . . . . 73

10.2 Traveling Salesman Problem . . . . . . . . . . . . . . . . . . . . . . . . . 75

10.3 Edge-disjoint spanning trees . . . . . . . . . . . . . . . . . . . . . . . . . 76

10.4 Steiner tree . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 77

10.5 Linear arboricity . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 78

10.6 Acyclic edge coloring . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 78

10.7 H-minor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 79

A GNU Free Documentation License 81

1. APPLICABILITY AND DEFINITIONS . . . . . . . . . . . . . . . . . . . . 81

2. VERBATIM COPYING . . . . . . . . . . . . . . . . . . . . . . . . . . . . 83

3. COPYING IN QUANTITY . . . . . . . . . . . . . . . . . . . . . . . . . . . 83

4. MODIFICATIONS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 84

5. COMBINING DOCUMENTS . . . . . . . . . . . . . . . . . . . . . . . . . 85

6. COLLECTIONS OF DOCUMENTS . . . . . . . . . . . . . . . . . . . . . . 86

7. AGGREGATION WITH INDEPENDENT WORKS . . . . . . . . . . . . . 86

8. TRANSLATION . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 86

9. TERMINATION . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 87

10. FUTURE REVISIONS OF THIS LICENSE . . . . . . . . . . . . . . . . . 87

11. RELICENSING . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 87

ADDENDUM: How to use this License for your documents . . . . . . . . . . . 88

Acknowledgements

• Fidel Barrera-Cruz: reported typos in Chapter 3. See changeset 101.

• Daniel Black: reported a typo in Chapter 1. See changeset 61.

iii

iv ACKNOWLEDGEMENTS

List of Algorithms

1.1 Computing graph isomorphism using canonical labels. . . . . . . . . . . . 16

2.1 Breadth-ﬁrst search. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26

2.2 Depth-ﬁrst search. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28

2.3 Dijkstra’s algorithm. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30

2.4 Johnson’s algorithm. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36

3.1 Prim’s algorithm. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43

v

vi LIST OF ALGORITHMS

List of Figures

1.1 A house graph. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1

1.2 A ﬁgure with a self-loop. . . . . . . . . . . . . . . . . . . . . . . . . . . . 3

1.3 A triangle as a directed graph. . . . . . . . . . . . . . . . . . . . . . . . . 4

1.4 Walking along a graph. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6

1.5 A graph and one of its subgraphs. . . . . . . . . . . . . . . . . . . . . . . 7

1.6 Complete graphs K

n

for 1 ≤ n ≤ 5. . . . . . . . . . . . . . . . . . . . . . 8

1.7 Cycle graphs C

n

for 3 ≤ n ≤ 6. . . . . . . . . . . . . . . . . . . . . . . . 9

1.8 Bipartite, complete bipartite, and star graphs. . . . . . . . . . . . . . . . 9

1.9 Adjacency matrices of directed and undirected graphs. . . . . . . . . . . 11

1.10 Tanner graph for H. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12

1.11 Isomorphic and non-isomorphic graphs. . . . . . . . . . . . . . . . . . . . 15

1.12 The wheel graphs W

n

for n = 4, . . . , 9. . . . . . . . . . . . . . . . . . . . 19

1.13 Hypercube graphs Q

n

for n = 1, . . . , 4. . . . . . . . . . . . . . . . . . . . 22

2.1 Searching a weighted digraph using Dijkstra’s algorithm. . . . . . . . . . 29

2.2 Searching a directed house graph using Dijkstra’s algorithm. . . . . . . . 31

2.3 Shortest paths in a weighted graph using the Bellman-Ford algorithm. . . 33

2.4 Searching a digraph with negative weight using the Bellman-Ford algorithm. 33

2.5 Demonstrating the Floyd-Roy-Warshall algorithm. . . . . . . . . . . . . . 35

2.6 Another demonstration of the Floyd-Roy-Warshall algorithm. . . . . . . 35

3.1 Spanning trees for the 4 ×4 grid graph. . . . . . . . . . . . . . . . . . . . 37

3.2 Kruskal’s algorithm for the 4 ×4 grid graph. . . . . . . . . . . . . . . . . 42

3.3 Prim’s algorithm for digraphs. Above is the original digraph and below is

the MST produced by Prim’s algorithm. . . . . . . . . . . . . . . . . . . 44

3.4 Another example of Prim’s algorithm. On the left is the original graph.

On the right is the MST produced by Prim’s algorithm. . . . . . . . . . . 45

3.5 An example of Borovka’s algorithm. On the left is the original graph. On

the right is the MST produced by Boruvka’s algorithm. . . . . . . . . . . 46

3.6 Morse code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49

3.7 Viewing Γ

3

as a Hamiltonian path on Q

3

. . . . . . . . . . . . . . . . . . . 51

3.8 List plot of Γ

8

created using Sage. . . . . . . . . . . . . . . . . . . . . . . 53

3.9 Example of a tree representation of a binary code . . . . . . . . . . . . . 54

3.10 Huﬀman code example . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57

vii

viii LIST OF FIGURES

List of Tables

2.1 Stepping through Dijkstra’s algorithm. . . . . . . . . . . . . . . . . . . . 30

2.2 Another walk-through of Dijkstra’s algorithm. . . . . . . . . . . . . . . . 31

ix

x LIST OF TABLES

Chapter 1

Introduction to Graph Theory

To paraphrase what Felix Klein said about curves,

1

it is easy to deﬁne a graph until you

realize the countless number of exceptions. There are directed graphs, weighted graphs,

multigraphs, simple graphs, and so on. Where do we begin?

1.1 Graphs and digraphs

We start by calling a “graph” what some call an “unweighted, undirected graph without

multiple edges.”

Deﬁnition 1.1. Graphs. A graph G = (V, E) is an ordered pair of sets. Elements of

V are called vertices or nodes, and elements of E ⊆ V × V 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.

One can label a graph by attaching labels to its vertices. If (v

1

, v

2

) ∈ E is an edge of

a graph G = (V, E), we say that v

1

and v

2

are adjacent vertices. For ease of notation, we

write the edge (v

1

, v

2

) as v

1

v

2

. The edge v

1

v

2

is also said to be incident with the vertices

v

1

and v

2

.

a

b

e

c

d

Figure 1.1: A house graph.

1

“Everyone knows what a curve is, until he has studied enough mathematics to become confused

through the countless number of possible exceptions.”

1

2 CHAPTER 1. INTRODUCTION TO GRAPH THEORY

Example 1.2. Consider the graph in Figure 1.1.

1. List the vertex and edge sets of the graph.

2. For each vertex, list all vertices that are adjacent to it.

3. Which vertex or vertices have the largest number of adjacent vertices? Similarly,

which vertex or vertices have the smallest number of adjacent vertices?

4. If all edges of the graph are removed, is the resulting ﬁgure still a graph? Why or

why not?

5. If all vertices of the graph are removed, is the resulting ﬁgure still a graph? Why

or why not?

Solution. (1) Let G = (V, E) denote the graph in Figure 1.1. Then the vertex set of G

is V = {a, b, c, d, e}. The edge set of G is given by

E = {ab, ae, ba, bc, be, cb, cd, dc, de, ed, eb, ea}. (1.1)

We can also use Sage to construct the graph G and list its vertex and edge sets:

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 neighbours. We can use the function

G.neighbors() to list all the neighbours of each vertex.

sage: G.neighbors("a")

[’b’, ’e’]

sage: G.neighbors("b")

[’a’, ’c’, ’e’]

sage: G.neighbors("c")

[’b’, ’d’]

sage: G.neighbors("d")

[’c’, ’e’]

sage: G.neighbors("e")

[’a’, ’b’, ’d’]

1.1. GRAPHS AND DIGRAPHS 3

(3) Taking the cardinalities of the above ﬁve 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 deﬁnition, the edge set of any graph is a subset of V ×V . 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.1 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 an empty or null graph.

Example 1.3. Consider the illustration in Figure 1.2. Does Figure 1.2 represent a

graph? Why or why not?

Solution. If V = {a, b, c} and E = {aa, bc}, it is clear that E ⊆ V × V . 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.

c

b

a

Figure 1.2: A ﬁgure with a self-loop.

In Figure 1.1, 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.1, 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 deﬁnition captures the situation where the direction of

the edges are taken into account.

Deﬁnition 1.4. Directed graphs. 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. A directed edge uv is said to be directed from its tail u to its head

v. A directed graph or digraph G is a graph such that each of whose edges is directed.

The indegree of a vertex v ∈ V (G) counts the number of edges such that v is the head

of those edges. The outdegree of a vertex v ∈ V (G) is the number of edges such that v

is the tail of those edges.

It is important to distinguish a graph G as being directed or undirected. If G is

undirected and uv ∈ E(G), then uv and vu represent the same edge. In case G is a

digraph, then uv and vu are diﬀerent directed edges. Another important class of graphs

consist of those graphs having multiple edges between pairs of vertices.

Deﬁnition 1.5. Multigraphs. 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 undi-

rected. Similarly, a multidigraph is a directed multigraph.

4 CHAPTER 1. INTRODUCTION TO GRAPH THEORY

Deﬁnition 1.6. Simple graphs. A simple graph is a graph with no self-loops and no

multiple edges.

The edges of a digraph can be visually represented as directed arrows, similarly to

the digraph in Figure 1.3. The graph in Figure 1.3 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.3.

c

b

a

Figure 1.3: A triangle as a directed graph.

For any vertex v in a graph G = (V, E), the cardinality of adj(v) 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, we say that v is an isolated vertex. For

example, in the graph in Figure 1.1, we have deg(b) = 3. For the graph in Figure 1.3,

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.1 have

the smallest degree in the graph of that ﬁgure, 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.1 then we have δ(G) = 2

and ∆(G) = 3. In the following Sage session, we construct the digraph in Figure 1.3 and

computes 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

So for the graph G in Figure 1.3, we have δ(G) = ∆(G) = 2.

The graph G in Figure 1.3 has the special property that its minimum degree is the

same as its maximum degree, i.e. δ(G) = ∆(G). Graphs with this property are referred

to as regular. An r-regular graph is a regular graph each of whose vertices has degree r.

For instance, G is a 2-regular graph. The following result, due to Euler, counts the total

number of degrees in any graph.

Theorem 1.7. Euler. If G = (V, E) is a graph, then

v∈V

deg(v) = 2|E|.

Proof. Each edge e = v

1

v

2

∈ E is incident with two vertices, so e is counted twice

towards the total sum of degrees. The ﬁrst time, we count e towards the degree of vertex

v

1

and the second time we count e towards the degree of v

2

.

1.2. SUBGRAPHS AND OTHER GRAPH TYPES 5

Theorem 1.7 is sometimes called the “handshaking lemma,” due to its interpretation

as in the following story. Suppose you go into a room. Suppose there are n people in the

room (including yourself) and some people shake hands with others and some do not.

Create the graph with n vertices, where each vertex is associated with a diﬀerent person.

Draw an edge between two people if they shook hands. The degree of a vertex is the

number of times that person has shaken hands (we assume that there are no multiple

edges, i.e. that no two people shake hands twice). The theorem above simply says that

the total number of handshakes is even. This is “obvious” when you look at it this way

since each handshake is counted twice (A shaking B’s hand is counted, B shaking A’s

hand, since the sum in the theorem is over all vertices).

As E ⊆ V × V , 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.7, the total degree of G is non-negative and even. This result is an immediate

consequence of Theorem 1.7 and is captured in the following corollary.

Corollary 1.8. If G is a graph, then its total number of degrees is non-negative and

even.

If G = (V, E) is an r-regular graph with n vertices and m edges, it is clear by deﬁnition

of r-regular graphs that the total degree of G is rn. By Theorem 1.7 we have 2m = rn

and therefore m = rn/2. This result is captured in the following corollary.

Corollary 1.9. If G = (V, E) is an r-regular graph having n vertices and m edges, then

m = rn/2.

1.2 Subgraphs and other graph types

1.2.1 Walks, trails, and paths

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.

For the graph in Figure 1.4, 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 trail is a walk with no repeating edges. For example, the a-b walk a, b, c, d, f, g, b in

Figure 1.4 is a trail. It does not contain any repeated edges, but it contains one repeated

vertex, i.e. b. Nothing in the deﬁnition of a trail restricts a trail from having repeated

vertices. A walk with no repeating vertices is called a path. Without any repeating

vertices, a path cannot have repeating edges, hence a path is also a trail.

A path whose start and end vertices are the same is called a cycle. For example, the

walk a, b, c, e, a in Figure 1.4 is a path and a cycle. A walk which has no repeated edges

and the start and end vertices are the same, but otherwise has no repeated vertices, is

a closed path (with apologies for slightly abusing terminology).

2

Thus the walk a, b, e, a

in Figure 1.4 is a closed path. It is easy to see that if you remove any edge from a cycle,

2

A closed path in a graph is sometimes also called a “circuit.” Since that terminology unfortunately

conﬂicts with the closely related notion of a circuit of a matroid, we do not use it here.

6 CHAPTER 1. INTRODUCTION TO GRAPH THEORY

b a

c

d

e

g

f

Figure 1.4: Walking along a graph.

then the resulting walk contains no closed paths. An Euler subgraph of a graph G is

either a cycle or an edge-disjoint union of cycles in G.

The length of the shortest cycle in a graph is called the girth of the graph. An acyclic

graph is said to have inﬁnite girth, by convention.

Example 1.10. Consider the graph in Figure 1.4.

1. Find two distinct walks that are not trails and determine their lengths.

2. Find two distinct trails that are not paths and determine their lengths.

3. Find two distinct paths and determine their lengths.

4. Find a closed path that is not a cycle.

5. Find a closed path C which has an edge e such that C −e contains a cycle.

Solution. (1) Here are two distinct walks that are not trails: w

1

: g, b, e, a, b, e and

w

2

: f, d, c, e, f, d. The length of walk w

1

is 5 and the length of walk w

2

is also 5.

(2) Here are two distinct trails that are not paths: t

1

: a, b, c, d, f and t

2

: b, e, f, d, c.

The length of trail t

1

is 4 and the length of trail t

2

is also 4.

(3) Here are two distinct paths: p

1

: a, b, c, d, f, e and p

2

: g, b, a, e, f, d. The length of

path p

1

is 5 and the length of path p

2

is also 5.

(4) Here is a closed path that is not a cycle: d, c, e, b, a, e, f, d.

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 non-empty graph with an isolated vertex.

However, the graph in Figure 1.3 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 non-empty graph

may have several shortest paths between some distinct pair of vertices. For the graph

in Figure 1.4, both a, b, c and a, e, c are geodesic paths between a and c. The number of

connected components of a graph G will be denoted ω(G).

Example 1.11. Determine whether or not the graph in Figure 1.4 is connected. Find a

shortest path from g to d.

1.2. SUBGRAPHS AND OTHER GRAPH TYPES 7

Solution. In the following Sage session, we ﬁrst construct the graph in Figure 1.4 and

use the method is_connected() to determine whether or not the graph is connected.

Finally, we use the method shortest_path() to ﬁnd a geodesic path between g and d.

sage: 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"]})

sage: g.is_connected()

True

sage: g.shortest_path("g", "d")

[’g’, ’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.

We will explain Dijkstra’s algorithm in Chapter 2, which gives one of the best al-

gorithms for ﬁnding shortest paths between two vertices in a connected graph. What

is very remarkable is that, at the present state of knowledge, ﬁnding the shortest path

from a vertex v to a particular (but arbitrarily given) vertex w appears to be as hard as

ﬁnding the shortest path from a vertex v to all other vertices in the graph!

1.2.2 Subgraphs, complete and bipartite graphs

Deﬁnition 1.12. Let G be a graph 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 uv ∈ E(H) then

u, v ∈ V (H). Then H is called a subgraph of G and 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.5, 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)

Figure 1.5: A graph and one of its subgraphs.

8 CHAPTER 1. INTRODUCTION TO GRAPH THEORY

We now consider several standard classes of graphs. The complete graph K

n

on n

vertices is a graph such that any two distinct vertices are adjacent. As |V (K

n

)| = n,

then |E(K

n

)| is equivalent to the total number of 2-combinations from a set of n objects:

|E(K

n

)| =

_

n

2

_

=

n(n −1)

2

. (1.2)

Thus for any graph G with n vertices, its total number of edges |E(G)| is bounded above

by

|E(G)| ≤

n(n −1)

2

.

Figure 1.6 shows complete graphs each of whose total number of vertices is bounded by

1 ≤ n ≤ 5. The complete graph K

1

has one vertex with no edges. It is also called the

trivial graph.

(a) K

5

(b) K

4

(c) K

3

(d)

K

2

(e)

K

1

Figure 1.6: Complete graphs K

n

for 1 ≤ n ≤ 5.

The cycle graph on n ≥ 3 vertices, denoted C

n

, is the connected 2-regular graph on n

vertices. Each vertex in C

n

has degree exactly 2 and C

n

is connected. Figure 1.7 shows

cycles graphs C

n

where 3 ≤ n ≤ 6. The path graph on n ≥ 1 vertices is denoted P

n

. For

n = 1, 2 we have P

1

= K

1

and P

2

= K

2

. Where n ≥ 3, then P

n

is a spanning subgraph

of C

n

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 V

1

and V

2

, both non-empty. Every edge uv ∈ E(G) is such that

u ∈ V

1

and v ∈ V

2

, or v ∈ V

1

and u ∈ V

2

.

The complete bipartite graph K

m,n

is the bipartite graph whose vertex set is parti-

tioned into two non-empty disjoint sets V

1

and V

2

with |V

1

| = m and |V

2

| = n. Any

vertex in V

1

is adjacent to each vertex in V

2

, and any two distinct vertices in V

i

are not

adjacent to each other. If m = n, then K

n,n

is n-regular. Where m = 1 then K

1,n

is

called the star graph. Figure 1.8 shows a bipartite graph together with the complete

bipartite graphs K

4,3

and K

3,3

, and the star graph K

1,4

.

As an example of K

3,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 K

3,3

.

1.2. SUBGRAPHS AND OTHER GRAPH TYPES 9

(a) C

6

(b) C

5

(c) C

4

(d) C

3

Figure 1.7: Cycle graphs C

n

for 3 ≤ n ≤ 6.

(a) Bipartite (b) K

4,3

(c) K

3,3

(d) K

1,4

Figure 1.8: Bipartite, complete bipartite, and star graphs.

10 CHAPTER 1. INTRODUCTION TO GRAPH THEORY

1.3 Representing graphs using matrices

An m×n matrix A can be represented as

A =

_

¸

¸

_

a

11

a

12

· · · a

1n

a

21

a

22

· · · a

2n

. . . . . . . . . . . . . . . . . . .

a

m1

a

m2

· · · a

mn

_

¸

¸

_

.

The positive integers m and n are the row and column dimensions of A, respectively.

The entry in row i column j is denoted a

ij

. Where the dimensions of A are clear from

context, A is also written as A = [a

ij

].

Representing a graph as a matrix is very ineﬃcient 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.7. 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. This section requires some concepts and techniques from linear

algebra, especially matrix theory. See introductory texts on linear algebra and matrix

theory [4] for coverage of such concepts and techniques.

1.3.1 Adjacency matrix

Let G be an undirected graph with vertices V = {v

1

, . . . , v

n

} and edge set E. The

adjacency matrix of G is the n ×n matrix A = [a

ij

] deﬁned by

a

ij

=

_

1, if v

i

v

j

∈ E,

0, otherwise.

As G is an undirected graph, then A is a symmetric matrix. That is, A is a square

matrix such that a

ij

= a

ji

.

Now let G be a directed graph with vertices V = {v

1

, . . . , v

n

} and edge set E. The

(0, −1, 1)-adjacency matrix of G is the n ×n matrix A = [a

ij

] deﬁned by

a

ij

=

_

¸

_

¸

_

1, if v

i

v

j

∈ E,

−1, if v

j

v

i

∈ E,

0, otherwise.

Example 1.13. Compute the adjacency matrices of the graphs in Figure 1.9.

Solution. Deﬁne the graphs in Figure 1.9 using DiGraph and Graph. Then call the

method adjacency_matrix().

sage: G1 = DiGraph({1: [2], 2: [1], 3: [2, 6], 4: [1, 5], 5: [6], 6: [5]})

sage: G2 = Graph({"a": ["b", "c"], "b": ["a", "d"], "c": ["a", "e"], \

....: "d": ["b", "f"], "e": ["c", "f"], "f": ["d", "e"]})

sage: m1 = G1.adjacency_matrix(); m1

[0 1 0 0 0 0]

[1 0 0 0 0 0]

[0 1 0 0 0 1]

[1 0 0 0 1 0]

1.3. REPRESENTING GRAPHS USING MATRICES 11

1

2

3

4

5

6

(a)

c

e

f

a

b

d

(b)

Figure 1.9: Adjacency matrices of directed and undirected graphs.

[0 0 0 0 0 1]

[0 0 0 0 1 0]

sage: m2 = G2.adjacency_matrix(); m2

[0 1 1 0 0 0]

[1 0 0 1 0 0]

[1 0 0 0 1 0]

[0 1 0 0 0 1]

[0 0 1 0 0 1]

[0 0 0 1 1 0]

sage: m1.is_symmetric()

False

sage: m2.is_symmetric()

True

In general, the adjacency matrix of a digraph is not symmetric, while that of an undi-

rected graph is symmetric.

More generally, if G is an undirected multigraph with edge e

ij

= v

i

v

j

having mul-

tiplicity w

ij

, or a weighted graph with edge e

ij

= v

i

v

j

having weight w

ij

, then we can

deﬁne the (weighted) adjacency matrix A = [a

ij

] by

a

ij

=

_

w

ij

, if v

i

v

j

∈ 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.weighted_adjacency_matrix(); 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 and V = V

1

∪ V

2

is the partition

of the vertices into n

1

vertices in V

1

and n

2

vertices in V

2

, so |V | = n

1

+ n

2

. Then the

adjacency matrix A of G can be realized as a block diagonal matrix A =

_

A

1

0

0 A

2

_

,

12 CHAPTER 1. INTRODUCTION TO GRAPH THEORY

where A

1

is an n

1

× n

2

matrix and A

2

is an n

2

× n

1

matrix. Since G is undirected,

A

2

= A

T

1

. The matrix is called a reduced adjacency matrix or a bi-adjacency matrix

(the literature also uses the terms “transfer matrix” or the ambiguous term “adjacency

matrix”).

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 = V

1

∪V

2

is partitioned into two sets: V

1

corresponding

to the m rows of H and V

2

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.reduced_adjacency_matrix()

[1 1 1 0 0]

[0 0 1 0 1]

[1 0 0 1 1]

sage: B.plot(graph_border=True)

The corresponding graph is similar to that in Figure 1.10.

1

2

3

4

5

1

2

3

Figure 1.10: Tanner graph for H.

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 ﬁnite 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 = {e

1

, . . . , e

m

} and vertex set V = {v

1

, . . . , v

n

}.

The incidence matrix of G is the n ×m matrix B = [b

ij

] deﬁned by

b

ij

=

_

¸

¸

¸

_

¸

¸

¸

_

−1, if v

i

is the tail of e

j

,

1, if v

i

is the head of e

j

,

2, if e

j

is a self-loop at v

i

,

0, otherwise.

(1.3)

1.3. REPRESENTING GRAPHS USING MATRICES 13

Each column of B corresponds to an edge and each row corresponds to a vertex. The

deﬁnition of incidence matrix of a digraph as contained in expression (1.3) 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 = {e

1

, . . . , e

m

}

and vertex set V = {v

1

, . . . , v

n

}. The unoriented incidence matrix of G is the n × m

matrix B = [b

ij

] deﬁned by

b

ij

=

_

¸

_

¸

_

1, if v

i

is incident to e

j

,

2, if e

j

is a self-loop at v

i

,

0, otherwise.

An orientation of an undirected graph G is an assignment of direction to each edge of G.

The oriented incidence matrix of G is deﬁned similarly to the case where G is a digraph:

it is the incidence matrix of any orientation of G. For each column of B, we have 1 as

an entry in the row corresponding to one vertex of the edge under consideration and −1

as an entry in the row corresponding to the other vertex. Similarly, b

ij

= 2 if e

j

is a

self-loop at v

i

.

1.3.3 Laplacian matrix

The degree matrix of a graph G = (V, E) is an n × n diagonal matrix D whose i-th

diagonal entry is the degree of the i-th vertex in V . The Laplacian matrix L of G is the

diﬀerence between the degree matrix and the adjacency matrix:

L = D −A.

In other words, for an undirected unweighted simple graph, L = [

ij

] is given by

ij

=

_

¸

_

¸

_

−1, if i = j and v

i

v

j

∈ E,

d

i

, if i = j,

0, otherwise,

where d

i

= deg(v

i

) is the degree of vertex v

i

.

Sage allows you to compute the Laplacian 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: G.laplacian_matrix()

[ 3 -1 0 -1 0 -1]

[-1 4 -1 -1 -1 0]

[ 0 -1 3 -1 0 -1]

[-1 -1 -1 4 -1 0]

[ 0 -1 0 -1 2 0]

[-1 0 -1 0 0 2]

There are many remarkable properties of the Laplacian matrix. It shall be discussed

further in Chapter 4.

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(v

i

, v

j

)] is the distance matrix of G. Sage helps you to compute the

distance matrix of a graph:

14 CHAPTER 1. INTRODUCTION TO GRAPH THEORY

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 4 and 9.

Problems 1.3

1. Let G be an undirected graph whose unoriented incidence matrix is M

u

and whose

oriented incidence matrix is M

o

.

(a) Show that the sum of the entries in any row of M

u

is the degree of the

corresponding vertex.

(b) Show that the sum of the entries in any column of M

u

is equal to 2.

(c) If G has no self-loops, show that each column of M

o

sums to zero.

2. 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.

3. 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.

4. Let G be an undirected graph without self-loops and let M and its oriented in-

cidence matrix. Show that the Laplacian matrix L of G satisﬁes L = M × M

T

,

where M

T

is the transpose of M.

1.4 Isomorphic graphs

Determining whether or not two graphs are, in some sense, the “same” is a hard but

important problem.

Deﬁnition 1.14. Isomorphic graphs. 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.

1.4. ISOMORPHIC GRAPHS 15

a

b

c

d

e f

(a) C

6

1 2

3 4

5 6

(b) G

1

a

b

c

d

e f

(c) G

2

Figure 1.11: Isomorphic and non-isomorphic graphs.

A graph G is isomorphic to a graph H if these two graphs can be labelled in such a

way that if u and v are adjacent in G, then their counterparts in V (H) are also adjacent

in H. To determine whether or not two graphs are isomorphic is to determine if they are

structurally equivalent. Graphs G and H may be drawn diﬀerently so that they seem

diﬀerent. However, if G

∼

= H then the isomorphism f : V (G) −→ V (H) shows that both

of these graphs are fundamentally the same. In particular, the order and size of G are

equal to those of H, the isomorphism f preserves adjacencies, and deg(v) = deg(f(v)) for

all v ∈ G. Since f preserves adjacencies, then adjacencies along a given geodesic path are

preserved as well. That is, if v

1

, v

2

, v

3

, . . . , v

k

is a shortest path between v

1

, v

k

∈ V (G),

then f(v

1

), f(v

2

), f(v

3

), . . . , f(v

k

) is a geodesic path between f(v

1

), f(v

k

) ∈ V (H).

Example 1.15. Consider the graphs in Figure 1.11. Which pair of graphs are isomor-

phic, and which two graphs are non-isomorphic?

Solution. If G is a Sage graph, one can use the method G.is_isomorphic() to determine

whether or not the graph G is isomorphic to another graph. The following Sage session

illustrates how to use G.is_isomorphic().

sage: C6 = Graph({"a": ["b", "c"], "b": ["a", "d"], "c": ["a", "e"], \

....: "d": ["b", "f"], "e": ["c", "f"], "f": ["d", "e"]})

sage: G1 = Graph({1: [2, 4], 2: [1, 3], 3: [2, 6], 4: [1, 5], \

....: 5: [4, 6], 6: [3, 5]})

sage: G2 = Graph({"a": ["d", "e"], "b": ["c", "f"], "c": ["b", "f"], \

....: "d": ["a", "e"], "e": ["a", "d"], "f": ["b", "c"]})

sage: C6.is_isomorphic(G1)

True

sage: C6.is_isomorphic(G2)

False

sage: G1.is_isomorphic(G2)

False

Thus, for the graphs C

6

, G

1

and G

2

in Figure 1.11, C

6

and G

1

are isomorphic, but G

1

and G

2

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.

16 CHAPTER 1. INTRODUCTION TO GRAPH THEORY

1.4.1 Adjacency matrices

Two n × n matrices A

1

and A

2

are permutation equivalent if there is a permutation

matrix P such that A

1

= PA

2

P

−1

. In other words, A

1

is the same as A

2

after a suitable

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.16. Consider two directed or undirected graphs G

1

and G

2

with respective

adjacency matrices A

1

and A

2

. Then G

1

and G

2

are isomorphic if and only if A

1

is

permutation equivalent to A

2

.

This says that the permutation equivalence class of the adjacency matrix is an in-

variant.

Deﬁne an ordering on the set of n×n (0, 1)-matrices as follows: we say A

1

< A

2

if the

list of entries of A

1

is less than or equal to the list of entries of A

2

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

0 1

_

<

_

1 1

1 1

_

.

Input : Two undirected simple graphs G

1

and G

2

, each having n vertices.

Output: True, if G

1

∼

= G

2

; False, otherwise.

Compute the adjacency matrix A

i

of G

i

(i = 1, 2). 1

Compute the lexicographically maximal element A

i

of the permutation 2

equivalence class of A

i

, for i = 1, 2.

if A

1

= A

2

then 3

return True 4

else 5

return False 6

end 7

Algorithm 1.1: Computing graph isomorphism using canonical labels.

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.

1.4.2 Degree sequence

Deﬁnition 1.17. 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 C

6

is 2, 2, 2, 2, 2, 2 and the degree sequence

1.4. ISOMORPHIC GRAPHS 17

of the house graph in Figure 1.1 is 3, 3, 2, 2, 2. If n ≥ 3 then the cycle graph C

n

has the

degree sequence

2, 2, 2, . . . , 2

. ¸¸ .

n copies of 2

.

The path P

n

, for n ≥ 3, has the degree sequence

2, 2, 2, . . . , 2, 1, 1

. ¸¸ .

n−2 copies of 2

.

For positive integer values of n and m, the complete graph K

n

has the degree sequence

n −1, n −1, n −1, . . . , n −1

. ¸¸ .

n copies of n−1

and the complete bipartite graph K

m,n

has the degree sequence

n, n, n, . . . , n,

. ¸¸ .

m copies of n

m, m, m, . . . , m

. ¸¸ .

n copies of m

.

Deﬁnition 1.18. Graphical sequence. Let S be a non-increasing sequence of non-

negative integers. Then S is said to be graphical if it is the degree sequence of some

graph.

Let S = (d

i

)

n

i=1

be a graphical sequence, i.e. d

i

≥ d

j

for all i ≤ j such that 1 ≤ i, j ≤

n. From Corollary 1.8 we see that

d

i

∈S

d

i

= 2k for some integer k ≥ 0. In other words,

the sum of a graphical sequence is non-negative and even.

1.4.3 Invariants revisited

In some cases, one can distinguish non-isomorphic graphs by considering graph invariants.

For instance, the graphs C

6

and G

1

in Figure 1.11 are isomorphic so they have the same

number of vertices and edges. Also, G

1

and G

2

in Figure 1.11 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 diﬀerent values for a given graph

invariant. The following list contains some items to check oﬀ 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.

18 CHAPTER 1. INTRODUCTION TO GRAPH THEORY

Problems 1.4

1. Let J

1

denote the incidence matrix of G

1

and let J

2

denote the incidence matrix of

G

2

. Find matrix theoretic criteria on J

1

and J

2

which hold if and only if G

1

∼

= G

2

.

In other words, ﬁnd the analog of Theorem 1.16 for incidence matrices.)

1.5 New graphs from old

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 brieﬂy described below.

1.5.1 Union, intersection, and join

The disjoint union of graphs is deﬁned as follows. For two graphs G

1

= (V

1

, E

1

) and

G

2

= (V

2

, E

2

) with disjoint vertex sets, their disjoint union is the graph

G

1

∪ G

2

= (V

1

∪ V

2

, E1 ∪ E2).

The adjacency matrix A of the disjoint union of two graphs G

1

and G

2

is the diagonal

block matrix obtained from the adjacency matrices A

1

and A

2

, respectively. Namely,

A =

_

A

1

0

0 A

2

_

.

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 V

1

= V

2

, then G

1

∪ G

2

is simply the graph consisting of all edges in

G

1

or in G

2

.

The intersection of graphs is deﬁned as follows. For two graphs G

1

= (V

1

, E

1

) and

G

2

= (V

2

, E

2

) with disjoint vertex sets, their disjoint union is the graph

G

1

∩ G

2

= (V

1

∩ V

2

, E1 ∩ E2).

In case V

1

= V

2

, then G

1

∩ G

2

is simply the graph consisting of all edges in G

1

and in

G

2

.

The symmetric diﬀerence (or ring sum) of graphs is deﬁned as follows. For two graphs

G

1

= (V

1

, E

1

) and G

2

= (V

2

, E

2

) with disjoint vertex sets, their symmetric diﬀerence is

the graph

G

1

∆G

2

= (V

1

∆V

2

, E1∆E2).

1.5. NEW GRAPHS FROM OLD 19

Recall that the symmetric diﬀerence of two sets S

1

and S

2

is deﬁned by

S

1

∆S

2

= {x ∈ S

1

∪ S

2

| x / ∈ S

1

∩ S

2

}.

In the case where V

1

= V

2

, then G

1

∆G

2

is simply the graph consisting of all edges in G

1

or in G

2

, but not in both. In this case, sometimes G

1

∆G

2

is written as G

1

⊕G

2

.

The join of two disjoint graphs G

1

and G

2

, denoted G

1

+G

2

, is their graph union, with

each vertex of one graph connecting to each vertex of the other graph. For example, the

join of the cycle graph C

n−1

with a single vertex graph is the wheel graph W

n

. Figure 1.12

shows various wheel graphs.

(a) W

4

(b) W

5

(c) W

6

(d) W

7

(e) W

8

(f) W

9

Figure 1.12: The wheel graphs W

n

for n = 4, . . . , 9.

1.5.2 Edge or vertex deletion/insertion

Vertex deletion subgraph

If G = (V, E) is any graph with at least 2 vertices, then the vertex deletion subgraph is

the subgraph obtained from G by deleting a vertex v ∈ V and also all the edges incident

to that vertex. The vertex deletion subgraph of G is sometimes denoted G−{v}. Sage

can compute vertex deletions, 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: G.vertices()

[1, 2, 3, 4, 5, 6]

sage: E1 = Set(G.edges(labels=False)); E1

{(1, 2), (4, 5), (1, 4), (2, 3), (3, 6), (1, 6), (2, 5), (3, 4), (2, 4)}

sage: E4 = Set(G.edges_incident(vertices=[4], labels=False)); E4

{(4, 5), (3, 4), (2, 4), (1, 4)}

sage: G.delete_vertex(4)

20 CHAPTER 1. INTRODUCTION TO GRAPH THEORY

sage: G.vertices()

[1, 2, 3, 5, 6]

sage: E2 = Set(G.edges(labels=False)); E2

{(1, 2), (1, 6), (2, 5), (2, 3), (3, 6)}

sage: E1.difference(E2) == E4

True

Edge deletion subgraph

If G = (V, E) is any graph with at least 1 edge, then the edge deletion subgraph is the

subgraph obtained from G by deleting an edge e ∈ E, but not the vertices incident to

that edge. The edge deletion subgraph of G is sometimes denoted G − {e}. Sage can

compute edge deletions, 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: E1 = Set(G.edges(labels=False)); E1

{(1, 2), (4, 5), (1, 4), (2, 3), (3, 6), (1, 6), (2, 5), (3, 4), (2, 4)}

sage: V1 = G.vertices(); V1

[1, 2, 3, 4, 5, 6]

sage: E14 = Set([(1,4)]); E14

{(1, 4)}

sage: G.delete_edge([1,4])

sage: E2 = Set(G.edges(labels=False)); E2

{(1, 2), (4, 5), (2, 3), (3, 6), (1, 6), (2, 5), (3, 4), (2, 4)}

sage: E1.difference(E2) == E14

True

Vertex cut, cut vertex, or cutpoint

A vertex cut (or separating set) of a connected graph G = (V, E) is a subset W ⊆ V

such that the vertex deletion subgraph G−W is disconnected. In fact, if v

1

, v

2

∈ V are

two non-adjacent vertices, then you can ask for a vertex cut W for which v

1

, v

2

belong

to diﬀerent components of G−W. Sage’s vertex_cut method allows you to compute a

minimal cut having this property.

Edge cut, cut edge, or bridge

If deleting a single, speciﬁc edge would disconnect a graph G, that edge is called a

bridge. More generally, the edge cut (or disconnecting set or seg) of a connected graph

G = (V, E) is a set of edges F ⊆ E whose removal yields an edge deletion subgraph

G−F that is disconnected. A minimal edge cut is called a cut set or a bond. In fact, if

v

1

, v

2

∈ V are two vertices, then you can ask for an edge cut F for which v

1

, v

2

belong

to diﬀerent components of G − F. Sage’s edge_cut method allows you to compute a

minimal cut having this property.

Edge contraction

An edge contraction is an operation which, like edge deletion, removes an edge from a

graph. However, unlike edge deletion, edge contraction also merges together the two

vertices the edge used to connect.

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 G

c

= (V, E

c

) is the complement of

1.5. NEW GRAPHS FROM OLD 21

G = (V, E), then two distinct vertices v, w ∈ V are adjacent in G

c

if and only if they

are not adjacent in G. The sum of the adjacency matrix of G and that of G

c

is the

matrix with 1’s everywhere, except for 0’s on the main diagonal. A simple graph that is

isomorphic to its complement is called a self-complementary 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)

{}

Theorem 1.19. If G = (V, E) is self-complementary, then the order of G is |V | = 4k

or |V | = 4k +1 for some non-negative integer k. Furthermore, if n = |V | is the order of

G, then the size of G is |E| = n(n −1)/4.

Proof. Let G be a self-complementary graph of order n. Each of G and G

c

contains half

the number of edges in K

n

. From (1.2), we have

|E(G)| = |E(G

c

)| =

1

2

·

n(n −1)

2

=

n(n −1)

4

.

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 [23, Theorem 1.9] we have 4 | n and so n = 4k

for some non-negative k ∈ Z. If n − 1 is even, use a similar argument to conclude that

n = 4k + 1 for some non-negative k ∈ Z.

1.5.4 Cartesian product

The Cartesian product GH of graphs G and H is a graph such that the vertex set of

GH is the Cartesian product V (G) × V (H). Any two vertices (u, u

) and (v, v

) are

adjacent in GH 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 GH is V (GH) = V (G) × V (H) and the edge set of GH is the

union

E(GH) =

_

V (G) ×E(H)

_

∪

_

E(G) ×V (H)

_

.

Sage can compute Cartesian products, as the following example shows.

22 CHAPTER 1. INTRODUCTION TO GRAPH THEORY

sage: Z = graphs.CompleteGraph(2); len(Z.vertices()); len(Z.edges())

2

1

sage: C = graphs.CycleGraph(5); len(C.vertices()); len(C.edges())

5

5

sage: P = C.cartesian_product(Z); len(P.vertices()); len(P.edges())

10

15

The hypercube graph Q

n

is the n-regular graph with vertex set

V =

_

(

1

, . . . ,

n

) |

i

∈ {0, 1}

_

of cardinality 2

n

. That is, each vertex of Q

n

is a bit string of length n. Two vertices

v, w ∈ V are connected by an edge if and only if v and w diﬀer in exactly one coordinate.

3

The Cartesian product of n edge graphs K

2

is a hypercube:

(K

2

)

n

= Q

n

.

Figure 1.13 illustrates the hypercube graphs Q

n

for n = 1, . . . , 4.

(a) Q

1

(b) Q

2

(c) Q

3

(d) Q

4

Figure 1.13: Hypercube graphs Q

n

for n = 1, . . . , 4.

Example 1.20. The Cartesian product of two hypercube graphs is another hypercube:

Q

i

Q

j

= Q

i+j

.

The path graph P

n

is a tree with n vertices V = {v

1

, . . . , v

n

} and edges E =

{(v

i

, v

i+1

) | 1 ≤ i ≤ n − 1}. In this case, deg(v

1

) = deg(v

n

) = 1 and deg(v

i

) = 2

for 1 < i < n. The path graph P

n

can be obtained from the cycle graph C

n

by delet-

ing one edge of C

n

. The ladder graph L

n

is the Cartesian product of path graphs, i.e.

L

n

= P

n

P

1

.

3

In other words, the “Hamming distance” between v and w is equal to 1.

1.6. COMMON APPLICATIONS 23

1.5.5 Graph minors

A graph H is called a minor of a graph G if H is isomorphic to a graph obtained by a

sequence of edge contractions on a subgraph of G. The order in which a sequence of such

contractions is performed on G does not aﬀect the resulting graph H. A graph minor

is not in general a subgraph. However, if G

1

is a minor of G

2

and G

2

is a minor of G

3

,

then G

1

is a minor of G

3

. Therefore, the relation“being a minor of” is a partial ordering

on the set of graphs.

The following non-intuitive fact about graph minors was proven by Neil Robertson

and Paul Seymour in a series of 20 papers spanning 1983 to 2004. This result is known

by various names including the Robertson-Seymour theorem, the graph minor theorem,

or Wagner’s conjecture (named after Klaus Wagner).

Theorem 1.21. Robertson & Seymour 1983–2004. If an inﬁnite list G

1

, G

2

, . . .

of ﬁnite graphs is given, then there always exist two indices i < j such that G

i

is a minor

of G

j

.

Many classes of graphs can be characterized by forbidden minors: a graph belongs

to the class if and only if it does not have a minor from a certain speciﬁed list. We shall

see examples of this in Chapter 6.

Problems 1.5

1. Show that the complement of an edgeless graph is a complete graph.

2. Let GH be the Cartesian product of two graphs Gand H. Show that |E(GH)| =

|V (G)| · |E(H)| +|E(G)| · |V (H)|.

1.6 Common applications

A few other common problems arising in applications of weighted graphs are listed below.

• If the edge weights are all non-negative, ﬁnd a “cheapest” closed path which con-

tains all the vertices. This is related to the famous traveling salesman problem and

is further discussed in Chapters 2 and 5.

• 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 4 and 9.

• A planar graph is a graph that can be drawn on the plane in such a way that its

edges intersect only at their endpoints. Can a graph be drawn entirely in the plane,

with no crossing edges? In other words, is a given graph planar? This problem is

important for designing computer chips and wiring diagrams. Further discussion

is contained in Chapter 6.

24 CHAPTER 1. INTRODUCTION TO GRAPH THEORY

• Can you label or “color” all the vertices of a graph in such a way that no adjacent

vertices have the same color? If so, this is called a vertex coloring. Can you label or

“color” all the edges of a graph in such a way that no incident edges have the same

color? If so, this is called an edge coloring. Graph coloring has several remarkable

applications, one of which is to scheduling of jobs relying on a shared resource.

This is discussed further in Chapter 7.

• In some ﬁelds, such as operations research, a directed graph with non-negative edge

weights is called a network, the vertices are called nodes, the edges are called arcs,

and the weight on an edge is called its capacity. A “network ﬂow” must satisfy

the restriction that the amount of ﬂow into a node equals the amount of ﬂow out

of it, except when it is a “source node”, which has more outgoing ﬂow, or a “sink

node”, which has more incoming ﬂow. The ﬂow along an edge must not exceed the

capacity. What is the maximum ﬂow on a network and how to you ﬁnd it? This

problem, which has many industrial applications, is discussed in Chapter 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 city-

to-city, you must eﬀectively 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. We shall examine several in this chapter.

2.1 Graph searching

This section discusses algorithms for

• breadth-ﬁrst searches,

• depth-ﬁrst searches, and

• we explain how these relate to determining a graph’s connectivity.

2.1.1 Breadth-ﬁrst search

Breadth-ﬁrst search (BFS) is a strategy for running through the nodes of a graph. Sup-

pose you want to count the number of vertices (or edges) satisfying a property P. Algo-

rithm 2.1 presents a technique for ﬁnding the number of vertices satisfying P.

Another version of Algorithm 2.1 is where you are searching the graph for a vertex

(or edge) satisfying a certain property P. In that situation, you simply quit at the step

where you increment the counter, i.e. line 7 in Algorithm 2.1. Other variations are also

possible as well.

For the example of the graph in Figure 1.4, the list of distances from vertex a to any

other vertex is

[[’a’, 0], [’b’, 1], [’c’, 2], [’d’, 3], [’e’, 1], [’f’, 2], [’g’, 2]]

To create this list,

• Start at a and compute the distance from a to itself.

25

26 CHAPTER 2. GRAPH ALGORITHMS

Input : A connected graph G = (V, E) (and, optionally, a starting or “root”

vertex v

0

∈ V ). A property P to be tested.

Output: The number of vertices of G satisfying P.

Create a queue Q of “unseen” vertices initially containing a starting vertex v

0

. 1

Start a list T of “already seen” vertices initially empty. 2

count ← 0 3

for w ∈ Q do 4

Test w for P. 5

if P(w) = True then 6

count ← count + 1 7

end 8

Add all neighbors of w not in T to Q. 9

Remove all “seen” vertices w from Q. 10

Add such w to T. 11

if T = V then 12

return count 13

end 14

end 15

Algorithm 2.1: Breadth-ﬁrst search.

• Move to each neighbor of a, namely b and e, and compute the distance from a to

each of them.

• Move to each “unseen” neighbor of b, namely just c, and compute the distance

from a to it.

• Move to each “unseen” neighbor of e, namely just f, and compute the distance

from a to it.

• Move to each “unseen” neighbor of c, namely just d, and compute the distance

from a to it.

• Move to each “unseen” neighbor of f, namely just g, and compute the distance

from a to it.

As an example, here is some Sage code which implements BFS to compute the list

distances from a given vertex.

def graph_distance(G, v0):

"""

Breadth first search algorithm to find the

distance from a fixed vertex $v_0$ to any

other vertex.

INPUT:

G - a connected graph

v0 - a vertex

OUTPUT:

D - a list of distances to

every other vertex

EXAMPLES:

sage: G = Graph({1: [2, 4], 2: [1, 4], 3: [2, 6],

2.1. GRAPH SEARCHING 27

4: [1, 3], 5: [4, 2], 6: [3, 1]})

sage: v0 = 1

sage: graph_distance(G,v0)

[[1, 0], [2, 1], [3, 2], [4, 1], [5, 2], [6, 1]]

sage: G = Graph({"a": ["b", "e"], "b": ["c", "e"], \

"c": ["d", "e"], "d": ["f"], "e": ["f"], "f": ["g"], "g":["b"]})

sage: v0 = "a"

sage: graph_distance(G, v0)

[[’a’, 0], [’b’, 1], [’c’, 2], [’d’, 3], [’e’, 1],

[’f’, 2], [’g’, 2]]

sage: G = Graph({1: [2,3], 2: [1, 3], 3: [2], 4: [5], 5: [6], 6: [5]})

sage: v0 = 1

sage: graph_distance(G, v0) # note G is disconnected

[[1, 0], [2, 1], [3, 1]]

"""

V = G.vertices()

Q = [v0]

T = []

D = []

while Q<>[] and T<>V:

for v in Q:

if not(v in T):

D.append([v,G.distance(v0,v)])

if v in Q:

Q.remove(v)

T.append(v)

T = list(Set(T))

Q = Q+[x for x in G.neighbors(v) if not(x in T+Q)]

if T == V:

break

D.sort()

print Q, T

return D

Exercise 2.1. Using Sage’s shortest_path method, can you modify the above function

to return a list of shortest paths from v

0

to any other vertex?

2.1.2 Depth-ﬁrst search

A depth-ﬁrst search is a type of algorithm that visits each vertex of a graph, proceeding

from vertex-to-vertex in this search but moving along a spanning tree of that graph.

Suppose you have a normal 8×8 chess board in front of you, with a single knight piece

on the board. If you can ﬁnd a sequence of knight moves which visits each and every

square exactly once, then you will have found a so-called complete knight tour. Naively,

how do you ﬁnd a complete knight tour? Intuitively, you would make one knight move

after another, recording each move to ensure that you did not step on a square you have

already visited, until you could not make any more moves. It is very, very unlikely that

if do this you will have visited every square exactly once (if you don’t believe me, please

try it yourself!). Acknowledging defeat, at this stage, it might make sense to backtrack a

few moves and try again, hoping you will not get “stuck” so soon. If you fail again, try

backtracking a few move moves and traverse yet another path, hoping to make further

progress. Repeat this until a compete tour is found. This is an example of depth-ﬁrst

search, also sometimes called backtracking.

Similar to BFS, depth-ﬁrst search (DFS) is an algorithm for traversing a graph.

One starts at a root vertex and explores as far as possible along each branch before, if

necessary, backtracking along a new path. It is easier to see what this means in the case

of a rooted tree than for more general graphs, as illustrated below.

Suppose you want to count the number of vertices (or edges) satisfying a property P.

In the case of a graph, you can modify Algorithm 2.2 to a so-called iterative DFS.

This modiﬁcation applies DFS repeatedly with an increasing depth of search at each

28 CHAPTER 2. GRAPH ALGORITHMS

Input : A rooted tree G = (V, E) with root vertex v

0

∈ V .

Output: True if G has a vertex satisfying P; False otherwise.

Create a queue Q of “child” vertices of the root v

0

. 1

Initialize a list S of “seen” vertices. 2

count ← 0 3

for w ∈ Q do 4

Test w for P. 5

if P(w) = True then 6

count ← count + 1 7

end 8

Add w to S. 9

if S = V then 10

return count 11

end 12

end 13

Call the DFS algorithm iteratively with the rooted subtree having w and all its 14

children as vertices and w as the rooted vertex.

Algorithm 2.2: Depth-ﬁrst search.

step, until the diameter of the graph is reached and all vertices are seen.

2.1.3 Application: connectivity of a graph

A simple algorithm to determine if a graph is connected might be described as follows:

• Begin at any arbitrary vertex of the graph, Γ = (V, E).

• Proceed from that vertex using either DFS or BFS, counting all vertices reached.

• Once the connected component of the graph has been entirely traversed, if the

number of vertices counted is equal to |V |, the graph is connected and otherwise

it is disconnected.

2.2 Shortest path algorithms

Let G = (V, E) be a graph with non-negative edge weights, w(e) for e ∈ E. The length

of a path P from v ∈ V to w ∈ V is the sum of the edge weights for each edge in the

path P, denoted δ(P). We write δ(v, w) for the smallest value of δ(P) for all paths P

from v to w. When we regard these weights w as distances, a path from v to w which

realizes δ(v, w) is sometimes called a shortest path from v to w.

There are a number of diﬀerent algorithms for computing a shortest path in a weighted

graph. Some only work if the graph has no negative weight cycles. Some assume that

there is a single start or source vertex. Some compute the shortest paths from any vertex

to any other, and also detect if the graph has a negative weight cycle.

No matter what algorithm you use, the length of the shortest path cannot exceed the

number of vertices in the graph.

2.2. SHORTEST PATH ALGORITHMS 29

Lemma 2.2. Fix a vertex v in the connected graph G = (V, E) and let n denote the

number of vertices of G, n = |V |. If there are no negative weight cycles in G then there

exists a shortest path from v to any other vertex w ∈ V which uses at most n −1 edges.

proof: Suppose that G contains no negative weight cycles. Observe that at most

n −1 edges are required to construct a path from v to any vertex w. Let P denote such

a path,

P = (v

0

= v → v

1

→ v

2

→ · · · → v

k

= w).

Since G has no negative weight cycles, the weight of P is no less than the weight of P

,

where P

**is the same as p except that all cycles have been removed. Thus, we can remove
**

all cycles from P and obtain a path P

**from v to w of lower weight. Since the ﬁnal path
**

is acyclic, it must have no more than n −1 edges.

2.2.1 Dijkstra’s algorithm

See Dijkstra [12], section 24.3 of Cormen et al. [11], and section 12.6 of Berman and

Paul [5].

Dijkstra’s algorithm, discovered by E. Dijkstra in 1959, is a graph search algorithm

that solves the single-source shortest path problem for a graph with non-negative edge

weights. For example, if the vertices of a weighted graph represent cities and edge

weights represent distances between pairs of cities connected by a direct road, Dijkstra’s

algorithm can be used to ﬁnd the shortest route from a ﬁxed city to all other cities.

Let G = (V, E) be a graph with non-negative edge weights, as above. Fix a start or

source vertex v

0

∈ V .

Dijkstra’s algorithm performs a number of steps, basically one step for each vertex

in V . We partition the vertex set V into two subsets: the set F of vertices where we

have found the shortest path to v

0

; and the “queue” Q where we do not yet know for

sure the shortest path to v

0

. The vertices v ∈ F are labeled with δ(v, v

0

). The vertices

v ∈ Q are labeled with a temporary label L(v). This temporary label can be either ∞ if

no path from v to v

0

has yet been examined, or an upper bound on δ(v, v

0

) obtained by

computing δ(P) for a path P from v to v

0

which has been found (but may not be the

shortest path).

The simplest implementation of Dijkstra’s algorithm has running time O(|V |

2

) =

O(n

2

) (where n = |V | is the number of vertices of the graph)

1

.

0 2 4

1 3

10

3

1

2

4

8

2

7

9

Figure 2.1: Searching a weighted digraph using Dijkstra’s algorithm.

1

This can be improved, with some clever programming, in the case of “sparse” graphs to O(nlog n).

30 CHAPTER 2. GRAPH ALGORITHMS

Input : A connected graph G = (V, E) having non-negative edge weights and a

starting vertex v

0

∈ V .

Output: A shortest path from v

0

to an vertex in V .

Create a queue Q of “unseen” vertices initially being all of V . 1

Start a list F of “already seen” vertices initially empty. 2

Initialize labels L(v

0

) = 0 and L(v) = ∞ for all v ∈ V with v = v

0

. 3

Find v ∈ Q for which L(v) is ﬁnite and minimum. 4

if no such v exists then 5

return 6

else 7

Label v with the distance δ(v, v

0

) = L(v). 8

Add v to F. 9

Remove v from Q. 10

if F = V then 11

return 12

end 13

end 14

for w ∈ Q such that w is adjacent to v do 15

Replace L(w) by min(L(w), L(v) + wt(v, w)). 16

Go to step 4. 17

end 18

Algorithm 2.3: Dijkstra’s algorithm.

v

0

v

1

v

2

v

3

v

4

0 ∞ ∞ ∞ ∞

10 3 ∞ ∞

7 11 5

7 11

9

Table 2.1: Stepping through Dijkstra’s algorithm.

2.2. SHORTEST PATH ALGORITHMS 31

Example 2.3. Apply Dijkstra’s algorithm to the graph in Figure 2.1.

Solution. Dijkstra’s algorithm applied to the graph in Figure 2.1 yields Table 2.1. The

steps below explain how this table is created.

1. Start at v

0

, let Q = V and F = ∅. Initialize the labels L(v) to be ∞ for all v = v

0

.

This is the ﬁrst row of the table. Take the vertex v

0

out of the queue.

2. Consider the set of all adjacent nodes to v

0

. Replace the labels in the ﬁrst row by

the weights of the associated edges. Underline the smallest one and take its vertex

(i.e. v

2

) out of the queue. This is the second row of the table.

3. Consider the set of all nodes w which are adjacent to v

2

. Replace the labels in the

second row by min(L(w), L(v

2

) +wt(v

2

, w)). Underline the smallest one and take

its vertex (i.e. v

4

) out of the queue. This is the third row of the table.

4. Finally, start from v

4

and ﬁnd the path to the remaining vertex v

3

in Q. Take the

smallest distance from v

0

to v

3

. This is the last row of the table.

Exercise 2.4. Dijkstra’s algorithm applied to the graph in Figure 2.2 results in Table 2.2.

Verify the steps to create this table.

0 1

2 3

4

1

3

6

1 3

1

2

1

3

2

1

Figure 2.2: Searching a directed house graph using Dijkstra’s algorithm.

v

0

v

1

v

2

v

3

v

4

0 ∞ ∞ ∞ ∞

1 3 ∞ 6

2 4 6

3 6

5

Table 2.2: Another walk-through of Dijkstra’s algorithm.

32 CHAPTER 2. GRAPH ALGORITHMS

2.2.2 Bellman-Ford algorithm

See section 24.1 of Cormen et al. [11], and section 8.5 of Berman and Paul [5].

The Bellman-Ford algorithm computes single-source shortest paths in a weighted

graph or digraph, where some of the edge weights may be negative. Instead of the

“greedy” approach that Dijkstra’s algorithm took, 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 implementation below takes in a graph or digraph, and creates two Python

dictionaries dist and predecessor, keyed on the list of vertices, which store the distance

and shortest paths. However, if a negative weight cycle exists (in the case of a digraph),

then an error is raised.

def bellman_ford(Gamma, s):

"""

Computes the shortest distance from s to all other vertices in Gamma.

If Gamma has a negative weight cycle, then return an error.

INPUT:

- Gamma -- a graph.

- s -- the source vertex.

OUTPUT:

- (d,p) -- pair of dictionaries keyed on the list of vertices,

which store the distance and shortest paths.

REFERENCE:

http://en.wikipedia.org/wiki/Bellman-Ford_algorithm

"""

P = []

dist = {}

predecessor = {}

V = Gamma.vertices()

E = Gamma.edges()

for v in V:

if v == s:

dist[v] = 0

else:

dist[v] = infinity

predecessor[v] = 0

for i in range(1, len(V)):

for e in E:

u = e[0]

v = e[1]

wt = e[2]

if dist[u] + wt < dist[v]:

dist[v] = dist[u] + wt

predecessor[v] = u

# check for negative-weight cycles

for e in E:

u = e[0]

v = e[1]

wt = e[2]

if dist[u] + wt < dist[v]:

raise ValueError("Graph contains a negative-weight cycle")

return dist, predecessor

Bellman-Ford runs in O(|V | · |E|)-time, which is O(n

3

) for “dense” connected graphs

(where n = |V |).

Here are some examples.

2.2. SHORTEST PATH ALGORITHMS 33

sage: M = matrix([[0,1,4,0], [0,0,1,5], [0,0,0,3], [0,0,0,0]])

sage: G = Graph(M, format="weighted_adjacency_matrix")

sage: bellman_ford(G, G.vertices()[0])

{0: 0, 1: 1, 2: 2, 3: 5}

The plot of this graph is given in Figure 2.3.

0 2

1 3

1

4

1

5

3

Figure 2.3: Shortest paths in a weighted graph using the Bellman-Ford algorithm.

The following example illustrates the case of a negative-weight cycle.

sage: M = matrix([[0,1,0,0],[1,0,-4,1],[1,1,0,0],[0,0,1,0]])

sage: G = DiGraph(M, format = "weighted_adjacency_matrix")

sage: bellman_ford(G, G.vertices()[0])

---------------------------------------------------------------------------

...

ValueError: Graph contains a negative-weight cycle

The plot of this graph is given in Figure 2.4.

0 1

2 3

1

1

−4

1 1

1

1

Figure 2.4: Searching a digraph with negative weight using the Bellman-Ford algorithm.

2.2.3 Floyd-Roy-Warshall algorithm

See section 25.2 of Cormen et al. [11], and section 14.4 of Berman and Paul [5].

The Floyd-Roy-Warshall algorithm (FRW), or the Floyd-Warshall algorithm, is an

algorithm for ﬁnding shortest paths in a weighted, directed graph. Like the Bellman-

Ford 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 the FRW algorithm will ﬁnd the shortest paths between all pairs of vertices. It was

34 CHAPTER 2. GRAPH ALGORITHMS

discovered independently by Bernard Roy in 1959, Robert Floyd in 1962, and by Stephen

Warshall 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 G as V =

{1, 2, . . . , n}. Call SD(i, j, k) a shortest distance from vertex i to vertex j that only uses

vertices 1 through k. This can be computed using the recursive expression

SD(i, j, k) = min{SD(i, j, k −1), SD(i, k, k −1) + SD(k, j, k −1)}.

The key to the Floyd-Roy-Warshall algorithm lies in exploiting this formula. If n = |V |,

then this is a O(n

3

) time algorithm. For comparison, the Bellman-Ford algorithm has

complexity O(|V | · |E|), which is O(n

3

) time for “dense” graphs. However, Bellman-Ford

only yields the shortest paths eminating from a single vertex. To achieve comparable

output, we would need to iterate Bellman-Ford over all vertices, which would be an

O(n

4

) time algorithm for “dense” graphs. Except possibly for “sparse” graphs, Floyd-

Roy-Warshall is better than an interated implementation of Bellman-Ford.

Here is an implementation in Sage.

def floyd_roy_warshall(A):

"""

Shortest paths

INPUT:

- A -- weighted adjacency matrix

OUTPUT:

- dist -- a matrix of distances of shortest paths.

- paths -- a matrix of shortest paths.

"""

G = Graph(A, format="weighted_adjacency_matrix")

V = G.vertices()

E = [(e[0],e[1]) for e in G.edges()]

n = len(V)

dist = [[0]*n for i in range(n)]

paths = [[-1]*n for i in range(n)]

# initialization step

for i in range(n):

for j in range(n):

if (i,j) in E:

paths[i][j] = j

if i == j:

dist[i][j] = 0

elif A[i][j]<>0:

dist[i][j] = A[i][j]

else:

dist[i][j] = infinity

# iteratively finding the shortest path

for j in range(n):

for i in range(n):

if i <> j:

for k in range(n):

if k <> j:

if dist[i][k]>dist[i][j]+dist[j][k]:

paths[i][k] = V[j]

dist[i][k] = min(dist[i][k], dist[i][j] +dist[j][k])

for i in range(n):

if dist[i][i] < 0:

raise ValueError, "A negative edge weight cycle exists."

return dist, matrix(paths)

Here are some examples.

2.2. SHORTEST PATH ALGORITHMS 35

sage: A = matrix([[0,1,2,3],[0,0,2,1],[-5,0,0,3],[1,0,1,0]]); A

sage: floyd_roy_warshall(A)

Traceback (click to the left of this block for traceback)

...

ValueError: A negative edge weight cycle exists.

The plot of this weighted digraph with four vertices appears in Figure 2.5.

0 1

2 3

1

2

3 2 1

5

3

1

1

Figure 2.5: Demonstrating the Floyd-Roy-Warshall algorithm.

sage: A = matrix([[0,1,2,3],[0,0,2,1],[-1/2,0,0,3],[1,0,1,0]]); A

sage: floyd_roy_warshall(A)

([[0, 1, 2, 2], [3/2, 0, 2, 1], [-1/2, 1/2, 0, 3/2], [1/2, 3/2, 1, 0]],

[-1 1 2 1]

[ 2 -1 2 3]

[-1 0 -1 1]

[ 2 2 -1 -1])

The plot of this weighted digraph with four vertices appears in Figure 2.6.

2.2.4 Johnson’s algorithm

See section 25.3 of Cormen et al. [11] and Johnson [16].

Let G = (V, E) be a graph with edge weights but no negative cycles. Johnson’s

algorithm ﬁnds a shortest path between all pairs of vertices in a “sparse” directed graph.

The time complexity, for sparse graphs, is O(|V |

2

log |V | + |V | · |E)| = O(n

2

log n),

where n = |V | is the number of vertices of the original graph G.

36 CHAPTER 2. GRAPH ALGORITHMS

0

1

2 3 1

2

3

2 1

−1/2

3

1

1

Figure 2.6: Another demonstration of the Floyd-Roy-Warshall algorithm.

Input : A connected graph G = (V, E) having (possibly negative) edge weights.

Output: A shortest path between all pairs of vertices in V (or terminate if a

negative edge cycle is detected).

Add a new vertex v

0

with zero weight edges from it to all v ∈ V . 1

Run the Bellman-Ford algorithm to check for negative weight cycles and ﬁnd h(v), 2

the least weight of a path from the new node v

0

to v ∈ V .

If the last step detects a negative cycle, the algorithm is terminated. 3

Reweight the edges using the vertices’ h(v) values: an edge from v ∈ V to w ∈ V , 4

having length wt(v, w), is given the new length wt(v, w) + h(v) −h(w).

For each v ∈ V , run Dijkstra’s algorithm and store the computed least weight to 5

other vertices.

Algorithm 2.4: Johnson’s algorithm.

Chapter 3

Trees and Forests

Recall, 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. A vertex of a forest of degree

one is called an endpoint or a leaf. A connected forest is a tree.

A rooted tree is a tree with a speciﬁed root vertex v

0

. (However, if G is a rooted tree

with root vertex v

0

and if the degree of v

0

is one then, by convention, we do not call

v

0

an endpoint or a leaf.) A directed tree is a directed graph which would be a tree if

the directions on the edges were ignored. A rooted tree can be regarded as a directed

tree since you imagine an edge E = {u, v}, for u, v ∈ V , being directed from u to v,

e = (u, v), if and only if v is further away from v

0

than u is. If e = (u, v) is an edge in

a rooted tree, then we call v a child vertex with parent u. An ordered tree is a rooted

tree for which an ordering is speciﬁed for the children of each vertex. An n-ary tree is

a rooted tree for which each vertex that is not a leaf has at most n children. The case

n = 2 are called binary trees.

Directed trees are pervasive in theoretical computer science, as they are useful struc-

tures for describing algorithms and relationships between objects in certain data sets.

A spanning tree T of a connected, undirected graph G is a subgraph containing all

vertices of G which is a tree.

Example 3.1. Consider the 3 × 3 grid graph with 16 vertices and 18 edges. Two

examples of a spanning tree are given in Figure 3.1 by using thicker line width for its

edges.

Figure 3.1: Spanning trees for the 4 ×4 grid graph.

37

38 CHAPTER 3. TREES AND FORESTS

The following game is a variant of the Shannon switching game, due to Edmunds

and Lehman. We follow the description in Oxley’s survey (What is a matroid?’ ... add

reference later ... ).

Recall 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. Alice’s goal is to tag the edges of

a spanning tree, while Bob’s goal is to tag the edges of a bond. If we think of this game

in terms of a communication network, then Bob’s 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.

Theorem 3.2. The following statements are equivalent for a connected graph G.

• Bob plays ﬁrst and Alice can win against all possible strategies of Bob.

• The graph G has 2 edge-disjoint spanning trees.

• For all partitions P of the vertex set V of G, the number of edges of G that join

vertices in diﬀerent classes of the partition is at least 2(|P| −1).

3.1 Properties of trees

The following theorem gives several basic characterizations of trees.

Theorem 3.3. 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 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 = (V

1

, E

2

) be a graph and T = (V

2

, E

2

) a subgraph of G which is a tree. As

in (6) we see adding just one edge in E

1

−E

2

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.)

Solution. (1) =⇒ (2): This basically follows by induction on the number of vertices.

By deﬁnition, a tree has no cycles. Make the following induction hypothesis: for any

tree T = (V, E), |E| = |V | − 1. This holds in the base case where |V | = 1 since in

that case, there can be no edges. Assume it is true for all trees with |V | = k, for some

k > 1. Let T = (V, E) be a tree having k + 1 vertices. Remove an edge (but not the

vertices it is incident to). This disconnects T into T

1

= (V

1

, E

1

) union T

2

= (V

2

, E

2

),

3.1. PROPERTIES OF TREES 39

where |E| = |E

1

| +|E

2

| + 1 and |V | = |V

1

| +|V

2

| (and possibly one of the E

i

is empty),

each of which is a tree satisfying the conditions of the induction hypothesis. Therefore,

|E| = |E

1

| +|E

2

| + 1 = |V

1

| −1 +|V

2

| −1 + 1 = |V | −1.

(2) =⇒ (3): If T = (V, E) has k connected components then it is a disjoint union of

trees T

i

= (V

i

, E

i

), i = 1, 2, . . . , k, for some k. Each of these satisfy, by (2),

|E

i

| = |V

i

| −1,

so

|E| =

k

i=1

|E

i

| =

k

i=1

|V

i

| −k = |V | −k.

This contradicts (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

= E−e. However, this means that |E

| = |E| −1 = |V | −1−1 = |V | −2,

which contradicts (3). Therefore e is a cut set.

(4) =⇒ (5): Let

P = (v

0

= u → v

1

→ v

2

→ · · · → v

k

= v)

and

P

= (v

0

= u → v

1

→ v

2

→ · · · → v

= v)

be two paths from u to v.

(5) =⇒ (6): Let e = (u, v) be a new edge connecting u, v ∈ V . Suppose that

P = (v

0

= w → v

1

→ v

2

→ · · · → v

k

= w)

and

P

= (v

0

= w → v

1

→ v

2

→ · · · → v

= w)

are two cycles in T ∪ ({u, v}, {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 = v

0

and let v = v

1

. The edge v

0

= w → v

1

is a u-v path and the sequence

v = v

1

→ v

2

→ · · · → v

k

= w = u taken in reverse order is another u-v path. This is a

contradiction to (5).

We may suppose now that P and P

**both contain e. Therefore, P contains a subpath
**

P

0

= 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 P

0

= P

**−e (which is not closed), that is the same
**

as P

**except it lacks the edge from u to v. By (5), these u-v paths p
**

0

and P

0

must be

the same. This forces P and P

**to be the same, which proves (6).
**

(6) =⇒ (1): Condition (6) 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, T

1

say, of T and v a vertex in another

component, T

2

say, of T. Adding the edge e = (u, v) does not create a cycle (if it did

then T

1

and T

2

would not be disjoint), which contradicts (6).

Exercise 3.4. Let G = (V

1

, E

2

) be a graph and T = (V

2

, E

2

) a spanning tree of G. Show

there is a one-to-one correspondence between fundamental cycles in G and edges not in

T.

40 CHAPTER 3. TREES AND FORESTS

Exercise 3.5. Let G = (V, E) be the 3 × 3 grid graph and T

1

= (V

1

, E

1

), T

2

= (V

2

, E

2

)

be spanning trees of G in Example 3.1. Find a fundamental cycle in G for T

1

which is

not a fundamental cycle in G for T

2

.

Exercise 3.6. Usually there exist many spanning trees of a graph. Can you classify

those graphs for which there is only one spanning tree? In other words, ﬁnd necessary

and suﬃcient conditions for a graph G such that if T is a spanning tree then T is unique.

3.2 Minimum spanning trees

Suppose you want to design an electronic circuit connecting several components. If these

components represent the vertices of the graph and a wire connecting two components

represents an edge of the graph then, for economical reasons, you will want to connect

these together using the least amount of wire. This amounts to ﬁnding a minimum

spanning tree in the complete graph on these vertices.

• spanning trees

We can characterize a spanning tree in several ways. Each of these conditions lead

to an algorithm for constructing them.

One condition is that spanning tree of a connected graph G can also be deﬁned as

a maximal set of edges of G that contains no cycle. Another condition is that it is

a minimal set of edges that connect all vertices.

Exploiting the former criteria gives rise to Kruskal’s algorithm. Exploiting the

latter criteria gives rise to Prim’s algorithm. Both of these argorithms are discussed

in more detail below.

• minimum-cost spanning trees

A minimum spanning tree (MST) is a spanning tree of an edge weighted graph

having lowest total weight among all possible spanning trees.

• Kruskal’s algorithm [18]; see also section 23.2 of Cormen et al. [11].

Kruskal’s algorithm is a greedy algorithm to compute a MST. It was discovered by

J. Kruskal in the 1950’s.

Kruskal’s algorithm can be shown to run in O(|E| log |E|) time.

• Prim’s algorithm [22]; see also section 23.2 of Cormen et al. [11].

Prim’s algorithm is a greedy algorithm to compute a MST. In can be implemented

in time O(|E| +|V | log |V |), which is O(n

2

) for a dense graph having n vertices.

The algorithm was developed in the 1930’s by Czech mathematician V. Jarn´ık and

later independently by both the computer scientists R. Prim and E. Dijkstra in the

1950’s.

• Bor˚uvka’s algorithm [8, 9]

Bor˚uvka’s algorithm is an algorithm for ﬁnding a minimum spanning tree in a

graph for which all edge weights are distinct. It was ﬁrst published in 1926 by

Otakar Borøuvka but then rediscovered by many others.

Bor˚uvka’s algorithm can be shown to run in time O(|E| log |V |).

3.2. MINIMUM SPANNING TREES 41

3.2.1 Kruskal’s algorithm

Kruskal’s algorithm starts with an edge-weighted digraph G = (V, E) as input. Let

w : E → R denote the weight function. The ﬁrst stage is to create a “skeleton” of the

tree T which is initially set to be a graph with no edges: T = (V, ∅). The next stage is

to sort the edges of G by weight. In other words, we label the edges of G as

E = {e

1

, e

2

, . . . , e

m

},

where w(e

1

) ≤ w(e

2

) ≤ · · · ≤ w(e

m

). Next, start a for loop over e ∈ E. You add e to

T as an edge provided it does not create a cycle. The only way adding e = (u, v) to T

would create a cycle would be if both u and v were endpoints of an edge already in T. As

long as this cycle condition fails, you add e to T and otherwise, go to the next element

of E in the for loop. At the end of the for loop, the edges of T have been completely

found and the algorithm stops.

SAGE

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]

42 CHAPTER 3. TREES AND FORESTS

if not(u in TV and v in TV):

T_edges.append([u,v])

# find adj mat of T

if G.weighted():

AG = G.weighted_adjacency_matrix()

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 are some examples.

We start with the grid graph. This is implemented in Sage in a way 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 assumes that the vertices are V = {0, 1, . . . , n −1}, we

ﬁrst redeﬁne the graph suitable and run the Kruskal algorithm on that.

sage: G = graphs.GridGraph([4,4])

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)]

The plot of this graph is given in Figure 3.2.1.

Figure 3.2: Kruskal’s algorithm for the 4 ×4 grid graph.

3.2.2 Prim’s algorithm

Prim’s algorithm is an algorithm that ﬁnds a minimum spanning tree for a connected

weighted undirected graph Γ = (V, E). It is very similar to Kruskal’s algorithm except

that it starts with an empty vertex set, rather than a full one.

3.2. MINIMUM SPANNING TREES 43

Input : A connected graph G = (V, E) having edge weights.

Output: A MST T for G.

Initialize: V (T) = {v

0

}, where v

0

is an arbitrary vertex, E(T) = ∅ 1

While V (T) = V : 2

Choose edge (u, v) with minimal weight such that u is in V (T) but v is not, 3

Add v to V (T), add (u, v) to E(T). 4

Algorithm 3.1: Prim’s algorithm.

SAGE

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.weighted_adjacency_matrix()

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)

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)

44 CHAPTER 3. TREES AND FORESTS

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)]

0

1

2

3

1

2

3

3

2

1

2

1

3 1

1

1

(a)

0

1

2

3

1

1

2

(b)

Figure 3.3: Prim’s algorithm for digraphs. Above is the original digraph and below is

the MST produced by Prim’s algorithm.

sage: A = matrix([[0,7,0,5,0,0,0],[0,0,8,9,7,0,0],[0,0,0,0,5,0,0],

[0,0,0,0,15,6,0],[0,0,0,0,0,8,9],[0,0,0,0,0,0,11],[0,0,0,0,0,0,0]])

3.2. MINIMUM SPANNING TREES 45

sage: G = Graph(A, format = "adjacency_matrix", weighted = True)

sage: E = G.edges(); E

[(0, 1, 7), (0, 3, 5), (1, 2, 8), (1, 3, 9), (1, 4, 7), (2, 4, 5),

(3, 4, 15), (3, 5, 6), (4, 5, 8), (4, 6, 9), (5, 6, 11)]

sage: prim(G).edges()

[(0, 1, 7), (0, 3, 5), (1, 2, 8), (1, 4, 7), (3, 5, 6), (4, 6, 9)]

7

5

8 9

7

5 15

6

8

9

11

(a)

6

4

1

2

0

3

5

9

7

8

7

5

6

(b)

Figure 3.4: Another example of Prim’s algorithm. On the left is the original graph. On

the right is the MST produced by Prim’s algorithm.

3.2.3 Bor˚uvka’s algorithm

Bor˚uvka’s algorithm algorithm is an algorithm for ﬁnding a minimum spanning tree in

a connected graph for which all edge weights are distinct.

Pseudocode for Bor˚uvka’s algorithm is:

• Begin with a connected graph G containing edges of distinct weights, and an empty

set of edges T

• While the vertices of G connected by T are disjoint:

– Begin with an empty set of edges E

– For each component:

∗ Begin with an empty set of edges S

∗ For each vertex in the component:

46 CHAPTER 3. TREES AND FORESTS

· Add the cheapest edge from the vertex in the component to another

vertex in a disjoint component to S

Add the cheapest edge in S to E

Add the resulting set of edges E to T.

The resulting set of edges T is the minimum spanning tree of G.

Example 3.7. In Figure 3.5 , we plot the following example.

sage: A = matrix([[0,1,2,5],[0,0,3,6],[0,0,0,4],[0,0,0,0]])

sage: G = Graph(A, format = "adjacency_matrix", weighted = True)

sage: boruvka(G)

0 1

2 3

1

2

5 3

6

4

(a)

3

2

0

1

4

2

1

(b)

Figure 3.5: An example of Borovka’s algorithm. On the left is the original graph. On

the right is the MST produced by Boruvka’s algorithm.

SAGE

def which_index(x,L):

"""

L is a list of sublists (or tuple of sets or list

of tuples, etc).

Returns the index of the first sublist which x belongs

to, or None if x is not in flatten(L).

The 0-th element in

Lx = [L.index(S) for S in L if x in S]

almost works, but if the list is empty then Lx[0]

throws an exception.

EXAMPLES:

sage: L = [[1,2,3],[4,5],[6,7,8]]

sage: which_index(3,L)

0

sage: which_index(4,L)

1

sage: which_index(7,L)

2

3.2. MINIMUM SPANNING TREES 47

sage: which_index(9,L)

sage: which_index(9,L) == None

True

"""

for S in L:

if x in S:

return L.index(S)

return None

def boruvka(G):

"""

Implements Boruvka’s algorithm to compute a MST of a graph.

INPUT:

G - a connected edge-weighted graph with distinct weights.

OUTPUT:

T - a minimum weight spanning tree.

REFERENCES:

http://en.wikipedia.org/wiki/Boruvka’s_algorithm

"""

T_vertices = [] # assumes G.vertices = range(n)

T_edges = []

T = Graph()

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 e in E:

# create about |V|/2 edges of T "cheaply"

TV = T.vertices()

if not(e[0] in TV) or not(e[1] in TV):

T.add_edge(e)

for e in E:

# connect the "cheapest" components to get T

C = T.connected_components_subgraphs()

VC = [S.vertices() for S in C]

if not(e in T.edges()) and (which_index(e[0],VC) != which_index(e[1],VC)):

if T.is_connected():

break

T.add_edge(e)

return T

Some examples using Sage:

sage: A = matrix([[0,1,2,3],[4,0,5,6],[7,8,0,9],[10,11,12,0]])

sage: G = DiGraph(A, format = "adjacency_matrix", weighted = True)

sage: boruvka(G)

Multi-graph on 4 vertices

sage: boruvka(G).edges()

[(0, 1, 1), (0, 2, 2), (0, 3, 3)]

sage: A = matrix([[0,2,0,5,0,0,0],[0,0,8,9,7,0,0],[0,0,0,0,1,0,0],\

[0,0,0,0,15,6,0],[0,0,0,0,0,3,4],[0,0,0,0,0,0,11],[0,0,0,0,0,0,0]])

sage: G = Graph(A, format = "adjacency_matrix", weighted = True)

sage: E = G.edges(); E

[(0, 1, 2), (0, 3, 5), (1, 2, 8), (1, 3, 9), (1, 4, 7),

(2, 4, 1), (3, 4, 15), (3, 5, 6), (4, 5, 3), (4,6, 4), (5, 6, 11)]

sage: boruvka(G)

Multi-graph on 7 vertices

48 CHAPTER 3. TREES AND FORESTS

sage: boruvka(G).edges()

[(0, 1, 2), (0, 3, 5), (2, 4, 1), (3, 5, 6), (4, 5, 3), (4, 6, 4)]

sage: A = matrix([[0,1,2,5],[0,0,3,6],[0,0,0,4],[0,0,0,0]])

sage: G = Graph(A, format = "adjacency_matrix", weighted = True)

sage: boruvka(G).edges()

[(0, 1, 1), (0, 2, 2), (2, 3, 4)]

sage: A = matrix([[0,1,5,0,4],[0,0,0,0,3],[0,0,0,2,0],[0,0,0,0,0],[0,0,0,0,0]])

sage: G = Graph(A, format = "adjacency_matrix", weighted = True)

sage: boruvka(G).edges()

[(0, 1, 1), (0, 2, 5), (1, 4, 3), (2, 3, 2)]

3.3 Binary trees

See section 3.3 of Gross and Yellen [14].

A binary tree is a rooted tree with at most 2 children per parent.

In this section, we consider

• binary codes,

• Gray codes, and

• Huﬀman codes.

3.3.1 Binary codes

What is a code?

A code is a rule for converting data in one format, or well-deﬁned tangible representation,

into sequences of symbols in another format (and the ﬁnite set of symbols used is called

the alphabet). We shall identify a code as a ﬁnite 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 {0, 1, . . . , 255}. If these numbers are written in binary then each

codeword of a letter has length 8. In this way, we can reformat, or encode, a “string”

into a sequence of binary symbols (i.e., 0’s and 1’s). 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.

Codes are used for

• Economy. Sometimes this is called “entropy encoding” since there is an entropy

function which describes how much information a channel (with a given error rate)

can carry and such codes are designed to maximize entropy as best as possible. In

this case, in addition to simply being given an alphabet A, one might be given a

“weighted alphabet,” i.e., an alphabet for which each symbol a ∈ A is associated

with a non-negative number w

a

≥ 0 (in practice, the probability that the symbol

a occurs in a typical word).

• Reliability. Such codes are called “error-correcting codes,” since such codes are

designed to communicate information over a noisy channel in such a way that the

errors in transmission are likely to be correctable.

3.3. BINARY TREES 49

• Security. Such codes ae called “cryptosystems.” In this case, the inverse of the

coding function c : A → B

∗

is designed to be computationally infeasible. In other

words, the coding function c is designed to be a “trapdoor function.”

Other codes are merely simpler ways to communicate information (ﬂag 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 deﬁnitions

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 preﬁx-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 preﬁx (start) of any other codeword

1

. This is the preﬁx-free condition.

One example of a preﬁx-free code is the ASCII code. Another example is

00, 01, 100.

On the other hand, a non-example is the code

00, 01, 010, 100

since the second codeword is a preﬁx of the third one. Another non-example is Morse

code recalled in Figure 3.6 (we use 0 for · (“dit”) and 1 for − (“dah”)).

A 01 N 10

B 1000 O 111

C 1010 P 0110

D 100 Q 1101

E 0 R 010

F 0010 S 000

G 110 T 1

H 0000 U 001

I 00 V 0001

J 0111 W 011

K 101 X 1001

L 0100 Y 1011

M 11 Z 1100

Figure 3.6: Morse code

For example, look at the Morse code for a and the Morse code for w. These codewords

violate the preﬁx-free condition.

1

In other words, a codeword s = s

1

. . . s

m

is a preﬁx of a codeword t = t

1

. . . t

n

if and only if m ≤ n

and s

1

= t

1

, . . . , s

m

= t

m

. Codes which are preﬁx-free are easier to decode than codes which are not

preﬁx-free.

50 CHAPTER 3. TREES AND FORESTS

Gray codes

History

2

: Frank Gray (1887-1969) wrote about the so-called Gray codes in a 1951 paper

published in the Bell System Technical Journal, and then patented a device (used for

television sets) based on it in 1953. 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 E. Baudot’s (a French engineer) telegraph machine of 1878 and in a French booklet

by L. Gros on the solution to the “Chinese ring puzzle” published in 1872.

The term “Gray code” is ambiguous. It is actually a large family of sequences of

n-tuples. Let Z

m

= {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 (namely, N = m

n

)

n-tuples

g

1

, g

2

, . . . , g

N

,

where

• each g

i

∈ Z

n

m

,

• g

i

and g

i+1

diﬀer by 1 in exactly one coordinate.

In other words, an m-ary Gray code of length n is a particular way to order the set of

all m

n

n-tuples whose coordinates are taken from Z

m

. From the transmission/commu-

nication perspective, this sequence has two advantages:

• It is easy and fast to produce the sequence, since successive entries diﬀer in only

one coordinate.

• An error is relatively easy to detect, since you can compare an n-tuple with the

previous one. If they difer in more than one coordinate, you know an error was

made.

Example 3.8. Here is a 3-ary Gray code of length 2:

[0, 0], [1, 0], [2, 0], [2, 1], [1, 1], [0, 1], [0, 2], [1, 2], [2, 2]

and here is a binary Gray code of length 3:

[0, 0, 0], [1, 0, 0], [1, 1, 0], [0, 1, 0], [0, 1, 1], [1, 1, 1], [1, 0, 1], [0, 0, 1].

Gray codes have applications to engineering, recreational mathematics (solving the

Tower of Hanoi puzzle, “The Brain” puzzle, the “Chinese ring puzzle”, and others), and

to mathematics (for example, aspects of combinatorics, computational group theory and

the computational aspects of linear codes).

2

This history comes from an unpublished section 7.2.1.1 (“Generating all n-tuples”) in volume 4 of

Donald Knuth’s The Art of Computer Programming.

3.3. BINARY TREES 51

Binary Gray codes

Consider the so-called n-hypercube graph Q

n

. This can be envisioned as the graph whose

vertices are the vertices of a cube in n-space

{(x

1

, . . . , x

n

) | 0 ≤ x

i

≤ 1},

and whose edges are those line segments in R

n

connecting two “neighboring” vertices

(namely, two vertices which diﬀer in exactly one coordinate). A binary Gray code of

length n can be regarded as a path on the hypercube graph Q

n

which visits each vertex

of the cube exactly once. In other words, a binary Gray code of length n may be identiﬁed

with a Hamiltonian cycle on the graph Q

n

(see Figure 3.7 for an example).

Figure 3.7: Viewing Γ

3

as a Hamiltonian path on Q

3

.

How do you eﬃciently compute a Gray code?

Perhaps the simplest way to state the idea of quickly constructing the reﬂected binary

Gray code Γ

n

of length n is as follows:

Γ

0

= [], Γ

n

= [0, Γ

n−1

], [1, Γ

rev

n−1

],

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],

and so on. This is a nice procedure if you want to create the entire list at once (which,

by the way, gets very long very fast).

An implementation of the reﬂected Gray code using Python is given below.

Python 3.0

def graycode(length,modulus):

"""

Returns the n-tuple reflected Gray code mod m.

EXAMPLES:

sage: graycode(2,4)

[[0, 0],

52 CHAPTER 3. TREES AND FORESTS

[1, 0],

[2, 0],

[3, 0],

[3, 1],

[2, 1],

[1, 1],

[0, 1],

[0, 2],

[1, 2],

[2, 2],

[3, 2],

[3, 3],

[2, 3],

[1, 3],

[0, 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

Consider the reﬂected binary code of length 8, Γ

8

. This has 2

8

= 256 codewords.

SAGE can easily create the list plot of the coordinates (x, y), where x is an integer j ∈ Z

256

which 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 ??.

What if you only want to compute the i-th Gray codeword in the Gray code of length

n? Can it be computed quickly as well without computing the entire list? At least in the

case of the reﬂected binary Gray code, there is a very simple way to do this. The k-th

element in the above-described reﬂected binary Gray code of length n is obtained by

simply adding the binary representation of k to the binary representation of the integer

part of k/2.

An example using SAGE is given below.

SAGE

def int2binary(m, n):

’’’

returns GF(2) vector of length n obtained

from the binary repr of m, padded by 0’s

(on the left) to length n.

EXAMPLES:

sage: for j in range(8):

....: print int2binary(j,3)+int2binary(int(j/2),3)

3.3. BINARY TREES 53

Figure 3.8: List plot of Γ

8

created using Sage.

....:

(0, 0, 0)

(0, 0, 1)

(0, 1, 1)

(0, 1, 0)

(1, 1, 0)

(1, 1, 1)

(1, 0, 1)

(1, 0, 0)

’’’

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):

’’’

returns the kth codeword in the reflected binary Gray code

of length n.

EXAMPLES:

sage: graycodeword(3,3)

(0, 1, 0)

’’’

return int2binary(m,n)+int2binary(int(m/2),n)

Exercise 3.9. Convert the above function graycodeword into a pure Python function.

3.3.2 Huﬀman codes and Huﬀman’s algorithm

An alphabet A is a ﬁnite set, whose elements are referred to as symbols.

A word (or string or message) in A is a ﬁnite sequence of symbols in A, usually written

by simply concatenating them together: a

1

a

2

. . . a

k

(a

i

∈ A) is a message of length k.

A commonly occurring alphabet in practice is the binary alphabet {0, 1}, in which case

a word is simply a ﬁnite sequence of 0’s and 1’s. If A is an alphabet, let

54 CHAPTER 3. TREES AND FORESTS

A

∗

denote the set of all words in A. The length of a word is denoted by vertical bars: if

w = a

1

. . . a

k

is a word in A then deﬁne | . . . | : A

∗

→ R by

|a

1

. . . a

k

| = 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.

Tree representation

Any binary code can be represented by a tree.

Example 3.10. Here is how to represent the code B

**consisting of all binary strings of
**

length ≤ . Start with the “root node” v

∅

being the empty string. The two children of

this node, v

0

and v

1

, correspond to the two strings of length 1. Label v

0

with a “0” and

v

1

with a “1.” The two children of v

0

, v

00

and v

01

, correspond to the strings of length 2

which start with a 0, and the two children of v

1

, v

10

and v

11

, correspond to the strings

of length 2 which start with a 1. Continue creating child nodes until you reach length

then stop. There are a total of 2

+1

− 1 nodes in this tree and 2

**of them are “leaves”
**

(vertices of a tree with degree 1, i.e., childless nodes). Note that the parent of any node

is a preﬁx to that node. Label each node v

s

with the string “s,” where s is a binary

sequence of length ≤ .

See Figure 3.9 for an example when = 2.

0

•

•

d

d

d

d

d

d

d

d

•

1

d

d

d

d

•

11

•

10

d

d

d

d

•

01

•

00

Figure 3.9: Example of a tree representation of a binary code

In general, if C is a code contained in B

**then to create the tree for C, start with
**

the tree for B

**. First, remove all nodes associated to a binary string for which it and all
**

of its descendents are not in C. Next, remove all labels which do not correspond with

codewords in C. The resulting labeled graph is the tree associated to the binary code C.

3.3. BINARY TREES 55

For “visualizing” the construction of Huﬀman codes later, it is important to see that

one can reverse this construction to start from such a binary tree and recover a binary

code from it. (The codewords are determined by the following rules

• The root node gets the empty codeword.

• Each left-ward branch gets a 0 appended to the end of its parent and each right-

ward branch gets a 1 appended to the end.

Uniquely decodable codes

If c : A → B

∗

is a code then we can extend c to A

∗

by concatenation:

c(a

1

a

2

. . . a

k

) = c(a

1

)c(a

2

) . . . c(a

k

).

If the extension c : A

∗

→ T

∗

is also an injection then c is called uniquely decodable.

Example 3.11. Recall the Morse code in Table ??. Note these Morse codewords all

have length less than or equal to 4. Other commonly occurring symbols used (the digits

0 through 9, punctuation symbols, and some others), are also encodable in Morse code

but they use longer codewords.

Let A denote the English alphabet, B = {0, 1} the binary alphabet and C : A → B

∗

the Morse code. Since c(ET) = 01 = c(A), it is clear that the Morse code is not uniquely

decodable.

Exercise 3.12. Show by giving an example that the Morse code is not preﬁx-free.

In fact, preﬁx-free implies uniquely decodable.

Theorem 3.13. If a code c : A → B

∗

is preﬁx-free then it is uniquely decodable.

Solution. The proof is by induction on the length of a message. We want to show that

if x

1

. . . x

k

and y

1

. . . y

**are messages with c(x
**

1

) . . . c(x

k

) = c(y

1

) . . . c(y

) then x

1

. . . x

k

=

y

1

. . . y

**(which in turn implies k = and x
**

i

= y

i

for all i).

The case of length 1 follows from the fact that c : A → B

∗

is injective (by the

deﬁnition of a code).

Suppose that the statement of the theorem holds for all codes of length < m. We

must show that the length m case is true. Suppose c(x

1

) . . . c(x

k

) = c(y

1

) . . . c(y

) where

m = max(k, ). These strings are equal, so the substring c(x

1

) of the left-hand side and

the substring c(y

1

) of the right-hand side are either equal or one is contained in the other.

If (for instance) c(x

1

) is properly contained in c(y

1

) then c is not preﬁx-free. Likewise,

if c(y

1

) is properly contained in c(x

1

). Therefore, c(x

1

) = c(y

1

). This implies x

1

= y

1

.

Now remove this codeword from both sides, so c(x

2

) . . . c(x

k

) = c(y

2

) . . . c(y

). By the

induction hypothesis, x

2

. . . x

k

= y

2

. . . y

**. These facts together imply k = and x
**

i

= y

i

for all i.

Consider now a weighted alphabet (A, p), where p : A → [0, 1] satisﬁes

a∈A

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 an typical message.

The average word length L(c) is

3

:

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.

56 CHAPTER 3. TREES AND FORESTS

L(c) =

a∈A

p(a) · |c(a)|,

where | . . . | denotes 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.

Lemma 3.14. Suppose c : A → B

∗

is a binary optimal preﬁx-free code and let =

max

a∈A

|c(a)| denote the maximum length of a codeword. The following statements

hold.

• If |c(a

)| > |c(a)| then p(a

) ≤ p(a).

• The subset of codewords of length ,

C

= {c ∈ c(A) | |c(a)| = },

contains two codewords of the form b0 and b1, for some b ∈ B

∗

.

For the proof (which is very easy and highly recommended for the student who is

curious to see more), see Biggs§3.6, [6].

The Huﬀman code construction is based on the amazing second property in the above

lemma yields an optimal preﬁx-free binary code.

Huﬀman code construction: Here is the recursive/inductive construction. We shall

regard the binary Huﬀman 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 preﬁx-free binary code for any weighted alphabet (A

, p

) having

< n symbols.

Huﬀman’s 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.
**

Huﬀman’s 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.

These two rules tell us how to inductively built the tree representation for the Huﬀman

code of (A, p) up from its leaves (associated to the low weight symbols).

• Find two diﬀerent symbols of lowest weight, a and a

**. If two such symbols don’t
**

exist, stop. Replace the weighted alphabet with the new weighted alphabet as in

Huﬀman’s rule 1.

• Add two nodes (labeled with a and a

**, resp.) to the tree, with parent a
**

∗

(see

Huﬀman’s rule 1).

• If there are no remaining symbols in A, label the parent a

∗

with the empty set and

stop. Otherwise, go to the ﬁrst step.

An example of this is below.

3.4. APPLICATIONS TO COMPUTER SCIENCE 57

a

0

•

•

d

d

d

d

•

1

d

d

d

d

•

11

c

•

10

b

Figure 3.10: Huﬀman code example

Example 3.15. A very simple of this makes Huﬀman’s ingenious construction easier to

understand.

Suppse A = {a, b, c} and p(a) = 0.5, p(b) = 0.3, p(c) = 0.2.

A Huﬀman code for this is C = {0, 10, 11}, as is depicted in Figure 3.10.

Exercise 3.16. Verify that C = {1, 00, 01} is another Huﬀman code for this weighted

alphabet and to draw its tree representation.

Exercise 3.17. Find the Huﬀman code for the letters of the English alphabet weighted

by the frequency of common American usage

4

.

3.4 Applications to computer science

3.4.1 Tree traversals

See section 3.5 of Gross and Yellen [14]. See also http://en.wikipedia.org/wiki/

Tree_traversal.

• stacks and queues

• breadth-ﬁrst, or level-order, traversal

• depth-ﬁrst, or pre-order, traversal

• post-order traversal

• symmetric, or in-order, traversal

In computer science, tree traversal refers to the process of examining each node in a

tree data structure exactly once. We restrict our discussion to binary rooted trees.

Starting at the root of a binary tree, there are three main steps that can be performed

and the order in which they are performed deﬁnes the traversal type.

Depth-ﬁrst traversal:

• Visit the root vertex.

4

You can ﬁnd this on the internet or in the literature. Part of this exercise is ﬁnding this frequency

distribution yourself.

58 CHAPTER 3. TREES AND FORESTS

• Traverse the left subtree recursively.

• Traverse the right subtree recursively.

Breadth-ﬁrst traversal:

• Initialize i = 0 and set N equal to the maximum depth of the tree (i.e., the

maximum distance from the root vertex to any other vertex in the tree).

• Visit the vertices of depth i.

• Increment i = i + 1. If i > N then stop. Otherwise, go to the previous step.

post-order traversal:

• Traverse the left subtree recursively.

• Visit the root vertex.

• Traverse the right subtree recursively.

symmetric traversal:

• Traverse the left subtree recursively.

• Visit the root vertex.

• Traverse the right subtree recursively.

3.4.2 Binary search trees

See section 3.6 of Gross and Yellen [14], and chapter 12 of Cormen et al. [11]. See also

http://en.wikipedia.org/wiki/Binary_search_tree.

• records and keys

• searching a binary search tree (BST)

• inserting into a BST

• deleting from a BST

• traversing a BST

• sorting using BST

A binary search tree (BST) is a rooted binary tree T = (V, E) having weighted vertices

wt : V → R satisfying:

• The left subtree of a vertex v contains only vertices whose label (or “key”) is less

than the label of v.

• The right subtree of a vertex v contains only vertices whose label is greater than

the label of v.

• Both the left and right subtrees must also be binary search trees.

From the above properties it naturally follows that: Each vertex has a distinct label.

Generally, the information represented by each vertex is a record (or list or dictio-

nary), rather than a single data element. However, for sequencing purposes, vertices are

compared according to their labels rather than any part of their associated records.

3.4. APPLICATIONS TO COMPUTER SCIENCE 59

Traversal

The vertices of a BST T can be visited retrieved in-order of the weights of the vertices

(i.e., using a symmetric search type) by recursively traversing the left subtree of the root

vertex, then accessing the root vertex itself, then recursively traversing the right subtree

of the root node.

Searching

We are given a BST (i.e., a binary rooted tree with weighted vertices having distinct

weights satisfying the above criteria) T and a label . For this search, we are looking for

a vertex in T whose label is , if one exists.

We begin by examining the root vertex, v

0

. If = wt(v

0

), the search is successful.

If the < wt(v

0

), search the left subtree. Similarly, if > wt(v

0

), search the right

subtree. This process is repeated until a vertex v ∈ V is found for which = wt(v), or

the indicated subtree is empty.

Insertion

We are given a BST (i.e., a binary rooted tree with weighted vertices having distinct

weights satisfying the above criteria) T and a label . We assume is between the

lowest weight of T and the highest weight. For this procedure, we are looking for a

“parent” vertex in T which can “adopt” a new vertex v having weight and for which

this augmented tree T ∪ v satisﬁes the criteria above.

Insertion proceeds as a search does. However, in this case, you are searching for

vertices v

1

, v

2

∈ V for which wt(v

1

) < < wt(v

2

). Once found, these vertices will tell

you where to insert v.

Deletion

As above, we are given a BST T and a label . We assume is between the lowest weight

of T and the highest weight. For this procedure, we are looking for a vertex v of T which

has weight . We want to remove v from T (and therefore also the weight from the list

of weights), thereby creating a “smaller” tree T −v satisfying the criteria above.

Deletion proceeds as a search does. However, in this case, you are searching for vertix

v ∈ V for which wt(v) = . Once found, we remove v from V and any edge (u, v) ∈ E is

replaced by (u, w

1

) and (u, w

2

), where w

1

.w

2

∈ V were the children of v in T.

Sorting

A binary search tree can be used to implement a simple but eﬃcient sorting algorithm.

Suppose we wish to sort a list of numbers L = [

1

,

2

, . . . ,

n

]. First, let V = {1, 2, . . . , n}

be the vertices of a tree and weight vertex i with

i

, for 1 ≤ i ≤ n. In this case, we can

traverse this tree in order of its weights, thereby building a BST recursively. This BST

represents the sorting of the list L.

60 CHAPTER 3. TREES AND FORESTS

Chapter 4

Distance and Connectivity

4.1 Paths and distance

• distance and metrics

• distance matrix

• eccentricity, center, radius, diameter

• trees: distance, center, centroid

• distance in self-complementary graphs

4.2 Vertex and edge connectivity

• vertex-cut and cut-vertex

• cut-edge or bridge

• vertex and edge connectivity

Theorem 4.1. Menger’s Theorem. Let u and v be distinct, non-adjacent vertices in

a graph G. Then the maximum number of internally disjoint u-v paths in G equals the

minimum number of vertices needed to separate u and v.

Theorem 4.2. Whitney’s 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.

4.3 Centrality of a vertex

• degree centrality

• betweenness centrality

• closeness centrality

• eigenvector centrality

61

62 CHAPTER 4. DISTANCE AND CONNECTIVITY

4.4 Network reliability

• Whitney synthesis

• Tutte’s synthesis of 3-connected graphs

• Harary graphs

• constructing an optimal k-connected n-vertex graph

Chapter 5

Optimal Graph Traversals

5.1 Eulerian graphs

• multigraphs and simple graphs

• Eulerian tours

• Eulerian trails

5.2 Hamiltonian graphs

• hamiltonian paths (or cycles)

• hamiltonian graphs

Theorem 5.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 5.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.

5.3 The Chinese Postman Problem

See section 6.2 of Gross and Yellen [14].

• de Bruijn sequences

• de Bruijn digraphs

• constructing a (2, n)-de Bruijn sequence

• postman tours and optimal postman tours

• constructing an optimal postman tour

63

64 CHAPTER 5. OPTIMAL GRAPH TRAVERSALS

5.4 The Traveling Salesman Problem

See section 6.4 of Gross and Yellen [14], and section 35.2 of Cormen et al. [11].

• Gray codes and n-dimensional hypercubes

• the Traveling Salesman Problem (TSP)

• nearest neighbor heuristic for TSP

• some other heuristics for solving TSP

Chapter 6

Planar Graphs

See chapter 9 of Gross and Yellen [14].

6.1 Planarity and Euler’s Formula

• planarity, non-planarity, planar and plane graphs

• crossing numbers

Theorem 6.1. The complete bipartite graph K

3,n

is non-planar for n ≥ 3.

Theorem 6.2. Euler’s Formula. Let G be a connected plane graph having n vertices,

e edges and f faces. Then n −e +f = 2.

6.2 Kuratowski’s Theorem

• Kuratowski graphs

The objective of this section is to prove the following theorem.

Theorem 6.3. Kuratowski’s Theorem. A graph is planar if and only if it contains

no subgraph homeomorphic to K

5

or K

3,3

.

6.3 Planarity algorithms

• planarity testing for 2-connected graphs

• planarity testing algorithm of Hopcroft and Tarjan [15]

• planarity testing algorithm of Boyer and Myrvold [10]

65

66 CHAPTER 6. PLANAR GRAPHS

Chapter 7

Graph Coloring

7.1 Vertex coloring

• vertex coloring

• chromatic numbers

• algorithm for sequential vertex coloring

• Brook’s Theorem

• heuristics for vertex coloring

7.2 Edge coloring

• edge coloring

• edge chromatic numbers

• chromatic incidence

• algorithm for edge coloring by maximum matching

• algorithm for sequential edge coloring

• Vizing’s Theorem

7.3 Applications of graph coloring

• assignment problems

• scheduling problems

• matching problems

• map coloring and the Four Color Problem

67

68 CHAPTER 7. GRAPH COLORING

Chapter 8

Network Flows

See Jungnickel [17], and chapter 12 of Gross and Yellen [14].

8.1 Flows and cuts

• single source-single sink networks

• feasible networks

• maximum ﬂow and minimum cut

8.2 Ford and Fulkerson’s theorem

The objective of this section is to prove the following theorem.

Theorem 8.1. Maximum ﬂow-minimum cut theorem. For a given network, the

value of a maximum ﬂow is equal to the capacity of a minimum cut.

8.3 Edmonds and Karp’s algorithm

The objective of this section is to prove Edmond and Karp’s algorithm for the maximum

ﬂow-minimum cut problem with polynomial complexity.

8.4 Goldberg and Tarjan’s algorithm

The objective of this section is to prove Goldberg and Tarjan’s algorithm for ﬁnding

maximal ﬂows with polynomial complexity.

69

70 CHAPTER 8. NETWORK FLOWS

Chapter 9

Random Graphs

See Bollob´as [7].

9.1 Erd¨os-R´enyi graphs

Describe the random graph model of Erd¨ os and R´enyi [13]. Algorithms for eﬃcient

generation of random networks; see Batagelj and Brandes [3].

9.2 Small-world networks

The small-world network model of Watts and Strogatz [24]. The economic small-world

model of Latora and Marchiori [19]. See also Milgram [20], Newman [21], and Albert

and Barab´asi [1].

9.3 Scale-free networks

The power-law degree distribution model of Barab´asi and Albert [2]. See also New-

man [21], and Albert and Barab´ asi [1].

9.4 Evolving networks

Preferential attachment models. See Newman [21], and Albert and Barab´ asi [1].

71

72 CHAPTER 9. RANDOM GRAPHS

Chapter 10

Graph Problems and Their LP

Formulations

This document is meant as an explanation of several graph theoretical functions deﬁned

in Sage’s Graph Library (http://www.sagemath.org/), which use Linear Programming

to solve optimization of existence problems.

10.1 Maximum average degree

The average degree of a graph G is deﬁned as ad(G) =

2|E(G)|

|V (G)|

. The maximum average

degree of G is meant to represent its densest part, and is formally deﬁned as :

mad(G) = max

H⊆G

ad(H)

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

ﬂow problem deﬁned on a bipartite graph. Let D be a directed graph whose vertex set

we ﬁrst deﬁne 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 e’s endpoints. Each edge will then have a

ﬂow 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 ﬂow 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 ﬂow

of 2|E(H)| to their |V (H)| vertices, such a ﬂow being feasible only if z ≥

2|E(H)|

|V (H)|

. An

elementary application of the max-ﬂow/min-cut theorem, or of Hall’s bipartite matching

theorem shows that such a value for z is also suﬃcient. This LP can thus let us compute

the Maximum Average Degree of the graph.

LP Formulation :

• Mimimize : z

• Such that :

– a vertex can absorb at most z

∀v ∈ V (G),

e∈E(G)

e∼v

x

e,v

≤ z

73

74 CHAPTER 10. GRAPH PROBLEMS AND THEIR LP FORMULATIONS

– each edge sends a ﬂow of 2

∀e = uv ∈ E(G), x

e,u

+x

e,u

= 2

• x

e,v

real positive variable

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

that z ≤ 2 −

2

|V (G)|

. This is a handy way to write in LP the constraint that “the set of

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.

corresponding patch :

http://trac.sagemath.org/sage_trac/ticket/7529

10.2. TRAVELING SALESMAN PROBLEM 75

10.2 Traveling Salesman Problem

Given a graph G whose edges are weighted by a function w : E(G) → R, a solution to

the TSP is a hamiltonian (spanning) cycle whose weight (the sum of the weight of its

edges) is minimal. It is easy to deﬁne both the objective and the constraint that each

vertex must have exactly two neighbors, but this could produce solutions such that the

set of edges deﬁne the disjoint union of several cycles. One way to formulate this linear

program is hence to add the constraint that, given an arbitrary vertex v, the set S of

edges in the solution must contain no cycle in G − v, which amounts to checking that

the set of edges in S no adjacent to v is of maximal average degree strictly less than 2,

using the remark from section 10.1.

We will then, in this case, deﬁne variables representing the edges included in the

solution, along with variables representing the weight that each of these edges will send

to their endpoints.

LP Formulation :

• Mimimize

e∈E(G)

w(e)b

e

• Such that :

– Each vertex is of degree 2

∀v ∈ V (G),

e∈E(G)

e∼v

b

e

= 2

– No cycle disjoint from a special vertex v

∗

∗ Each edge sends a ﬂow of 2 if it is taken

∀e = uv ∈ E(G−v

∗

), x

e,u

+x

e,v

= 2b

e

∗ Vertices receive strictly less than 2

∀v ∈ V (G−v

∗

),

e∈E(G)

e∼v

x

e,v

≤ 2 −

2

|V (G)|

• Variables

– x

e,v

real positive variable (ﬂow sent by the edge)

– b

e

binary (is the edge in the solution ?)

corresponding patch :

http://trac.sagemath.org/sage_trac/ticket/7529

76 CHAPTER 10. GRAPH PROBLEMS AND THEIR LP FORMULATIONS

10.3 Edge-disjoint spanning trees

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 ﬁnding, given a graph G and an integer k, edge-disjoint

spanning trees T

1

, . . . , T

k

which are subgraphs of G. In this case, we will chose to deﬁne

a spanning tree as an acyclic set of |V (G)| −1 edges.

LP Formulation :

• Maximize : nothing

• Such that :

– An edge belongs to at most one set

∀e ∈ E(G),

i∈[1,...,k]

b

e,k

≤ 1

– Each set contains |V (G)| −1 edges

∀i ∈ [1, . . . , k],

e∈E(G)

b

e,k

= |V (G)| −1

– No cycles

∗ In each set, each edge sends a ﬂow of 2 if it is taken

∀i ∈ [1, . . . , k], ∀e = uv ∈ E(G), x

e,k,u

+x

e,k,u

= 2b

e,k

∗ Vertices receive strictly less than 2

∀i ∈ [1, . . . , k], ∀v ∈ V (G),

e∈E(G)

e∼v

x

e,k,v

≤ 2 −

2

|V (G)|

• Variables

– b

e,k

binary (is edge e in set k ?)

– x

e,k,u

positive real (ﬂow sent by edge e to vertex u in set k)

corresponding patch :

http://trac.sagemath.org/sage_trac/ticket/7476

10.4. STEINER TREE 77

10.4 Steiner tree

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 ﬁnd 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

LP Formulation :

• Minimize :

e∈E(G)

w(e)b

e

• Such that :

– Each vertex from S is in the tree

∀v ∈ S,

e∈E(G)

e∼v

b

e

≥ 1

– c is equal to 1 when a vertex v is in the tree

∀v ∈ V (G), ∀e ∈ E(G), e ∼ v, b

e

≤ c

v

– The tree contains |V (H)| vertices and |E(H)| = |V (H)| −1 edges

v∈G

c

v

−

e∈E(G)

b

e

= 1

– No Cycles

∗ Each edge sends a ﬂow of 2 if it is taken

∀e = uv ∈ E(G), x

e,u

+x

e,u

= 2b

e,k

∗ Vertices receive strictly less than 2

∀v ∈ V (G),

e∈E(G)

e∼v

x

e,v

≤ 2 −

2

|V (G)|

• Variables :

– b

e

binary (is e in the tree ?)

– c

v

binary (does the tree contain v ?)

– x

e,v

real positive variable (ﬂow sent by the edge)

corresponding patch :

http://trac.sagemath.org/sage_trac/ticket/8403

78 CHAPTER 10. GRAPH PROBLEMS AND THEIR LP FORMULATIONS

10.5 Linear arboricity

The linear arboricity of a graph G is the least number k such that the edges of G can

be partitionned 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 :

– An edge belongs to exactly one set

∀e ∈ E(G),

i∈[1,...,k]

b

e,k

= 1

– Each class has maximal degree 2

∀v ∈ V (G), ∀i ∈ [1, . . . , k],

e∈E(G)

e∼v

b

e,k

≤ 2

– No cycles

∗ In each set, each edge sends a ﬂow of 2 if it is taken

∀i ∈ [1, . . . , k], ∀e = uv ∈ E(G), x

e,k,u

+x

e,k,v

= 2b

e,k

∗ Vertices receive strictly less than 2

∀i ∈ [1, . . . , k], ∀v ∈ V (G),

e∈E(G)

e∼v

x

e,k,v

≤ 2 −

2

|V (G)|

• Variables

– b

e,k

binary (is edge e in set k ?)

– x

e,k,u

positive real (ﬂow sent by edge e to vertex u in set k)

10.6 Acyclic edge coloring

An edge coloring with k colors is said to be acyclic if it is proper (each color class is a

matching – maximal degree 1), and if the union of the edges of any two color classes is

acyclic. The corresponding LP is almost a copy of the previous one, except that we need

to ensure that

_

k

2

_

diﬀerent classes are acyclic.

corresponding patch :

http://trac.sagemath.org/sage_trac/ticket/8405

10.7. H-MINOR 79

10.7 H-minor

For more information on minor theory, please see

http://en.wikipedia.org/wiki/Minor_%28graph_theory%29

It is a wonderful subject, and I do not want to begin talking about it when I know I

couldn’t freely ﬁll pages with remarks :-)

For our purposes, we will just say that ﬁnding a minor H in a graph G, consists in :

1. Associating to each vertex h ∈ H a set S

h

of representants in H, diﬀerent vertices

h having disjoints representative sets

2. Ensuring that each of these sets is connected (can be contracted)

3. If there is an edge between h

1

and h

2

in H, there must be an edge between the

corresponding representative sets

Here is how we will address these constraints :

1. Easy

2. For any h, we can ﬁnd a spanning tree in S

h

(an acyclic set of |S

h

| −1 edges)

3. This one is very costly.

To each directed edge g

1

g

2

(I consider g

1

g

2

and g

2

g

1

as diﬀerent) and every edge

h

1

h

2

is associated a binary variable which can be equal to one only if g

1

represents

h

1

and g

2

represents g

2

. We then sum all these variables to be sure there is at least

one edge from one set to the other.

80 CHAPTER 10. GRAPH PROBLEMS AND THEIR LP FORMULATIONS

LP Formulation :

• Maximize : nothing

• Such that :

– A vertex g ∈ V (G) can represent at most one vertex h ∈ V (H)

∀g ∈ V (G),

h∈V (H)

rs

h,g

≤ 1

– An edge e can only belong to the tree of h if both its endpoints represent h

∀e = g

1

g

2

∈ E(G), t

e,h

≤ rs

h,g

1

and t

e,h

≤ rs

h,g

2

– In each representative set, the number of vertices is one more than the number

of edges in the corresponding tree

∀h,

g∈V (G)

rs

h,g

−

e∈E(G)

t

e,h

= 1

– No cycles in the union of the spanning trees

∗ Each edge sends a ﬂow of 2 if it is taken

∀e = uv ∈ E(G), x

e,u

+x

e,v

= 2

h∈V (H)

t

e,h

∗ Vertices receive strictly less than 2

∀v ∈ V (G),

e∈E(G)

e∼v

x

e,k,v

≤ 2 −

2

|V (G)|

– arc

(g

1

,g

2

),(h

1

,h

2

)

can only be equal to 1 if g

1

g

2

is leaving the representative set

of h

1

to enter the one of h

2

. (note that this constraints has to be written both

for g

1

, g

2

, and then for g

2

, g

1

)

∀g

1

, g

2

∈ V (G), g

1

= g

2

, ∀h

1

h

2

∈ E(H)

arc

(g

1

,g

2

),(h

1

,h

2

)

≤ rs

h

1

,g

1

and arc

(g

1

,g

2

),(h

1

,h

2

)

≤ rs

h

2

,g

2

– We have the necessary edges between the representative sets

∀h

1

h

2

∈ E(H)

∀g

1

,g

2

∈V (G),g

1

=g

2

arc

(g

1

,g

2

),(h

1

,h

2

)

≥ 1

• Variables

– rs

h,g

binary (does g represent h ? rs = “representative set”)

– t

e,h

binary (does e belong to the spanning tree of the set representing h ?)

– x

e,v

real positive (ﬂow sent from edge e to vertex v)

– arc

(g

1

,g

2

),(h

1

,h

2

)

binary (is edge g

1

g

2

leaving the representative set of h

1

to enter

the one of h

2

?)

corresponding patch : http://trac.sagemath.org/sage_trac/ticket/8404

Appendix A

GNU Free Documentation License

Version 1.3, 3 November 2008

Copyright c 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 eﬀective

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 modiﬁcations

made by others.

This License is a kind of “copyleft”, which means that derivative works of the doc-

ument 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, be-

cause 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.

1. APPLICABILITY AND DEFINITIONS

This License applies to any manual or other work, in any medium, that contains a

notice placed by the copyright holder saying it can be distributed under the terms of this

License. Such a notice grants a world-wide, royalty-free license, unlimited in duration,

to use that work under the conditions stated herein. The “Document”, below, refers

to any such manual or work. Any member of the public is a licensee, and is addressed

as “you”. You accept the license if you copy, modify or distribute the work in a way

requiring permission under copyright law.

81

82 APPENDIX A. GNU FREE DOCUMENTATION LICENSE

A “Modiﬁed Version” of the Document means any work containing the Document

or a portion of it, either copied verbatim, or with modiﬁcations and/or translated into

another language.

A “Secondary Section” is a named appendix or a front-matter section of the Doc-

ument that deals exclusively with the relationship of the publishers or authors of the

Document to the Document’s overall subject (or to related matters) and contains noth-

ing 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 desig-

nated, as being those of Invariant Sections, in the notice that says that the Document is

released under this License. If a section does not ﬁt the above deﬁnition 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, repre-

sented in a format whose speciﬁcation is available to the general public, that is suitable

for revising the document straightforwardly with generic text editors or (for images com-

posed 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 other-

wise Transparent ﬁle format whose markup, or absence of markup, has been arranged to

thwart or discourage subsequent modiﬁcation 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 modiﬁcation. 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 work’s 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

83

in another language. (Here XYZ stands for a speciﬁc 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 deﬁnition.

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 eﬀect

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 Document’s 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 ﬁt legibly, you should put

the ﬁrst ones listed (as many as ﬁt 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.

84 APPENDIX A. GNU FREE DOCUMENTATION LICENSE

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 Modiﬁed Version of the Document under the condi-

tions of sections 2 and 3 above, provided that you release the Modiﬁed Version under

precisely this License, with the Modiﬁed Version ﬁlling the role of the Document, thus

licensing distribution and modiﬁcation of the Modiﬁed Version to whoever possesses a

copy of it. In addition, you must do these things in the Modiﬁed 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 modiﬁcations in the Modiﬁed Version, together with at least ﬁve

of the principal authors of the Document (all of its principal authors, if it has fewer

than ﬁve), unless they release you from this requirement.

C. State on the Title page the name of the publisher of the Modiﬁed Version, as the

publisher.

D. Preserve all the copyright notices of the Document.

E. Add an appropriate copyright notice for your modiﬁcations adjacent to the other

copyright notices.

F. Include, immediately after the copyright notices, a license notice giving the public

permission to use the Modiﬁed 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 Document’s 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 Modiﬁed 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 Modiﬁed 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.

85

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 Modiﬁed Version.

N. Do not retitle any existing section to be Entitled “Endorsements” or to conﬂict in

title with any Invariant Section.

O. Preserve any Warranty Disclaimers.

If the Modiﬁed 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 Modiﬁed Version’s 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 Modiﬁed Version by various parties—for example, statements of

peer review or that the text has been approved by an organization as the authoritative

deﬁnition of a standard.

You may add a passage of up to ﬁve 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 Modiﬁed

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 per-

mission to use their names for publicity for or to assert or imply endorsement of any

Modiﬁed Version.

5. COMBINING DOCUMENTS

You may combine the Document with other documents released under this License,

under the terms deﬁned in section 4 above for modiﬁed versions, provided that you

include in the combination all of the Invariant Sections of all of the original documents,

unmodiﬁed, 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 diﬀerent 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

86 APPENDIX A. GNU FREE DOCUMENTATION LICENSE

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 sec-

tions 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 docu-

ments 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 individ-

ually 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.

7. AGGREGATION WITH INDEPENDENT

WORKS

A compilation of the Document or its derivatives with other separate and independent

documents or works, in or on a volume of a storage or distribution medium, is called an

“aggregate” if the copyright resulting from the compilation is not used to limit the legal

rights of the compilation’s users beyond what the individual works permit. When the

Document is included in an aggregate, this License does not apply to the other works in

the aggregate which are not themselves derivative works of the Document.

If the Cover Text requirement of section 3 is applicable to these copies of the Docu-

ment, then if the Document is less than one half of the entire aggregate, the Document’s

Cover Texts may be placed on covers that bracket the Document within the aggregate,

or the electronic equivalent of covers if the Document is in electronic form. Otherwise

they must appear on printed covers that bracket the whole aggregate.

8. TRANSLATION

Translation is considered a kind of modiﬁcation, so you may distribute translations

of the Document under the terms of section 4. Replacing Invariant Sections with trans-

lations 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.

87

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 ﬁnally 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 notiﬁes you of the violation by some reasonable means, this is the

ﬁrst 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.

10. FUTURE REVISIONS OF THIS LICENSE

The Free Software Foundation may publish new, revised versions of the GNU Free

Documentation License from time to time. Such new versions will be similar in spirit to

the present version, but may diﬀer in detail to address new problems or concerns. See

http://www.gnu.org/copyleft/.

Each version of the License is given a distinguishing version number. If the Docu-

ment speciﬁes that a particular numbered version of this License “or any later version”

applies to it, you have the option of following the terms and conditions either of that

speciﬁed version or of any later version that has been published (not as a draft) by the

Free Software Foundation. If the Document does not specify a version number of this

License, you may choose any version ever published (not as a draft) by the Free Software

Foundation. If the Document speciﬁes that a proxy can decide which future versions

of this License can be used, that proxy’s public statement of acceptance of a version

permanently authorizes you to choose that version for the Document.

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 pub-

lished by Creative Commons Corporation, a not-for-proﬁt 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.

88 APPENDIX A. GNU FREE DOCUMENTATION LICENSE

“Incorporate” means to publish or republish a Document, in whole or in part, as part

of another Document.

An MMC is “eligible for relicensing” if it is licensed under this License, and if all

works that were ﬁrst published under this License somewhere other than this MMC, and

subsequently incorporated in whole or in part into the MMC, (1) had no cover texts or

invariant sections, and (2) were thus incorporated prior to November 1, 2008.

The operator of an MMC Site may republish an MMC contained in the site under

CC-BY-SA on the same site at any time before August 1, 2009, provided the MMC is

eligible for relicensing.

ADDENDUM: How to use this License for your

documents

To use this License in a document you have written, include a copy of the License

in the document and put the following copyright and license notices just after the title

page:

Copyright c YEAR YOUR NAME. Permission is granted to copy, distribute

and/or modify this document under the terms of the GNU Free Documenta-

tion License, Version 1.3 or any later version published by the Free Software

Foundation; with no Invariant Sections, no Front-Cover Texts, and no Back-

Cover Texts. A copy of the license is included in the section entitled “GNU

Free Documentation License”.

If you have Invariant Sections, Front-Cover Texts and Back-Cover Texts, replace the

“with . . . Texts.” line with this:

with the Invariant Sections being LIST THEIR TITLES, with the Front-

Cover Texts being LIST, and with the Back-Cover Texts being LIST.

If you have Invariant Sections without Cover Texts, or some other combination of

the three, merge those two alternatives to suit the situation.

If your document contains nontrivial examples of program code, we recommend re-

leasing these examples in parallel under your choice of free software license, such as the

GNU General Public License, to permit their use in free software.

Bibliography

[1] R. Albert and A.-L. Barab´asi. Statistical mechanics of complex networks. Reviews of

Modern Physics, 74:47–97, 2002.

[2] A.-L. Barab´asi and R. Albert. Emergence of scaling in random networks. Science, 286:509–

512, 1999.

[3] V. Batagelj and U. Brandes. Eﬃcient generation of large random networks. Physical

Review E, 71(3):036113, 2005.

[4] R. A. Beezer. A First Course in Linear Algebra. Robert A. Beezer, University of Puget

Sound, Tacoma, Washington, USA, 2009. http://linear.ups.edu.

[5] K. A. Berman and J. L. Paul. Fundamentals of Sequential and Parallel Algorithms. PWS

Publishing Company, 1997.

[6] N. Biggs. Codes: An Introduction to Information, Communication, and Cryptography.

Springer, 2009.

[7] B. Bollob´as. Random Graphs. Cambridge University Press, 2nd edition, 2001.

[8] O. Bor˚uvka. O jist´em probl´emu minim´aln´ım (about a certain minimal problem). Pr´ace

mor. pˇr´ırodovˇed. spol. v Brnˇe III, 3:37–58, 1926.

[9] O. Bor˚uvka. Pˇr´ıspˇevek k ˇreˇsen´ı ot´ azky ekonomick´e stavby elektrovodn´ıch s´ıt´ı (contribution

to the solution of a problem of economical construction of electrical networks). Elektronick´y

Obzor, 15:153–154, 1926.

[10] J. M. Boyer and W. J. Myrvold. On the cutting edge: Simpliﬁed O(n) planarity by edge

addition. Journal of Graph Algorithms and Applications, 8(2):241–273, 2004.

[11] T. H. Cormen, C. E. Leiserson, R. L. Rivest, and C. Stein. Introduction to Algorithms.

MIT Press and McGraw-Hill, 2nd edition, 2001.

[12] E. W. Dijkstra. A note on two problems in connexion with graphs. Numerische Mathe-

matik, 1:269–271, 1959.

[13] P. Erd¨os and A. R´enyi. On random graphs. Publicationes Mathematicae, 6:290–297, 1959.

[14] J. Gross and J. Yellen. Graph Theory and Its Applications. CRC Press, 1999.

[15] J. E. Hopcroft and R. E. Tarjan. Eﬃcient planarity testing. Journal of the ACM,

21(4):549–568, 1974.

[16] D. B. Johnson. Eﬃcient algorithms for shortest paths in sparse networks. Journal of the

ACM, 24(1):1–13, 1977.

[17] D. Jungnickel. Graphs, Networks and Algorithms. Springer, 3rd edition, 2008.

[18] J. B. Kruskal. On the shortest spanning subtree of a graph and the traveling salesman

problem. Proceedings of the American Mathematical Society, 7(1):48–50, 1956.

[19] V. Latora and M. Marchiori. Economic small-world behavior in weighted networks. The

European Physical Journal B, 32(2):249–263, 2003.

[20] S. Milgram. The small world problem. Psychology Today, 2:60–67, 1967.

[21] M. E. J. Newman. The structure and function of complex networks. SIAM Review,

45(2):167–256, 2003.

[22] R. C. Prim. Shortest connection networks and some generalizations. Bell System Technical

Journal, 36:1389–1401, 1957.

[23] V. Shoup. A Computational Introduction to Number Theory and Algebra. Cambridge

University Press, 2nd edition, 2008. http://www.shoup.net/ntb.

89

90 BIBLIOGRAPHY

[24] D. J. Watts and S. H. Strogatz. Collective dynamics of ‘small-world’ networks. Nature,

393:440–442, 1998.

Index

C

n

, 8

G

c

, 20

K

n

, 7

K

m,n

, 8

L

n

, 22

P

n

, 8

Q

n

, 22

∆, 4, 19

∼

=, 15

deg(v), 4

δ, 4

L, 13

ω, 6

⊕, 19

, 21

adj(v), 2

r-regular, 4

acyclic, 37

adjacency matrix, 10

reduced, 12

alphabet, 48, 53

backtracking, 27

BFS, 25

bi-adjacency matrix, 12

bipartite graph, 8

bond, 20, 38

breadth-ﬁrst search, 25

bridge, 20

canonical label, 16

Cartesian product, 21

check matrix, 12

child, 37

closed path, 6

code, 48, 54

binary, 54

block, 49

Morse, 49, 55

optimal, 56

preﬁx-free, 49

tree, 54

uniquely decodable, 55

variable-length, 49

codeword, 48, 54

complement, 20

complete bipartite graph, 8

complete graph, 7

connected graph, 6

cut set, 20

cycle, 6

cycle graph, 8

decode, 48

degree

maximum, 4

minimum, 4

degree matrix, 13

degree of a vertex, 4

degree sequence, 16

graphical, 17

depth-ﬁrst search, 27

DFS, 27

digraph, 3

Dijkstra’s algorithm, 7

directed edge, 3

disconnected graph, 6

disconnecting set, 20

distance matrix, 13

edge contraction, 20

edge cut, 20

edge deletion subgraph, 20

edges, 1

incident, 1

encode, 48

endpoint, 37

Euler subgraph, 6

Euler, Leonhard, 4

forbidden minor, 23

forest, 37

fundamental cycle, 38

91

92 INDEX

geodesic path, 6

girth, 6

graph, 1

applications, 23

bipartite, 8

canonical label, 16

complete, 7

complete bipartite, 8

connected, 6

cut, 20

hypercube, 22, 51

intersection, 18

join, 19

ladder, 22

null, 3

path, 22

planar, 23

star, 8

symmetric diﬀerence, 19

trivial, 8

union, 18

graph invariant, 15, 17

graph isomorphism, 15

graph minor, 23

graphs

directed, 3

isomorphic, 15

multigraphs, 3

regular, 4

simple, 4

Gray code

m-ary, 50

binary, 50

Hamming distance, 22

handshaking lemma, 4

house graph, 1

hypercube graph, 22

incidence matrix, 12

indegree, 3

invariant, 15

isolated vertex, 4

Johnson’s algorithm, 36

Klein, Felix, 1

knight tour, 27

ladder graph, 22

Laplacian matrix, 13

leaf, 37

length of codeword, 56

matrix, 10

message, 53

multi-undirected graph, 3

multidigraph, 3

multigraphs, 3

null graph, 3

order, 1

orientation, 13

outdegree, 3

parent, 37

path, 5

closed, 6

geodesic, 6

path graph, 8, 22

path length, 28

permutation equivalent, 16

regular graph, 4

relative complement, 20

ring sum, 19

Robertson, Neil, 23

Robertson-Seymour theorem, 23

self-complementary graph, 20

self-loop, 3

separating set, 20

Seymour, Paul, 23

shortest path, 6

simple graph, 4

size, 1

social network analysis, 23

spanning subgraph, 7

star graph, 8

string, 53

subgraph, 7

supergraph, 7

symmetric diﬀerence, 19

Tanner graph, 12

trail, 5

traveling salesman problem, 23

tree, 37

n-ary, 37

INDEX 93

binary, 37

depth, 58

directed, 37

ordered, 37

rooted, 37

tree traversal, 57

breadth-ﬁrst, 58

depth-ﬁrst, 57

in-order, 58

level-order, 58

post-order, 58

pre-order, 57

symmetric, 58

trivial graph, 8

vertex cut, 20

vertex deletion subgraph, 19

vertices, 1

adjacent, 1

Wagner’s conjecture, 23

Wagner, Klaus, 23

walk, 5

length, 5

wheel graph, 19

word, 53

Copyright c 2009–2010 David Joyner <wdjoyner@gmail.com> Minh Van Nguyen <nguyenminh2@gmail.com> Nathann Cohen <nathann.cohen@gmail.com> Permission is granted to copy, distribute and/or modify this document under the terms of the GNU Free Documentation License, Version 1.3 or any later version published by the Free Software Foundation; with no Invariant Sections, no Front-Cover Texts, and no Back-Cover Texts. A copy of the license is included in the section entitled “GNU Free Documentation License”. Edition Version 0.3 19 March 2010

Contents

Acknowledgements List of Algorithms List of Figures List of Tables 1 Introduction to Graph Theory 1.1 Graphs and digraphs . . . . . . . . 1.2 Subgraphs and other graph types . 1.3 Representing graphs using matrices 1.4 Isomorphic graphs . . . . . . . . . 1.5 New graphs from old . . . . . . . . 1.6 Common applications . . . . . . . . iii v vii ix 1 1 5 10 14 18 23 25 25 28 37 38 40 48 57 61 61 61 61 62 63 63 63 63 64

. . . . . .

. . . . . .

. . . . . .

. . . . . .

. . . . . .

. . . . . .

. . . . . .

. . . . . .

. . . . . .

. . . . . .

. . . . . .

. . . . . .

. . . . . .

. . . . . .

. . . . . .

. . . . . .

. . . . . .

. . . . . .

. . . . . .

. . . . . .

. . . . . .

2 Graph Algorithms 2.1 Graph searching . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2.2 Shortest path algorithms . . . . . . . . . . . . . . . . . . . . . . . . . . . 3 Trees and Forests 3.1 Properties of trees . . . . . . . . . 3.2 Minimum spanning trees . . . . . 3.3 Binary trees . . . . . . . . . . . . 3.4 Applications to computer science 4 Distance and Connectivity 4.1 Paths and distance . . . . . . 4.2 Vertex and edge connectivity . 4.3 Centrality of a vertex . . . . . 4.4 Network reliability . . . . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

5 Optimal Graph Traversals 5.1 Eulerian graphs . . . . . . . . . . 5.2 Hamiltonian graphs . . . . . . . . 5.3 The Chinese Postman Problem . 5.4 The Traveling Salesman Problem

. . . . i

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . . 2. . . . . . . 7. . . . . . . . . . . . .5 Linear arboricity . .2 Ford and Fulkerson’s theorem . . . . . . . . . . . . . . . .3 Scale-free networks . . . . . . . . . . 8.2 Kuratowski’s Theorem . . . . . . . . . .1 Planarity and Euler’s Formula . . RELICENSING . . . . . . . . . . . 10. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .3 Edmonds and Karp’s algorithm . . . . . . . . . . . . . . . . . AGGREGATION WITH INDEPENDENT WORKS . . . . . . . . . . . . . . . . . . 7. . . 8. . . . . . . . . . . . 10 Graph Problems and Their LP 10. .3 Planarity algorithms . . . . . . 9. . . . ADDENDUM: How to use this License for your documents . . . . . . . . . . 6. . . . . . . . . . . . . . . . APPLICABILITY AND DEFINITIONS . . . . . . .3 Edge-disjoint spanning trees . . . . TERMINATION . . . . . . . . . . . . . . . . . . . . . . . .1 Vertex coloring . . . . . . . . . . . . . . . . . . . . . . . . . . . . TRANSLATION . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . COMBINING DOCUMENTS . . . . . . . . . .4 Evolving networks . . . . . . . . . . . . . . . . . . .1 Maximum average degree . Formulations . . . . .ii CONTENTS 65 65 65 65 67 67 67 67 69 69 69 69 69 71 71 71 71 71 73 73 75 76 77 78 78 79 81 81 83 83 84 85 86 86 86 87 87 87 88 6 Planar Graphs 6. . . . . . 8. . . . . . . . . . . . .7 H-minor . . . . . . . . 11. . . . . . . . . 8. . . . . . 4. . 6. . . . . . . . . . . 10. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .2 Small-world networks 9. . . . . . . . . MODIFICATIONS . . . . . . . . . . 5. . . . . . . . . . . . . . . . . . 8 Network Flows 8. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6. . . . . . . . . . . . . . . . . . . . .3 Applications of graph coloring . . . . 10. 10. . . . 10. . . . . . . . . . . . . . . . . . . . . . . . . 7 Graph Coloring 7. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .2 Traveling Salesman Problem . . . . . . .2 Edge coloring . o e 9.4 Goldberg and Tarjan’s algorithm 9 Random Graphs 9. . . . . COPYING IN QUANTITY . . . . . . . . . . . . . . . . . .1 Erd¨s-R´nyi graphs . . 9. . . . . . . . . . . . . . . . . . VERBATIM COPYING . . . . A GNU Free Documentation License 1. .4 Steiner tree . . 10. . . . . . . . . . . . . . .6 Acyclic edge coloring . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3. . . . COLLECTIONS OF DOCUMENTS . . . FUTURE REVISIONS OF THIS LICENSE . . . . . . . . . . . . . . . . . . . . . 7. . . . . . . . . . . . . . . . . . .1 Flows and cuts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

• Daniel Black: reported a typo in Chapter 1. See changeset 61.Acknowledgements • Fidel Barrera-Cruz: reported typos in Chapter 3. iii . See changeset 101.

iv ACKNOWLEDGEMENTS .

. . .List of Algorithms 1. . . . . . . . . . . . . . . 16 26 28 30 36 43 v . . . . . . . . . . . Johnson’s algorithm. . . . . . . . . . .1 2. . . . . . . . . . . . . canonical labels. .2 2. . . . . . . . . . . . . . . . . . . Dijkstra’s algorithm. . . . . . . . . . . . . . . . . . . . Prim’s algorithm. . . . . . . . . . . . . . . . . . . .4 3. . . . . . . . .1 2. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .1 Computing graph isomorphism using Breadth-ﬁrst search. . . . . . . Depth-ﬁrst search. . . . . . . . . . . . . .3 2. .

vi LIST OF ALGORITHMS .

. . . . . . . Adjacency matrices of directed and undirected Tanner graph for H. Kruskal’s algorithm for the 4 × 4 grid graph. . . . . . . . A triangle as a directed graph. . . . . . . . . . . . . . .2 3. 9. . . . .5 1. . . . . . . .3 2.6 Morse code . . . . . . . . . . . . . complete bipartite. . . . . Walking along a graph.6 3. . . . . . . . . . . . . . . . . . . . . . . . . . On the left is the original graph. Hypercube graphs Qn for n = 1. . . . . . . . . . . . . . . . . . A ﬁgure with a self-loop. . . . . . . . . . . . 3. . . . . . Searching a digraph with negative weight using the Bellman-Ford algorithm. . . . . . . . . . . . . . .3 A house graph. . . 3.8 1. . . . .2 2. . . .11 1. . . .1 1. . . . . . . . . . . . . . . 3. . . . . . . . . . . . .8 List plot of Γ8 created using Sage. . . . . . . . . . . . . .9 1. . . . The wheel graphs Wn for n = 4. . . . . . . .5 An example of Borovka’s algorithm.1 3. . . . . . . . . . . . . . . . Searching a directed house graph using Dijkstra’s algorithm. . . . . . . . . . . . . . . . Shortest paths in a weighted graph using the Bellman-Ford algorithm. . . . . . . . . Isomorphic and non-isomorphic graphs. . . . . . 3. . Cycle graphs Cn for 3 ≤ n ≤ 6. . 3. . Complete graphs Kn for 1 ≤ n ≤ 5. . . . . . . . . . . . . .2 1. . . . Another demonstration of the Floyd-Roy-Warshall algorithm. . . . . and star graphs. . . . . . . . 3. . . . . . . . . .13 2.List of Figures 1. . . . .3 1. .10 Huﬀman code example . . . . . . . . . . . . . . . . . . . . . .6 1. . . . . . . .4 2. . . . . . . . . . . . . . . . . . . . . .4 1. . . . . . . . . Above is the original digraph and below is the MST produced by Prim’s algorithm. . . . . . . . . . . . . . . . . . .7 1. vii . . . . . Demonstrating the Floyd-Roy-Warshall algorithm. . . . . . . . . . On the left is the original graph. . . . . . . . . . . . . . .10 1. . . Bipartite. . . .4 Another example of Prim’s algorithm.5 2. . . . . . . . . . . . . . . . . . . . . . 4. . . .9 Example of a tree representation of a binary code . . . . . . . . . .12 1. . . . . . . . A graph and one of its subgraphs. . . . . . . . . Prim’s algorithm for digraphs. . . . . . . . . . . . . . . . . . . . . . .1 2. . 3. . . . . . . . . . . . . . . . . . . . . . . . On the right is the MST produced by Boruvka’s algorithm. . . . . . . . . . . . . . . . . . . . . . graphs. . . .7 Viewing Γ3 as a Hamiltonian path on Q3 . . . . . . On the right is the MST produced by Prim’s algorithm. . . . . . . . Spanning trees for the 4 × 4 grid graph. . . . . 1 3 4 6 7 8 9 9 11 12 15 19 22 29 31 33 33 35 35 37 42 44 45 46 49 51 53 54 57 Searching a weighted digraph using Dijkstra’s algorithm. . .

viii LIST OF FIGURES .

. .2 Stepping through Dijkstra’s algorithm. Another walk-through of Dijkstra’s algorithm. . .1 2. . . . . . . . .List of Tables 2. . . . . . . . . . . . . . . . . . . . . . . 30 31 ix .

x LIST OF TABLES .

“Everyone knows what a curve is.1. multigraphs.” Deﬁnition 1.Chapter 1 Introduction to Graph Theory To paraphrase what Felix Klein said about curves. and |E| is called the size of G. we say that v1 and v2 are adjacent vertices. E) is an ordered pair of sets. with E being the edge set. a e b d c Figure 1.1 it is easy to deﬁne a graph until you realize the countless number of exceptions. v2 ) as v1 v2 . Elements of V are called vertices or nodes. and so on. E). There are directed graphs.1 Graphs and digraphs We start by calling a “graph” what some call an “unweighted. We refer to V as the vertex set of G. For ease of notation. until he has studied enough mathematics to become confused through the countless number of possible exceptions. v2 ) ∈ E is an edge of a graph G = (V. Graphs. Where do we begin? 1.1: A house graph. One can label a graph by attaching labels to its vertices. simple graphs. The cardinality of V is called the order of G. weighted graphs. we write the edge (v1 . and elements of E ⊆ V × V are called edges or arcs.” 1 1 . A graph G = (V. undirected graph without multiple edges. If (v1 . The edge v1 v2 is also said to be incident with the vertices v1 and v2 .

(’b’. ed. be. dc. That is why G. sage: [’a’. bc. We can also use Sage to construct the graph G and list its vertex and edge sets: sage: G = Graph({"a": ["b". is the resulting ﬁgure still a graph? Why or why not? 5. \ . ’e’). sage: [’a’. sage: [’b’.neighbors("b") ’c’. "d"]}) sage: G Graph on 5 vertices sage: G. ’c’. We can use the function G. (’c’. The vertices adjacent to v are also referred to as its neighbours. ’d’. "c": ["b". d} adj(d) = {c..neighbors("e") ’b’. 2.2. "c". "e"]. Then we have adj(a) = {b. c. (’c’.vertices() [’a’. (’e’.neighbors("d") ’e’] G. ba. G.. (1) Let G = (V. e} adj(e) = {a.. "d"]. The edge set of G is given by E = {ab. 1. the edge ab is the same as the edge ba. "e"]. "e"]. "b". which vertex or vertices have the smallest number of adjacent vertices? 4. cd. List the vertex and edge sets of the graph. (’a’. sage: [’b’. ’e’] sage: G. e}. is the resulting ﬁgure still a graph? Why or why not? Solution. (2) Let adj(v) be the set of all vertices that are adjacent to v. If all vertices of the graph are removed.edges(labels=False) [(’a’. 3.neighbors() to list all the neighbours of each vertex. list all vertices that are adjacent to it.edges() returns six edges instead of the 12 edges listed in (1. ea}. "e": ["a".2 CHAPTER 1. d}. eb. ’d’). e} adj(c) = {b. ’b’). Then the vertex set of G is V = {a. INTRODUCTION TO GRAPH THEORY Example 1. ae. meaning that we do not impose direction on any edges. ’d’)] (1. Which vertex or vertices have the largest number of adjacent vertices? Similarly. Without any direction on the edges. ’e’).1). "b": ["a". e} adj(b) = {a. cb. Consider the graph in Figure 1. de. ’d’] . ’b’).1. For each vertex.neighbors("a") ’e’] G.1. b.1) The graph G is undirected. d. E) denote the graph in Figure 1. sage: [’c’. If all edges of the graph are removed. ’b’. ’e’] G.neighbors("c") ’d’] G.: "d": ["c". b. c.

then the graph has six edges. while b and e have the largest number of adjacent vertices. Another 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. In case G is a digraph. Consider the illustration in Figure 1. Then (V. (4) If all the edges in G are removed.2. GRAPHS AND DIGRAPHS 3 (3) Taking the cardinalities of the above ﬁve sets. The following deﬁnition captures the situation where the direction of the edges are taken into account. then there are 12 edges as listed in (1. Thus a. In general. If G is undirected and uv ∈ E(G).1 and in the process all edges are removed as well. a multidigraph is a directed multigraph. bc}. Directed graphs. although one without any edges. However.1. In Figure 1. A directed edge uv is said to be directed from its tail u to its head v. The edge aa is called a self-loop of the graph. Removing all edges of G leaves us with the empty set ∅. The indegree of a vertex v ∈ V (G) counts the number of edges such that v is the head of those edges. By deﬁnition. (5) Say we remove all of the vertices from the graph in Figure 1.1. any edge of the form vv is a self-loop. Similarly. 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. Example 1. The result is that both of the vertex and edge sets are empty. If we do not consider the direction of the edges in the graph of Figure 1. then uv and vu represent the same edge. c and d have the smallest number of adjacent vertices.2: A ﬁgure with a self-loop. .1).3. a c b Figure 1.5. A directed graph or digraph G is a graph such that each of whose edges is directed. then uv and vu are diﬀerent directed edges. if the direction of each edge is taken into account.4. the edges ae and ea represent one and the same edge. the edge set of any graph is a subset of V × V . If V = {a. it is clear that E ⊆ V × V . Does Figure 1. Deﬁnition 1. Multigraphs. Deﬁnition 1.1. The outdegree of a vertex v ∈ V (G) is the number of edges such that v is the tail of those edges. c} and E = {aa. It is important to distinguish a graph G as being directed or undirected.2 represent a graph? Why or why not? Solution. b.1. This is a special graph known as an empty or null graph. we get |adj(a)| = |adj(c)| = |adj(d)| = 2 and |adj(b)| = |adj(e)| = 3. A multi-undirected graph is a multigraph that is undirected. which is a subset of every set. the result is still a graph. E) is a graph.

3.3: A triangle as a directed graph. INTRODUCTION TO GRAPH THEORY Deﬁnition 1. the cardinality of adj(v) is called the degree of v and written as deg(v) = |adj(v)|.3.degree("c") 2 So for the graph G in Figure 1.2 we know that the vertices a. so e is counted twice towards the total sum of degrees. The degree of v counts the number of vertices in G that are adjacent to v. The graph G in Figure 1. we have δ(G) = ∆(G) = 2. From Example 1. e have the largest degree. The minimum degree among all vertices in G is denoted δ(G). Theorem 1. if G denotes the graph in Figure 1.1 then we have δ(G) = 2 and ∆(G) = 3.4 CHAPTER 1. we construct the digraph in Figure 1. in the graph in Figure 1. E) is a graph.3. For any vertex v in a graph G = (V. b. . "b": "c". we say that v is an isolated vertex. In the following Sage session.7. Graphs with this property are referred to as regular.e.3 has the special property that its minimum degree is the same as its maximum degree. The graph in Figure 1. An r-regular graph is a regular graph each of whose vertices has degree r. whereas the maximum degree is written as ∆(G). c. then v∈V deg(v) = 2|E|.6.1. The following result. while b. we count e towards the degree of vertex v1 and the second time we count e towards the degree of v2 . Thus. For instance. G is a 2-regular graph. If V = ∅ and E = ∅.3 and computes its maximum and minimum number of degrees. Proof. there is no arrow from b to a. we have deg(b) = 3. There is an arrow from vertex a to vertex b. bc. For the graph in Figure 1. i. c} and the edge set {ab. However. d in Figure 1. Euler. so ba is not in the edge set of the graph in Figure 1.degree("a") 2 sage: G. hence ab is in the edge set. similarly to the digraph in Figure 1. A simple graph is a graph with no self-loops and no multiple edges. E). The ﬁrst time. due to Euler. If deg(v) = 0.3. Simple graphs. "c": "a"}) sage: G Digraph on 3 vertices sage: G.3 has the vertex set {a.1 have the smallest degree in the graph of that ﬁgure. If G = (V. a c b Figure 1. sage: G = DiGraph({"a": "b". δ(G) = ∆(G). we have deg(b) = 2. ca}.degree("b") 2 sage: G. counts the total number of degrees in any graph. Each edge e = v1 v2 ∈ E is incident with two vertices. then G is a graph consisting entirely of isolated vertices. The edges of a digraph can be visually represented as directed arrows. For example.

Draw an edge between two people if they shook hands. B shaking A’s hand.2. This result is captured in the following corollary. then the total degree of G is greater than zero. 2 . As E ⊆ V × V . a u-v walk is an alternating sequence of vertices and edges starting with u and ending at v.4 is a path and a cycle. b. We are allowed to have repeated vertices and edges in a walk. Nothing in the deﬁnition of a trail restricts a trail from having repeated vertices. c.7 is sometimes called the “handshaking lemma. A trail is a walk with no repeating edges.4 is a trail. e. E) is an r-regular graph with n vertices and m edges. where each vertex is associated with a diﬀerent person. SUBGRAPHS AND OTHER GRAPH TYPES 5 Theorem 1.1. By Theorem 1. If G = (V. e. For the graph in Figure 1. This result is an immediate consequence of Theorem 1. In other words. e has length 4.8. hence a path is also a trail.” due to its interpretation as in the following story. c. f. Corollary 1. From b.4. c.7. a in Figure 1. that no two people shake hands twice). a in Figure 1.e. E) is an r-regular graph having n vertices and m edges. g.4 is a closed path. Suppose there are n people in the room (including yourself) and some people shake hands with others and some do not. A path whose start and end vertices are the same is called a cycle. then E can be the empty set. For instance. Without any repeating vertices. an example of a walk is an a-e walk: a. the walk a. Consecutive vertices and edges are incident. i. i. If G = (V. the total degree of G is non-negative and even. we start at vertex a and travel to vertex b. then its total number of degrees is non-negative and even. Notice that consecutive vertices in a walk are adjacent to each other. a path cannot have repeating edges. b. By Theorem 1. For example. Then we end our journey at e. the walk a. E) is zero. trails. This is “obvious” when you look at it this way since each handshake is counted twice (A shaking B’s hand is counted. is a closed path (with apologies for slightly abusing terminology). Suppose you go into a room. c. For example. b. A closed path in a graph is sometimes also called a “circuit. Create the graph with n vertices. b. say. the a-b walk a. Corollary 1. It does not contain any repeated edges. The degree of a vertex is the number of times that person has shaken hands (we assume that there are no multiple edges. One can think of vertices as destinations and edges as footpaths. A walk with no repeating vertices is called a path. The number of edges in a walk is called its length. it is clear by deﬁnition of r-regular graphs that the total degree of G is rn. b. b. then m = rn/2. but it contains one repeated vertex. If G is a graph.9.e. b.2 1.” Since that terminology unfortunately conﬂicts with the closely related notion of a circuit of a matroid. we do not use it here. 1. but otherwise has no repeated vertices. b. we go to c and then back to b again. since the sum in the theorem is over all vertices). in which case the total degree of G = (V. It is easy to see that if you remove any edge from a cycle.2.7 we have 2m = rn and therefore m = rn/2. A walk which has no repeated edges and the start and end vertices are the same. Where E = ∅. e. b in Figure 1.1 Subgraphs and other graph types Walks. The theorem above simply says that the total number of handshakes is even. d.2 Thus the walk a. and paths If u and v are two vertices in a graph G.7 and is captured in the following corollary.

d.4. b. e. c. d. An Euler subgraph of a graph G is either a cycle or an edge-disjoint union of cycles in G.4 is connected. b. Find two distinct trails that are not paths and determine their lengths.4. Find a closed path that is not a cycle.4: Walking along a graph. d. (3) Here are two distinct paths: p1 : a. a.11. Find a shortest path from g to d. the graph in Figure 1. Find two distinct paths and determine their lengths. (4) Here is a closed path that is not a cycle: d. f. b. d. f. c. a. e and w2 : f.3 is connected. (2) Here are two distinct trails that are not paths: t1 : a. e. b. Determine whether or not the graph in Figure 1. c are geodesic paths between a and c.6 CHAPTER 1. A non-empty graph may have several shortest paths between some distinct pair of vertices. c. However. e. . The length of walk w1 is 5 and the length of walk w2 is also 5. c. (1) Here are two distinct walks that are not trails: w1 : g. e. e. The length of the shortest cycle in a graph is called the girth of the graph. A graph is said to be connected if for every pair of distinct vertices u. e. f and t2 : b. 1. then the resulting walk contains no closed paths. 2. c and a. 4. The length of trail t1 is 4 and the length of trail t2 is also 4. The empty graph is disconnected and so is any non-empty graph with an isolated vertex. f. INTRODUCTION TO GRAPH THEORY a b c g d f e Figure 1. 5. e and p2 : g. b. d. c. b. v of a graph is a u-v path of minimum length. b. f. Find a closed path C which has an edge e such that C − e contains a cycle. The number of connected components of a graph G will be denoted ω(G). A graph that is not connected is referred to as disconnected. Consider the graph in Figure 1. A geodesic path or shortest path between two distinct vertices u. The length of path p1 is 5 and the length of path p2 is also 5. d. An acyclic graph is said to have inﬁnite girth. For the graph in Figure 1. 3. f. a. v there is a u-v path joining them. d. Find two distinct walks that are not trails and determine their lengths. by convention. both a. e. Example 1.10. Solution. Example 1.

To obtain a spanning subgraph from a given graph. "b": ["a". Starting from G. if uv ∈ E(H) then u. let G be the left-hand side graph and let H be the right-hand side graph. d is a shortest path from g to d. d is the shortest path between g and d. sage: . "d") ’f’. .. "c"].: sage: True sage: [’g’. We will explain Dijkstra’s algorithm in Chapter 2. \ "f": ["g". "f"]}) g. "b".5: A graph and one of its subgraphs.. "d".4 and use the method is_connected() to determine whether or not the graph is connected.is_connected() g. ﬁnding the shortest path from a vertex v to a particular (but arbitrarily given) vertex w appears to be as hard as ﬁnding the shortest path from a vertex v to all other vertices in the graph! 1. Then it is clear that H is a spanning subgraph of G.. we use the method shortest_path() to ﬁnd a geodesic path between g and d. "g": ["b". Furthermore. Consider a graph H such that V (H) ⊆ V (G) and E(H) ⊆ E(G). f. Finally.12.2.. "e": ["f". "e"]. "e"]. one can obtain its subgraph H by deleting edges and/or vertices from G. f. v ∈ V (H).: . "e". then all edges incident with v are also removed. In Figure 1.5.2. then H is called a spanning subgraph of G. In the following Sage session. we delete edges from the given graph. at the present state of knowledge. "d": ["c". "g". complete and bipartite graphs Deﬁnition 1. which gives one of the best algorithms for ﬁnding shortest paths between two vertices in a connected graph. "c"]. Let G be a graph with vertex set V (G) and edge set E(G). "a". "d"]. Note that when a vertex v is removed from G. (a) (b) Figure 1. "f"]. we ﬁrst construct the graph in Figure 1. Then H is called a subgraph of G and G is referred to as a supergraph of H. If V (H) = V (G).. \ "c": ["b".shortest_path("g".2 Subgraphs. g = Graph({"a": ["b". "e". any other g-d path has length greater than 2. In fact.1. SUBGRAPHS AND OTHER GRAPH TYPES 7 Solution.. What is very remarkable is that. so we can say that g. ’d’] This shows that g.

2 Figure 1. The complete graph K1 has one vertex with no edges. and the star graph K1.3 . then the resulting graph is K3. Figure 1. once with with each of the three boys. and draw an edge form one vertex to another if the two people dance together. As an example of K3. both non-empty. 2 (1. The path graph on n ≥ 1 vertices is denoted Pn . Figure 1.n is n-regular. If m = n. is the connected 2-regular graph on n vertices. The boys and girls naturally partition the set of all people in the room. then Kn.7 shows cycles graphs Cn where 3 ≤ n ≤ 6. 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 . suppose that there are 3 boys and 3 girls dancing in a room. and any two distinct vertices in Vi are not adjacent to each other. It is also called the trivial graph. its total number of edges |E(G)| is bounded above by |E(G)| ≤ n(n − 1) . 2 we have P1 = K1 and P2 = K2 . (a) K5 (b) K4 (c) K3 (d) K2 (e) K1 Figure 1. or v ∈ V1 and u ∈ V2 .2) Thus for any graph G with n vertices. If each girl dances three times.6 shows complete graphs each of whose total number of vertices is bounded by 1 ≤ n ≤ 5. The complete bipartite graph Km. Where m = 1 then K1. Construct a graph having 6 vertices. As |V (Kn )| = n.3 . . The cycle graph on n ≥ 3 vertices. then |E(Kn )| is equivalent to the total number of 2-combinations from a set of n objects: |E(Kn )| = n 2 = n(n − 1) .8 shows a bipartite graph together with the complete bipartite graphs K4. Every edge uv ∈ E(G) is such that u ∈ V1 and v ∈ V2 .4 . Where n ≥ 3.6: Complete graphs Kn for 1 ≤ n ≤ 5. The complete graph Kn on n vertices is a graph such that any two distinct vertices are adjacent. each vertex corresponding to a person in the room.3 and K3.8 CHAPTER 1. Any vertex in V1 is adjacent to each vertex in V2 . INTRODUCTION TO GRAPH THEORY We now consider several standard classes of graphs. Each vertex in Cn has degree exactly 2 and Cn is connected.n is called the star graph. denoted Cn .n is the bipartite graph whose vertex set is partitioned into two non-empty disjoint sets V1 and V2 with |V1 | = m and |V2 | = n.3 . For n = 1.

4 Figure 1.2. complete bipartite. and star graphs.3 (c) K3.1.8: Bipartite. . SUBGRAPHS AND OTHER GRAPH TYPES 9 (a) C6 (b) C5 (c) C4 (d) C3 Figure 1. (a) Bipartite (b) K4.7: Cycle graphs Cn for 3 ≤ n ≤ 6.3 (d) K1.

m1 0] 0] 1] 0] . if vi vj ∈ E.10 CHAPTER 1. vn } and edge set E. .13. 1. Then call the method adjacency_matrix(). "f": ["d". This section requires some concepts and techniques from linear algebra. 0. . Compute the adjacency matrices of the graphs in Figure 1. "f"]. . 6: [5]}) Graph({"a": ["b". "e": ["c".. if vi vj ∈ E. A is a square matrix such that aij = aji . 0. The (0. \ ["b". "c": ["a".adjacency_matrix().. 5: [6]. "b": ["a". The adjacency matrix of G is the n × n matrix A = [aij ] deﬁned by aij = 1. Solution. vn } and edge set E. If not many people shake hands in the room. "e"]}) G1. it would be much easier to simply record the handshakes as a Python dictionary. . As G is an undirected graph. otherwise. 6]. The entry in row i column j is denoted aij . especially matrix theory.3. . . . sage: G1 = sage: G2 = . .9. 2: [1]. A is also written as A = [aij ]. .: "d": sage: m1 = [0 1 0 0 0 [1 0 0 0 0 [0 1 0 0 0 [1 0 0 0 1 DiGraph({1: [2]. INTRODUCTION TO GRAPH THEORY 1. In this kind of “sparse graph” situation. aij = −1. Where the dimensions of A are clear from context. then A is a symmetric matrix. if vj vi ∈ E. −1.7.9 using DiGraph and Graph. . Now let G be a directed graph with vertices V = {v1 . . Imagine you walk into a large room full of people and you consider the “handshaking graph” discussed in connection with Theorem 1. Example 1. . 3: [2. See introductory texts on linear algebra and matrix theory [4] for coverage of such concepts and techniques. otherwise. . . am1 am2 · · · amn The positive integers m and n are the row and column dimensions of A. Representing a graph as a matrix is very ineﬃcient in some cases and not so in other cases. 4: [1. "c"]. "e"]. .3 Representing graphs using matrices An m × n matrix A can be represented as a11 a12 · · · a1n a a22 · · · a2n A = 21 . . Deﬁne the graphs in Figure 1. .. 1)-adjacency matrix of G is the n × n matrix A = [aij ] deﬁned by 1.1 Adjacency matrix Let G be an undirected graph with vertices V = {v1 . . 5]. . it is a waste of time recording all the handshakes and also all the “non-handshakes. . . "f"].” This is basically what the adjacency matrix does. respectively. . . That is. . "d"]. . . .

Sage allows you to easily compute a weighted adjacency matrix. if G is an undirected multigraph with edge eij = vi vj having multiplicity wij .adjacency_matrix().3). (0. (1.is_symmetric() True In general. otherwise. m2 [0 1 1 0 0 0] [1 0 0 1 0 0] [1 0 0 0 1 0] [0 1 0 0 0 1] [0 0 1 0 0 1] [0 0 0 1 1 0] sage: m1. Then the A1 0 adjacency matrix A of G can be realized as a block diagonal matrix A = .3. then we can deﬁne the (weighted) adjacency matrix A = [aij ] by aij = wij . 0. E) is an undirected bipartite graph and V = V1 ∪ V2 is the partition of the vertices into n1 vertices in V1 and n2 vertices in V2 .add_edges([(0.4)]) sage: M = G.1). (0. REPRESENTING GRAPHS USING MATRICES 3 6 2 f 11 d e 5 4 (a) 1 b a (b) c Figure 1. M [0 1 3 4] [1 0 2 0] [3 2 0 0] [4 0 0 0] Bipartite case Suppose G = (V. For example. or a weighted graph with edge eij = vi vj having weight wij . [0 0 0 0 0 1] [0 0 0 0 1 0] sage: m2 = G2. if vi vj ∈ E.9: Adjacency matrices of directed and undirected graphs. so |V | = n1 + n2 .weighted_adjacency_matrix().1. the adjacency matrix of a digraph is not symmetric.2). 0 A2 .3.2.is_symmetric() False sage: m2.2. while that of an undirected graph is symmetric. More generally.1. weighted=True) sage: G. sage: G = Graph(sparse=True.

. otherwise. INTRODUCTION TO GRAPH THEORY where A1 is an n1 × n2 matrix and A2 is an n2 × n1 matrix. . there is an edge ij ∈ E if and only if the (i. if ej is a self-loop at vi .1. 1.10: Tanner graph for H.0. 0. A2 = AT .3) 2.0). (0.2 Incidence matrix The relationship between edges and vertices provides a very strong constraint on the data structure. For any i. .12 CHAPTER 1. if vi is the head of ej . .0. .0. This matrix H is sometimes called the reduced adjacency matrix or the check matrix of the Tanner graph.1. vn }.0. if vi is the tail of ej .reduced_adjacency_matrix() [1 1 1 0 0] [0 0 1 0 1] [1 0 0 1 1] sage: B. The incidence matrix of G is the n × m matrix B = [bij ] deﬁned by −1. bij = (1.plot(graph_border=True) The corresponding graph is similar to that in Figure 1. 1 2 3 4 5 3 2 1 Figure 1. j with 1 ≤ i ≤ m and 1 ≤ j ≤ n.1).0. (1.10.1. much like the relationship between points and blocks in a combinatorial design or points and lines in a ﬁnite plane geometry. Let G be a digraph with edge set E = {e1 . 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. Tanner graphs are used in the theory of error-correcting codes. Tanner graphs If H is an m × n (0. . Sage allows you to easily compute such a bipartite graph from its matrix. 1. sage: H = Matrix([(1.3. For example. . The matrix is called a reduced adjacency matrix or a bi-adjacency matrix 1 (the literature also uses the terms “transfer matrix” or the ambiguous term “adjacency matrix”). . This incidence structure gives rise to another way to describe a graph using a matrix.1. then the Tanner graph of H is the bipartite graph G = (V. em } and vertex set V = {v1 .1)]) sage: B = BipartiteGraph(H) sage: B. Since G is undirected. . j)-th entry of H is 1. 1)-matrix.

vj )] is the distance matrix of G.3) is applicable to digraphs with self-loops as well as multidigraphs. vn }. for an undirected unweighted simple graph. E) is the number of edges in a shortest path connecting them. E) is an n × n diagonal matrix D whose i-th diagonal entry is the degree of the i-th vertex in V .3. In other words.3 Laplacian matrix The degree matrix of a graph G = (V. REPRESENTING GRAPHS USING MATRICES 13 Each column of B corresponds to an edge and each row corresponds to a vertex. . . .4]. It shall be discussed further in Chapter 4. d .3]. An orientation of an undirected graph G is an assignment of direction to each edge of G.1]}) sage: G. . bij = 2 if ej is a self-loop at vi .1. . The n × n matrix [d(vi . For each column of B. . 1. otherwise. The Laplacian matrix L of G is the diﬀerence between the degree matrix and the adjacency matrix: L = D − A. Similarly. 5: [4. For the undirected case. Sage allows you to compute the Laplacian matrix of a graph: sage: G = Graph({1: [2. if vi is incident to ej . let G be an undirected graph with edge set E = {e1 . if i = j and vi vj ∈ E. em } and vertex set V = {v1 . 4: [1.3. 1.4 Distance matrix Recall that the distance (or geodesic distance) d(v.laplacian_matrix() [ 3 -1 0 -1 0 -1] [-1 4 -1 -1 -1 0] [ 0 -1 3 -1 0 -1] [-1 -1 -1 4 -1 0] [ 0 -1 0 -1 2 0] [-1 0 -1 0 0 2] ij ] is given by There are many remarkable properties of the Laplacian matrix. The unoriented incidence matrix of G is the n × m matrix B = [bij ] deﬁned by 1. if ej is a self-loop at vi . w ∈ V in a connected graph G = (V. 2: [1.3. we have 1 as an entry in the row corresponding to one vertex of the edge under consideration and −1 as an entry in the row corresponding to the other vertex.6].4]. . The deﬁnition of incidence matrix of a digraph as contained in expression (1. 6: [3. bij = 2. where di = deg(vi ) is the degree of vertex vi .2]. otherwise. The oriented incidence matrix of G is deﬁned similarly to the case where G is a digraph: it is the incidence matrix of any orientation of G. w) between two vertices v. L = [ −1. Sage helps you to compute the distance matrix of a graph: . 0. . 3: [2. if i = j. ij = i 0.

14

CHAPTER 1. INTRODUCTION TO GRAPH THEORY

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 4 and 9.

Problems 1.3

1. 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. 2. 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. 3. 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. 4. 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 satisﬁes L = M × M T , where M T is the transpose of M .

1.4

Isomorphic graphs

Determining whether or not two graphs are, in some sense, the “same” is a hard but important problem. Deﬁnition 1.14. Isomorphic graphs. 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 nonisomorphic. If G and H are isomorphic, we write G ∼ H. =

1.4. ISOMORPHIC GRAPHS e f 1 2 e f

15

c

d

3

4

c

d

a

(a) C6

b

5

(b) G1

6

a

(c) G2

b

Figure 1.11: Isomorphic and non-isomorphic graphs. A graph G is isomorphic to a graph H if these two graphs can be labelled in such a way that if u and v are adjacent in G, then their counterparts in V (H) are also adjacent in H. To determine whether or not two graphs are isomorphic is to determine if they are structurally equivalent. Graphs G and H may be drawn diﬀerently so that they seem diﬀerent. However, if G ∼ H then the isomorphism f : V (G) −→ V (H) shows that both = of these graphs are fundamentally the same. In particular, the order and size of G are equal to those of H, the isomorphism f preserves adjacencies, and deg(v) = deg(f (v)) for all v ∈ G. Since f preserves adjacencies, then adjacencies along a given geodesic path are preserved as well. That is, if v1 , v2 , v3 , . . . , vk is a shortest path between v1 , vk ∈ V (G), then f (v1 ), f (v2 ), f (v3 ), . . . , f (vk ) is a geodesic path between f (v1 ), f (vk ) ∈ V (H). Example 1.15. Consider the graphs in Figure 1.11. Which pair of graphs are isomorphic, and which two graphs are non-isomorphic? Solution. If G is a Sage graph, one can use the method G.is_isomorphic() to determine whether or not the graph G is isomorphic to another graph. The following Sage session illustrates how to use G.is_isomorphic().

sage: ....: sage: ....: sage: ....: sage: True sage: False sage: False C6 = Graph({"a": ["b", "c"], "b": "d": ["b", "f"], "e": ["c", "f"], G1 = Graph({1: [2, 4], 2: [1, 3], 5: [4, 6], 6: [3, 5]}) G2 = Graph({"a": ["d", "e"], "b": "d": ["a", "e"], "e": ["a", "d"], C6.is_isomorphic(G1) C6.is_isomorphic(G2) G1.is_isomorphic(G2) ["a", "d"], "c": ["a", "e"], \ "f": ["d", "e"]}) 3: [2, 6], 4: [1, 5], \ ["c", "f"], "c": ["b", "f"], \ "f": ["b", "c"]})

Thus, for the graphs C6 , G1 and G2 in Figure 1.11, 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.

16

CHAPTER 1. INTRODUCTION TO GRAPH THEORY

1.4.1

Adjacency matrices

Two n × n matrices A1 and A2 are permutation equivalent if there is a permutation matrix P such that A1 = P A2 P −1 . In other words, A1 is the same as A2 after a suitable 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.16. 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. Deﬁne an ordering on the set of n×n (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

**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

Compute the adjacency matrix Ai of Gi (i = 1, 2). Compute the lexicographically maximal element Ai of the permutation equivalence class of Ai , for i = 1, 2. if A1 = A2 then return True else return False end Algorithm 1.1: Computing graph isomorphism using canonical labels.

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.

1.4.2

Degree sequence

Deﬁnition 1.17. 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

n − 1. . . From Corollary 1. Also. m. 2. the number of connected components of a graph. 2. Let S = (di )n be a graphical sequence. . 4. n. has the degree sequence 2. n. di ≥ dj for all i ≤ j such that 1 ≤ i. 2. . the degree sequence. . the number of vertices.4. Graphical sequence. the complete graph Kn has the degree sequence n − 1.4. 2. 2.n has the degree sequence n. Let S be a non-increasing sequence of nonnegative integers. .3 Invariants revisited In some cases. the length of the longest path. . the graphs C6 and G1 in Figure 1. m. m copies of n n copies of m Deﬁnition 1. the length of a geodesic path. 2. one could show that they have diﬀerent values for a given graph invariant. j ≤ i=1 n. To prove that two graphs are non-isomorphic. 2 . . for n ≥ 3. 1. 1. ISOMORPHIC GRAPHS 17 of the house graph in Figure 1. the sum of a graphical sequence is non-negative and even.18. 3. m.1 is 3. n copies of 2 The path Pn .8 we see that di ∈S di = 2k for some integer k ≥ 0. . . n − 1. one can distinguish non-isomorphic graphs by considering graph invariants. .1. . 2. Then S is said to be graphical if it is the degree sequence of some graph. 3. 2. . 6. . . 2. The following list contains some items to check oﬀ when showing that two graphs are non-isomorphic: 1. n − 1 n copies of n−1 and the complete bipartite graph Km.11 are non-isomorphic because the former is connected. For instance. .e.11 are isomorphic so they have the same number of vertices and edges. G1 and G2 in Figure 1. In other words. m . . n−2 copies of 2 For positive integer values of n and m. . while the latter is not connected. 5. . . the number of edges. If n ≥ 3 then the cycle graph Cn has the degree sequence 2. 1 . . i. n.

6: [3. 10: [7. as the following example shows. vertex addition. E1 ) and G2 = (V2 .5 New graphs from old This section provides a brief survey of operations on graphs to obtain new graphs from old graphs. A= A1 0 . Find matrix theoretic criteria on J1 and J2 which hold if and only if G1 ∼ G2 . The symmetric diﬀerence (or ring sum) of graphs is deﬁned as follows.4]. then G1 ∪ G2 is simply the graph consisting of all edges in G1 or in G2 . E1 ∩ E2). products.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 . 1. 0 A2 Sage can compute graph unions. ﬁnd the analog of Theorem 1.10].16 for incidence matrices. edge addition.6]. The intersection of graphs is deﬁned as follows. then G1 ∩ G2 is simply the graph consisting of all edges in G1 and in G2 . and join The disjoint union of graphs is deﬁned as follows. For two graphs G1 = (V1 .) 1. 2: [1.10]. sage: G1 = Graph({1: [2. their disjoint union is the graph G1 ∩ G2 = (V1 ∩ V2 .5]}) sage: G2 = Graph({7: [8. 4: [1. 8: [7.8].union(G2) sage: G1u2. their symmetric diﬀerence is the graph G1 ∆G2 = (V1 ∆V2 . E2 ) with disjoint vertex sets. 11: [10. E1 ) and G2 = (V2 . respectively.4 1. 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 . edge deletion. In case V1 = V2 .6]. intersection. = In other words. INTRODUCTION TO GRAPH THEORY Problems 1. E1∆E2). 9: [8. E1 ∪ E2). E1 ) and G2 = (V2 . Namely.5].12]. their disjoint union is the graph G1 ∪ G2 = (V1 ∪ V2 . Such graph operations include unions.3]. 3: [2.18 CHAPTER 1. E2 ) with disjoint vertex sets. 12: [9. Let J1 denote the incidence matrix of G1 and let J2 denote the incidence matrix of G2 . E2 ) with disjoint vertex sets.1 Union. and vertex deletion.9]. For two graphs G1 = (V1 . 5: [4. .7]}) sage: G1u2 = G1. Several of these are brieﬂy described below.5. For two graphs G1 = (V1 .

12 shows various wheel graphs.12: The wheel graphs Wn for n = 4. 4: [1. . 1. (3. (2.4]. 4). . (1. E) is any graph with at least 2 vertices. For example. Figure 1. (a) W4 (b) W5 (c) W6 (d) W7 (e) W8 (f) W9 Figure 1. 2). 3. 4. Sage can compute vertex deletions. The vertex deletion subgraph of G is sometimes denoted G − {v}.edges(labels=False)). 4). 5. E1 {(1. 4). but not in both. sage: G = Graph({1: [2.1. 4)} sage: E4 = Set(G. then G1 ∆G2 is simply the graph consisting of all edges in G1 or in G2 . NEW GRAPHS FROM OLD Recall that the symmetric diﬀerence of two sets S1 and S2 is deﬁned by S1 ∆S2 = {x ∈ S1 ∪ S2 | x ∈ S1 ∩ S2 }.6]. as the following example shows.edges_incident(vertices=[4]. (3.vertices() [1. . 4)} sage: G. then the vertex deletion subgraph is the subgraph obtained from G by deleting a vertex v ∈ V and also all the edges incident to that vertex. with each vertex of one graph connecting to each vertex of the other graph. 6).2]. 5). (2. (1.3]. 2. 9. / 19 In the case where V1 = V2 . is their graph union. labels=False)). 5: [4.4]. (4. sometimes G1 ∆G2 is written as G1 ⊕ G2 . 5). 2: [1. (2. The join of two disjoint graphs G1 and G2 . . 4). In this case. (2. the join of the cycle graph Cn−1 with a single vertex graph is the wheel graph Wn . 6: [3. 3: [2.2 Edge or vertex deletion/insertion Vertex deletion subgraph If G = (V.1]}) sage: G. denoted G1 +G2 .delete_vertex(4) . E4 {(4.5. 6). (1.5. 5). 3). (3. 6] sage: E1 = Set(G.

3). (1.edges(labels=False)). Edge cut. (2. but exactly those edges that are not in the original graph. 6). 4). 2). v2 ∈ V are two non-adjacent vertices.4]. 6).2]. 6: [3.4]. 4. edge contraction also merges together the two vertices the edge used to connect. The edge deletion subgraph of G is sometimes denoted G − {e}.3 Complements The complement of a simple graph has the same vertices. removes an edge from a graph. 3: [2. (2. 4).4)]). 6] sage: E14 = Set([(1. E c ) is the complement of . However. that edge is called a bridge. 6). 5). (3. unlike edge deletion. Sage can compute edge deletions. 6] sage: E2 = Set(G. Sage’s edge_cut method allows you to compute a minimal cut having this property. cut vertex. In other words.difference(E2) == E4 True Edge deletion subgraph If G = (V. (4. 3). E) is a subset W ⊆ V such that the vertex deletion subgraph G − W is disconnected. as the following example shows. 5: [4.6]. cut edge.vertices() [1. 2: [1. 6)} sage: E1. 4)} sage: E1. v2 belong to diﬀerent components of G − F . 3. V1 [1. then you can ask for an edge cut F for which v1 . 5).difference(E2) == E14 True Vertex cut. E) is a set of edges F ⊆ E whose removal yields an edge deletion subgraph G − F that is disconnected. 3). (2. E14 {(1.edges(labels=False)). 4)} sage: G. or bridge If deleting a single. 5). if Gc = (V. then you can ask for a vertex cut W for which v1 . 5. 3. sage: G = Graph({1: [2. (1. (2. (3. the edge cut (or disconnecting set or seg) of a connected graph G = (V. A minimal edge cut is called a cut set or a bond. 2.vertices(). More generally. 4)} sage: V1 = G. (2.delete_edge([1. (3. Edge contraction An edge contraction is an operation which. 2). speciﬁc edge would disconnect a graph G. if v1 . v2 ∈ V are two vertices. (3. E) is any graph with at least 1 edge. 2). 4). E1 {(1. but not the vertices incident to that edge. (2. if v1 .3]. 5). 2. 1.5. E2 {(1. (1. v2 belong to diﬀerent components of G − W .4]) sage: E2 = Set(G. 5. or cutpoint A vertex cut (or separating set) of a connected graph G = (V. then the edge deletion subgraph is the subgraph obtained from G by deleting an edge e ∈ E. 6). like edge deletion. (2. Sage’s vertex_cut method allows you to compute a minimal cut having this property. In fact. In fact. 6). 5). INTRODUCTION TO GRAPH THEORY sage: G. 4: [1.edges(labels=False)). (4.1]}) sage: E1 = Set(G. (3. E2 {(1.20 CHAPTER 1. (2. (1.

19.intersection(EGc) {} Theorem 1. u = v and u is adjacent with v in G. except for 0’s on the main diagonal.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) × V (H). (1. (2.5. Sage can compute Cartesian products. 5).2]. 6). (3. then two distinct vertices v. 3: [2. u = v and u is adjacent with v in H. A simple graph that is isomorphic to its complement is called a self-complementary graph.9] we have 4 | n and so n = 4k for some non-negative k ∈ Z. 1. The relative complement of G and H is the edge deletion subgraph G − E(H). . (5. Any two vertices (u. EG {(1.edges(labels=False)). 3). 4: [1. If G = (V. 3). Each of G and Gc contains half the number of edges in Kn . Sage can compute edge complements. If n − 1 is even. n − 1) = 1. 2 2 4 Then n | n(n − 1). From (1.5. 6). E) is self-complementary. Let G be a self-complementary graph of order n. then the size of G is |E| = n(n − 1)/4.1]}) sage: Gc = G.4]. 6).difference(EG) == EGc True sage: EG. as the following example shows. 5). with one of n and n − 1 being even and the other odd. (4. w ∈ V are adjacent in Gc if and only if they are not adjacent in G. (1. hence by [23. u ) and (v. n − 1 is odd so gcd(4. 5: [4. 2). (2. E). 5)} sage: EG.3]. sage: G = Graph({1: [2. or 2. 4)} sage: EGc = Set(Gc.2). The sum of the adjacency matrix of G and that of Gc is the matrix with 1’s everywhere. use a similar argument to conclude that n = 4k + 1 for some non-negative k ∈ Z.4]. (3.complement() sage: EG = Set(G. NEW GRAPHS FROM OLD 21 G = (V. if n = |V | is the order of G. Proof. 6). Furthermore. EGc {(1.edges(labels=False)).1.6]. v ) are adjacent in G H if and only if either 1. 2: [1.difference(EGc) == EG True sage: EGc. we delete from G all edges in H. (3. That is. (4. we have |E(G)| = |E(Gc )| = n(n − 1) 1 n(n − 1) · = . as the following example shows. (2. 6: [3. 5). Theorem 1. 4). (2. 6). (1. If n is even. Let H be a subgraph of G. 4). then the order of G is |V | = 4k or |V | = 4k + 1 for some non-negative integer k. The vertex set of G H is V (G H) = V (G) × V (H) and the edge set of G H is the union E(G H) = V (G) × E(H) ∪ E(G) × V (H) .

len(Z. INTRODUCTION TO GRAPH THEORY sage: Z = graphs. . (a) Q1 (b) Q2 (c) Q3 (d) Q4 Figure 1.13: Hypercube graphs Qn for n = 1.CycleGraph(5). . .cartesian_product(Z). That is. . vi+1 ) | 1 ≤ i ≤ n − 1}. . In this case. . The ladder graph Ln is the Cartesian product of path graphs. . n) | i ∈ {0. .22 CHAPTER 1. .vertices()).CompleteGraph(2).e. w ∈ V are connected by an edge if and only if v and w diﬀer in exactly one coordinate.13 illustrates the hypercube graphs Qn for n = 1. The path graph Pn can be obtained from the cycle graph Cn by deleting one edge of Cn . Two vertices v. .edges()) 2 1 sage: C = graphs. len(C. .edges()) 10 15 The hypercube graph Qn is the n-regular graph with vertex set V = ( 1.20. Example 1. Ln = P n P 1 . . 4. .edges()) 5 5 sage: P = C. deg(v1 ) = deg(vn ) = 1 and deg(vi ) = 2 for 1 < i < n. . . 4. vn } and edges E = {(vi . . each vertex of Qn is a bit string of length n. len(P.3 The Cartesian product of n edge graphs K2 is a hypercube: (K2 ) n = Qn . .vertices()). len(Z. len(P. 1} of cardinality 2n . the “Hamming distance” between v and w is equal to 1. 3 In other words.vertices()). The Cartesian product of two hypercube graphs is another hypercube: Qi Qj = Qi+j . Figure 1. The path graph Pn is a tree with n vertices V = {v1 . len(C. i.

We shall see examples of this in Chapter 6.5 1. • Find a walk that visits each vertex. Show that |E(G H)| = |V (G)| · |E(H)| + |E(G)| · |V (H)|. However. This is connected with various applications to “social network analysis” and is covered in more details in Chapters 4 and 9. A graph minor is not in general a subgraph.1. is a given graph planar? This problem is important for designing computer chips and wiring diagrams. • A planar graph is a graph that can be drawn on the plane in such a way that its edges intersect only at their endpoints. ﬁnd a “cheapest” closed path which contains all the vertices. This type of problem is related to “spanning trees” and is discussed in further details in Chapter 3. This result is known by various names including the Robertson-Seymour theorem. Show that the complement of an edgeless graph is a complete graph. .5 Graph minors A graph H is called a minor of a graph G if H is isomorphic to a graph obtained by a sequence of edge contractions on a subgraph of G. . or Wagner’s conjecture (named after Klaus Wagner). with no crossing edges? In other words. Robertson & Seymour 1983–2004. the graph minor theorem. the relation“being a minor of” is a partial ordering on the set of graphs. then G1 is a minor of G3 . of ﬁnite graphs is given. If an inﬁnite list G1 . The order in which a sequence of such contractions is performed on G does not aﬀect the resulting graph H.5. • Determine which vertices are “more central” than others. • If the edge weights are all non-negative. 1. Many classes of graphs can be characterized by forbidden minors: a graph belongs to the class if and only if it does not have a minor from a certain speciﬁed list. Therefore. then there always exist two indices i < j such that Gi is a minor of Gj . COMMON APPLICATIONS 23 1. if G1 is a minor of G2 and G2 is a minor of G3 . .21. The following non-intuitive fact about graph minors was proven by Neil Robertson and Paul Seymour in a series of 20 papers spanning 1983 to 2004.6. Further discussion is contained in Chapter 6. Can a graph be drawn entirely in the plane. . Let G H be the Cartesian product of two graphs G and H.6 Common applications A few other common problems arising in applications of weighted graphs are listed below. Problems 1. but contains as few edges as possible and contains no cycles. This is related to the famous traveling salesman problem and is further discussed in Chapters 2 and 5. Theorem 1. G2 . 2.

one of which is to scheduling of jobs relying on a shared resource. such as operations research. this is called an edge coloring. this is called a vertex coloring. • In some ﬁelds. which has many industrial applications. which has more incoming ﬂow. except when it is a “source node”. Can you label or “color” all the edges of a graph in such a way that no incident edges have the same color? If so. the vertices are called nodes. What is the maximum ﬂow on a network and how to you ﬁnd it? This problem. the edges are called arcs. and the weight on an edge is called its capacity. This is discussed further in Chapter 7. which has more outgoing ﬂow. or a “sink node”. A “network ﬂow” must satisfy the restriction that the amount of ﬂow into a node equals the amount of ﬂow out of it. is discussed in Chapter 8.24 CHAPTER 1. INTRODUCTION TO GRAPH THEORY • Can you label or “color” all the vertices of a graph in such a way that no adjacent vertices have the same color? If so. a directed graph with non-negative edge weights is called a network. Graph coloring has several remarkable applications. . The ﬂow along an edge must not exceed the capacity.

2. We shall examine several in this chapter. line 7 in Algorithm 2. Suppose you want to count the number of vertices (or edges) satisfying a property P . Other variations are also possible as well.4. [’e’. Shortest path algorithms are some of the most important algorithms in algorithmic graph theory.1 Graph searching This section discusses algorithms for • breadth-ﬁrst searches. 0]. To determine the cheapest travel route from cityto-city.e. For the example of the graph in Figure 1. [’b’. i.1 is where you are searching the graph for a vertex (or edge) satisfying a certain property P . the list of distances from vertex a to any other vertex is [[’a’. Another version of Algorithm 2. 1]. 25 . 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. 2]] To create this list. [’f’. and • we explain how these relate to determining a graph’s connectivity.1. • Start at a and compute the distance from a to itself. [’g’. 2].1. you simply quit at the step where you increment the counter. Suppose you are a salesman with a product you would like to sell in several cities. [’d’. 2]. In that situation.1 presents a technique for ﬁnding the number of vertices satisfying P . • depth-ﬁrst searches. Algorithm 2.1 Breadth-ﬁrst search Breadth-ﬁrst search (BFS) is a strategy for running through the nodes of a graph. 2. 3]. [’c’.Chapter 2 Graph Algorithms Graph algorithms have many applications. you must eﬀectively search a graph having weighted edges for the “cheapest” route visiting each city once. 1].

and compute the distance from a to it.a connected graph v0 . E) (and. namely just c. 4]. Add such w to T .26 CHAPTER 2. a starting or “root” vertex v0 ∈ V ). • Move to each “unseen” neighbor of b. namely just f. • Move to each neighbor of a. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 Create a queue Q of “unseen” vertices initially containing a starting vertex v0 . As an example. optionally. and compute the distance from a to it. • Move to each “unseen” neighbor of e. 2: [1. Start a list T of “already seen” vertices initially empty. namely just g. 4]. here is some Sage code which implements BFS to compute the list distances from a given vertex. v0): """ Breadth first search algorithm to find the distance from a fixed vertex $v_0$ to any other vertex. def graph_distance(G.a vertex OUTPUT: D . • Move to each “unseen” neighbor of f. namely b and e. • Move to each “unseen” neighbor of c. Remove all “seen” vertices w from Q. and compute the distance from a to it. 3: [2. if T = V then return count end end Algorithm 2. INPUT: G . if P (w) = True then count ← count + 1 end Add all neighbors of w not in T to Q. .a list of distances to every other vertex EXAMPLES: sage: G = Graph({1: [2. count ← 0 for w ∈ Q do Test w for P . namely just d.1: Breadth-ﬁrst search. A property P to be tested. GRAPH ALGORITHMS Input : A connected graph G = (V. and compute the distance from a to each of them. and compute the distance from a to it. 6]. Output: The number of vertices of G satisfying P .

vertices() Q = [v0] T = [] D = [] while Q<>[] and T<>V: for v in Q: if not(v in T): D. [’c’. 2]. 5: [6]. Repeat this until a compete tour is found.v)]) if v in Q: Q. 2]. 6: [3. 6: [5]}) sage: v0 = 1 sage: graph_distance(G. In the case of a graph. [2.append([v. with a single knight piece on the board. [’g’. v0) [[’a’. If you fail again.G. [4. Naively. then you will have found a so-called complete knight tour. [3. 1]. It is very. 1]. as illustrated below.sort() print Q.1. "d": ["f"]. 3].2. can you modify the above function to return a list of shortest paths from v0 to any other vertex? 2. "e"]. [’b’. 2].2 to a so-called iterative DFS. "f": ["g"]. T return D 27 Exercise 2. [6. This is an example of depth-ﬁrst search. 2]] sage: G = Graph({1: [2. proceeding from vertex-to-vertex in this search but moving along a spanning tree of that graph. very unlikely that if do this you will have visited every square exactly once (if you don’t believe me. Acknowledging defeat. 1]] """ V = G.2 Depth-ﬁrst search A depth-ﬁrst search is a type of algorithm that visits each vertex of a graph. Suppose you want to count the number of vertices (or edges) satisfying a property P . until you could not make any more moves. GRAPH SEARCHING 4: [1. hoping to make further progress. recording each move to ensure that you did not step on a square you have already visited. [’e’. "e": ["f"]. 2]. 1]}) sage: v0 = 1 sage: graph_distance(G. backtracking along a new path. "e"]. 2: [1. Similar to BFS.neighbors(v) if not(x in T+Q)] if T == V: break D. Using Sage’s shortest_path method. 5: [4. v0) # note G is disconnected [[1. [3. 2]. please try it yourself!). how do you ﬁnd a complete knight tour? Intuitively. 1].3]. 3]. Suppose you have a normal 8×8 chess board in front of you. [5. It is easier to see what this means in the case of a rooted tree than for more general graphs. try backtracking a few move moves and traverse yet another path. "b": ["c". hoping you will not get “stuck” so soon. you can modify Algorithm 2. \ "c": ["d". 0]. if necessary.v0) [[1. 0]. [2. 0]. at this stage. One starts at a root vertex and explores as far as possible along each branch before.remove(v) T. [’f’. 3]. depth-ﬁrst search (DFS) is an algorithm for traversing a graph.1. "e"]. If you can ﬁnd a sequence of knight moves which visits each and every square exactly once. 3: [2]. This modiﬁcation applies DFS repeatedly with an increasing depth of search at each .distance(v0. 4: [5].append(v) T = list(Set(T)) Q = Q+[x for x in G. also sometimes called backtracking. [’d’.1. 1]. it might make sense to backtrack a few moves and try again. 1]] sage: G = Graph({"a": ["b". "g":["b"]}) sage: v0 = "a" sage: graph_distance(G. you would make one knight move after another. 1].

and also detect if the graph has a negative weight cycle. if P (w) = True then count ← count + 1 end Add w to S. Some compute the shortest paths from any vertex to any other. 2. • Proceed from that vertex using either DFS or BFS. False otherwise. Some only work if the graph has no negative weight cycles.1.2 Shortest path algorithms Let G = (V. We write δ(v. Initialize a list S of “seen” vertices.28 CHAPTER 2. .3 Application: connectivity of a graph A simple algorithm to determine if a graph is connected might be described as follows: • Begin at any arbitrary vertex of the graph. Algorithm 2. • Once the connected component of the graph has been entirely traversed. E) with root vertex v0 ∈ V . w) is sometimes called a shortest path from v to w. Γ = (V. step. until the diameter of the graph is reached and all vertices are seen. The length of a path P from v ∈ V to w ∈ V is the sum of the edge weights for each edge in the path P . No matter what algorithm you use. count ← 0 for w ∈ Q do Test w for P . GRAPH ALGORITHMS Input : A rooted tree G = (V. There are a number of diﬀerent algorithms for computing a shortest path in a weighted graph. the graph is connected and otherwise it is disconnected. denoted δ(P ). if S = V then return count end end Call the DFS algorithm iteratively with the rooted subtree having w and all its children as vertices and w as the rooted vertex.2: Depth-ﬁrst search. 2. E). w(e) for e ∈ E. if the number of vertices counted is equal to |V |. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 Create a queue Q of “child” vertices of the root v0 . a path from v to w which realizes δ(v. When we regard these weights w as distances. the length of the shortest path cannot exceed the number of vertices in the graph. Some assume that there is a single start or source vertex. Output: True if G has a vertex satisfying P . w) for the smallest value of δ(P ) for all paths P from v to w. E) be a graph with non-negative edge weights. counting all vertices reached.

6 of Berman and Paul [5]. Dijkstra’s algorithm performs a number of steps. v0 ) obtained by computing δ(P ) for a path P from v to v0 which has been found (but may not be the shortest path). and section 12. For example.2.2.2. 1 This can be improved. in the case of “sparse” graphs to O(n log n). or an upper bound on δ(v. as above. Dijkstra in 1959. [11]. The vertices v ∈ F are labeled with δ(v. Observe that at most n − 1 edges are required to construct a path from v to any vertex w. section 24. This temporary label can be either ∞ if no path from v to v0 has yet been examined. Let G = (V. 1 1 4 3 2 2 3 7 9 2 4 10 8 0 Figure 2. if the vertices of a weighted graph represent cities and edge weights represent distances between pairs of cities connected by a direct road.3 of Cormen et al. P = (v0 = v → v1 → v2 → · · · → vk = w). Fix a start or source vertex v0 ∈ V . where P is the same as p except that all cycles have been removed. E) and let n denote the number of vertices of G. is a graph search algorithm that solves the single-source shortest path problem for a graph with non-negative edge weights. it must have no more than n − 1 edges. SHORTEST PATH ALGORITHMS 29 Lemma 2. basically one step for each vertex in V . The vertices v ∈ Q are labeled with a temporary label L(v). with some clever programming. Let P denote such a path.1: Searching a weighted digraph using Dijkstra’s algorithm. The simplest implementation of Dijkstra’s algorithm has running time O(|V |2 ) = O(n2 ) (where n = |V | is the number of vertices of the graph)1 . Since the ﬁnal path is acyclic. We partition the vertex set V into two subsets: the set F of vertices where we have found the shortest path to v0 . . we can remove all cycles from P and obtain a path P from v to w of lower weight. v0 ). E) be a graph with non-negative edge weights. If there are no negative weight cycles in G then there exists a shortest path from v to any other vertex w ∈ V which uses at most n − 1 edges. discovered by E. proof: Suppose that G contains no negative weight cycles. Fix a vertex v in the connected graph G = (V. and the “queue” Q where we do not yet know for sure the shortest path to v0 . Dijkstra’s algorithm can be used to ﬁnd the shortest route from a ﬁxed city to all other cities.1 Dijkstra’s algorithm See Dijkstra [12]. 2. Since G has no negative weight cycles. Dijkstra’s algorithm.2. the weight of P is no less than the weight of P . Thus. n = |V |.

Remove v from Q. Initialize labels L(v0 ) = 0 and L(v) = ∞ for all v ∈ V with v = v0 . E) having non-negative edge weights and a starting vertex v0 ∈ V . if F = V then return end end for w ∈ Q such that w is adjacent to v do Replace L(w) by min(L(w). L(v) + wt(v. Add v to F .3: Dijkstra’s algorithm. v0 ) = L(v). Go to step 4. w)). . if no such v exists then return else Label v with the distance δ(v. Output: A shortest path from v0 to an vertex in V .1: Stepping through Dijkstra’s algorithm. v0 0 v1 v2 v3 v4 ∞ ∞ ∞ ∞ 10 3 ∞ ∞ 7 11 5 7 11 9 Table 2. GRAPH ALGORITHMS Input : A connected graph G = (V. Find v ∈ Q for which L(v) is ﬁnite and minimum.30 CHAPTER 2. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 Create a queue Q of “unseen” vertices initially being all of V . end Algorithm 2. Start a list F of “already seen” vertices initially empty.

Apply Dijkstra’s algorithm to the graph in Figure 2. 2 1 1 3 2 1 2 3 3 3 1 6 4 1 1 0 Figure 2. Take the smallest distance from v0 to v3 . This is the ﬁrst row of the table.2. 4. Take the vertex v0 out of the queue. This is the last row of the table. Underline the smallest one and take its vertex (i. Replace the labels in the second row by min(L(w). v4 ) out of the queue. Dijkstra’s algorithm applied to the graph in Figure 2.2.2: Searching a directed house graph using Dijkstra’s algorithm. SHORTEST PATH ALGORITHMS Example 2.1.1 yields Table 2. Underline the smallest one and take its vertex (i.2. v2 ) out of the queue.e. v0 0 v1 v2 v3 v4 ∞ ∞ ∞ ∞ 1 3 ∞ 6 2 4 6 3 6 5 Table 2. Consider the set of all adjacent nodes to v0 . Replace the labels in the ﬁrst row by the weights of the associated edges. 3. Initialize the labels L(v) to be ∞ for all v = v0 .4. The steps below explain how this table is created. . 31 Solution. L(v2 ) + wt(v2 .3. Verify the steps to create this table.e. 2. Start at v0 . This is the third row of the table. start from v4 and ﬁnd the path to the remaining vertex v3 in Q. w)).1. Dijkstra’s algorithm applied to the graph in Figure 2. Finally. 1. Consider the set of all nodes w which are adjacent to v2 . let Q = V and F = ∅. This is the second row of the table. Exercise 2.2: Another walk-through of Dijkstra’s algorithm.2 results in Table 2.

e. However. and creates two Python dictionaries dist and predecessor. s): """ Computes the shortest distance from s to all other vertices in Gamma.vertices() E = Gamma. INPUT: .wikipedia.2 Bellman-Ford algorithm See section 24. then an error is raised.32 CHAPTER 2. The Bellman-Ford algorithm computes single-source shortest paths in a weighted graph or digraph. . [11]. len(V)): for e in E: u = e[0] v = e[1] wt = e[2] if dist[u] + wt < dist[v]: dist[v] = dist[u] + wt predecessor[v] = u # check for negative-weight cycles for e in E: u = e[0] v = e[1] wt = e[2] if dist[u] + wt < dist[v]: raise ValueError("Graph contains a negative-weight cycle") return dist.s -. if a negative weight cycle exists (in the case of a digraph).2. OUTPUT: . then return an error. Here are some examples. def bellman_ford(Gamma.the source vertex.1 of Cormen et al.org/wiki/Bellman-Ford_algorithm """ P = [] dist = {} predecessor = {} V = Gamma. which store the distance and shortest paths. If Gamma has a negative weight cycle. the Bellman-Ford algorithm searches over all edges and keeps track of the shortest one found as it searches. i. keyed on the list of vertices. GRAPH ALGORITHMS 2.p) -.pair of dictionaries keyed on the list of vertices. and section 8. The implementation below takes in a graph or digraph.edges() for v in V: if v == s: dist[v] = 0 else: dist[v] = infinity predecessor[v] = 0 for i in range(1. where some of the edge weights may be negative.5 of Berman and Paul [5]. which is O(n3 ) for “dense” connected graphs (where n = |V |). which store the distance and shortest paths.a graph. Instead of the “greedy” approach that Dijkstra’s algorithm took. searching for the “cheapest” path.(d. REFERENCE: http://en. . predecessor Bellman-Ford runs in O(|V | · |E|)-time.Gamma -.

and section 14. ValueError: Graph contains a negative-weight cycle The plot of this graph is given in Figure 2.[1.3: Shortest paths in a weighted graph using the Bellman-Ford algorithm.0].4. The Floyd-Roy-Warshall algorithm (FRW).1..3 Floyd-Roy-Warshall algorithm See section 25.1.5].2. [0.0]. 1 5 3 1 1 3 0 4 2 Figure 2. 2.0.0.0. it allows for negative edge weights and detects a negative weight cycle if one exists.-4.1. It was .2. G.vertices()[0]) --------------------------------------------------------------------------.0]]) G = Graph(M. format = "weighted_adjacency_matrix") sage: bellman_ford(G.0.1. a single execution of the FRW algorithm will ﬁnd the shortest paths between all pairs of vertices.0].[1. format="weighted_adjacency_matrix") bellman_ford(G. sage: M = matrix([[0..3]. or the Floyd-Warshall algorithm. 2: 2.1.4 of Berman and Paul [5].2.2 of Cormen et al. Assuming that there are no negative weight cycles. Like the BellmanFord algorithm.0. is an algorithm for ﬁnding shortest paths in a weighted.vertices()[0]) 0.0. G.4: Searching a digraph with negative weight using the Bellman-Ford algorithm.4. directed graph. [0.0.0. The following example illustrates the case of a negative-weight cycle.1]. [11].3.0. 3 1 2 1 −4 1 1 1 1 1 0 Figure 2.[0. SHORTEST PATH ALGORITHMS 33 sage: sage: sage: {0: M = matrix([[0. [0. 1: 1.0]]) sage: G = DiGraph(M. 3: 5} The plot of this graph is given in Figure 2.

a matrix of distances of shortest paths.a matrix of shortest paths. Here is an implementation in Sage. The key to the Floyd-Roy-Warshall algorithm lies in exploiting this formula. SD(i. matrix(paths) Here are some examples.” which allows one to break the computation into simpler steps using some sort of recursive procedure. "A negative edge weight cycle exists. """ G = Graph(A. . n}. dist[i][j] +dist[j][k]) for i in range(n): if dist[i][i] < 0: raise ValueError. Temporarily label the vertices of G as V = {1." return dist. k) = min{SD(i.A -. the FRW algorithm is an example of “dynamic programming.j) in E: paths[i][j] = j if i == j: dist[i][j] = 0 elif A[i][j]<>0: dist[i][j] = A[i][j] else: dist[i][j] = infinity # iteratively finding the shortest path for j in range(n): for i in range(n): if i <> j: for k in range(n): if k <> j: if dist[i][k]>dist[i][j]+dist[j][k]: paths[i][k] = V[j] dist[i][k] = min(dist[i][k]. .dist -. . k) a shortest distance from vertex i to vertex j that only uses vertices 1 through k. which would be an O(n4 ) time algorithm for “dense” graphs. which is O(n3 ) time for “dense” graphs. If n = |V |. In some sense. . 2. j. k. FloydRoy-Warshall is better than an interated implementation of Bellman-Ford. This can be computed using the recursive expression SD(i. then this is a O(n3 ) time algorithm. The rough idea is as follows. we would need to iterate Bellman-Ford over all vertices. k − 1). j. Robert Floyd in 1962. Bellman-Ford only yields the shortest paths eminating from a single vertex.paths -.edges()] n = len(V) dist = [[0]*n for i in range(n)] paths = [[-1]*n for i in range(n)] # initialization step for i in range(n): for j in range(n): if (i. GRAPH ALGORITHMS discovered independently by Bernard Roy in 1959. def floyd_roy_warshall(A): """ Shortest paths INPUT: . For comparison. To achieve comparable output.vertices() E = [(e[0]. the Bellman-Ford algorithm has complexity O(|V | · |E|). k − 1)}. Call SD(i. j. and by Stephen Warshall in 1962. However. k − 1) + SD(k. format="weighted_adjacency_matrix") V = G.34 CHAPTER 2. .e[1]) for e in G. j. . Except possibly for “sparse” graphs.weighted adjacency matrix OUTPUT: .

ValueError: A negative edge weight cycle exists.0]])... [-1/2.1.0. 2. where n = |V | is the number of vertices of the original graph G. A floyd_roy_warshall(A) 1. The plot of this weighted digraph with four vertices appears in Figure 2. 1].1]. 1/2.1].2. E) be a graph with edge weights but no negative cycles.3 of Cormen et al.1.2. . 2.[-5. 3/2].0]]). [-1 [ 2 [-1 [ 2 A = matrix([[0.0. sage: sage: ([[0. 1. is O(|V |2 log |V | + |V | · |E)| = O(n2 log n).[1.0.2.4 Johnson’s algorithm See section 25.[-1/2. 3/2. Let G = (V.5: Demonstrating the Floyd-Roy-Warshall algorithm.0.3]. 0.2.0.[1.3]. for sparse graphs. 1 2 1] -1 2 3] 0 -1 1] 2 -1 -1]) The plot of this weighted digraph with four vertices appears in Figure 2. The time complexity.[0. 2. A sage: floyd_roy_warshall(A) Traceback (click to the left of this block for traceback) .3].0.[0. SHORTEST PATH ALGORITHMS 35 sage: A = matrix([[0. [11] and Johnson [16].2. 2].2.5.1. [1/2.0. [3/2.3]. 3 1 3 2 3 1 2 1 5 2 0 1 1 Figure 2. 0.2.0.1.6. Johnson’s algorithm ﬁnds a shortest path between all pairs of vertices in a “sparse” directed graph. 0]].

E) having (possibly negative) edge weights. w) + h(v) − h(w). Algorithm 2. the algorithm is terminated. run Dijkstra’s algorithm and store the computed least weight to other vertices. . 1 2 3 4 5 Add a new vertex v0 with zero weight edges from it to all v ∈ V . If the last step detects a negative cycle.6: Another demonstration of the Floyd-Roy-Warshall algorithm. Output: A shortest path between all pairs of vertices in V (or terminate if a negative edge cycle is detected). Input : A connected graph G = (V.36 CHAPTER 2.4: Johnson’s algorithm. GRAPH ALGORITHMS 1 1 1 3 1 3 1 3 2 2 2 −1/2 0 Figure 2. For each v ∈ V . is given the new length wt(v. Reweight the edges using the vertices’ h(v) values: an edge from v ∈ V to w ∈ V . w). the least weight of a path from the new node v0 to v ∈ V . Run the Bellman-Ford algorithm to check for negative weight cycles and ﬁnd h(v). having length wt(v.

Directed trees are pervasive in theoretical computer science.1 by using thicker line width for its edges.) A directed tree is a directed graph which would be a tree if the directions on the edges were ignored.1. Consider the 3 × 3 grid graph with 16 vertices and 18 edges. An n-ary tree is a rooted tree for which each vertex that is not a leaf has at most n children.Chapter 3 Trees and Forests Recall. we do not call v0 an endpoint or a leaf. v) is an edge in a rooted tree. A rooted tree can be regarded as a directed tree since you imagine an edge E = {u. if G is a rooted tree with root vertex v0 and if the degree of v0 is one then. a path in a graph G = (V. being directed from u to v. We say G is acyclic. as they are useful structures for describing algorithms and relationships between objects in certain data sets. If e = (u. A vertex of a forest of degree one is called an endpoint or a leaf. or a forest. A spanning tree T of a connected. v). (However. undirected graph G is a subgraph containing all vertices of G which is a tree. if and only if v is further away from v0 than u is. 37 . An ordered tree is a rooted tree for which an ordering is speciﬁed for the children of each vertex. Two examples of a spanning tree are given in Figure 3. for u. v ∈ V . A connected forest is a tree. v}. Figure 3. e = (u. The case n = 2 are called binary trees. by convention. if it has no cycles. A rooted tree is a tree with a speciﬁed root vertex v0 . then we call v a child vertex with parent u. E) whose start and end vertices are the same is called a cycle.1: Spanning trees for the 4 × 4 grid graph. Example 3.

For any new edge e. there can be no edges. E) is a graph with n vertices. Theorem 3. If we think of this game in terms of a communication network. the join T + e has exactly one cycle. 3.38 CHAPTER 3. If T = (V. By deﬁnition. Alice’s goal is to tag the edges of a spanning tree. Two players Alice and Bob alternately tag elements of E. v ∈ V .) Solution.2. E1 ) union T2 = (V2 . Let T = (V. Assume it is true for all trees with |V | = k. (1) =⇒ (2): This basically follows by induction on the number of vertices. then Bob’s goal is to separate the network into pieces that are no longer connected to each other. E). Each move for Bob consists of destroying one edge. .. a tree has no cycles. while each move for Alice involves securing an edge against destruction. As in (6) we see adding just one edge in E1 − E2 to T will create a unique cycle in G. 4. Such a cycle is called a fundamental cycle of G. Let G = (V1 . for some k > 1. then the following statements are equivalent: 1. ). E2 ) a subgraph of G which is a tree. • For all partitions P of the vertex set V of G... 3. E) be a tree having k + 1 vertices. the number of edges of G that join vertices in diﬀerent classes of the partition is at least 2(|P | − 1).3. E2 ) be a graph and T = (V2 . E2 ). • Bob plays ﬁrst and Alice can win against all possible strategies of Bob. 6. due to Edmunds and Lehman. add reference later . |E| = |V | − 1. Theorem 3. This holds in the base case where |V | = 1 since in that case. T is connected and has n − 1 edges. We follow the description in Oxley’s survey (What is a matroid?’ . For any u. E). there is exactly one u-v path. The following statements are equivalent for a connected graph G. The following two-person game is played on a connected graph G = (V. • The graph G has 2 edge-disjoint spanning trees. 5. Recall a minimal edge cut of a graph is also called a bond of the graph. while Alice is aiming to reinforce edges of the network to prevent their destruction. Remove an edge (but not the vertices it is incident to). T is a tree. This disconnects T into T1 = (V1 . TREES AND FORESTS The following game is a variant of the Shannon switching game. Make the following induction hypothesis: for any tree T = (V. (The set of such fundamental cycles of G depends on T .. 2.1 Properties of trees The following theorem gives several basic characterizations of trees. T contains no cycles and has n − 1 edges. while Bob’s goal is to tag the edges of a bond. Every edge of T is a cut set.

Therefore. Let G = (V1 . Likewise. i = 1. Suppose that P = (v0 = w → v1 → v2 → · · · → vk = w) and P = (v0 = w → v1 → v2 → · · · → v = w) are two cycles in T ∪ ({u. Each of these satisfy. (5) =⇒ (6): Let e = (u. (2) =⇒ (3): If T = (V.1. (6) =⇒ (1): Condition (6) implies that T is acyclic. |E| = |E1 | + |E2 | + 1 = |V1 | − 1 + |V2 | − 1 + 1 = |V | − 1.) We must show T is connected. Therefore. E) connected then T = (V. where E = E −e. E2 ) be a graph and T = (V2 . This contradicts (2) unless k = 1. E ) is a tree. Ei ). . for some k. PROPERTIES OF TREES 39 where |E| = |E1 | + |E2 | + 1 and |V | = |V1 | + |V2 | (and possibly one of the Ei is empty). P contains a subpath P0 = P − e (which is not closed). . Adding the edge e = (u. {e}). . E) has k connected components then it is a disjoint union of trees Ti = (Vi . Suppose T is disconnected.4. T is connected. of T and v a vertex in another component. which contradicts (6). that is the same as P except it lacks the edge from u to v. (Otherwise. . it is trivial to make two cycles by adding an extra edge. these u-v paths p0 and P0 must be the same. so k k |E| = i=1 |Ei | = i=1 |Vi | − k = |V | − k. 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. (3) =⇒ (4): If removing an edge e ∈ E leaves T = (V. . then P is a cycle in T . say P does not contain e. This forces P and P to be the same. This is a contradiction to (5). v) be a new edge connecting u. that is the same as P except it lacks the edge from u to v. v) does not create a cycle (if it did then T1 and T2 would not be disjoint). Exercise 3. v}. P contains a subpath P0 = P − e (which is not closed). We may suppose now that P and P both contain e. T1 say. which proves (6). |Ei | = |Vi | − 1. this means that |E | = |E|−1 = |V |−1−1 = |V |−2. Therefore e is a cut set. Therefore. If either P or P does not contain e. which contradicts (3). Let u be a vertex in one component. each of which is a tree satisfying the conditions of the induction hypothesis. by (2).3. k. However. Let u = v0 and let v = v1 . v ∈ V . T2 say. (4) =⇒ (5): Let P = (v0 = u → v1 → v2 → · · · → vk = v) and P = (v0 = u → v1 → v2 → · · · → v = v) be two paths from u to v. 2. By (5). of T . Show there is a one-to-one correspondence between fundamental cycles in G and edges not in T. E2 ) a spanning tree of G.

Usually there exist many spanning trees of a graph.2 Minimum spanning trees Suppose you want to design an electronic circuit connecting several components. 3. Exercise 3. [11]. Kruskal’s algorithm is a greedy algorithm to compute a MST.5. It was discovered by J. ﬁnd necessary and suﬃcient conditions for a graph G such that if T is a spanning tree then T is unique. Dijkstra in the 1950’s. This amounts to ﬁnding a minimum spanning tree in the complete graph on these vertices. • Bor˚vka’s algorithm [8. E) be the 3 × 3 grid graph and T1 = (V1 . Exploiting the former criteria gives rise to Kruskal’s algorithm. Kruskal in the 1950’s. you will want to connect these together using the least amount of wire. Each of these conditions lead to an algorithm for constructing them.40 CHAPTER 3. E2 ) be spanning trees of G in Example 3. Find a fundamental cycle in G for T1 which is not a fundamental cycle in G for T2 . • spanning trees We can characterize a spanning tree in several ways. which is O(n2 ) for a dense graph having n vertices. • minimum-cost spanning trees A minimum spanning tree (MST) is a spanning tree of an edge weighted graph having lowest total weight among all possible spanning trees. Exploiting the latter criteria gives rise to Prim’s algorithm. 9] u Bor˚vka’s algorithm is an algorithm for ﬁnding a minimum spanning tree in a u graph for which all edge weights are distinct. Prim and E. Let G = (V. In can be implemented in time O(|E| + |V | log |V |). u . Prim’s algorithm is a greedy algorithm to compute a MST. Jarn´ and ık later independently by both the computer scientists R. The algorithm was developed in the 1930’s by Czech mathematician V.2 of Cormen et al. T2 = (V2 . [11].2 of Cormen et al. Both of these argorithms are discussed in more detail below. Kruskal’s algorithm can be shown to run in O(|E| log |E|) time. E1 ). see also section 23. TREES AND FORESTS Exercise 3. Can you classify those graphs for which there is only one spanning tree? In other words. for economical reasons. It was ﬁrst published in 1926 by Otakar Borøuvka but then rediscovered by many others. • Prim’s algorithm [22]. Another condition is that it is a minimal set of edges that connect all vertices. If these components represent the vertices of the graph and a wire connecting two components represents an edge of the graph then. • Kruskal’s algorithm [18]. see also section 23. One condition is that spanning tree of a connected graph G can also be deﬁned as a maximal set of edges of G that contains no cycle.6. Bor˚vka’s algorithm can be shown to run in time O(|E| log |V |).1.

1.[0. 1). format = "adjacency_matrix". you add e to T and otherwise.) """ T_vertices = G. 1.[0. In other words. we label the edges of G as E = {e1 .0. The tree T returned is a weighted graph.2. start a for loop over e ∈ E. SAGE def kruskal(G): """ Implements Kruskal’s algorithm to compute a MST of a graph. .0. Next. 7.0.edges() [(0. em }. 1). 1. OUTPUT: T .2. 1). (4.3]. 5. 1).PetersenGraph() sage: TE = kruskal(G). go to the next element of E in the for loop. (0. (1. INPUT: G . Let w : E → R denote the weight function. 3). n-1. 2. 6.a minimum weight spanning tree. (1. the edges of T have been completely found and the algorithm stops. where w(e1 ) ≤ w(e2 ) ≤ · · · ≤ w(em ). TE.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] . At the end of the for loop.edges() # a list of triples # start ugly hack Er = [list(x) for x in E] E0 = [] for x in Er: x. As long as this cycle condition fails. even if G is not. 4. (0. 2). 1). 1). .. 1)] TODO: Add ’’verbose’’ option to make steps more transparent. 3)] sage: G = graphs. v) to T would create a cycle would be if both u and v were endpoints of an edge already in T . 3.. MINIMUM SPANNING TREES 41 3..[0.append(x) E0. 1). weighted = True) sage: TE = kruskal(G). 1. 2. . 2. (1. (2. (3.2.a connected edge-weighted graph or digraph whose vertices are assumed to be 0. (0. The ﬁrst stage is to create a “skeleton” of the tree T which is initially set to be a graph with no edges: T = (V. E) as input. 9. (1. . 1). 1).0.sort() E = [] for x in E0: x. The next stage is to sort the edges of G by weight. (2.1. TE.0. 3. EXAMPLES: sage: A = matrix([[0.. 3. 8. (1. ∅). 2).3].reverse() E0.1 Kruskal’s algorithm Kruskal’s algorithm starts with an edge-weighted digraph G = (V. 3. (2. 2).1].edges() [(0. 1).edges() [(0.0]]) sage: G = DiGraph(A.vertices() # a list of the form range(n) T_edges = [] E = G. 3.2.3. (0. 1).reverse() E. . 1)] sage: G. You add e to T as an edge provided it does not create a cycle. The only way adding e = (u. 2. e2 . (Useful for teachers and students. If G is not explicitly edge-weighted then the algorithm assumes all edge weights are 1. (0.

edges() [(0.append(rw) AT = matrix(AT) return Graph(AT. . 1).j] in T_edges: rw[j] = AG[i][j] AT. sage: G = graphs. (11. .2 Prim’s algorithm Prim’s algorithm is an algorithm that ﬁnds a minimum spanning tree for a connected weighted undirected graph Γ = (V. . 1.v]) # find adj mat of T if G. 14. 11. 6. 1).adjacency_matrix() sage: G = Graph(A. (7. (8. 1). (1. (3. .weighted(): AG = G. n − 1}. 1).2. .1. 1). . 1).adjacency_matrix() GV = G. (1. 1). (2. (2. . (5.2: Kruskal’s algorithm for the 4 × 4 grid graph. T. 9. 12. (0. . We start with the grid graph. It is very similar to Kruskal’s algorithm except that it starts with an empty vertex set. This is implemented in Sage in a way that the vertices are given by the coordinates of the grid the graph lies on. Figure 3. Since the above implementation assumes that the vertices are V = {0. weighted = True) Here are some examples.42 CHAPTER 3. 1). 1).weighted_adjacency_matrix() else: AG = G. n − 1. E). 1.GridGraph([4. 1. . format = "adjacency_matrix". 8. 1). 1). (6. 1). 2. weighted = True) sage: T = kruskal(G).4]) sage: A = G.append([u. TREES AND FORESTS if not(u in TV and v in TV): T_edges. (10. 1).vertices() n = len(GV) AT = [] for i in GV: rw = [0]*n for j in GV: if [i. 15. rather than a full one. (9.7. 1)] The plot of this graph is given in Figure 3. 3. 1). we ﬁrst redeﬁne the graph suitable and run the Kruskal algorithm on that. 13. 4. format = "adjacency_matrix". 5.2. 3. as opposed to 0. 10. (4.

1 2 3 4 43 Initialize: V (T ) = {v0 }. SAGE def prim(G): """ Implements Prim’s algorithm to compute a MST of a graph. v) to E(T ).1. weighted = True) .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. Algorithm 3.wikipedia. T_edges # find adj mat of T if G. E) having edge weights.1: Prim’s algorithm.vertices() n = len(GV) AT = [] for i in GV: rw = [0]*n for j in GV: if [i.0]]) sage: G = DiGraph(A. format = "adjacency_matrix". add (u. Output: A MST T for G.append(v) # found T_vertices.3.adjacency_matrix() GV = G.reverse() E0.vertices = range(n) T_edges = [] E = G. E(T ) = ∅ While V (T ) = V : Choose edge (u. weighted = True) sage: A = matrix([[0.0.reverse() E.weighted_adjacency_matrix() else: AG = G.3].1.0. OUTPUT: T .vertices() # start ugly hack to sort E Er = [list(x) for x in E] E0 = [] for x in Er: x.3].[1.sort() E = [] for x in E0: x.2.append(rw) AT = matrix(AT) return Graph(AT. where v0 is an arbitrary vertex. Add v to V (T ). v) with minimal weight such that u is in V (T ) but v is not.a minimum weight spanning tree.1.edges() # a list of triples V = G.2. MINIMUM SPANNING TREES Input : A connected graph G = (V.weighted(): AG = G.[2.2. INPUT: G . REFERENCES: http://en.v]) T_vertices.1.a connected graph.[3.j] in T_edges: rw[j] = AG[i][j] AT.append(x) E0.1].org/wiki/Prim’s_algorithm """ T_vertices = [0] # assumes G. format = "adjacency_matrix".append([u.

0. 1). (0. [0.[0.0.0.5.0. 2). (0. 3. 2. 2).0. 0.0].0.8. 1). 1)] sage: prim(G) Multi-graph on 4 vertices sage: prim(G).0. 2. 3). 1.0.0.[0.0.0. 1)] 1 1 3 2 0 1 2 3 1 1 1 1 2 2 3 3 (a) 3 1 1 1 0 2 2 (b) Figure 3. TREES AND FORESTS sage: E = G. 3.0. (3.0. 3).0]. (1.0. 1.0.[0.5. 0. 1). 1).[0.0. (0.9]. (1.0. 1).0.3: Prim’s algorithm for digraphs. E [(0. Above is the original digraph and below is the MST produced by Prim’s algorithm. (3.15. (1.0. 2. 2). sage: A = matrix([[0.0].7. 2. 0.44 CHAPTER 3.0. 1. 2).0.0.8. 3. (3.0.edges().edges() [(0.[0.0. 1. (2. (1. 1).0.0].0.6.7.0]]) .11]. 3.9. (2. (2. 3).

1. On the left is the original graph. 4. (0. 3. 6. 6). 1. 4. and an empty set of edges T • While the vertices of G connected by T are disjoint: – Begin with an empty set of edges E – For each component: ∗ Begin with an empty set of edges S ∗ For each vertex in the component: . 5. 6. (2. E [(0. (1. 15). 6). 7). 4. 11)] sage: prim(G). 9)] 45 5 6 3 5 11 9 8 6 15 7 9 5 9 7 (a) 0 7 1 5 7 8 4 2 8 6 (b) Figure 3. 3. 2.3 Bor˚ uvka’s algorithm Bor˚vka’s algorithm algorithm is an algorithm for ﬁnding a minimum spanning tree in u a connected graph for which all edge weights are distinct.2. 9). 2. (4. 8). (3. 3.edges().edges() [(0. MINIMUM SPANNING TREES sage: G = Graph(A. (4. 3. 5). (3. 5).2. format = "adjacency_matrix". 8). 4. weighted = True) sage: E = G. On the right is the MST produced by Prim’s algorithm. 7). (5. (1. 5. (4. 7). 5. 7). 6. Pseudocode for Bor˚vka’s algorithm is: u • Begin with a connected graph G containing edges of distinct weights.3. (3. (1. (1. (0. 8). 9).4: Another example of Prim’s algorithm. 5). (1.

index(S) for S in L if x in S] almost works. On the right is the MST produced by Boruvka’s algorithm. TREES AND FORESTS · Add the cheapest edge from the vertex in the component to another vertex in a disjoint component to S Add the cheapest edge in S to E Add the resulting set of edges E to T.5 .0. Returns the index of the first sublist which x belongs to.0.7. SAGE def which_index(x.6].5]. The resulting set of edges T is the minimum spanning tree of G. On the left is the original graph. In Figure 3.L) which_index(7. weighted = True) sage: boruvka(G) 1 1 3 4 2 2 5 6 2 3 4 0 1 (a) 0 2 1 3 (b) Figure 3. we plot the following example. sage: A = matrix([[0.[6.[0.7.L) which_index(4.2.0. or None if x is not in flatten(L).L) .1.4].0.0]]) sage: G = Graph(A.3.2.5: An example of Borovka’s algorithm. Example 3.[4. EXAMPLES: sage: sage: 0 sage: 1 sage: 2 L = [[1. etc). format = "adjacency_matrix".0.5].8]] which_index(3.[0.46 CHAPTER 3. but if the list is empty then Lx[0] throws an exception.[0.L): """ L is a list of sublists (or tuple of sets or list of tuples. The 0-th element in Lx = [L.3].

4.0.0]]) sage: G = Graph(A.append(x) E0.edges().1. (1.[0.vertices = range(n) T_edges = [] T = Graph() E = G. 2. (4.12. 7).0.1. 3. (3. (0. 2). 11)] sage: boruvka(G) Multi-graph on 7 vertices .11]. MINIMUM SPANNING TREES sage: which_index(9. 1.L) == None True """ for S in L: if x in S: return L. 3). REFERENCES: http://en.reverse() E.vertices() if not(e[0] in TV) or not(e[1] in TV): T.0.0. 4.0.0. 8).sort() E = [] for x in E0: x.0].0.8.2.0.0.append(tuple(x)) # end ugly hack to get E is sorted by weight for e in E: # create about |V|/2 edges of T "cheaply" TV = T. 2.connected_components_subgraphs() VC = [S.vertices() # start ugly hack to sort E Er = [list(x) for x in E] E0 = [] for x in Er: x. 15).3].6. (1. 4).2.0. weighted = True) sage: boruvka(G) Multi-graph on 4 vertices sage: boruvka(G).5. (5.0.wikipedia. 3.0].7.0.[0.0. (0.3.is_connected(): break T. 3)] sage: A = matrix([[0.a minimum weight spanning tree.edges() [(0. 6.0]. 1).0.4].0. format = "adjacency_matrix".9. (1.[0.9]. format = "adjacency_matrix". 5.0.3. 1.0. 3.edges() # a list of triples V = G. (3.index(S) return None def boruvka(G): """ Implements Boruvka’s algorithm to compute a MST of a graph.add_edge(e) for e in E: # connect the "cheapest" components to get T C = T.L) sage: which_index(9.reverse() E0.org/wiki/Boruvka’s_algorithm """ T_vertices = [] # assumes G. (4.6]. OUTPUT: T .[10.0].\ [0. 6).6. E [(0. 9).[0. 2). INPUT: G . 5). weighted = True) sage: E = G.0. (0.[4.[0.0.0.VC) != which_index(e[1].[7.0]]) sage: G = DiGraph(A.a connected edge-weighted graph with distinct weights. 5.2.0.vertices() for S in C] if not(e in T. 1).8.0.0. (2.0.0.15.11.0.VC)): if T.add_edge(e) return T 47 Some examples using Sage: sage: A = matrix([[0.edges()) and (which_index(e[0].5.0. 4.0.

edges() [(0. 3). the letters in the English alphabet get converted into numbers {0.1.0. 1). . 3. The elements of this set are referred to as codewords. 0’s and 1’s).0.[0.” i. format = "adjacency_matrix". 5. For example.[0. (3. • Gray codes. 1. an alphabet for which each symbol a ∈ A is associated with a non-negative number wa ≥ 0 (in practice.3]. 5).0. 2). In this way.” since such codes are designed to communicate information over a noisy channel in such a way that the errors in transmission are likely to be correctable. 255}. 5. .0.edges() [(0.5.0. 2)] 3.2.. and • Huﬀman codes. using the ASCII code. 2. • Reliability. 3).0. If these numbers are written in binary then each codeword of a letter has length 8.[0.3 of Gross and Yellen [14]. in addition to simply being given an alphabet A. 4. 6.0.5].[0. 1). Such codes are called “error-correcting codes. (4. weighted = True) sage: boruvka(G).0.4]. we can reformat. TREES AND FORESTS sage: boruvka(G). (2. In this section. 6). .0]. 1. 1). 5).0].0. one might be given a “weighted alphabet.edges() [(0. a “string” into a sequence of binary symbols (i. (0.0.0]]) sage: G = Graph(A. .0. 4)] sage: A = matrix([[0. We shall identify a code as a ﬁnite set of symbols which are the image of the alphabet under this conversion rule.0.0]]) sage: G = Graph(A.1 Binary codes What is a code? A code is a rule for converting data in one format.. 3. weighted = True) sage: boruvka(G).6].0. A binary tree is a rooted tree with at most 2 children per parent.3.e.e. Decoding is the reverse process. 4)] sage: A = matrix([[0.48 CHAPTER 3. Sometimes this is called “entropy encoding” since there is an entropy function which describes how much information a channel (with a given error rate) can carry and such codes are designed to maximize entropy as best as possible.0. 1.2. (2. (1. 2).0.3 Binary trees See section 3.3.1. into sequences of symbols in another format (and the ﬁnite set of symbols used is called the alphabet). or encode. 3. 4.[0. Encoding is the conversion process one way. Codes are used for • Economy. 3.[0. (0. the probability that the symbol a occurs in a typical word). we consider • binary codes. (0.0. 2.4]. .0. 1.[0. converting these sequences of code-symbols back into information in the original format. (2. format = "adjacency_matrix". or well-deﬁned tangible representation. (4. In this case.

100 since the second codeword is a preﬁx of the third one. a non-example is the code 00. chess notation. A B C D E F G H I J K L M 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 1100 10 111 0110 1101 010 000 1 001 0001 011 1001 1011 Figure 3. the code is called a block code. .3. Basic deﬁnitions If every word in the code has the same length. genetic codes. In other words. . look at the Morse code for a and the Morse code for w. the coding function c is designed to be a “trapdoor function. 100.3. If a code is not a block code then it is called a variable-length code. football diagrams. 01. BINARY TREES 49 • Security.” Other codes are merely simpler ways to communicate information (ﬂag semaphores. . Another non-example is Morse code recalled in Figure 3. . and have little or no mathematical structure. We shall not study them. Codes which are preﬁx-free are easier to decode than codes which are not preﬁx-free. color codes. . These codewords violate the preﬁx-free condition.” In this case. the inverse of the coding function c : A → B ∗ is designed to be computationally infeasible.6: Morse code For example. A preﬁx-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 preﬁx (start) of any other codeword1 . One example of a preﬁx-free code is the ASCII code. 01. 010. tn if and only if m ≤ n and s1 = t1 . This is the preﬁx-free condition. braille codes. . Such codes ae called “cryptosystems. .6 (we use 0 for · (“dit”) and 1 for − (“dah”)). musical scores. a codeword s = s1 . . sm = tm . sm is a preﬁx of a codeword t = t1 . In other words. Another example is 00. 1 . On the other hand. and so on).

[1. aspects of combinatorics. since successive entries diﬀer in only one coordinate. Example 3. 1]. you know an error was made. 0. 0. 0. [1. . computational group theory and the computational aspects of linear codes). It was also used in E. 0]. since you can compare an n-tuple with the previous one. 0]. [0. 2]. and then patented a device (used for television sets) based on it in 1953. 0]. 1. [1. [1. [2. Gros on the solution to the “Chinese ring puzzle” published in 1872.50 Gray codes CHAPTER 3. Gray codes have applications to engineering. In fact. Baudot’s (a French engineer) telegraph machine of 1878 and in a French booklet by L. “The Brain” puzzle. . where • each gi ∈ Zn . [0. [1. 1. an m-ary Gray code of length n (called a binary Gray code when m = 2) is a sequence of all possible (namely. TREES AND FORESTS History2 : Frank Gray (1887-1969) wrote about the so-called Gray codes in a 1951 paper published in the Bell System Technical Journal. 2] and here is a binary Gray code of length 3: [0. 2]. Let Zm = {0. 1]. 1. [2.8. 1].2. 1]. N = mn ) n-tuples g1 . [1. . 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 . 1]. 0. 1. 1]. . 1]. If they difer in more than one coordinate. 0].1 (“Generating all n-tuples”) in volume 4 of Donald Knuth’s The Art of Computer Programming. and to mathematics (for example. m • gi and gi+1 diﬀer by 1 in exactly one coordinate. . 0]. . From the transmission/communication perspective. gN . 2 . the idea of a binary Gray code appeared earlier. The term “Gray code” is ambiguous. 0]. [0. [1. this sequence has two advantages: • It is easy and fast to produce the sequence. m − 1}. • An error is relatively easy to detect. the “Chinese ring puzzle”. [0. Here is a 3-ary Gray code of length 2: [0. However.1. [0. . This history comes from an unpublished section 7. it appeared in an earlier patent (one by Stibitz in 1943). g2 . recreational mathematics (solving the Tower of Hanoi puzzle. More precisely. and others). 1. . [2. In other words. 0]. It is actually a large family of sequences of n-tuples.

4) [[0. Figure 3. [0. 0].3. .3. [1]. 0].0 def graycode(length. This can be envisioned as the graph whose vertices are the vertices of a cube in n-space {(x1 . [1. Γn = [0. Γ1 = [0]. two vertices which diﬀer in exactly one coordinate). 1].7: Viewing Γ3 as a Hamiltonian path on Q3 . EXAMPLES: sage: graycode(2.modulus): """ Returns the n-tuple reflected Gray code mod m. BINARY TREES Binary Gray codes 51 Consider the so-called n-hypercube graph Qn . Γ2 = [[0. gets very long very fast). Γn−1 ]. In other words. by the way. . and so on. [1.7 for an example). Python 3. For instance. This is a nice procedure if you want to create the entire list at once (which. . A binary Gray code of length n can be regarded as a path on the hypercube graph Qn which visits each vertex of the cube exactly once. 0]. a binary Gray code of length n may be identiﬁed with a Hamiltonian cycle on the graph Qn (see Figure 3. n−1 where Γrev means the Gray code in reverse order. 1]. . How do you eﬃciently compute a Gray code? Perhaps the simplest way to state the idea of quickly constructing the reﬂected binary Gray code Γn of length n is as follows: Γ0 = []. . xn ) | 0 ≤ xi ≤ 1}. An implementation of the reﬂected Gray code using Python is given below. and whose edges are those line segments in Rn connecting two “neighboring” vertices (namely. we have m Γ0 = []. [1. Γrev ].

This has 28 = 256 codewords. TREES AND FORESTS """ n. 2]. Γ8 . [2. 3]..m = length. [3. 2].52 [1. 0]. 2].. The plot is given in Figure ??. The k-th element in the above-described reﬂected binary Gray code of length n is obtained by simply adding the binary representation of k to the binary representation of the integer part of k/2.3)+int2binary(int(j/2). This will give us some idea of how the Gray code “looks” in some sense. 1].. where x is an integer j ∈ Z256 which indexes the codewords in Γ8 and the corresponding y is the j-th codeword in Γ8 converted to decimal. SAGE def int2binary(m. 0]. [0.reverse() M0 = [] for i in range(m): M0 = M0+Mr[i] return M0 Consider the reﬂected binary code of length 8. [1.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]. [0. [3. y). [1. there is a very simple way to do this.modulus F = range(m) if n == 1: return [[i] for i in F] L = graycode(n-1.: print int2binary(j. 3]. 2]. [3. 0]. SAGE can easily create the list plot of the coordinates (x. [2. 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. padded by 0’s (on the left) to length n. An example using SAGE is given below.3) . What if you only want to compute the i-th Gray codeword in the Gray code of length n? Can it be computed quickly as well without computing the entire list? At least in the case of the reﬂected binary Gray code. EXAMPLES: sage: for j in range(8): . [2. n): ’’’ returns GF(2) vector of length n obtained from the binary repr of m. [3. [0. 3]] CHAPTER 3. [2. 3]. [1. 1]. 1]. 1].

1}. A word (or string or message) in A is a ﬁnite sequence of symbols in A. 0. ’’’ s = k = F = b = for 0) 1) 1) 0) 0) 1) 1) 0) bin(m) len(s) GF(2) [F(0)]*n i in range(2. 0. EXAMPLES: sage: graycodeword(3. If A is an alphabet.8: List plot of Γ8 created using Sage. 3. A commonly occurring alphabet in practice is the binary alphabet {0. 1. (0.. 0) ’’’ return int2binary(m.k): b[n-k+i] = F(int(s[i])) return vector(b) def graycodeword(m. (1. n): ’’’ returns the kth codeword in the reflected binary Gray code of length n.: (0.9. 1. 1. in which case a word is simply a ﬁnite sequence of 0’s and 1’s.3. . (0. (1. 1. (0. usually written by simply concatenating them together: a1 a2 . (1. Convert the above function graycodeword into a pure Python function.3) (0.3. 1. 0. .. whose elements are referred to as symbols. let .n) Exercise 3.3. 0. ak (ai ∈ A) is a message of length k.2 Huﬀman codes and Huﬀman’s algorithm An alphabet A is a ﬁnite set. . (1..n)+int2binary(int(m/2). BINARY TREES 53 Figure 3.

. The two children of this node. . i. Here is how to represent the code B consisting of all binary strings of length ≤ .” where s is a binary sequence of length ≤ . v0 and v1 . correspond to the strings of length 2 which start with a 0. v10 and v11 . TREES AND FORESTS A∗ denote the set of all words in A.9 for an example when • = 2. There are a total of 2 +1 − 1 nodes in this tree and 2 of them are “leaves” (vertices of a tree with degree 1. . The length of a word is denoted by vertical bars: if w = a1 . and the two children of v1 . Start with the “root node” v∅ being the empty string. Next.” The two children of v0 . v00 and v01 .54 CHAPTER 3. Let A and B be two alphabets. Continue creating child nodes until you reach length then stop. .e. Example 3.. A code for A using B is an injection c : A → B ∗ . The resulting labeled graph is the tree associated to the binary code C. First. start with the tree for B . Label v0 with a “0” and v1 with a “1. . ak | = k. remove all nodes associated to a binary string for which it and all of its descendents are not in C. we often denote the code simply by the set C = c(A) = {c(a) | a ∈ A}. The elements of C are called codewords.10. By abuse of notation. remove all labels which do not correspond with codewords in C. correspond to the strings of length 2 which start with a 1. ak is a word in A then deﬁne | .9: Example of a tree representation of a binary code In general. See Figure 3. . Label each node vs with the string “s. Tree representation Any binary code can be represented by a tree. correspond to the two strings of length 1. . If B is the binary alphabet then C is called a binary code. childless nodes). | : A∗ → R by |a1 . Note that the parent of any node is a preﬁx to that node. if C is a code contained in B then to create the tree for C. d 0 • d d d 00 • 10 • d d d d d d d 1 • d d d 11d• 01d• Figure 3.

. c(xk ) = c(y2 ) . Recall the Morse code in Table ??. 3 . This implies x1 = y1 . ak ) = c(a1 )c(a2 ) . x2 . . y are messages with c(x1 ) .12. B = {0. Other commonly occurring symbols used (the digits 0 through 9. so the substring c(x1 ) of the left-hand side and the substring c(y1 ) of the right-hand side are either equal or one is contained in the other. BINARY TREES 55 For “visualizing” the construction of Huﬀman codes later. . Suppose c(x1 ) . . Note these Morse codewords all have length less than or equal to 4.11. c(xk ) = c(y1 ) . . . . Example 3. . . If (for instance) c(x1 ) is properly contained in c(y1 ) then c is not preﬁx-free. (The codewords are determined by the following rules • The root node gets the empty codeword. ). Since c(ET ) = 01 = c(A). xk and y1 . if c(y1 ) is properly contained in c(x1 ).3. . y (which in turn implies k = and xi = yi for all i). . We want to show that if x1 . Show by giving an example that the Morse code is not preﬁx-free. 1} the binary alphabet and C : A → B ∗ the Morse code. The proof is by induction on the length of a message. Likewise. 1] satisﬁes a∈A p(a) = 1. this is the expected value E(X) of the random variable X which assigns to a randomly selected symbol in A. The case of length 1 follows from the fact that c : A → B ∗ is injective (by the deﬁnition of a code). and a code c : A → B ∗ . In fact. where p : A → [0. and some others). c(y ). In other words. it is clear that the Morse code is not uniquely decodable. Think of p(a) as the probability that the symbol a arises in an typical message. Therefore. the length of the associated codeword in C. . By the induction hypothesis. . . Solution. . c(ak ). Now remove this codeword from both sides. punctuation symbols. . .3. If the extension c : A∗ → T ∗ is also an injection then c is called uniquely decodable. Consider now a weighted alphabet (A. xk = y2 . If a code c : A → B ∗ is preﬁx-free then it is uniquely decodable. preﬁx-free implies uniquely decodable. These strings are equal. it is important to see that one can reverse this construction to start from such a binary tree and recover a binary code from it. . . . These facts together imply k = and xi = yi for all i. Let A denote the English alphabet. y . . .13. so c(x2 ) . Exercise 3. are also encodable in Morse code but they use longer codewords. c(xk ) = c(y1 ) . . We must show that the length m case is true. . p). c(y ) where m = max(k. Theorem 3. Suppose that the statement of the theorem holds for all codes of length < m. • Each left-ward branch gets a 0 appended to the end of its parent and each rightward branch gets a 1 appended to the end. p is a probability distribution on A. . . xk = y1 . . . The average word length L(c) is3 : In probability terminology. c(y ) then x1 . Uniquely decodable codes If c : A → B ∗ is a code then we can extend c to A∗ by concatenation: c(a1 a2 . c(x1 ) = c(y1 ).

56

CHAPTER 3. TREES AND FORESTS

L(c) =

a∈A

p(a) · |c(a)|,

where | . . . | denotes 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. Lemma 3.14. Suppose c : A → B ∗ is a binary optimal preﬁx-free code and let = maxa∈A |c(a)| denote the maximum length of a codeword. The following statements hold. • If |c(a )| > |c(a)| then p(a ) ≤ p(a). • The subset of codewords of length , C = {c ∈ c(A) | |c(a)| = }, contains two codewords of the form b0 and b1, for some b ∈ B ∗ . For the proof (which is very easy and highly recommended for the student who is curious to see more), see Biggs§3.6, [6]. The Huﬀman code construction is based on the amazing second property in the above lemma yields an optimal preﬁx-free binary code. Huﬀman code construction: Here is the recursive/inductive construction. We shall regard the binary Huﬀman 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 preﬁx-free binary code for any weighted alphabet (A , p ) having < n symbols. Huﬀman’s 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. Huﬀman’s 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. These two rules tell us how to inductively built the tree representation for the Huﬀman code of (A, p) up from its leaves (associated to the low weight symbols). • Find two diﬀerent symbols of lowest weight, a and a . If two such symbols don’t exist, stop. Replace the weighted alphabet with the new weighted alphabet as in Huﬀman’s rule 1. • Add two nodes (labeled with a and a , resp.) to the tree, with parent a∗ (see Huﬀman’s rule 1). • If there are no remaining symbols in A, label the parent a∗ with the empty set and stop. Otherwise, go to the ﬁrst step. An example of this is below.

3.4. APPLICATIONS TO COMPUTER SCIENCE

57

•

d d d 0 • a d 1 • d d d b 11d• •

10

c

Figure 3.10: Huﬀman code example Example 3.15. A very simple of this makes Huﬀman’s ingenious construction easier to understand. Suppse A = {a, b, c} and p(a) = 0.5, p(b) = 0.3, p(c) = 0.2. A Huﬀman code for this is C = {0, 10, 11}, as is depicted in Figure 3.10. Exercise 3.16. Verify that C = {1, 00, 01} is another Huﬀman code for this weighted alphabet and to draw its tree representation. Exercise 3.17. Find the Huﬀman code for the letters of the English alphabet weighted by the frequency of common American usage4 .

3.4

3.4.1

**Applications to computer science
**

Tree traversals

See section 3.5 of Gross and Yellen [14]. See also http://en.wikipedia.org/wiki/ Tree_traversal. • stacks and queues • breadth-ﬁrst, or level-order, traversal • depth-ﬁrst, or pre-order, traversal • post-order traversal • symmetric, or in-order, traversal In computer science, tree traversal refers to the process of examining each node in a tree data structure exactly once. We restrict our discussion to binary rooted trees. Starting at the root of a binary tree, there are three main steps that can be performed and the order in which they are performed deﬁnes the traversal type. Depth-ﬁrst traversal: • Visit the root vertex.

You can ﬁnd this on the internet or in the literature. Part of this exercise is ﬁnding this frequency distribution yourself.

4

58 • Traverse the left subtree recursively. • Traverse the right subtree recursively. Breadth-ﬁrst traversal:

CHAPTER 3. TREES AND FORESTS

• Initialize i = 0 and set N equal to the maximum depth of the tree (i.e., the maximum distance from the root vertex to any other vertex in the tree). • Visit the vertices of depth i. • Increment i = i + 1. If i > N then stop. Otherwise, go to the previous step. post-order traversal: • Traverse the left subtree recursively. • Visit the root vertex. • Traverse the right subtree recursively. symmetric traversal: • Traverse the left subtree recursively. • Visit the root vertex. • Traverse the right subtree recursively.

3.4.2

Binary search trees

See section 3.6 of Gross and Yellen [14], and chapter 12 of Cormen et al. [11]. See also http://en.wikipedia.org/wiki/Binary_search_tree. • records and keys • searching a binary search tree (BST) • inserting into a BST • deleting from a BST • traversing a BST • sorting using BST A binary search tree (BST) is a rooted binary tree T = (V, E) having weighted vertices wt : V → R satisfying: • The left subtree of a vertex v contains only vertices whose label (or “key”) is less than the label of v. • The right subtree of a vertex v contains only vertices whose label is greater than the label of v. • Both the left and right subtrees must also be binary search trees. From the above properties it naturally follows that: Each vertex has a distinct label. Generally, the information represented by each vertex is a record (or list or dictionary), rather than a single data element. However, for sequencing purposes, vertices are compared according to their labels rather than any part of their associated records.

then recursively traversing the right subtree of the root node. 2 . w2 ). This BST represents the sorting of the list L. we are given a BST T and a label . thereby creating a “smaller” tree T − v satisfying the criteria above. . the search is successful. However. For this procedure. w1 ) and (u. using a symmetric search type) by recursively traversing the left subtree of the root vertex. we are looking for a vertex in T whose label is . v2 ∈ V for which wt(v1 ) < < wt(v2 ). . This process is repeated until a vertex v ∈ V is found for which = wt(v). if one exists. Insertion proceeds as a search does. We assume is between the lowest weight of T and the highest weight. . We begin by examining the root vertex. Deletion proceeds as a search does. Similarly. APPLICATIONS TO COMPUTER SCIENCE Traversal 59 The vertices of a BST T can be visited retrieved in-order of the weights of the vertices (i. .w2 ∈ V were the children of v in T . Deletion As above. However.e. Sorting A binary search tree can be used to implement a simple but eﬃcient sorting algorithm. you are searching for vertix v ∈ V for which wt(v) = . . Once found. Once found. for 1 ≤ i ≤ n. search the right subtree. if > wt(v0 ). let V = {1. in this case. Searching We are given a BST (i.e... First. If the < wt(v0 ). n ]. n} be the vertices of a tree and weight vertex i with i . search the left subtree. a binary rooted tree with weighted vertices having distinct weights satisfying the above criteria) T and a label . or the indicated subtree is empty. In this case. these vertices will tell you where to insert v. where w1 . v0 . For this search. you are searching for vertices v1 . If = wt(v0 ). we can traverse this tree in order of its weights..3. we are looking for a “parent” vertex in T which can “adopt” a new vertex v having weight and for which this augmented tree T ∪ v satisﬁes the criteria above. then accessing the root vertex itself. .e. 2. v) ∈ E is replaced by (u. we remove v from V and any edge (u. . For this procedure.4. . in this case. thereby building a BST recursively. We assume is between the lowest weight of T and the highest weight. . Suppose we wish to sort a list of numbers L = [ 1 . a binary rooted tree with weighted vertices having distinct weights satisfying the above criteria) T and a label . We want to remove v from T (and therefore also the weight from the list of weights). we are looking for a vertex v of T which has weight . Insertion We are given a BST (i.

60 CHAPTER 3. TREES AND FORESTS .

Chapter 4 Distance and Connectivity 4. Then G is 2-connected if and only if any pair u. center. 4.3 Centrality of a vertex • degree centrality • betweenness centrality • closeness centrality • eigenvector centrality 61 .2 Vertex and edge connectivity • vertex-cut and cut-vertex • cut-edge or bridge • vertex and edge connectivity Theorem 4. Let G = (V. Theorem 4.2. diameter • trees: distance. Whitney’s Theorem. radius. non-adjacent vertices in a graph G. center. centroid • distance in self-complementary graphs 4. Then the maximum number of internally disjoint u-v paths in G equals the minimum number of vertices needed to separate u and v. v ∈ V has two internally disjoint paths between them. Menger’s Theorem.1 Paths and distance • distance and metrics • distance matrix • eccentricity.1. E) be a connected graph such that |V | ≥ 3. Let u and v be distinct.

DISTANCE AND CONNECTIVITY 4.4 Network reliability • Whitney synthesis • Tutte’s synthesis of 3-connected graphs • Harary graphs • constructing an optimal k-connected n-vertex graph .62 CHAPTER 4.

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).2 of Gross and Yellen [14].Chapter 5 Optimal Graph Traversals 5.2 Hamiltonian graphs • hamiltonian paths (or cycles) • hamiltonian graphs Theorem 5.1. 5. then G is hamiltonian. n)-de Bruijn sequence • postman tours and optimal postman tours • constructing an optimal postman tour 63 .2.1 Eulerian graphs • multigraphs and simple graphs • Eulerian tours • Eulerian trails 5. If deg(v) ≥ n/2 for all v ∈ V (G). then G is hamiltonian. Ore 1960. • de Bruijn sequences • de Bruijn digraphs • constructing a (2.3 The Chinese Postman Problem See section 6. Dirac 1952. Let G be a simple graph with n ≥ 3 vertices. Corollary 5.

[11]. • Gray codes and n-dimensional hypercubes • the Traveling Salesman Problem (TSP) • nearest neighbor heuristic for TSP • some other heuristics for solving TSP .4 The Traveling Salesman Problem See section 6.64 CHAPTER 5.2 of Cormen et al. and section 35. OPTIMAL GRAPH TRAVERSALS 5.4 of Gross and Yellen [14].

non-planarity. e edges and f faces.1 Planarity and Euler’s Formula • planarity. Kuratowski’s Theorem. Theorem 6. 6.1. The complete bipartite graph K3.2 Kuratowski’s Theorem • Kuratowski graphs The objective of this section is to prove the following theorem. Euler’s Formula.n is non-planar for n ≥ 3.3.3 .Chapter 6 Planar Graphs See chapter 9 of Gross and Yellen [14]. 6.3 Planarity algorithms • planarity testing for 2-connected graphs • planarity testing algorithm of Hopcroft and Tarjan [15] • planarity testing algorithm of Boyer and Myrvold [10] 65 . Then n − e + f = 2. A graph is planar if and only if it contains no subgraph homeomorphic to K5 or K3. Theorem 6. 6.2. Let G be a connected plane graph having n vertices. planar and plane graphs • crossing numbers Theorem 6.

PLANAR GRAPHS .66 CHAPTER 6.

2 Edge coloring • edge coloring • edge chromatic numbers • chromatic incidence • algorithm for edge coloring by maximum matching • algorithm for sequential edge coloring • Vizing’s Theorem 7.3 Applications of graph coloring • assignment problems • scheduling problems • matching problems • map coloring and the Four Color Problem 67 .Chapter 7 Graph Coloring 7.1 Vertex coloring • vertex coloring • chromatic numbers • algorithm for sequential vertex coloring • Brook’s Theorem • heuristics for vertex coloring 7.

68 CHAPTER 7. GRAPH COLORING .

2 Ford and Fulkerson’s theorem The objective of this section is to prove the following theorem. 8. 8.Chapter 8 Network Flows See Jungnickel [17].3 Edmonds and Karp’s algorithm The objective of this section is to prove Edmond and Karp’s algorithm for the maximum ﬂow-minimum cut problem with polynomial complexity. 69 . Theorem 8. 8. For a given network.1. Maximum ﬂow-minimum cut theorem. the value of a maximum ﬂow is equal to the capacity of a minimum cut.4 Goldberg and Tarjan’s algorithm The objective of this section is to prove Goldberg and Tarjan’s algorithm for ﬁnding maximal ﬂows with polynomial complexity.1 Flows and cuts • single source-single sink networks • feasible networks • maximum ﬂow and minimum cut 8. and chapter 12 of Gross and Yellen [14].

70 CHAPTER 8. NETWORK FLOWS .

and Albert and Barab´si [1]. See also Newa man [21].Chapter 9 Random Graphs See Bollob´s [7].1 Erd¨s-R´nyi graphs o e Describe the random graph model of Erd¨s and R´nyi [13].4 Evolving networks Preferential attachment models.3 Scale-free networks The power-law degree distribution model of Barab´si and Albert [2]. see Batagelj and Brandes [3]. 9. Newman [21]. a 9. See also Milgram [20]. See Newman [21]. a 71 . a 9. The economic small-world model of Latora and Marchiori [19].2 Small-world networks The small-world network model of Watts and Strogatz [24]. Algorithms for eﬃcient o e generation of random networks. and Albert and Barab´si [1]. and Albert and Barab´si [1]. a 9.

RANDOM GRAPHS .72 CHAPTER 9.

Indeed. e∈E(G) e∼v xe. and is formally deﬁned as : mad(G) = max ad(H) H⊆G Even though such a formulation does not show it. this quantity can be computed in polynomial time through Linear Programming. We then write in our linear program the constraint that each vertex can absorb a ﬂow of at most z (add to D the necessary sink and the edges with capacity z). 10.Chapter 10 Graph Problems and Their LP Formulations This document is meant as an explanation of several graph theoretical functions deﬁned in Sage’s Graph Library (http://www.1 Maximum average degree The average degree of a graph G is deﬁned as ad(G) = 2|E(G)| . its |E(H)| edges will send a ﬂow of 2|E(H)| to their |V (H)| vertices. such a ﬂow being feasible only if z ≥ 2|E(H)| . v) ∈ E(G) × V (G) if and only if v is one of e’s endpoints. or of Hall’s bipartite matching theorem shows that such a value for z is also suﬃcient. Clearly.org/). The maximum average |V (G)| degree of G is meant to represent its densest part. This LP can thus let us compute the Maximum Average Degree of the graph. Each edge will then have a ﬂow of 2 (through the addition in D of a source and the necessary edges) to distribute among its two endpoints. Let D be a directed graph whose vertex set we ﬁrst deﬁne as the disjoint union of E(G) and V (G). LP Formulation : • Mimimize : z • Such that : – a vertex can absorb at most z ∀v ∈ V (G). We add in D an edge between (e. An |V (H)| elementary application of the max-ﬂow/min-cut theorem.sagemath. we can think of this as a simple ﬂow problem deﬁned on a bipartite graph.v ≤ z 73 . if H ⊆ G is the densest subgraph in G. which use Linear Programming to solve optimization of existence problems.

we can then set the constraint 2 that z ≤ 2 − |V (G)| . For this to work.u = 2 • xe. The edges of G are in this case variables.74 CHAPTER 10.u + xe. this Linear Program is used as a constraint. whose value can be equal to 0 or 1 depending on whether they belong to such a graph H.sagemath.v real positive variable REMARK : In many if not all the other LP formulations.org/sage_trac/ticket/7529 . GRAPH PROBLEMS AND THEIR LP FORMULATIONS – each edge sends a ﬂow of 2 ∀e = uv ∈ E(G). In those problems. and that any cycles in a graph ensures its average degree is larger than 2. we need to ensure that the variables corresponding to our edges are binary variables. Based on the observation that the Maximum Average Degree of a tree on n vertices is exactly its average degree (= 2 − 2/n < 1). though. xe. corresponding patch : http://trac. This is a handy way to write in LP the constraint that “the set of edges belonging to H is acyclic”. we are always at some point looking for a subgraph H of G such that H does not contain any cycle.

2. along with variables representing the weight that each of these edges will send to their endpoints.u + xe. It is easy to deﬁne both the objective and the constraint that each vertex must have exactly two neighbors. but this could produce solutions such that the set of edges deﬁne the disjoint union of several cycles. using the remark from section 10. One way to formulate this linear program is hence to add the constraint that. e∈E(G) e∼v be = 2 – No cycle disjoint from a special vertex v ∗ ∗ Each edge sends a ﬂow of 2 if it is taken ∀e = uv ∈ E(G − v ∗ ). LP Formulation : • Mimimize w(e)be e∈E(G) • Such that : – Each vertex is of degree 2 ∀v ∈ V (G). TRAVELING SALESMAN PROBLEM 75 10.v real positive variable (ﬂow sent by the edge) – be binary (is the edge in the solution ?) corresponding patch : http://trac. We will then. xe.v = 2be ∗ Vertices receive strictly less than 2 ∀v ∈ V (G − v ∗ ).10. e∈E(G) e∼v xe. deﬁne variables representing the edges included in the solution.2 Traveling Salesman Problem Given a graph G whose edges are weighted by a function w : E(G) → R.sagemath. given an arbitrary vertex v. the set S of edges in the solution must contain no cycle in G − v. which amounts to checking that the set of edges in S no adjacent to v is of maximal average degree strictly less than 2.org/sage_trac/ticket/7529 . in this case.v ≤ 2 − 2 |V (G)| • Variables – xe. a solution to the T SP is a hamiltonian (spanning) cycle whose weight (the sum of the weight of its edges) is minimal.1.

.k.76 CHAPTER 10.. Tk which are subgraphs of G. k].k. . Obviously. . . nothing ensures the following formulation is a polynomial algorithm as it contains many integer variables.k] be. . e∈E(G) be. .sagemath. . . given a graph G and an integer k.v ≤ 2 − 2 |V (G)| • Variables – be. ∀e = uv ∈ E(G).k ∗ Vertices receive strictly less than 2 ∀i ∈ [1.k ≤ 1 – Each set contains |V (G)| − 1 edges ∀i ∈ [1. .k binary (is edge e in set k ?) – xe.u positive real (ﬂow sent by edge e to vertex u in set k) corresponding patch : http://trac.. ∀v ∈ V (G).u = 2be.. . each edge sends a ﬂow of 2 if it is taken ∀i ∈ [1. k]. . we will chose to deﬁne a spanning tree as an acyclic set of |V (G)| − 1 edges. k]. . .k = |V (G)| − 1 – No cycles ∗ In each set. .3 Edge-disjoint spanning trees This problem is polynomial by a result from Edmonds. LP Formulation : • Maximize : nothing • Such that : – An edge belongs to at most one set ∀e ∈ E(G).org/sage_trac/ticket/7476 . This problem amounts to ﬁnding. . GRAPH PROBLEMS AND THEIR LP FORMULATIONS 10. but it is still a short practical way to solve it.k.k. edge-disjoint spanning trees T1 .u + xe. e∈E(G) e∼v xe. xe. In this case. . . i∈[1.

be ≤ cv – The tree contains |V (H)| vertices and |E(H)| = |V (H)| − 1 edges cv − v∈G e∈E(G) be = 1 – No Cycles ∗ Each edge sends a ﬂow of 2 if it is taken ∀e = uv ∈ E(G). e∈E(G) e∼v be ≥ 1 – c is equal to 1 when a vertex v is in the tree ∀v ∈ V (G). we will be looking for an acyclic subgraph Hof G containing |V (H)| vertices and |E(H)| = |V (H)| − 1 edges.k ∗ Vertices receive strictly less than 2 ∀v ∈ V (G). to ﬁnd the tree of minimum cost connecting them all together. STEINER TREE 77 10. whereas computing a Steiner Tree is NP-hard.4.v real positive variable (ﬂow sent by the edge) corresponding patch : http://trac. ∀e ∈ E(G). a weight function w : E(G) → R and a set S of vertices.4 Steiner tree Finding a spanning tree in a Graph G can be done in linear time. which contains each vertex from S LP Formulation : • Minimize : w(e)be e∈E(G) • Such that : – Each vertex from S is in the tree ∀v ∈ S.u + xe. Equivalently.u = 2be.v ≤ 2 − 2 |V (G)| • Variables : – be binary (is e in the tree ?) – cv binary (does the tree contain v ?) – xe. xe. e∈E(G) e∼v xe.org/sage_trac/ticket/8403 .sagemath. The goal is in this case. given a graph. e ∼ v.10.

v = 2be.u + xe. 2 corresponding patch : http://trac. . .k.k ∗ Vertices receive strictly less than 2 ∀i ∈ [1.. .k.6 Acyclic edge coloring An edge coloring with k colors is said to be acyclic if it is proper (each color class is a matching – maximal degree 1). .k] be.k. e∈E(G) e∼v be. . . each of them being a forest of paths (the disjoints union of paths – trees of maximal degree 2). . . ∀v ∈ V (G).k = 1 – Each class has maximal degree 2 ∀v ∈ V (G)..v ≤ 2 − 2 |V (G)| • Variables – be. .. and if the union of the edges of any two color classes is acyclic. The corresponding LP is almost a copy of the previous one. The corresponding LP is very similar to the one giving edge-disjoint spanning trees LP Formulation : • Maximize : nothing • Such that : – An edge belongs to exactly one set ∀e ∈ E(G). .k. i∈[1. GRAPH PROBLEMS AND THEIR LP FORMULATIONS 10. ∀e = uv ∈ E(G). ..u positive real (ﬂow sent by edge e to vertex u in set k) 10.sagemath. k]. k]. e∈E(G) e∼v xe.5 Linear arboricity The linear arboricity of a graph G is the least number k such that the edges of G can be partitionned into k classes.org/sage_trac/ticket/8405 . except that we need to ensure that k diﬀerent classes are acyclic. each edge sends a ﬂow of 2 if it is taken ∀i ∈ [1.k binary (is edge e in set k ?) – xe.78 CHAPTER 10. ∀i ∈ [1. k]. . xe.k ≤ 2 – No cycles ∗ In each set.

consists in : 1. there must be an edge between the corresponding representative sets Here is how we will address these constraints : 1.10. If there is an edge between h1 and h2 in H. For any h. we can ﬁnd a spanning tree in Sh (an acyclic set of |Sh | − 1 edges) 3. diﬀerent vertices h having disjoints representative sets 2. Easy 2. we will just say that ﬁnding a minor H in a graph G. This one is very costly. Associating to each vertex h ∈ H a set Sh of representants in H. Ensuring that each of these sets is connected (can be contracted) 3.org/wiki/Minor_%28graph_theory%29 It is a wonderful subject. We then sum all these variables to be sure there is at least one edge from one set to the other. H-MINOR 79 10. . and I do not want to begin talking about it when I know I couldn’t freely ﬁll pages with remarks :-) For our purposes.wikipedia. To each directed edge g1 g2 (I consider g1 g2 and g2 g1 as diﬀerent) and every edge h1 h2 is associated a binary variable which can be equal to one only if g1 represents h1 and g2 represents g2 . please see http://en.7 H-minor For more information on minor theory.7.

h2 ) binary (is edge g1 g2 leaving the representative set of h1 to enter the one of h2 ?) corresponding patch : http://trac.g ≤ 1 – An edge e can only belong to the tree of h if both its endpoints represent h ∀e = g1 g2 ∈ E(G). the number of vertices is one more than the number of edges in the corresponding tree ∀h. and then for g2 .org/sage_trac/ticket/8404 .g − e∈E(G) te.g2 ). (note that this constraints has to be written both for g1 .sagemath.v = 2 h∈V (H) te.k.h2 ) ≤ rsh1 .(h1 . g2 ∈ V (G). h∈V (H) rsh.(h1 .80 CHAPTER 10. xe.v ≤ 2 − 2 |V (G)| – arc(g1 .g2 ).h ≤ rsh.g binary (does g represent h ? rs = “representative set”) – te.h binary (does e belong to the spanning tree of the set representing h ?) – xe. e∈E(G) e∼v xe. ∀h1 h2 ∈ E(H) arc(g1 .h ≤ rsh.g1 and arc(g1 . g2 .g2 – We have the necessary edges between the representative sets ∀h1 h2 ∈ E(H) arc(g1 .h ∗ Vertices receive strictly less than 2 ∀v ∈ V (G).g1 and te.h2 ) ≥ 1 ∀g1 .g2 ). g∈V (G) rsh.g2 – In each representative set.(h1 . te.g2 ).g2 ∈V (G).h2 ) ≤ rsh2 . g1 = g2 .h2 ) can only be equal to 1 if g1 g2 is leaving the representative set of h1 to enter the one of h2 . g1 ) ∀g1 .g2 ).g1 =g2 • Variables – rsh. GRAPH PROBLEMS AND THEIR LP FORMULATIONS LP Formulation : • Maximize : nothing • Such that : – A vertex g ∈ V (G) can represent at most one vertex h ∈ V (H) ∀g ∈ V (G).v real positive (ﬂow sent from edge e to vertex v) – arc(g1 .h = 1 – No cycles in the union of the spanning trees ∗ Each edge sends a ﬂow of 2 if it is taken ∀e = uv ∈ E(G).(h1 .(h1 .u + xe.

But this License is not limited to software manuals. while not being considered responsible for modiﬁcations made by others. regardless of subject matter or whether it is published as a printed book. below. unlimited in duration. it can be used for any textual work. that contains a notice placed by the copyright holder saying it can be distributed under the terms of this License. Such a notice grants a world-wide. refers to any such manual or work. You accept the license if you copy.org Everyone is permitted to copy and distribute verbatim copies of this license document. 2001. http://www.fsf. or other functional and useful document “free” in the sense of freedom: to assure everyone the eﬀective freedom to copy and redistribute it. 1. Preamble The purpose of this License is to make a manual. APPLICABILITY AND DEFINITIONS This License applies to any manual or other work. We have designed this License in order to use it for manuals for free software. 3 November 2008 Copyright c 2000. This License is a kind of “copyleft”. and is addressed as “you”. Any member of the public is a licensee. either commercially or noncommercially. modify or distribute the work in a way requiring permission under copyright law. which is a copyleft license designed for free software. which means that derivative works of the document must themselves be free in the same sense. with or without modifying it. 2008 Free Software Foundation. because free software needs free documentation: a free program should come with manuals providing the same freedoms that the software does. this License preserves for the author and publisher a way to get credit for their work. but changing it is not allowed. to use that work under the conditions stated herein. 2007. It complements the GNU General Public License. textbook. in any medium. 2002. royalty-free license. Secondarily. The “Document”.Appendix A GNU Free Documentation License Version 1. Inc.3. We recommend this License principally for works whose purpose is instruction or reference. 81 .

For works in formats which do not have any title page as such. legibly.82 APPENDIX A. or with modiﬁcations and/or translated into another language. GNU FREE DOCUMENTATION LICENSE A “Modiﬁed Version” of the Document means any work containing the Document or a portion of it. The “publisher” means any person or entity that distributes copies of the Document to the public. philosophical. or absence of markup. The “Title Page” means. and that is suitable for input to text formatters or for automatic translation to a variety of formats suitable for input to text formatters. as Front-Cover Texts or Back-Cover Texts. if the Document is in part a textbook of mathematics. or of legal. plus such following pages as are needed to hold. as being those of Invariant Sections. If a section does not ﬁt the above deﬁnition of Secondary then it is not allowed to be designated as Invariant. (Thus. for a printed book. 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. SGML or XML using a publicly available DTD. has been arranged to thwart or discourage subsequent modiﬁcation by readers is not Transparent. The Document may contain zero Invariant Sections. XCF and JPG. The “Invariant Sections” are certain Secondary Sections whose titles are designated. Opaque formats include proprietary formats that can be read and edited only by proprietary word processors. and standard-conforming simple HTML. A copy that is not “Transparent” is called “Opaque”. Texinfo input format. An image format is not Transparent if used for any substantial amount of text. A copy made in an otherwise Transparent ﬁle format whose markup. “Title Page” means the text near the most prominent appearance of the work’s title. in the notice that says that the Document is released under this License. preceding the beginning of the body of the text. A Front-Cover Text may be at most 5 words. a Secondary Section may not explain any mathematics. commercial. represented in a format whose speciﬁcation is available to the general public.) The relationship could be a matter of historical connection with the subject or with related matters. PostScript or PDF designed for human modiﬁcation. in the notice that says that the Document is released under this License. the title page itself. the material this License requires to appear in the title page. 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 Document’s overall subject (or to related matters) and contains nothing that could fall directly within that overall subject. and a Back-Cover Text may be at most 25 words. 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 the machine-generated HTML. either copied verbatim. 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 . LaTeX input format. PostScript or PDF produced by some word processors for output purposes only. ethical or political position regarding them. SGML or XML for which the DTD and/or processing tools are not generally available. Examples of suitable formats for Transparent copies include plain ASCII without markup. A “Transparent” copy of the Document means a machine-readable copy. Examples of transparent image formats include PNG.

under the same conditions stated above. you must take reasonably prudent steps. COPYING IN QUANTITY If you publish printed copies (or copies in media that commonly have printed covers) of the Document. 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. such as “Acknowledgements”. the copyright notices. “Dedications”. 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. 3. The Document may include Warranty Disclaimers next to the notice which states that this License applies to the Document. you may accept compensation in exchange for copies. Copying with changes limited to the covers. but only as regards disclaiming warranties: any other implication that these Warranty Disclaimers may have is void and has no eﬀect on the meaning of this License. can be treated as verbatim copying in other respects. when you begin distribution of Opaque copies in quantity. as long as they preserve the title of the Document and satisfy these conditions. These Warranty Disclaimers are considered to be included by reference in this License.) To “Preserve the Title” of such a section when you modify the Document means that it remains a section “Entitled XYZ” according to this deﬁnition. However. (Here XYZ stands for a speciﬁc section name mentioned below. provided that this License. and that you add no other conditions whatsoever to those of this License. VERBATIM COPYING You may copy and distribute the Document in any medium. all these Cover Texts: Front-Cover Texts on the front cover. and continue the rest onto adjacent pages. numbering more than 100. If you use the latter option. If the required texts for either cover are too voluminous to ﬁt legibly. you must either include a machine-readable Transparent copy along with each Opaque copy. You may not use technical measures to obstruct or control the reading or further copying of the copies you make or distribute. 2. . and you may publicly display copies. The front cover must present the full title with all words of the title equally prominent and visible. and Back-Cover Texts on the back cover. and the Document’s license notice requires Cover Texts. You may also lend copies. and the license notice saying this License applies to the Document are reproduced in all copies. If you distribute a large enough number of copies you must also follow the conditions in section 3. If you publish or distribute Opaque copies of the Document numbering more than 100. either commercially or noncommercially. you must enclose the copies in covers that carry. you should put the ﬁrst ones listed (as many as ﬁt reasonably) on the actual cover. or “History”. clearly and legibly. Both covers must also clearly and legibly identify you as the publisher of these copies. free of added material.83 in another language. You may add other material on the covers in addition. “Endorsements”.

Preserve all the copyright notices of the Document. with the Modiﬁed Version ﬁlling the role of the Document. If there is no section Entitled “History” in the Document. I. and publisher of the Modiﬁed Version as given on the Title Page. if there were any. one or more persons or entities responsible for authorship of the modiﬁcations in the Modiﬁed Version. B. if any. You may use the same title as a previous version if the original publisher of that version gives permission. as the publisher. Preserve in that license notice the full lists of Invariant Sections and required Cover Texts given in the Document’s license notice. given in the Document for public access to a Transparent copy of the Document. E. Preserve the section Entitled “History”. and publisher of the Document as given on its Title Page. C. H. year. G. Include an unaltered copy of this License. but not required. F. and add to it an item stating at least the title. unless they release you from this requirement. MODIFICATIONS You may copy and distribute a Modiﬁed Version of the Document under the conditions of sections 2 and 3 above. State on the Title page the name of the publisher of the Modiﬁed Version. that you contact the authors of the Document well before redistributing any large number of copies. and from those of previous versions (which should. 4. J. in the form shown in the Addendum below. then add an item describing the Modiﬁed Version as stated in the previous sentence. as authors. if it has fewer than ﬁve). Preserve the network location. you must do these things in the Modiﬁed Version: A. Add an appropriate copyright notice for your modiﬁcations adjacent to the other copyright notices. In addition. create one stating the title. new authors. together with at least ﬁve of the principal authors of the Document (all of its principal authors. or if the original publisher of the version it refers to gives permission. Preserve its Title. .84 APPENDIX A. List on the Title Page. provided that you release the Modiﬁed Version under precisely this License. authors. a license notice giving the public permission to use the Modiﬁed Version under the terms of this License. These may be placed in the “History” section. D. to give them a chance to provide you with an updated version of the Document. Use in the Title Page (and on the covers. be listed in the History section of the Document). if any) a title distinct from that of the Document. thus licensing distribution and modiﬁcation of the Modiﬁed Version to whoever possesses a copy of it. You may omit a network location for a work that was published at least four years before the Document itself. year. immediately after the copyright notices. and likewise the network locations given in the Document for previous versions it was based on. Include. GNU FREE DOCUMENTATION LICENSE It is requested.

previously added by you or by arrangement made by the same entity you are acting on behalf of. provided that you include in the combination all of the Invariant Sections of all of the original documents. For any section Entitled “Acknowledgements” or “Dedications”. M. you may not add another. add their titles to the list of Invariant Sections in the Modiﬁed Version’s license notice. and preserve in the section all the substance and tone of each of the contributor acknowledgements and/or dedications given therein. Preserve any Warranty Disclaimers. and list them all as Invariant Sections of your combined work in its license notice. Make the same adjustment . or else a unique number. but you may replace the old one.85 K. Do not retitle any existing section to be Entitled “Endorsements” or to conﬂict in title with any Invariant Section. 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. on explicit permission from the previous publisher that added the old one. and multiple identical Invariant Sections may be replaced with a single copy. You may add a passage of up to ﬁve words as a Front-Cover Text. The combined work need only contain one copy of this License. in parentheses. O. L. 5. statements of peer review or that the text has been approved by an organization as the authoritative deﬁnition of a standard. Section numbers or the equivalent are not considered part of the section titles. provided it contains nothing but endorsements of your Modiﬁed Version by various parties—for example. These titles must be distinct from any other section titles. If there are multiple Invariant Sections with the same name but diﬀerent contents. COMBINING DOCUMENTS You may combine the Document with other documents released under this License. to the end of the list of Cover Texts in the Modiﬁed Version. If the Document already includes a cover text for the same cover. N. the name of the original author or publisher of that section if known. Such a section may not be included in the Modiﬁed Version. under the terms deﬁned in section 4 above for modiﬁed versions. You may add a section Entitled “Endorsements”. Delete any section Entitled “Endorsements”. you may at your option designate some or all of these sections as invariant. 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 Modiﬁed Version. and a passage of up to 25 words as a Back-Cover Text. and that you preserve all their Warranty Disclaimers. unaltered in their text and in their titles. Preserve the Title of the section. To do this. unmodiﬁed. Preserve all the Invariant Sections of the Document. If the Modiﬁed Version includes new front-matter sections or appendices that qualify as Secondary Sections and contain no material copied from the Document. make the title of each such section unique by adding at the end of it.

provided you insert a copy of this License into the extracted document. You must delete all sections Entitled “Endorsements”. and distribute it individually under this License. You may include a translation of this License. you must combine any sections Entitled “History” in the various original documents. the requirement (section 4) to Preserve its Title (section 1) will typically require changing the actual title. this License does not apply to the other works in the aggregate which are not themselves derivative works of the Document. 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. and replace the individual copies of this License in the various documents with a single copy that is included in the collection. AGGREGATION WITH INDEPENDENT WORKS A compilation of the Document or its derivatives with other separate and independent documents or works. “Dedications”. Replacing Invariant Sections with translations requires special permission from their copyright holders. If a section in the Document is Entitled “Acknowledgements”. and all the license notices in the Document. 6. but you may include translations of some or all Invariant Sections in addition to the original versions of these Invariant Sections. When the Document is included in an aggregate. COLLECTIONS OF DOCUMENTS You may make a collection consisting of the Document and other documents released under this License. so you may distribute translations of the Document under the terms of section 4. If the Cover Text requirement of section 3 is applicable to these copies of the Document. . or “History”. GNU FREE DOCUMENTATION LICENSE to the section titles in the list of Invariant Sections in the license notice of the combined work. and follow this License in all other respects regarding verbatim copying of that document. and any sections Entitled “Dedications”. the Document’s Cover Texts may be placed on covers that bracket the Document within the aggregate. 8. likewise combine any sections Entitled “Acknowledgements”. and any Warranty Disclaimers. 7. is called an “aggregate” if the copyright resulting from the compilation is not used to limit the legal rights of the compilation’s users beyond what the individual works permit. forming one section Entitled “History”. then if the Document is less than one half of the entire aggregate. or the electronic equivalent of covers if the Document is in electronic form. provided that you follow the rules of this License for verbatim copying of each of the documents in all other respects. TRANSLATION Translation is considered a kind of modiﬁcation.86 APPENDIX A. You may extract a single document from such a collection. Otherwise they must appear on printed covers that bracket the whole aggregate. in or on a volume of a storage or distribution medium. In the combination.

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.0 license published by Creative Commons Corporation. A “Massive Multiauthor Collaboration” (or “MMC”) contained in the site means any set of copyrightable works thus published on the MMC site. If the Document does not specify a version number of this License. and will automatically terminate your rights under this License.org/copyleft/. “CC-BY-SA” means the Creative Commons Attribution-Share Alike 3. you may choose any version ever published (not as a draft) by the Free Software Foundation. Each version of the License is given a distinguishing version number. and you cure the violation prior to 30 days after your receipt of the notice. Such new versions will be similar in spirit to the present version. unless and until the copyright holder explicitly and ﬁnally terminates your license. then your license from a particular copyright holder is reinstated (a) provisionally. If the Document speciﬁes that a particular numbered version of this License “or any later version” applies to it. FUTURE REVISIONS OF THIS LICENSE The Free Software Foundation may publish new. if you cease all violation of this License. 10. A public wiki that anybody can edit is an example of such a server. California. modify. Any attempt otherwise to copy. as well as future copyleft versions of that license published by that same organization. If the Document speciﬁes that a proxy can decide which future versions of this License can be used. Moreover. receipt of a copy of some or all of the same material does not give you any rights to use it. 11. sublicense.gnu. your license from a particular copyright holder is reinstated permanently if the copyright holder notiﬁes you of the violation by some reasonable means. or distribute the Document except as expressly provided under this License. that proxy’s public statement of acceptance of a version permanently authorizes you to choose that version for the Document. 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. but may diﬀer in detail to address new problems or concerns. a not-for-proﬁt corporation with a principal place of business in San Francisco. If your rights have been terminated and not permanently reinstated. you have the option of following the terms and conditions either of that speciﬁed version or of any later version that has been published (not as a draft) by the Free Software Foundation. sublicense. revised versions of the GNU Free Documentation License from time to time. See http://www. TERMINATION You may not copy.87 9. or distribute it is void. . this is the ﬁrst time you have received notice of violation of this License (for any work) from that copyright holder. However. if the copyright holder fails to notify you of the violation by some reasonable means prior to 60 days after the cessation. and (b) permanently. modify.

ADDENDUM: How to use this License for your documents To use this License in a document you have written. such as the GNU General Public License. If your document contains nontrivial examples of program code. merge those two alternatives to suit the situation. provided the MMC is eligible for relicensing. Front-Cover Texts and Back-Cover Texts. no Front-Cover Texts. . and subsequently incorporated in whole or in part into the MMC. (1) had no cover texts or invariant sections. replace the “with .” line with this: with the Invariant Sections being LIST THEIR TITLES. and with the Back-Cover Texts being LIST. Texts. with no Invariant Sections. include a copy of the License in the document and put the following copyright and license notices just after the title page: Copyright c YEAR YOUR NAME. Version 1. as part of another Document.3 or any later version published by the Free Software Foundation. to permit their use in free software. with the FrontCover Texts being LIST. The operator of an MMC Site may republish an MMC contained in the site under CC-BY-SA on the same site at any time before August 1. . 2008. distribute and/or modify this document under the terms of the GNU Free Documentation License.88 APPENDIX A. in whole or in part. An MMC is “eligible for relicensing” if it is licensed under this License. and no BackCover Texts. and if all works that were ﬁrst published under this License somewhere other than this MMC. 2009. If you have Invariant Sections without Cover Texts. and (2) were thus incorporated prior to November 1. or some other combination of the three. we recommend releasing these examples in parallel under your choice of free software license. If you have Invariant Sections. Permission is granted to copy. . A copy of the license is included in the section entitled “GNU Free Documentation License”. GNU FREE DOCUMENTATION LICENSE “Incorporate” means to publish or republish a Document.

[13] P. and C. Myrvold. 45(2):167–256. and Cryptography.edu. Jungnickel.Bibliography [1] R. University of Puget Sound.ups. Stein. PWS Publishing Company. 2004. a [8] O. pˇ´ rırodovˇd. J. 7(1):48–50. Bollob´s. 2001. Kruskal. E. 24(1):1–13. B.-L. 1959. 2009. Tacoma. 1999. Albert and A. W. Washington. [23] V. [17] D. Eﬃcient algorithms for shortest paths in sparse networks. 8(2):241–273. 1957. Dijkstra. 1926. 32(2):249–263. Brandes. 1956. Science. Economic small-world behavior in weighted networks. Springer. 2009. Networks and Algorithms. Marchiori. Fundamentals of Sequential and Parallel Algorithms. [7] B. MIT Press and McGraw-Hill. 2nd edition. Pr´ce u e e a ım a mor. 6:290–297. C. Berman and J. Reviews of a Modern Physics. H. On the cutting edge: Simpliﬁed O(n) planarity by edge addition. Leiserson. Albert. A Computational Introduction to Number Theory and Algebra. Elektronick´ y Obzor. Cambridge University Press. Graphs. [3] V. Eﬃcient planarity testing. 36:1389–1401. 1977. 2008. 1959. 2002. 1997. 2nd edition.net/ntb. Latora and M. [4] R. E. [5] K. Graph Theory and Its Applications. R´nyi. Gross and J. Shoup. E. [18] J. Communication. M. 71(3):036113. CRC Press. o e [14] J. 74:47–97. [11] T. Milgram. [12] E. SIAM Review. Eﬃcient generation of large random networks. [19] V. The European Physical Journal B.shoup. 21(4):549–568. 2003. Beezer. A. 2005. Yellen. Boyer and W. e e [9] O. Biggs. Bor˚vka. L. Springer. Journal of the ACM. Emergence of scaling in random networks. Paul. Publicationes Mathematicae. A First Course in Linear Algebra. Tarjan. 3rd edition. [20] S. Statistical mechanics of complex networks. O jist´m probl´mu minim´ln´ (about a certain minimal problem). [16] D. spol. Bor˚vka. A. 2001. Cambridge University Press. Newman. Pˇ´ evek k ˇeˇen´ ot´zky ekonomick´ stavby elektrovodn´ s´ ı (contribution u rıspˇ r s ı a e ıch ıt´ to the solution of a problem of economical construction of electrical networks). Barab´si. [21] M. R. Prim. 89 . Physical Review E. Batagelj and U. The structure and function of complex networks. 3:37–58. 1926. [10] J. Bell System Technical Journal. Beezer. On the shortest spanning subtree of a graph and the traveling salesman problem. Journal of Graph Algorithms and Applications.-L. 1:269–271. Random Graphs. [22] R. On random graphs. C. Proceedings of the American Mathematical Society. http://linear. 2008. USA. 1967. Codes: An Introduction to Information. Shortest connection networks and some generalizations. http://www. v Brnˇ III. 1974. 286:509– a 512. 1999. Journal of the ACM. A note on two problems in connexion with graphs. [6] N. Psychology Today. Numerische Mathematik. Introduction to Algorithms. 2:60–67. Robert A. Erd¨s and A. B. Rivest. Hopcroft and R. 2nd edition. E. Barab´si and R. Johnson. L. [2] A. J. The small world problem. [15] J. 2003. 15:153–154. Cormen.

Strogatz. Nature.90 BIBLIOGRAPHY [24] D. . 1998. Collective dynamics of ‘small-world’ networks. 393:440–442. J. H. Watts and S.

49. 49 codeword. 20 cycle. 21 adj(v). 4 minimum. 3 disconnected graph. 38 91 . 6 code. 4 degree sequence.Index Cn . 25 bridge. 20 edge cut. 56 preﬁx-free. 6 cycle graph. 6 Euler. 8 bond. 13 ω. 8 complete graph. 38 breadth-ﬁrst search. 54 binary. 3 Dijkstra’s algorithm. 54 uniquely decodable. 4 acyclic. 48 endpoint. 16 graphical. Leonhard. 8 Gc . 17 depth-ﬁrst search. 20 Kn . 7 Km. 12 alphabet. 20 complete bipartite graph. 19 ∼ 15 =. 6 ⊕. 48. 4. 13 edge contraction. 54 complement. 12 bipartite graph. 49 tree. 27 DFS. 6 disconnecting set. 48 degree maximum. 54 block. 20 edge deletion subgraph. 55 optimal. 16 Cartesian product. 20 canonical label. 21 check matrix. 4 L. 2 r-regular. 53 backtracking. 48. 7 directed edge. 37 adjacency matrix. 1 incident. 8 Qn . 1 encode. 55 variable-length. 7 connected graph. 23 forest. 48. 49 Morse. 8 decode. 4 degree matrix. 20. 20 edges. 4 δ. deg(v). 13 degree of a vertex. 27 BFS. 20 distance matrix. 19 . 22 Pn . 12 child. 37 closed path. 6 cut set. 25 bi-adjacency matrix.n . 37 Euler subgraph. 8 Ln . 27 digraph. 10 reduced. 4 forbidden minor. 37 fundamental cycle. 22 ∆.

23 bipartite. 4 Gray code m-ary. 13 leaf. 20 hypercube. 20 Seymour. 22 Laplacian matrix. 16 regular graph. 22 null. 37 INDEX . 4 simple. 3 invariant. 6 simple graph. 7 complete bipartite. 3 regular. 36 Klein. 15 isolated vertex. 23 graphs directed. 6 path graph. 8 connected. 4 relative complement. 6 graph. 23 shortest path. 28 permutation equivalent. 23 self-complementary graph. 8 symmetric diﬀerence. Neil. 56 matrix. 4 Johnson’s algorithm. 50 Hamming distance. 4 size. 53 multi-undirected graph. 15 multigraphs. 3 isomorphic. 1 applications. 23 spanning subgraph. 15 graph minor. Felix. 3 multigraphs. 3 multidigraph. 17 graph isomorphism. 8. 5 traveling salesman problem. 12 indegree. 1 hypercube graph. 1 social network analysis. 5 closed. 22. 37 length of codeword. 7 star graph. 51 intersection. 16 complete. 8 string. 4 house graph. 3 parent. 37 n-ary. 18 join. 12 trail. 3 path. 22 path length. 3 order. 7 supergraph. 20 ring sum. 7 symmetric diﬀerence. 19 Tanner graph. 22 incidence matrix. 10 message. 13 outdegree. 1 knight tour. 3 null graph. 15. 6 girth. 6 cut. 8 union. 19 Robertson. 6 geodesic. 3 separating set. 19 ladder. 1 orientation. 22 handshaking lemma. 27 ladder graph. 8 canonical label. Paul. 23 Robertson-Seymour theorem. 23 tree. 23 star. 19 trivial. 18 graph invariant. 53 subgraph. 20 self-loop. 37 path.92 geodesic path. 22 planar. 50 binary.

19 word. 57 in-order. 53 93 . 1 Wagner’s conjecture. 58 trivial graph. 57 breadth-ﬁrst. 58 pre-order. 1 adjacent. 37 rooted. 58 depth-ﬁrst. 5 wheel graph. 58 directed.INDEX binary. 23 walk. 57 symmetric. Klaus. 58 level-order. 8 vertex cut. 19 vertices. 37 depth. 37 tree traversal. 20 vertex deletion subgraph. 5 length. 23 Wagner. 58 post-order. 37 ordered.

Are you sure?

This action might not be possible to undo. Are you sure you want to continue?

We've moved you to where you read on your other device.

Get the full title to continue

Get the full title to continue listening from where you left off, or restart the preview.

scribd