102
Algorithms and Complexity 2, CS2870 Gregory Gutin December 11, 2011

Algorithms and Complexity II

  • Upload
    sly2518

  • View
    746

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Algorithms and Complexity II

Algorithms and Complexity 2, CS2870

Gregory Gutin

December 11, 2011

Page 2: Algorithms and Complexity II

2

Page 3: Algorithms and Complexity II

Abstract

This notes accompany the second year course CS2870: Algorithms and Complexity 2. Allcomputer scientists should know basics of graph theory, algorithms and applications aswell as of computational complexity.

Permission is given to freely copy and distribute this document in an unchanged form.You may not modify the text and redistribute without written permission from the author.

3

Page 4: Algorithms and Complexity II

4

Page 5: Algorithms and Complexity II

Contents

1 Basic notions on undirected and directed graphs 9

1.1 Introduction to graph theory: Graph models . . . . . . . . . . . . . . . . . 9

1.1.1 Matchings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9

1.1.2 Traffic models . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11

1.2 Degrees in graphs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11

1.2.1 Basic definitions and results . . . . . . . . . . . . . . . . . . . . . . . 12

1.2.2 Havel-Hakimi algorithm . . . . . . . . . . . . . . . . . . . . . . . . . 13

1.2.3 Pseudocode of Havel-Hakimi algorithm (extra material) . . . . . . . 15

1.3 Degrees in digraphs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16

1.4 Subgraphs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17

1.5 Isomorphism of graphs and digraphs . . . . . . . . . . . . . . . . . . . . . . 19

1.6 Classes of graphs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21

1.7 Graph data structures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22

1.8 Solutions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23

2 Walks, Connectivity and Trees 25

2.1 Walks, trails, paths and cycles in graphs . . . . . . . . . . . . . . . . . . . . 25

2.2 Connectivity . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26

2.3 Edge-connectivity and Vertex-connectivity (extra material) . . . . . . . . . 30

2.4 Basic properties of trees and forests . . . . . . . . . . . . . . . . . . . . . . 32

2.5 Spanning trees and forests . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33

2.6 Greedy-type algorithms and minimum weight spanning trees . . . . . . . . 34

5

Page 6: Algorithms and Complexity II

6 CONTENTS

2.7 Solutions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38

3 Directed graphs 41

3.1 Acyclic digraphs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41

3.1.1 Acyclic ordering of acyclic digraphs . . . . . . . . . . . . . . . . . . 41

3.1.2 Longest and Shortest paths in acyclic digraphs . . . . . . . . . . . . 44

3.1.3 Analyzing projects using PERT/CPM (extra material) . . . . . . . . 46

3.2 Distances in digraphs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48

3.2.1 Breadth First Search . . . . . . . . . . . . . . . . . . . . . . . . . . . 48

3.2.2 Dijkstra’s algorithm . . . . . . . . . . . . . . . . . . . . . . . . . . . 50

3.2.3 The Floyd-Warshall algorithm . . . . . . . . . . . . . . . . . . . . . 50

3.3 Strong connectivity in digraphs . . . . . . . . . . . . . . . . . . . . . . . . . 53

3.3.1 Basics of strong connectivity . . . . . . . . . . . . . . . . . . . . . . 53

3.3.2 Algorithms for finding strong components . . . . . . . . . . . . . . . 54

3.4 Application: Solving the 2-Satisfiability Problem (extra material) . . . . . . 55

3.5 Solutions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 58

4 Colourings of Graphs, Independent Sets and Cliques 61

4.1 Basic definitions of vertex colourings . . . . . . . . . . . . . . . . . . . . . . 61

4.2 Bipartite graphs and digraphs . . . . . . . . . . . . . . . . . . . . . . . . . . 62

4.3 Periods of digraphs and Markov chains (extra material) . . . . . . . . . . . 64

4.4 Computing chromatic number . . . . . . . . . . . . . . . . . . . . . . . . . . 65

4.5 Greedy colouring and interval graphs . . . . . . . . . . . . . . . . . . . . . . 67

4.6 Edge colourings (extra material) . . . . . . . . . . . . . . . . . . . . . . . . 69

4.7 Independent Sets and Cliques . . . . . . . . . . . . . . . . . . . . . . . . . . 69

5 Matchings in graphs 73

5.1 Matchings in (general) graphs . . . . . . . . . . . . . . . . . . . . . . . . . . 73

5.2 Matchings in bipartite graphs . . . . . . . . . . . . . . . . . . . . . . . . . . 74

5.3 Application of matchings . . . . . . . . . . . . . . . . . . . . . . . . . . . . 77

5.4 Solutions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 78

Page 7: Algorithms and Complexity II

CONTENTS 7

6 Euler trails and Hamilton cycles in graphs 81

6.1 Euler trails in multigraphs . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81

6.2 Chinese Postman Problem . . . . . . . . . . . . . . . . . . . . . . . . . . . . 84

6.3 Hamilton cycles . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 86

6.4 Travelling Salesman Problem . . . . . . . . . . . . . . . . . . . . . . . . . . 87

7 NP-completeness 89

7.1 Why is arranging objects hard? . . . . . . . . . . . . . . . . . . . . . . . . . 89

7.2 Brute force optimisation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 89

7.3 How to spot an explosive algorithm . . . . . . . . . . . . . . . . . . . . . . . 90

7.3.1 Exponential functions . . . . . . . . . . . . . . . . . . . . . . . . . . 91

7.3.2 Good algorithms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91

7.4 Tractable problems . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 92

7.5 The class NP . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 93

7.5.1 Minimisation and backtracking . . . . . . . . . . . . . . . . . . . . . 93

7.5.2 Examples of NP-complete problems . . . . . . . . . . . . . . . . . . 94

7.5.3 Alternative definition on the class NP . . . . . . . . . . . . . . . . . 95

7.6 Proving that a problem is NP-complete . . . . . . . . . . . . . . . . . . . . 97

7.7 Proceeding in the face of intractable problems . . . . . . . . . . . . . . . . . 99

7.8 TSP Heuristics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 99

Page 8: Algorithms and Complexity II

8 CONTENTS

Page 9: Algorithms and Complexity II

Chapter 1

Basic notions on undirected anddirected graphs

1.1 Introduction to graph theory: Graph models

We start from some simple problems that can be usefully modeled as graphs. Whileconsidering these models we introduce some basic notions from graph theory.

1.1.1 Matchings

(Undirected) graphs are structures defined by vertices (sometimes, also called nodes)and edges between some pairs of vertices. Figure 1.1 gives an example of a graph H withvertices j1, j2, j3, j4, j5, p1, . . . , p7 and edges

j1p1, j1p3, j2p5, j3p2, j3p3, j4p3, j4p5, j4p6, j5p4, j5p7.

The graph H may represent the following problem. A recruitment agency currently has5 jobs in IT available and 7 people looking for a job in IT. Not every person can do every joband the list of jobs with persons able to do them is written above (j1p1, j1p3, . . . , j5p7). Theagency gets £2000 for every person employed, and thus is interested in finding appropriatepeople to all five jobs. The natural question is what is the maximum amount of moneythe agency can make in this situation. It is not difficult to check that the agency can make£2000× 5 = £10000 in this particular example by using the following assignment:

j1p1, j2p5, j3p2, j4p6, j5p7.

The above example can be easily generalized. We are given jobs j1, . . . , jm, peoplep1, . . . , pn, and a list of pairs job-person such that each pair jipk in the list indicates that

9

Page 10: Algorithms and Complexity II

10 CHAPTER 1. BASIC NOTIONS ON UNDIRECTED AND DIRECTED GRAPHS

p7

j1

j2

j3

j4

j5

p1

p2

p3

p4

p5

p6

Figure 1.1: A graph H.

job ji can be done by person pk. The objective is to determine the maximum number of jobsthat can be done under the condition that no person can do more than one job. The best(maximum) assignment of jobs to persons is a maximum matching in the correspondinggraph.

It turns out that the general problem can be better understood and solved if it modeledas a graph similar to one in Figure 1.1. In fact, graph theory provides a fast algorithm toquickly solve the problem (using computers) even if the numbers of jobs and people arequite large. The algorithm is non-trivial and is based on certain notions and results ingraph theory.

We will study this algorithm, but it is perhaps useful to try to ”design” such analgorithm yourself.

Already the graph H in Figure 1.1 and the corresponding problem allow us to introducesome important notions in graph theory. The graph H is bipartite, i.e., its set of verticescan be partitioned into two partite sets such that no edge joins vertices from the samepartite set. In our case no two jobs should be joined and no two people should be joined.We said we wanted to find a maximum matching in H. What is a matching in a graph? Itis a collection of edges with no common vertices. In particular, the collection j1p1, j2p5, j3p2is a matching, but j1p1, j1p3, j3p2 is not (j1 is in two edges). A matching is maximum ifit contains the maximum possible number of edges in the graph in hand.

The term matching appeared because of another ”practical” problem. In a small villagethere are a number of girls and a number of boys and some girls know some boys. What isthe maximum number of marriages that can be arranged such that a girl may only marrya boy she knows? There is a theorem, Hall’s theorem, which answers a weaker question: isit possible to marry all girls (boys)? Sometimes, the last question is called the marriageproblem.

Let us suppose that the graph H in Figure 1.1 ”models” an instance of the marriageproblems, where ji (Jane, Janet, etc.) are girls and pk (Peter, Paul, etc.) are boys. One

Page 11: Algorithms and Complexity II

1.2. DEGREES IN GRAPHS 11

uz

w

vx

y

Figure 1.2: A digraph D

of the simplest questions to ask is how many boys Jane (j1) knows. We see that Janeknows two boys p1 and p3. We say that the degree of j1 is 2. The degree of j2 (j3, j4, j5,respectively) is 1 (2,3,2, respectively). The degree of each of the vertices p1, p2, p4, p6, p7is 1, the degree of p5 is 2, and the degree of p3 is 3. You may check that the sum of alldegrees is twice the number of edges in H. This is true for every graph. Try to understandwhy. We prove this fact later on.

1.1.2 Traffic models

Graphs can be used for journey planning. Indeed, the system of UK roads can be consid-ered as a graph whose vertices are junctions and intervals of roads between junctions areedges. Clearly, every edge has a ”weight” (for example, the time needed to travel alongthis edge). Another example is the London Underground (LU). In LU people often needto find how to get from one vertex to another, i.e., from one station to another. Theirjourney is a path in the LU graph, i.e., a sequence of distinct vertices (= stations) suchthat each vertex joined to the previous vertex by an edge. In Figure 1.1, p1j1p3j4p6 is apath. Tourist buses in London and Oxford visit certain places of interest and return totheir original place. So, they move along a cycle, i.e., a path plus an extra edge betweenthe first and last vertices of the path.

The LU graph is an undirected graph as we can travel along each edge in bothdirections. In some other graph models, this is impossible. For example, the London roadsystem cannot be represented as an undirected graph since it has one-way streets. A one-way street has a direction in which the travel is allowed. This situation can be representedadequately by a directed graph, i.e., a graph in which every edge has direction. Edgesof directed graphs are normally called arcs. Figure 1.2 depicts a directed graph (for short,digraph).

1.2 Degrees in graphs

In this section, we will study degrees of undirected graphs.

Page 12: Algorithms and Complexity II

12 CHAPTER 1. BASIC NOTIONS ON UNDIRECTED AND DIRECTED GRAPHS

1.2.1 Basic definitions and results

The degree of a vertex x in a graph G, denoted by dG(x), is the number of edges inG in which x is one of the two end-vertices. Normally, a graph G is written as a pairG = (V,E), where V is the vertex set and E is the edge set. A vertex y is a neighbourof vertex x in G if xy ∈ E. Observe that the total number of neighbours of x is its degree.

One of the first observations in graph theory is the following proposition called thesum-of-degrees proposition. In the proposition we use the symbol |E| that is thenumber of elements in set E, i.e., the number of edges in G. Similarly, |V | denotes thenumber of vertices in G.

Proposition 1.2.1 The sum of degrees of vertices in a graph G = (V,E) equals twice thenumber of edges in G. In notation, ∑

x∈VdG(x) = 2|E|.

Proof: Every edge e ∈ E with end-vertices y and z contributes 2 to the sum∑

x∈V dG(x)as it contributes 1 to dG(y) and 1 to dG(z). QED

Question 1.2.2 Check the above proposition for the graph H in Fig. 1.1.

The sum-of-degrees proposition implies that every graph has even number of verticesof odd degree. Indeed, let G = (V,E) be a graph. We partition V into the set of verticesof odd degree V1 and the set of vertices of even degree V2. We have

2|E| =∑x∈V

dG(x) =∑x∈V1

dG(x) +∑x∈V2

dG(x),

i.e.,

2|E| =∑x∈V1

dG(x) +∑x∈V2

dG(x).

Since 2|E| is even and∑

x∈V2 dG(x) is even (as the sum of even numbers),∑

x∈V1 dG(x)must be even, too.

Theorem 1.2.3 Every graph G with at least two vertices has a pair of vertices of thesame degree.

Proof: Let G have n vertices. If all vertices of G have different degrees, the degrees willrange from 0 to n− 1 inclusive. However, a graph cannot have both vertex of degree 0 (avertex with no edges) and vertex of degree n− 1 (linked to all vertices in G). QED

Page 13: Algorithms and Complexity II

1.2. DEGREES IN GRAPHS 13

Question 1.2.4 (a) Compute the number of edges in a graph with 10 vertices, each ofdegree 3.

(b) Compute the number of edges in a graph with n vertices, each of degree 4.

(c) Is there a graph with vertex degrees 5,4,3,2,1 ?

(d) Draw a graph with vertex degrees 2,2,3,3,3,3.

(e) Draw a graph with vertex degrees 1,1,1,1,1,1,6.

1.2.2 Havel-Hakimi algorithm

Now we consider the question when a sequence of non-negative integers is a sequence ofdegrees of vertices of a graph. For example, the sequence 0, 0, 0, 0 is the sequence of degreesof a graph on 4 vertices with no edges. The sequence 1, 1 is the sequence of degrees of agraph with 2 vertices joined by a unique edge. However, 0, 1 is not a sequence of degreesof any graph: if we assume that a graph H with the sequence of degrees 0, 1 does exist,then we see that H has two vertices, one of degree 0 and another of degree 1. The vertexof degree 1 implies that H has an edge, while the vertex of degree 0 implies that H hasno edge, a contradiction.

Every sequence of non-negative integers, which is a sequence of degrees of some graph iscalled graphic. Havel and Hakimi suggested an algorithm to check whether the sequencein hand is graphic or not. This algorithm is recursive and based on the following theorem.

Theorem 1.2.5 (HH theorem) Let i1, i2, . . . , in with i1 ≥ i2 ≥ · · · ≥ in be a sequenceof non-negative integers. This sequence is graphic if and only if the following sequence isgraphic: replace i1 by 0 and decrease the first i1 numbers in i2, i3, . . . , in by one.

It is very important that the operation of this theorem is applied to a non-increasingsequence i1, i2, . . . , in.

Let us consider the following examples.

Question 1.2.6 Check whether the sequence 4, 2, 4, 3, 4, 1 is graphic and if it is constructa graph with this degree sequence.

Solution: First we rewrite the given sequence in non-increasing order: 4, 4, 4, 3, 2, 1.Assume that 4, 4, 4, 3, 2, 1 is graphic and H is a graph with this sequence with verticesu, v, w, x, y, z. We now apply the HH theorem.

uvwxyz

Page 14: Algorithms and Complexity II

14 CHAPTER 1. BASIC NOTIONS ON UNDIRECTED AND DIRECTED GRAPHS

u v

w

xy

z

Figure 1.3: A graphic realization of 4,4,4,3,2,1

444321 (apply HH)

033211 (apply HH)

002101 (apply HH)

000000

Obviously, the last sequence is graphic and, hence, the original sequence is graphic aswell. We build H going along the transformations above from bottom to top. First wedepict the six vertices, then we add edges wx and wz. Then we add edges vw, vx and vy.Finally we add edges between vertex u and vertices v, w, x, y. See Figure 1.3.

Question 1.2.7 Check whether the sequence 3, 2, 4, 3, 4, 1 is graphic and if it is constructa graph with this degree sequence.

Solution: First we rewrite the given sequence in non-increasing order: 4, 4, 3, 3, 2, 1.Assume that 4, 4, 3, 3, 2, 1 is graphic and H is a graph with this sequence with verticesu, v, w, x, y, z. We now apply the HH theorem.

uvwxyz

443321 (apply HH)

032211 (apply HH)

001101 (apply HH)

000001

Obviously, the last sequence is not graphic and, hence, the original sequence is notgraphic either.

Question 1.2.8 Check whether the following sequences are graphic and when it is con-struct the corresponding graph:

(a) 3, 3, 3, 3, 3, 3

Page 15: Algorithms and Complexity II

1.2. DEGREES IN GRAPHS 15

(b) 5, 4, 3, 3, 1, 0

(c) 4, 4, 4, 4, 4, 2, 2

(d) 5, 4, 4, 4, 4, 3, 2

Question 1.2.9 [A solution is given in the end of the chapter] Using the Havel-Hakimi algorithm check whether each of the following sequences is graphic and, when itis, construct the corresponding graph. Justify your answers.

(i) 5, 3, 3, 3, 3, 3, 2

(ii) 6, 4, 4, 2, 2, 1, 1

Question 1.2.10 [A solution is given in the end of the chapter] Consider the fol-lowing sequences of natural numbers. Which of the sequences are degree sequences of trees?Justify your answers. For every degree sequence of a tree, construct the corresponding tree.(You may use the Havel-Hakimi algorithm where appropriate.)

(i) 4, 3, 3, 4, 4, 2, 1

(ii) 2, 1, 1, 1, 1, 4

1.2.3 Pseudocode of Havel-Hakimi algorithm (extra material)

Now we will produce a pseudo-code for checking whether a sequence of non-negativeintegers is graphic. We use an array d to represent this sequence. Procedure sort in lines2 and 7 sorts the array d using any sorting algorithm (merge sort, for example). Itsinput is an unsorted array d and its output is a sorted array d in which d[0] ≥ d[1] ≥d[2] ≥ . . . . The input of our pseudo-code consists of an array d and the number m of itspositive elements.

1 // d is an array of n integers with m positive integers all smaller than n

2 sort(d)

3 while (m > 0 && d[0] <= m-1) do

4

5 for i from 1 to d[0] do d[i] := d[i]-1

6 d[0] := 0

7 sort(d)

8 k :=0

9 for j from 0 to m do

10

11 if (d[j]>0) k := k+1

12

13 m := k

Page 16: Algorithms and Complexity II

16 CHAPTER 1. BASIC NOTIONS ON UNDIRECTED AND DIRECTED GRAPHS

uz

w

vx

y

Figure 1.4: A digraph D

14

15 if ( m == 0) print "graphic"

16 else print "non-graphic"

To see that the above code is correct, it suffices to observe that the code implements theHavel-Hakimi procedure considered earlier. The pseudo-code discovers that the currentarray d is not graphic only if d[0], the largest element of d, is larger than the number mof positive elements in d minus 1 (d[0] itself). Otherwise, the pseudo-code is performed tothe end, i.e., m = 0, which means, by the Havel-Hakimi Theorem, that d is graphic.

To make our pseudo-code more efficient, we observe that the value of each d[i] isbounded from above by n − 1. Thus, d[i] = O(n) and we can implement sort to runin time O(n). To this end, we can use counting sort(see [CLR1990] for description andanalysis of counting sort). So, each time we use line 7, we perform O(n) operations.The same is true for the remaining lines. Clearly, we have at most n iterations in while

of line 3 and, thus, the overall time complexity of our pseudo-code is O(n2).

Question 1.2.11 What is the time complexity of the pseudo-code above when sort isimplemented by merge sort?

1.3 Degrees in digraphs

An undirected graph G = (V,E) has sets of vertices V and edges E. For an edge xy ∈ E,one can “move” from x to y and from y to x. A directed graph (digraph, for short)D = (V,A) has vertices V and arcs A. For an arc xy ∈ A, one can “move” only from x toy. (The situation when one can move from x to y and y to x in a digraph is reflected byhaving both arcs xy and yx.)

For example, the digraph D in Figure 1.4 has vertices V = x, y, z, u, v, w and arcsA = xz, yz, zu, uv, uw,wu.

Since there are arcs coming to a vertex x in a digraph H and ones leaving x, the notionof degree is not enough for digraphs. Instead, we have two parameters: the out-degree

Page 17: Algorithms and Complexity II

1.4. SUBGRAPHS 17

d+(x) and in-degree d−(x) of a vertex x is the number of arcs leaving and coming to xrespectively. For example, inD of Figure 1.4, d+(x) = 1, d−(x) = 0, d+(y) = 1, d−(y) = 0,and d+(z) = 1, d−(z) = 2. The out-degree and in-degree are called semi-degrees.

Instead of the sum-of-degrees proposition for undirected graphs, we have the sum-of-semi-degrees proposition:

Proposition 1.3.1 The sum of out-degrees of vertices in a digraph D = (V,A) equals thenumber of arcs in G. In notation,

∑x∈V d

+(x) = |A|. Also,∑

x∈V d−(x) = |A|.

The out-neighbours of a vertex x in a digraph D = (V,A) are all vertices y for whichxy ∈ A. The in-neighbours of a vertex x in a digraph D = (V,A) are all vertices y forwhich yx ∈ A. Observe that the number of out-neighbours of x is its out-degree and thenumber of in-neighbours of x is its in-degree. The vertex z is the only out-neighbour ofvertex x in Figure 1.4. The vertex z has two in-neighbours: x and y (in Figure 1.4).

Question 1.3.2 (a) Compute the number of arcs in a digraph with out-degree sequence3, 2, 2, 1, 1, 1, 0.

(b) Draw a digraph with in-degree sequence 2, 2, 2, 1, 1, 1.

(c) Draw a digraph with 6 vertices in which all in-degrees are different.

1.4 Subgraphs

The deletion of an edge e from a (undirected) graph G = (V,E) means the transformationthat changes G into G′ = (V,E − e). Consider the London Underground (LU) graph. Ifwe delete some of the edges of LU graph (i.e., stop trains running between some stations)we obtain a subgraph of the LU graph, which we also call a spanning subgraph sinceit has the same vertices as the LU graph, but less edges. In Figure 1.5, H is a spanningsubgraph of G.

The deletion of a vertex v from a graph G = (V,E) means deletion of v and all edgesof G with end-vertex v from G. Indeed, if we close one of the stations in the LU graph, weeffectively will close all edges to this station. If we delete some vertices and edges from agraph G, we get a subgraph of G. If only vertices are deleted, we speak of an inducedsubgraph. If G = (V,E) and we delete set W of vertices in G, we say that the remainingsubgraph is induced by V −W. The third (unnamed) graph in Figure 1.5 is an inducedsubgraph of G. It is induced by a, b, c, f. If we delete any vertex from graph H in Figure1.5, we obtain a graph which is neither spanning nor induced subgraph of G.

The deletion of vertices and/or edges is of interest for network reliability applications.Indeed, in a computer network we are interested what is the minimum number of links

Page 18: Algorithms and Complexity II

18 CHAPTER 1. BASIC NOTIONS ON UNDIRECTED AND DIRECTED GRAPHS

fa

bc d

ef

G

H

a

b c

Figure 1.5: A graph and its subgraphs

h

a

b c

d

e

f

g

Figure 1.6: A graph representing a network of computers

between computers that have to be shut down before one cannot communicate from somecomputer to another in the network. The larger the number the more reliable is thenetwork. Assume that the graph in Figure 1.6 represents a network of computers. Here, itis enough to shut down just one computer h to make the network disconnected. However,two links, edges ch, hd should be deleted before the network is disconnected. Clearly, nosingle link failure will make the network disconnected.

Some subgraphs of graphs are of special interest. The most important of them arepaths and cycles. Many lines of LU graph are paths. For example, the Jubilee Line is apath. Recall that a path P is a sequence of distinct vertices such that any vertex of Pis joined by an edge to its predecessor and/or successor in P . Some lines are not paths.For example, the Metropolitan Line is not a path as there two branches leaving Harrow-on-the-Hill station (one to Uxbridge and another to Watford, Chesham and Amersham,which is divided into more branches). The Circle Line is an example of a cycle.

Question 1.4.1 (a) Draw the subgraph of G (in Figure 1.6) induced by the verticesa, c, d, e, f, g.

(b) What are degrees of the vertices of a path (a cycle)?

Page 19: Algorithms and Complexity II

1.5. ISOMORPHISM OF GRAPHS AND DIGRAPHS 19

h

u v

xy

a

bcd

e

f

g

Figure 1.7: Isomorphic graphs

d

u v

xy

a b c

Figure 1.8: Isomorphic graphs

1.5 Isomorphism of graphs and digraphs

A graph can be drawn in many different ways. A casual viewer may think that all drawingsare different graphs. This provides a motivation to the following definition.

Two graphs G and H are isomorphic if there is a one-to-one correspondence (calledan isomorphism) between the vertices of G and H such that a pair of vertices in G arejoined by an edge if and only if the corresponding pair of vertices in H are joined by anedge. See Figure 1.7 for three different drawings of the same (up to isomorphism) graph.

To verify whether two graphs are isomorphic or not it is useful to check their parame-ters: to be isomorphic they must have the same number of vertices, edges, degree sequence(up to permutation of numbers), etc.

Question 1.5.1 Prove that two graphs in Figure 1.8 are isomorphic.

Solution: Consider the one-to-one correspondence given by a→v, b→x, c→y, d→u. Itis easy to verify that this correspondence is isomorphism. In particular, there is an edgebetween a and b, and there is an edge between v and x.

Question 1.5.2 Prove that two graphs in Figure 1.9 are not isomorphic.

Solution: Each of graphs G and H has just one vertex of degree 3. However, the vertexof G of degree 3 is linked by edge to just one vertex of degree 1, and the vertex of H ofdegree 3 is linked by edge to two vertices of degree 1. Hence, G and H are not isomorphic.

Page 20: Algorithms and Complexity II

20 CHAPTER 1. BASIC NOTIONS ON UNDIRECTED AND DIRECTED GRAPHS

z

a b c d e

f

u v w x y

Figure 1.9: Non-isomorphic graphs

h

a b

c d

e f

g

Figure 1.10: Isomorphic graphs

Question 1.5.3 Prove that two graphs in Figure 1.10 are isomorphic.

Question 1.5.4 Prove that two graphs in Figure 1.11 are not isomorphic.

Question 1.5.5 Draw two non-isomorphic graphs with

(a) 6 vertices and 10 edges

(b) 6 vertices and 11 edges

Two digraphs D and H are isomorphic if there is a one-to-one correspondence be-tween their vertices that ’preserves’ arcs, i.e., if vertices x and y in D correspond to verticesa and b in H, then xy is an arc in D if and only if ab is an arc in H.

Question 1.5.6 Prove that the two digraphs in Figure 1.12 are isomorphic.

h

a b

c d

e f

g

Figure 1.11: Non-isomorphic graphs

Page 21: Algorithms and Complexity II

1.6. CLASSES OF GRAPHS 21

R

a

b c

d e

f a

b c

d e

f

Q

Figure 1.12: A digraph Q and its converse R.

The converse of a digraph D is obtained from D by reversing the directions of allarcs in D. In Figure 1.12, R is the converse of Q.

Question 1.5.7 Give three examples of digraphs on 6 vertices whose converses are notisomorphic to the original digraphs.

1.6 Classes of graphs

Paths and cycles can be considered as graphs themselves. A path on n vertices, denotedby Pn, is a graph with vertices v1, v2, . . . , vn and edges v1v2, v2v3, . . . , vn−1vn. If we addedge vnv1 to Pn, we get a cycle on n vertices denoted by Cn.

A complete graph on n vertices, denoted by Kn, is a graph on n vertices in whichevery two vertices are joined by an edge (are adjacent). If we fix n, then there is only onecomplete graph on n vertices (up to isomorphism). Thus, we may speak of the completegraphs on 3,4,5, etc. vertices. See Figure 1.13 for K3 and K4 and Figure 1.7 for threedifferent drawings of K4. Clearly, C3 = K3.

Kn = (V,E) has n(n− 1)/2 edges. Indeed, the degree of every vertex is n− 1. By thesum-of-degrees proposition, 2|E| =

∑v∈V d(v) = n(n− 1). Hence, |E| = n(n− 1)/2.

A complete bipartite graph, denoted by Kp,q, is a graph on p + q vertices, whosevertices are partitioned into two partite sets P and Q such that |P | = p, |Q| = q, everyvertex in P is adjacent with every vertex in Q, and no two vertices in P (Q, respectively)are adjacent. Clearly, Kp,q has pq edges. See Figure 1.13 for K2,3 and K3,3.

The vertices of the n-cube can be viewed as binary (0, 1)-sequences with n elements(= coordinates). The n-cube is denoted by Qn. For example, Q3 has vertices

000, 001, 010, 100, 011, 101, 110, 111.

Graph Q3 is depicted in Figure 1.13. Two vertices of Qn are adjacent if and only if theydiffer only in one coordinate. For example, vertex 000 is adjacent only with 001, 010, 100.

Page 22: Algorithms and Complexity II

22 CHAPTER 1. BASIC NOTIONS ON UNDIRECTED AND DIRECTED GRAPHS

Q3

K3 K4 K2,3 K3,3

Figure 1.13: Some graphs

Thus, a vertex of n-cube is a sequence i1i2i3 . . . in of digits equal 0 or 1. There existexactly 2n such sequences, i.e., Qn has 2n vertices.

For a digit i = 1, i = 0 and for i = 0, i = 1. By definition, i1i2 . . . in is adjacentexactly with i1i2i3 . . . in, i1i2i3 . . . in, i1i2i3 . . . in, . . ., i1i2i3 . . . in. Thus, every vertex inQn is adjacent to exactly n vertices. To compute the number of edges in Qn = (V,E), weuse the sum-of-degrees proposition: 2|E| =

∑v∈V d(v) = n2n. Hence, |E| = n2n−1.

Question 1.6.1 Compute the number of vertices and edges in Q5.

1.7 Graph data structures

For the adjacency matrix representation of a digraph D = (V,A), we assume that thevertices of D are labeled v1, v2, . . . , vn in some arbitrary but fixed manner. The adjacencymatrix M(D) = [mij ] of a digraph D is an n × n-matrix such that mij = 1 if vivj ∈ Aand mij = 0 otherwise. The adjacency matrix representation is a very convenient andfast tool for checking whether there is an arc from a vertex to another one. A drawbackof this representation is the fact that to check all adjacencies, without using any otherinformation besides the adjacency matrix, one needs Ω(n2) time. Thus, the majority ofalgorithms using the adjacency matrix cannot have complexity lower than Ω(n2) (thisholds in particular if we include the time needed to construct the adjacency matrix).

The adjacency list representation of a digraph D = (V,A) consists of a pair ofarrays Adj+ and Adj−. Each of Adj+ and Adj− consists of |V | (linked) lists, one for everyvertex in V . For each x ∈ V , the linked list Adj+(x) (Adj−(x), respectively) containsall out-neighbours of x (in-neighbours of x, respectively) in some fixed order (see Figure1.14). Using the adjacency list Adj+(x) (Adj−(x)) one can obtain all out-neighbours

Page 23: Algorithms and Complexity II

1.8. SOLUTIONS 23

b

e

g

h

a

c

d

f

ga

c

d

e

f

g

h

b c a f c

d f e g

e

f

g

f

g

/

/

/

/

/

/

/

h

/d

Figure 1.14: A directed multigraph and a representation by adjacency lists Adj+.

(in-neighbours) of a vertex x in O(|Adj+(x)|) (O(|Adj−(x)|)) time. A drawback of theadjacency list representation is the fact that one needs, in general, more than constanttime to verify whether xy ∈ A. Indeed, to decide this we have to search sequentiallythrough Adj+(x) (or Adj−(x)) until we either find y (x) or reach the end of the list.

Question 1.7.1 Give the adjacency lists Adj− for the graph in Fig. 1.14.

In the rest of this book, we will sometimes use, for simplicity, adjacency matrices, butwe will not take into consideration the time needed to construct such matrices (they areinputs). Notice, however, that in practice adjacency lists are used more often.

1.8 Solutions

Question 1.2.9

(i) Assume that 5, 3, 3, 3, 3, 3, 2 is graphic and H is a graph with this degree sequencewith vertices u, v, w, t, x, y, z. We now apply the HH algorithm.

uvwtxyz

5333332

0222222

0011222

0011011

0000011

Since the last sequence is graphic, the initial one is graphic as well. The correspondinggraph is depicted in the figure below.

Page 24: Algorithms and Complexity II

24 CHAPTER 1. BASIC NOTIONS ON UNDIRECTED AND DIRECTED GRAPHS

z

uv

w t

x

y

(ii) The HH algorithm shows that the sequence is not graphic.

uvwtxyz

6442211

0331100

0020000

Question 1.2.10

(i): This is not a sequence of degrees of a tree as it has only one item 1 (every treewith at least 2 vertices has two leaves).

(ii) Let’s use the Havel-Hakimi algorithm for 2, 2, 1, 1, 1, 1, 4.

Order the sequence and assume that 4, 2, 1, 1, 1, 1 is graphic and H is a graph with thisdegree sequence with vertices u, v, w, x, y, z.

uvwxyz

421111

010001

000000

Since the last sequence is graphic, the initial one is graphic as well. The correspondinggraph G has edges uv, uw, ux, uy, vz, which is a tree.

Page 25: Algorithms and Complexity II

Chapter 2

Walks, Connectivity and Trees

2.1 Walks, trails, paths and cycles in graphs

A walk in a graphG is an alternating sequence of vertices and edges v1e1v2e2v3 . . . vn−1en−1vnsuch that ei = vivi+1, i = 1, 2, . . . , n− 1. For example, in Figure 2.1, x, xz, z, zx, x, xv, v isa walk. A walk is closed if v1 = vn and open if v1 6= vn. In Figure 2.1, x, xz, z, zx, x, xv, vis an open walk, and x, xz, z, zx, x is a closed walk.

Certainly, a walk is defined by v1v2 . . . vn, i.e., the sequence of its vertices. For example,walk x, xz, z, zx, x, xv, v can be written as xzxv. We say that a walk v1v2 . . . vn is fromv1 to vn, and its first vertex is v1 and the last vertex is vn. We say that n − 1 is thelength of the walk (its number of edges). Walk xzxv is from x to v and its length is 3.

A trail in a graph G is a walk in which all edges are distinct. Walk x, xz, z, zx, x, xv, vin Figure 2.1 is not a trail as edge xz = (zx) repeats itself. Walk uvwuy is a trail.

A path is a walk with distinct vertices. In a cycle all vertices are distinct withexception of the first and last ones, which coincide. Thus, in a cycle v1v2 . . . vn, v1 = vn.We say that v1v2 . . . vn is through vi for any i = 1, 2, . . . , n. In Figure 2.1, trail uvwuy isnot a path as u repeats itself. Trail uvwx is a path and trail uvwxu is a cycle.

t

u v

w

xy

z

Figure 2.1: A graph G

25

Page 26: Algorithms and Complexity II

26 CHAPTER 2. WALKS, CONNECTIVITY AND TREES

uz

w

vx

y

Figure 2.2: A digraph D

Question 2.1.1 Which of the following walks is a trail (path, cycle)

ztyt, ywvxw, ywvx, ywvzty ?

The following proposition is one of the reasons why mostly paths and cycles and notgeneral walks and trails are of interest in graph theory.

Proposition 2.1.2 Let G be a graph and let W be a walk in G with first vertex u andlast vertex v 6= u. Then G has a path from u to v.

Proof: Consider a shortest walkW from u to v. Suppose that some vertices inW coincide,i.e., there is a vertex w such that W = u . . . w . . . w . . . v. However, this means that G hasa walk u . . . w . . . v, which is shorter than W , a contradiction. Hence, all vertices of W aredistinct, and thus W is a path. QED

The definitions and results of this section hold also for digraphs. For example, in thedigraph of Figure 2.2, zuwuv is a trail, zuv is a path and uwu is a cycle.

2.2 Connectivity

A graph G is connected if there is a walk from any vertex of G to any other vertex ofG. By Proposition 2.1.2, G is connected if and only if there is a path from any vertex ofG to any other vertex of G. Clearly, every complete graph and every complete bipartitegraph are connected, for they are ”in one piece.”

Suppose that a graph G with vertices v1, v2, . . . , vn is connected. Merge a path fromv1 to v2 with a path from v2 to v3 with a path v3 to v4, etc. with a path from vn−1 to vn.As a result, we get a walk containing all vertices in G. At the same time, if a graph Hhas a walk containing all vertices, then parts of this walk provide walks between pairs ofvertices in H. Thus, we have obtained the following:

Proposition 2.2.1 A graph G is connected if and only if G has a walk containing allvertices.

Page 27: Algorithms and Complexity II

2.2. CONNECTIVITY 27

p7

j1

j2

j3

j4

j5

p1

p2

p3

p4

p5

p6

Figure 2.3: A graph H.

Some graphs consists of many pieces. Consider Figure 2.3. The graph H there is notconnected, it is disconnected. It consists of two ”pieces”, one is the subgraph induced byvertices j5, p4, p7 and the subgraph induced by the rest of the vertices. These two subgraphsare called connectivity components of H. In general, connectivity components of agraph G are maximum connected induced subgraphs of G.

There are many applications of connectivity. Consider one of them. Treat the countriesof the world as vertices of a graph. We say that two countries have strong economicalrelations if the total annual trade between them (in both directions) is at least £1,000,000.Finding connectivity components in this graph would allow us to see what countries areeconomically dependent of each other directly or indirectly.

Now we will introduce a simple, yet very important, technique in algorithmic graphtheory called depth-first search (DFS). DFS allows, in particular, to find connectivitycomponents of a graph very efficiently.

Let G = (V,E) be a graph. In DFS, we start from an arbitrary vertex of G. At everystage of DFS, we visit some vertex x of G. If x has an unvisited neighbour y, we visitthe vertex y (if x has more than one unvisited neighbour, we choose y as an arbitraryunvisited neighbour). If x has no unvisited neighbour, we call x explored and return tothe predecessor of x (the vertex from which we have moved to x). If x does not have apredecessor, we find an unvisited vertex to “restart” the above procedure. If such a vertexdoes not exist, we stop. Each time we restart we start a new connectivity component.Actually, DFS can be used not only to find connectivity components, but also to computea spanning forest in a graph as we see later.

In our formal description of DFS for connectivity components (DFS-CC), each vertexx of G gets a stamp: visit(x) = 0 when x has not been visited yet and visit(x) = 1 oncex has been visited. In the following description, N(v) is the set of neighbours of a vertexv. To list all vertices of each connectivity component, we use root(v) and List(v): root(v)equals to some vertex x in the connectivity component containing v (we may call x a

Page 28: Algorithms and Complexity II

28 CHAPTER 2. WALKS, CONNECTIVITY AND TREES

h

k l

mn

a

bcd

e

f

g

Figure 2.4: Disconnected graph H.

root-vertex) and List(v) is a set such that if List(v) is non-empty it contains all verticesbelonging to the same component as v, i.e., vertices with the same root-vertex.

DFS-CC

Input: A graph G = (V,E).

Output: List(v) such that if List(v) is non-empty it contains all vertices belonging to thesame component as v

1. for v ∈ V do root(v) := v; visit(v) := 0

2. for v ∈ V do if visit(v) = 0 then DFS-PROC(v)

3. for v ∈ V do List(v) := ∅

4. for v ∈ V do u := root(v); List(u) := List(u) ∪ v

DFS-PROC(v):

1. visit(v) := 1

2. for u ∈ N(v) do if visit(u) = 0 then root(u) := root(v); DFS-PROC(u)

Clearly, the main body of the algorithm takes O(|V |) time. The total time for executingthe different calls of the procedure DFS-PROC is O(|E|) since

∑x∈V d(x) = 2|E| by the

sum-of-degrees proposition. As a result, the time complexity of DFS-CC is O(|V |+ |E|).Thus, we have the following:

Proposition 2.2.2 For a graph G = (V,E), we can find all connectivity components intime O(|V |+ |E|).

Page 29: Algorithms and Complexity II

2.2. CONNECTIVITY 29

Question 2.2.3 Apply DFS-CC to find connectivity components in graph H of Figure 2.4.Assume that in the loops of DFS-CC and DFS-PROC the vertices of H are considered inalphabetical order.

Solution: Since in line 2 of DFS the vertices of H are considered in alphabetical order,a is visited first and the first four vertices to be visited are a, d, b, c (in this order). We’llhave root(a) = root(d) = root(b) = root(c) = a. After that the next four vertices willbe visited in the order e, g, f, h. As a result, root(e) = root(g) = root(f) = root(h) = e.Finally, the last four vertices will be visited in the following order: k, l,m, n and we’llhave root(k) = root(l) = root(m) = root(n) = k. In line 4 of DFS, we’ll get List(a) =a, b, c, d, List(e) = e, f, g, h, List(k) = k, l,m, n. The rest of the lists will remainempty. Thus, we’ve obtained three connectivity components in H.

12

4

3

5

7 8

9 10

11 12

13 14

1516

17

6

Figure 2.5: A graph G

Question 2.2.4 The algorithm DFS-CC is applied to find connectivity components in thegraph G of Figure 2.5. Assume that in the loops of DFS-CC and DFS-PROC the verticesof G are considered in the natural order. Give the order in which the vertices of G arevisited.

Solution: The vertices of G are visited in the following order:

1, 2, 3, 5, 4, 6, 13, 16, 14, 15, 17, 7, 8, 10, 9, 11, 12.

[This answer can be considered as a model answer to an exam question of thistype.]

Question 2.2.5 The algorithm DFS-CC is applied to find connectivity components in thegraph G of Figure 2.6. Assume that in the loops of DFS-CC and DFS-PROC the verticesof G are considered in the natural order. Give the order in which the vertices of G arevisited.

Page 30: Algorithms and Complexity II

30 CHAPTER 2. WALKS, CONNECTIVITY AND TREES

12

34

5

6

7 8

9 10

11 12

13 14

1516

17

18

19

Figure 2.6: Disconnected graph G

Question 2.2.6 The algorithm DFS-CC is applied to find connectivity components ingraph H of Figure 2.3. Assume that in the loops of DFS-CC and DFS-PROC the verticesof H are considered in the following order: j1, j2, . . . , j5, p1, p2, . . . , p7. Give the order inwhich the vertices of H are visited. How many connectivity components are in H?

2.3 Edge-connectivity and Vertex-connectivity (extra ma-terial)

Some other applications of connectivity are related to reliability of networks. A typicalquestion is as follows: a graph G is connected, what is the minimum number of edgeshas to be deleted from G to make G disconnected? Clearly, the larger that number,called the edge-connectivity of G, the more reliable the network represented by G. Theedge-connectivity of G is denoted by λ(G). One can easily see that λ(Pn) = 1 (n ≥ 2),λ(Cn) = 2 (n ≥ 3).

Question 2.3.1 Prove that λ(Kn) = n−1 (n ≥ 2) and λ(Kp,q) = minp, q (maxp, q >1).

In general, for a graph G = (V,E), λ(G) ≤ minx∈V dG(x) since deleting all edges witha common end-vertex will leave that vertex isolated from the rest of the graph. For manygraphs we have simply λ(G) = minx∈V dG(x). In particular, λ(Kn) = minx∈V d(x) = n−1.However, for graph G in Figure 2.7, λ(G) = 2 (delete edges ch, hd and G becomes discon-nected, deletion of any single edge will not make G disconnected), but minx∈V dG(x) = 3.

Page 31: Algorithms and Complexity II

2.3. EDGE-CONNECTIVITY ANDVERTEX-CONNECTIVITY (EXTRAMATERIAL)31

h

a

b c

d

e

f

g

Figure 2.7: A graph

For a pair x, y of vertices in a graph G, λxy(G) is the minimum number of edges whosedeletion from G results in a graph in which x and y belong to different connectivity com-ponents. The parameter λxy(G) is called the local edge-connectivity between x andy. Notice that λ(G) = minλxy(G) : x 6= y ∈ V (G).

A pair P,Q of paths are called edge-disjoint if no edge of P is an edge of Q and noedge of Q is an edge of P . The following important theorem links edge-disjoint paths andlocal edge-connectivity.

Theorem 2.3.2 (Menger) For a pair x, y of distinct vertices of a graph G, λxy(G) equalsthe maximum number of edge-disjoint paths between x and y.

Sometimes, reliability of a network (graph) G is determined not by the minimumnumber of edges whose deletion makes G disconnected, but by the minimum numberof vertices of G whose deletion makes G disconnected. The last parameter is called theconnectivity (or, vertex connectivity) of G. The connectivity of G is denoted by κ(G).One can easily see that κ(Pn) = 1 (n ≥ 3) and κ(Cn) = 2 (n ≥ 3). By definition, weassume that κ(Kn) = n − 1. All connected graphs apart from K1 have positive vertexconnectivity.

Question 2.3.3 Prove that κ(Kp,q) = minp, q (maxp, q > 1).

Theorem 2.3.4 For every graph G = (V,E), κ(G) ≤ λ(G) ≤ minx∈V d(x).

Proof: If G is disconnected, then κ(G) = λ(G) = 0 and thus the inequality follows.

Assume now that G is connected. Let F be a subset of edges in G such that G− F isdisconnected and |F | = λ(G). Let U be a set of vertices formed by taking one vertex fromeach edge in F . We have G − U is disconnected and κ(G) ≤ |U | ≤ |F | = λ(G). Thus,κ(G) ≤ λ(G). Let y ∈ V such that d(y) = minx∈V d(x). If we delete all edges having y asan end-vertex, we obtain two components y and G−y. Thus, minx∈V d(x) = d(y) ≥ λ(G).QED

Page 32: Algorithms and Complexity II

32 CHAPTER 2. WALKS, CONNECTIVITY AND TREES

z

a b c d e

f

u v w x y

Figure 2.8: Trees

Similarly to λxy(G), we define κxy(G), which is the minimum number of vertices whosedeletion makes G disconnected such that x and y belong to different components. Noticethat κ(G) = minκxy(G) : x 6= y ∈ V (G).

Theorem 2.3.5 (Menger) For a pair x, y of distinct vertices of a graph G, κxy(G) equalsthe maximum number of paths between x and y with the following property: no pair of thepaths has any common vertices apart from x and y.

Menger’s Theorems make it possible to construct efficient algorithms to compute λ(G)and κ(G) and their local variations, but this material is outside the scope of this lecturenotes.

2.4 Basic properties of trees and forests

A forest is a graph with no cycle. A tree is a connected forest. Trees and forests play animportant role in many applications of graph theory especially in computer science.

Theorem 2.4.1 Let T be a tree with n vertices. Then

(a) T has n− 1 edges

(b) addition of an edge between two non-adjacent vertices in T creates exactly one cycle

(c) there is exactly one path between any pair of vertices in T

(d) deletion of an edge from T creates a disconnected graph with two connectivitycomponents

According to (d) a tree is minimally connected graph. Thus, if we want to connect aset of newly created camps by roads with minimum expenses, we should construct a treesystem of the roads.

Question 2.4.2 Prove that for a forest F = (V,E) consisting of c trees, we have |E| =|V | − c.

Page 33: Algorithms and Complexity II

2.5. SPANNING TREES AND FORESTS 33

Solution: Let T1 = (V1, E1), T2 = (V2, E2), . . . , Tc = (Vc, Ec) be trees of F = (V,E). By(a) of Theorem 2.4.1, we have |Ei| = |Vi| − 1 (i = 1, 2, . . . , c). Thus,

|E| =c∑i=1

|Ei| =c∑i=1

(|Vi| − 1) =c∑i=1

|Vi| −c∑i=1

1 = |V | − c.

QED

A vertex of degree 1 is called a leaf.

Theorem 2.4.3 (Leaf Theorem) Every tree has a vertex of degree 1.

Proof: Let T = (V,E) be a tree. Since T is connected d(v) ≥ 1 for every v ∈ V. Assumethat d(v) ≥ 2 for every v ∈ V. By the sum-of-degrees proposition, 2|E| ≥ 2n, wheren = |V |. Thus, |E| ≥ n. But |E| = n− 1, a contradiction. So, there is a vertex of degree1. QED

Leaf Theorem can be improved as follows:

Question 2.4.4 [A solution is given in the end of the chapter.] Let T be a treewith at least two vertices. Prove that T has at least two leaves.

2.5 Spanning trees and forests

Let G be a connected graph. Let us construct a connected spanning subgraph H of Gwith minimum number of edges by deleting edges one by one. We claim that H is a tree.Indeed, H is connected. Assume that H has a cycle. But by deleting an edge in that cyclewe get a connected subgraph of G, a contradiction to the minimality of H. Thus, everyconnected graph G has a tree as a spanning subgraph, it is called a spanning tree T ofG.

If G has several components, we can find a spanning tree in each component. As aresult we get a spanning forest of G. The following pseudo-code finds a spanning forestin a graph G. The pseudo-code DFS-SF is a DFS algorithm, a modification of DFS-CC inSection 2.2.

DFS-SF

Input: A graph G = (V,E).

Output: a set of all edges F that belong to a forest of G

1. F := ∅; for v ∈ V do visit(v) := 0

Page 34: Algorithms and Complexity II

34 CHAPTER 2. WALKS, CONNECTIVITY AND TREES

v

a b

fG H

a b

d c d cQ

a b

cd

x

y z

u

Figure 2.9: Graphs

2. for v ∈ V do if visit(v) = 0 then DFS-PROC1(v)

DFS-PROC1(v):

1. visit(v) := 1

2. for u ∈ N(v) do if visit(u) = 0 then F := F ∪ uv; DFS-PROC1(u)

Question 2.5.1 Apply DFS-SF to find spanning trees in graphs depicted in Figure 2.9.

2.6 Greedy-type algorithms and minimum weight spanningtrees

In many applications, weighted graphs are of interest. A graph G = (V,E) is weightedif there is an assignment of edges to non-negative real numbers such that every edges hasweight. The weights may reflect various parameters such as distances between vertices,time or cost of going between vertices. Graph G in Figure 2.10 is a weighted graph.

The weight of a graph is the sum of the weights of its edges. The following minimumconnector problem is of interest: Given a weighted connected graph G, find a spanningconnected subgraph of G of minimum weight. This problem arises in applications. Forexample, suppose we created a number of new villages in ’the middle of nowhere’ and wantto connect the villages with roads such that the total distance of the roads is minimum.

According to the properties of trees, the minimum weight connector is a spanning treeof minimum cost. To find this tree T the following greedy algorithm can be used. Rankedges of G e1, e2, . . . , em such that w(e1) ≤ w(e2) ≤ w(e3) ≤ · · · ≤ w(em). Pick edges inthat order one by one and add them to (initally empty) T except when the current edgecreates a cycles with previously chosen edges.

Page 35: Algorithms and Complexity II

2.6. GREEDY-TYPE ALGORITHMS ANDMINIMUMWEIGHT SPANNING TREES35

Q

5

6

1

2

3

3ab

cd

e

f2

GT

Figure 2.10: A weighted graph and its minimum weight spanning trees

The usefulness of the greedy algorithm is due to the following result, which we do notprove.

Theorem 2.6.1 The greedy algorithm always finds a minimum weight spanning tree.

Question 2.6.2 Find a minimum weight spanning tree in graph G in Figure 2.10. Howmany minimum weight spanning trees G has?

Solution: We use the greedy algorithm. We rank edges of G in the following order:cd, bc, ef, bf, be, ad, ab. We start from empty T. We pick edges cd, bc, ef and bf withoutcreating any cycle and thus add them to T . Edge be cannot be added to T as it createscycle befb with edges ef and bf chosen earlier. We add edge ad to T , but we do not addab to T as it creates cycle abcda with previously chosen edges. As a result we get T inFigure 2.10.

We can rank edges of G slightly differently: cd, bc, ef, be, bf, ad, ab. Then the greedyalgorithm constructs Q in Figure 2.10 (edge be gets chosen before bf and bf cannot bepicked up as it creates cycle with previously chosen edges). Thus, we get another minimumweight spanning tree. Since bc and ef have the same weight we can have several rakningsof the edges, but the order of the last two edges does not matter since they both will bechosen no matter what ranking is considered. Thus, G has exactly two minimum weightspanning trees.

Question 2.6.3 Find a minimum weight spanning tree and the number of minimumweight spanning trees in the graphs of Figure 2.11.

The greedy algorithm for finding a minimum weight spanning tree is often calledKruskal’s algorithm. We give a pseudo-code of an implementation of Kruskal’s algo-rithm below. This is a relatively simple code, but not the most efficient implementation ofthe algorithm though. More efficient implementations won’t be considered in this course.

Page 36: Algorithms and Complexity II

36 CHAPTER 2. WALKS, CONNECTIVITY AND TREES

5

1

F

ab

cd

e

f2

3

4

7

6

9

1

1

1

1

2

2

2

HG

10

a b

c d

e f

a b

cd

e

f

g

1

1

2

2

3 4

5

6

6

6

Figure 2.11: Weighted graphs

Kruskal’s Algorithm

Input: A connected graph G = (V,E) with weights on the edges.

Output: A minimum weight spanning tree T = (V, F ) of G.

1. for v ∈ V do root(v) := v

2. F := ∅

3. sort the edges of G in the non-decreasing order e1, e2, . . . , em of their weights (i.e.,w(e1) ≤ . . . ≤ w(em)) and output it as a queue Q.

4. until Q = ∅ do

delete the head uv of Q from Q; if root(u) 6= root(v) then

add uv to F ; for each x ∈ V do if root(x) = root(v) then root(x) := root(u)

Theorem 2.6.4 The above implementation of Kruskal’s algorithm is correct (i.e., alwaysproduces the right solution to the minimum weight spanning tree problem).

Page 37: Algorithms and Complexity II

2.6. GREEDY-TYPE ALGORITHMS ANDMINIMUMWEIGHT SPANNING TREES37

Proof: We apply Theorem 2.6.1.

Initially any greedy algorithm starts from the zero-edge subgraph of the graph G.Assume that in the course of the algorithm we have produced a forest with connectivitycomponents (i.e., trees) T1, T2, . . . Tp (the trees include all vertices of G, i.e., some ofthem may consist of a single vertex). The algorithm chooses the next edge uv, which isthe lightest edge among those edges that have not been considered (for inclusion in theminimum weight spanning tree) by the algorithm so far. The algorithm must include uvif and only if its inclusion does not create a cycle in T1 ∪ T2 ∪ . . . ∪ Tp. This means thatthe algorithm must include uv if and only if u and v belong to different components ofT1 ∪ T2 ∪ . . . ∪ Tp, i.e. to different trees.

Thus, the implementation of the algorithm above starts from creating the zero-edgesubgraph of G in Steps 1 and 2. Step 1 indicates that each component of the subgraphconsists of a single vertex, which is the root of the component. In Step 3 we sort all edgesof G and keep them in a queue Q (the lightest edge is the head of Q). In Step 4 we deletethe lightest edge uv from the current Q. Then we check whether u and v belong to thesame connectivity component of the current forest by comparing the root vertices of theircomponents. If u and v belong to different components, we add uv to T and merge thetwo components by assigning the root of u as a root to all vertices in the component of v.The loop of Step 4 lasts until Q is empty. QED

The next theorem give the running time of the implementation.

Theorem 2.6.5 The above implementation of Kruskal’s algorithm can be run in timeO(|V |2 + |E| log |E|).

Proof: Step 1 and 2 run in time O(|V |). Step 3 can run in time O(|E| log |E|) using oneof the efficient sort algorithms. In Step 4 we consider all E edges of G, but only |V | − 1 ofthem will be included in T (since T has |V | vertices and |V | − 1 edges by Theorem 2.4.1).Any included edge uv will require O(|V |) operations to merge the components of u and v.Thus, Step 4 will require O(|E|+ (|V | − 1)|V |) ⊆ O(|V |2) operations. QED

Question 2.6.6 Applying Kruskal’s algorithm, find a minimum weight spanning tree ingraphs G of Figures 2.10 and 2.11.

There is another algorithm for finding a minimum spanning tree T . This is not thegreedy algorithm, but a greedy-type algorithm called Prim’s algorithm. The main ideaof Prim’s algorithm is to build T by starting from a single vertex and increasing the currentT by appending to it new vertices. It is possible to prove that Prim’s algorithm alwayssolves the problem if at each iteration we add a vertex x ∈ V − VT with minimum weightminw(xz) : z ∈ VT , i.e., with the minimum distance to the current T .

Page 38: Algorithms and Complexity II

38 CHAPTER 2. WALKS, CONNECTIVITY AND TREES

Prim’s Algorithm

Input: A connected graph G = (V,E) with weight w(uv) on every edge uv. We assumethat w(uv) =∞ if uv 6∈ E.

Output: A minimum weight spanning tree T = (VT , ET ) of G.

1. ET := ∅; choose a vertex u; VT := u

2. for v ∈ V − u do dist(v) := w(vu)

3. until V = VT do

4. find x ∈ V −VT with minimum dist(x) (here dist(x) = w(xv), v ∈ VT ); VT := VT ∪x;ET := ET ∪ xv

5. for y ∈ V − VT do dist(y) := mindist(y), w(yx)

Step 1 initializes T . In Step 2 we choose the first vertex to include in T by giving it theminimum value of dist. The loop starting at Step 3 increases T by adding one vertex x ata time. The added vertex x, as we pointed out above, must have the minimum distanceto the current T . Step 5 updates dist(y) for each y outside T . The update is correct sinceonly one vertex has been added to T , so the distance decreases only of dist(y) < w(yx).

Theorem 2.6.7 The above implementation of Prim’s algorithm runs in time O(|V |2).

Proof: Steps 1 and 2 require O(|V |) operations. In each loop of Step 3 we spend O(|V |)time finding x (and v), updating T and dist(y) for each y outside T . There are |V |iterations of the loop. So, we get O(|V |2) operations as required. QED

Question 2.6.8 Applying Prim’s algorithm, find a minimum weight spanning tree ingraphs G of Figures 2.10 and 2.11.

2.7 Solutions

Question 2.4.4

Induction on the number of vertices n. It’s trivial for n = 2.

Page 39: Algorithms and Complexity II

2.7. SOLUTIONS 39

Suppose it’s true for all trees with n− 1 ≥ 2 vertices. We prove it for an arbitrary treeT with n vertices.

By Leaf Theorem, T has a leaf x. Consider T − x; T − x has two leaves y, z.

If x is not adjacent to either of them, T has tree leaves: x, y, z. Since x can be adjacentto only one of them, let’s assume that x is adjacent to y. Then T has two leaves: x, z.

Page 40: Algorithms and Complexity II

40 CHAPTER 2. WALKS, CONNECTIVITY AND TREES

Page 41: Algorithms and Complexity II

Chapter 3

Directed graphs

3.1 Acyclic digraphs

For undirected graphs we have studied trees, which are connected acyclic graphs, sincetrees have numerous applications. Similarly, acyclic digraphs have numerous applications.A digraph is acyclic if it has no directed cycle. Actually, when we speak of cycles orpaths in digraphs, we always mean directed cycles and paths without stating it. DigraphD in Figure 1.4 is not acyclic as it has cycle uwu. The digraphs in Figure 1.12 are acyclic.Clearly, the operation of converse cannot create cycles in acyclic digraphs, and thus theconverse of an acyclic digraph is an acyclic digraph.

3.1.1 Acyclic ordering of acyclic digraphs

Proposition 3.1.1 Every acyclic digraph has a vertex of in-degree zero as well as a vertexof out-degree zero.

Proof: Let D be a digraph in which all vertices have positive out-degrees. We show thatD = (V,A) has a cycle. Choose a vertex v1 in D. Since d+(v1) > 0, there is a vertex v2such that v1v2 is an arc. As d+(v2) > 0, v2v3 is an arc for some v3. Proceeding in thismanner, we obtain walks of the form v1v2 . . . vk. As V is finite, there exists the least k > 2such that vk = vi for some 1 ≤ i < k. Clearly, vivi+1 . . . vk is a cycle.

Thus an acyclic digraph D has a vertex of out-degree zero. Since the converse H ofD is also acyclic, H has a vertex v of out-degree zero. Clearly, the vertex v has in-degreezero in D. QED

41

Page 42: Algorithms and Complexity II

42 CHAPTER 3. DIRECTED GRAPHS

Proposition 3.1.1 allows one to check whether a digraph D is acyclic: if D has a vertexof out-degree zero, then delete this vertex from D and consider the resulting digraph;otherwise, D contains a cycle.

Let D be a digraph and let x1, x2, . . . , xn be an ordering of its vertices. We call thisordering an acyclic ordering if, for every arc xixj in D, we have i < j. Clearly, anacyclic ordering of D induces an acyclic ordering of every subdigraph H of D. Since nocycle has an acyclic ordering, no digraph with a cycle has an acyclic ordering. On theother hand, the following holds:

Proposition 3.1.2 Every acyclic digraph has an acyclic ordering of its vertices.

Proof: We give a constructive proof by describing a procedure that generates an acyclicordering of the vertices in an acyclic digraph D = (V,A). At the first step, we choosea vertex v with in-degree zero. (Such a vertex exists by Proposition 3.1.1.) Set x1 = vand delete x1 from D. At the ith step, we find a vertex u of in-degree zero in theremaining acyclic digraph, set xi = u and delete xi from the remaining acyclic digraph.The procedure has |V | steps.

Suppose that xixj in an arc in D, but i > j. As xj was chosen before xi, it means thatxj was not of in-degree zero at the jth step of the procedure; a contradiction. QED

Question 3.1.3 Find all acyclic orderings for digraph Q in Figure 1.12.

Solution: Vertex a is the only vertex of in-degree 0 in Q. Hence a is the first vertex inany acyclic ordering. When we delete a from Q, we get only b of in-degree 0. So, a, b isthe beginning of any acyclic ordering. After deleting b, we get c and d of in-degree 0. So,now we may choose either c or d as the next vertex in an acyclic ordering.

If we choose c, getting the partial acyclic ordering a, b, c, and delete it, d becomes theonly vertex of in-degree 0. We include d in the ordering and get a, b, c, d, delete d, choosee, and then remaining f. Thus, a, b, c, d, e, f is an acyclic ordering.

If we choose d, getting the partial acyclic ordering abd, and delete it, c becomes theonly vertex of in-degree 0. We include c in the ordering and get a, b, d, c, delete d, choosee, and then remaining f. Thus, a, b, d, c, e, f is another acyclic ordering.

We see that there are only two acyclic orderings of Q and they are given above.

Question 3.1.4 Find all acyclic orderings for digraphs in Figure 3.1.

Now we consider an algorithm for finding an acyclic ordering of an acyclic digraph.Recall that, for a vertex x of a digraph D = (V,A), N+(x) denotes the set of out-neighbours of x, i.e., vertices y such that xy ∈ A.

Page 43: Algorithms and Complexity II

3.1. ACYCLIC DIGRAPHS 43

R

a

b c

d e

f a

b c

d e

f

Q

Figure 3.1: Acyclic digraphs

DFS-A(D)Input: A digraph D = (V,A) on n vertices.Output: An acyclic ordering v1, . . . , vn of D.

1. for v ∈ V do tvisit(v) := 0

2. i := n+ 1

3. for v ∈ V do if tvisit(v) = 0 then DFS-PROC2(v)

DFS-PROC2(v)

1. tvisit(v) := 1

2. for u ∈ N+(v) do if tvisit(u) = 0 then DFS-PROC2(u)

3. i := i− 1, vi := v.

Theorem 3.1.5 The algorithm DFS-A correctly determines an acyclic ordering of anyacyclic digraph in time O(|V |+ |A|).

Figure 3.2 illustrates the result of applying DFS-A to an acyclic digraph starting fromvertex x and restarting from vertex z. The resulting acyclic ordering is z, w, u, y, x, v.

Question 3.1.6 Find all acyclic orderings for digraphs in Figure 3.1 using DFS-A.

Page 44: Algorithms and Complexity II

44 CHAPTER 3. DIRECTED GRAPHS

x

y

z

u

t

w

v5

v4

v1 v6

v2v3

Figure 3.2: The result of applying DFS-A to an acyclic digraph

3.1.2 Longest and Shortest paths in acyclic digraphs

Let D = (V,A,w) be an arc-weighted acyclic digraph. We’ll show that longest paths from avertex s to the rest of the vertices can be found quite easily, using dynamic programming.Without loss of generality, we may assume that the in-degree of s is zero. Let L =v1, v2, . . . , vn be an acyclic ordering of the vertices of D such that v1 = s. Denote by `(vi)the length of the longest path from s to vi. Clearly, `(v1) = 0. For every i, 2 ≤ i ≤ |V |,we have

`(vi) =

max`(vj) + w(vj , vi) : vj ∈ N−(vi) if N−(vi) 6= ∅∞ otherwise,

(3.1)

where N−(vi) is the set of in-neighbours of vi. The correctness of this formula can beshown by the following argument. We may assume that vi is reachable from s. Since theordering L is acyclic, the vertices of a longest path P from s to vi belong to v1, v2, . . . , vi.Let vk be the vertex just before vi in P . By induction, `(vk) is computed correctly using(3.1). The term `(s, vk) + w(vk, vi) is one of the terms in the right-hand side of (3.1).Clearly, it provides the maximum.

The algorithm has two phases: the first finds an acyclic ordering, the second imple-ments Formula (3.1). The complexity of this algorithm is O(|V |+ |A|) since the first phaseruns in time O(|V |+ |A|) (see DFS-A) and the second phase requires the same asymptotictime due to the formula

∑x∈V d

−(x) = |A|.

We illustrate the above algorithm and how to find an actual longest path from s toanother vertex in Question 3.1.9.

Question 3.1.7 Give a pseudo-code of the above algorithm.

Page 45: Algorithms and Complexity II

3.1. ACYCLIC DIGRAPHS 45

a

b

-1

c

d e

f2

3

14

3

235

Q

a

b c

d e

f2

1

3

4

5-2

3

R

4

Figure 3.3: Weighted digraphs

Question 3.1.8 What modifications to the above algorithm are required to obtain an al-gorithm for finding a shortest path from a fixed vertex s to each other vertex of an arc-weighted acyclic digraph D? [A solution is given in the end of the chapter.]

Question 3.1.9 Use the above algorithm to find the length of a longest path from a to fin digraph Q in Figure 3.3.

Solution: For a vertex x, let `(x) be the length of a longest path from a to x.

Consider the acyclic ordering a, b, d, c, e, f . Let’s establish s(x) in the order of theacyclic ordering. We have `(a) = 0. Vertex a is the only in-neighbour of b, so `(b) =`(a)+w(ab) = 2. Similarly for d: `(d) = `(a)+w(ad) = 3. Vertex c has three in-neighboursa, b, d. Hence, `(c) = max`(a) + w(ac), `(b) + l(bc), `(d) + l`(dc) = max1, 6, 8 = 8.For e, we have `(e) = max`(c) + w(ce), `(d) + w(de) = max12, 6 = 12. Finally,`(f) = max`(c) + w(cf), `(e) + w(ef) = 14. So, the length of a longest path from a tof is 14.

Question 3.1.10 Use the above algorithm to find the length of a shortest path from a tof in digraph R of Figure 3.3.

Solution: For a vertex x let s(x) be the length of a shortest path from a to x.

Note that a, b, c, d, e, f is an acyclic ordering of vertices in R. Let’s establish s(x) inthe order of the acyclic ordering. We have s(a) = 0, s(b) = s(a) + w(ab) = 2, s(c) =s(b) + w(bc) = 2 + 3 = 5. Since d has two in-neighbours, s(d) = mins(a) + w(ad), s(b) +w(bd) = min1, 6 = 1. We have s(e) = mins(c) +w(ce), s(d) +w(de) = min8, 6 = 6,s(f) = mins(c) + w(cf), s(e) + w(ef) = min5− 1, 6− 2 = 4.

Question 3.1.11 Find an acyclic ordering of the vertices of the digraph D in Figure3.4. Using the longest path algorithm for acyclic digraphs, compute the length of a longestpath from s to t in D. Justify your answer. [A solution is given in the end of thechapter.]

Page 46: Algorithms and Complexity II

46 CHAPTER 3. DIRECTED GRAPHS

1

−1−2

3

4

−2

3

2

4

−3

5

6

2

s tc d

a b

e f

Figure 3.4: An acyclic digraph D

3.1.3 Analyzing projects using PERT/CPM (extra material)

Often a large project consists of many activities some of which can be done in parallel,others can start only after certain activities have been accomplished. In such cases, thecritical path method (CPM) and Program Evaluation and Review Technique(PERT) are of interest. They allow one to predict when the project will be finished andmonitor the progress of the project. They allow one to identify certain activities whichshould be finished on time if the predicted completion time is to be achieved.

CPM and PERT were developed independently in the late 1950s. They have many fea-tures in common and several others that distinguish them. However, over the years the twomethods have practically merged into one combined approach often called PERT/CPM.Notice that PERT/CPM has been used in a large number of projects including a new plantconstruction, NASA space exploration, movie production and ship building. PERT/CPMhas many tools for project management, but we will restrict ourselves only to a brief intro-duction and refer the reader to various books on operations research for more informationon the method.

We will introduce PERT/CPM using an example. Suppose the tasks to completeconstruction of a house are as follows (in brackets we give their duration in days): Wiring(5), Plumbing (8), Walls & Ceilings (10), Floors (4), Exterior Decorating (3) and InteriorDecorating (12). We cannot start doing Floors before Wiring and Plumbing have beenaccomplished, we cannot do Walls & Ceilings before Wiring has been finished, we cannotdo Exterior Decorating before the task Walls & Ceilings has been completed, and wecannot do Interior Decorating before Walls & Ceilings and Floors have been finished.How much time do we need to accomplish the construction?

To solve the problem we first construct a digraph N , which is called an activity-on-node (AON) project network1. We associate the vertices of N with the starting and

1Original versions of PERT and CPM used another type of netwoks, activity-on-arc (AOA) projectnetwork, but AOA networks are significantly harder to construct and change than AON networks and it

Page 47: Algorithms and Complexity II

3.1. ACYCLIC DIGRAPHS 47

S

P l

Wi F l

WC

ID

ED

F

8

5

4

10

12

3

0

Figure 3.5: House construction network

finishing points of the projects (vertices S and F ) and with the activities described above,i.e., Wiring (Wi), Plumbing (Pl), Floors (Fl), Walls & Ceiling (WC), Interior Decoration(ID) and Exterior Decorating (ED). The network N is a vertex-weighted digraph, wherethe weights of S and F are 0 and the weight of any other vertex is the duration of thecorresponding activities. Observe that the duration of the house construction projectequals the maximum weight of an (S, F )-path.

As in the example above, in the general case, an AON network D is a vertex-weighteddigraph with the starting and finishing vertices S and F . Our initial aim is to find themaximum weight of an (S, F )-path in D. Since D is an acyclic digraph, this can be donein linear time using the algorithm described above after the vertex splitting procedure(replacing every vertex x by two vertices x′, x′′ and arc x′x′′; every arc xy is replaced byx′′y′). We can also use dynamic programming directly: for a vertex x of D let t(x) be theearlier time when the activity corresponding to x can be accomplished. Then t(S) = 0and for any other vertex x, we have t(x) = `(x) + maxt(y) : y ∈ N−(x), where N−(x)is the set of in-neighbours of x and `(x) is the duration of the activity associated with x.The ensure that we know the value of t(y) for each in-neighbour of y of x, we consider thevertices of D in an acyclic ordering.

In the example above, S, P l,Wi, F l,WC, ID,ED,F is an acyclic ordering. Thus, we

makes more sense to use AON networks rather than AOA ones

Page 48: Algorithms and Complexity II

48 CHAPTER 3. DIRECTED GRAPHS

have: t(S) = 0, t(Pl) = 8 + 0 = 8, t(Wi) = 5 + 0 = 5,

t(Fl) = l(Fl) + maxt(Pl), t(Wi) = 4 + 8 = 12,

t(WC) = 10 + 5 = 15,

t(ID) = l(ID) + maxt(Fl), t(WC) = 12 + 15 = 27,

t(ED) = 3 + 15 = 18, t(F ) = maxt(ID), t(ED) = 27. The following path is of weight27: S,Wi,WC, ID, F . Every maximum weight (S, F )-path is called critical and everyvertex (and the corresponding activity) belonging to a critical path is critical. Observethat to ensure that the project takes no longer than required, no critical activity should bedelayed. At the same time, delay with non-critical activities may not affect the durationof the project. For example, if we do Plumbing 13 days instead of 8 days, the projectwill be finished in 27 days anyway. This means that the project manager should monitormainly critical activities and may delay non-critical activities in order to enforce criticalones (e.g., by moving workforce from a non-critical activity to a critical one).

The manager may want to expedite the project (if, for example, earlier completionwill result in a considerable bonus) by spending more money on it. This issue can beinvestigated using linear programming (studied, e.g., in CS3490).

3.2 Distances in digraphs

The distance from a vertex x in a digraph D to a vertex y is the length of a shortestpath from x to y, if such a path exists and it equals∞, otherwise. The distance is denotedby dist(x, y). Algorithms for finding distances are of importance in many applications ofdigraphs.

3.2.1 Breadth First Search

Breadth First Search (BFS) is an algorithm for finding distances from a vertex s in adigraph D to all other vertices in D. BFS is based on the following simple idea. Startingat s, we visit each out-neighbour x of s. We set dist′(s, x) := 1 and s := pred(x) (sis the predecessor of x). Now we visit all vertices y not yet visited and which are out-neighbours of vertices x of distance 1 from s. We set dist′(s, y) := 2 and x := pred(y). Wecontinue in this fashion until we have reached all vertices which are reachable from s (thiswill happen after at most n − 1 iterations, where n is the number of vertices in D). Forthe rest of the vertices z (not reachable from s), we set dist′(s, z) := ∞. A more formaldescription of BFS is as follows. At the end of the algorithm, pred(v) = nil means thateither v = s or v is not reachable from s. The correctness of the algorithm is due to the

Page 49: Algorithms and Complexity II

3.2. DISTANCES IN DIGRAPHS 49

s

y

zxw

u v

Figure 3.6: A digraph D in which the bold arcs indicate arcs used by BFS to find distancesfrom s.

fact that dist(s, x) = dist′(s, x) for every x ∈ V . For a vertex x, N+(x) denotes the set ofout-neighbours of x.

BFS

Input: A digraph D = (V,A) and a vertex s ∈ V.

Output: dist′(s, v) and pred(v) for all v ∈ V.

1. for v ∈ V do dist′(s, v) :=∞; pred(v) := nil

2. dist′(s, s) := 0; a queue Q := s

3. while Q 6= ∅ do

delete a vertex u, the head of Q, from Q; for v ∈ N+(u) do if dist′(s, v) =∞ thendist′(s, v) := dist′(s, u) + 1; pred(v) := u; put v to the end of Q

The complexity of the above algorithm is O(n+m), where n = |V | andm = |A|. Indeed,Step 1 requires O(n) time. The time to perform Step 3 is O(m) as the out-neighbours ofevery vertex are considered only once and

∑x∈V d

+(x) = m, by the sum-of-semi-degreesproposition.

The algorithm is illustrated in Figure 3.6.

Question 3.2.1 (a) Using BFS, find distances from the vertex b in the digraph of Figure1.14.

(b) Using BFS, find distances from the vertex a in the digraph of Figure 1.14.

Question 3.2.2 Explain how we can use pred in BFS to find the actual shortest pathsfrom s.

Page 50: Algorithms and Complexity II

50 CHAPTER 3. DIRECTED GRAPHS

3.2.2 Dijkstra’s algorithm

The next algorithm, due to Dijkstra, finds the distances from a given vertex s in a weighteddigraph D = (V,A, c) to the rest of the vertices, provided that all the weights c(xy) ofarcs xy are non-negative.

In the course of the execution of Dijkstra’s algorithm, the vertex set of D is partitionedinto two sets, P and Q. Moreover, a parameter δv is assigned to every vertex v ∈ V .Initially all vertices are in Q. In the process of the algorithm, the vertices reachable froms move from Q to P . While a vertex v is in Q, the corresponding parameter δv is an upperbound on dist(s, v). Once v moves to P , we have δv = dist(s, v). A formal description ofDijkstra’s algorithm follows. In the description, N+(v) is the set of out-neighbours of v.

Dijkstra’s algorithm

Input: A weighted digraph D = (V,A, c), such that c(a) ≥ 0 for every a ∈ A, and avertex s ∈ V.

Output: The parameter δv for every v ∈ V such that δv = dist(s, v).

1. P := ∅; Q := V ; δs := 0; for v ∈ V − s do δv :=∞

2. while Q 6= ∅ do

find v ∈ Q such that δv = minδu : u ∈ Q

Q := Q− v; P := P ∪ v

for u ∈ Q ∩N+(v) do δu := minδu, δv + c(v, u)

Theorem 3.2.3 Dijkstra’s algorithm determines the distances from s to all other verticesin time O(n2 +m), where n = |V |, m = |A|.

Figure 3.7 illustrates Dijkstra’s algorithm.

Question 3.2.4 Execute Dijkstra’s algorithm on the digraph in Figure 3.8.

3.2.3 The Floyd-Warshall algorithm

Strong connectivity for digraphs is an equivalent of connectivity for undirected graphs. Adigraph D is strongly connected if for any pair x, y of vertices in D, there is a pathfrom x to y and a path from y to x.

Page 51: Algorithms and Complexity II

3.2. DISTANCES IN DIGRAPHS 51

9

3

2 6

17

2

2

s0

∞ ∞

∞1

9

3

2 6

17

2

2

s0

1

9

3

2 6

17

2

2

s0

1

9

3

2 6

17

2

2

s0

1

9

3

2 6

17

2

2

s0

1

9

3

2 6

17

2

2

s0

1

9

3

2 6

17

2

2

s0

1

9

5

43

5

4

6

11

5

4

6

11

5

4

6

7

5

4

6

7

(g)

(e) (f)

(d)(c)

(a) (b)

2 2

2 23

3

32 2

3

2

2

2

2

2

2

2

2

3

Figure 3.7: Execution of Dijkstra’s algorithm. The white vertices are in Q; the blackvertices are in P. The number above each vertex is the current value of the parameter δ.(a) The situation after performing the first step of the algorithm. (b)–(g) The situationafter each successive iteration of the loop in the second step of the algorithm. The boldarcs give a “shortest path tree.”

The Floyd-Warshall algorithm allows one to find all pairwise distances between thevertices of a strongly connected digraph D. We assume that we are given a strong weighteddigraph D = (V,A, c) such that some weights c(a) of arcs are negative, but D has no cycleof total negative weight. (If such a cycle exists, no distance is defined as the distancebetween some pair of vertices of the cycle is negative, which is impractical.)

In this subsection, it is convenient to assume that V = 1, 2, . . . , n. Denote by δmij thelength of a shortest (i, j)-path in the subgraph of D induced by 1, 2, . . . ,m− 1 ∪ i, j,

Page 52: Algorithms and Complexity II

52 CHAPTER 3. DIRECTED GRAPHS

s

1

5

3

12

7

1

1

1

1

2

1

4

2

10

7

4

Figure 3.8: A digraph with non-negative weights on the arcs.

for all 1 ≤ m ≤ n− 1. In particular, δ1ij is the length of the path ij, if it exists. Observethat a shortest (i, j)-path in the subgraph of D induced by 1, 2, . . . ,m ∪ i, j eitherdoes not include the vertex m, in which case δm+1

ij = δmij , or does include it, in which case

δm+1ij = δmim + δmmj . Therefore,

δm+1ij = minδmij , δmim + δmmj. (3.2)

Observe that δmii = 0 for all i = 1, 2, . . . , n, and, furthermore, for all pairs i, j such thati 6= j, δ1ij = c(i, j) if ij ∈ A and δ1ij = ∞, otherwise. Formula (3.2) is also correct whenthere is no (i, j)-path in the subgraph of D induced by 1, 2, . . . ,m ∪ i, j. Clearly,δn+1ij is the length of a shortest (i, j)-path (in D). It is also easy to verify that O(|V |3)

operations are required to compute δn+1ij for all pairs i, j.

The above assertions can readily be implemented as a formal algorithm, but we leaveit as an exercise.

Question 3.2.5 Give a pseudo-code of the Floyd-Warshall algorithm.

Theorem 3.2.6 A weighted digraph D has a negative cycle if and only if δmii < 0 for somem, i ∈ 1, 2, . . . , n.

Question 3.2.7 Let D be a weighted digraph and let s be a fixed vertex in D. Whichalgorithms can be used for finding distances from s to the rest of the vertices if

(a) D is acyclic and s is of in-degree zero ?

(b) D is strongly connected and all weights are non-negative ?

(c) D is strongly connected, some weights are negative, but there is no negative cycle ?

Page 53: Algorithms and Complexity II

3.3. STRONG CONNECTIVITY IN DIGRAPHS 53

Q

a

b c

e

f a

b c

d e

f

d H

R

x

y

xy abd cfe

Figure 3.9: Digraphs

3.3 Strong connectivity in digraphs

Recall that a digraph D is strongly connected if for any pair x, y of vertices in D, thereis a path from x to y and a path from y to x. Strong connectivity has many applications,see, e.g., [BG2000].

3.3.1 Basics of strong connectivity

Proposition 3.3.1 A digraph D is strongly connected if and only if it has a closed walkcontaining all vertices of D.

Proof: If D has a closed walk W , a part of W from x to y gives a walk from x to y foreach x 6= y ∈ V (D). Thus, D is strongly connected.

If D is strongly connected, list it vertices v1, v2, . . . vn and merge a (v1, v2)-path with a(v2, v3)-path with a (v3, v4)-path . . . with a (vn, v1)-path. We have obtained a closed walkcontaining all vertices of D. QED

In Figure 3.9 H is strongly connected, while R is not. To see that H is stronglyconnected it is enough to observe, by Proposition 3.3.1, thatH has a closed walk containingall vertices. Indeed, abcefcedca is such a walk. To see that R is not strongly connected,it is enough to observe that there is no path from a to x as all arcs between vertices x, yand the rest of the digraph are directed from x, y.

A maximum strongly connected induced subgraph of a digraph D is called a strongcomponent of D.

Proposition 3.3.2 The vertices of a digraph D can be partitioned into several subsetseach of which induces a strong component of D.

The essence of this proposition is that any digraph can be partitioned in strong com-ponents and arcs between them. If we contract strong components to single vertices and

Page 54: Algorithms and Complexity II

54 CHAPTER 3. DIRECTED GRAPHS

Qa

b c

e

f a

b c

d e

f

d H

R

x

y

d abcfe

Figure 3.10: Digraphs

get rid of parallel arcs, we get an acyclic digraph called the strong component digraphof D. For example, digraph R in Figure 3.9 has strong components induced by verticesx, y, a, b, d, c, f, e, respectively and Q is the strong component digraph of R.

3.3.2 Algorithms for finding strong components

To find strong components of a digraph D the following algorithm called the cycle-contraction algorithm can be used. Find a cycle in D and contract it to a singlevertex naming this vertex by the set of vertices of this cycle. We proceed in this wayuntil we get an acyclic digraph H. The “names” of its vertices are vertex sets of strongcomponents of D and H itself is the strong component digraph of D. To have a full

Question 3.3.3 Find the strong components and strong component digraph of digraph Hof Figure 3.10.

Solution: Contract cycle abca and then cycle cefc. We get digraph Q in Figure 3.10,which is the strong component digraph of H. The strong components of H are the sub-graph of H induced by a, b, c, f, e and vertex d.

Question 3.3.4 Find the strong components and strong component digraph of digraph Rof Figure 3.10.

To have a full description of the cycle-contraction algorithm one needs to specify howto find a cycle. This can be done, but the algorithm will not be very efficient. Instead,we’ll define a much faster algorithm based on ... the algorithm DFS-A for acyclic orderingsin acyclic digraphs.

SCA(D)Input: A digraph D = (V,A).Output: The vertex sets of strong components of D.

Page 55: Algorithms and Complexity II

3.4. APPLICATION: SOLVING THE 2-SATISFIABILITY PROBLEM (EXTRAMATERIAL)55

6

7

8

1

2

5

4

3

6

7

8

1

2 3

4

5

(b)(a)

Figure 3.11: (a) A digraph D; the order of vertices found by DFSA is shown. (b) Theconverse D′ of D; the bold arcs are the arcs of a DFS forest for D′.

1. Call DFS-A(D) to compute the “acyclic” ordering v1, v2, . . . , vn.

2. Compute the converse D′ of D.

3. Call DFS-CC(D′) in Section 2.2, but in the main loop of DFS-CC consider the verticesaccording to the ordering v1, v2, . . . , vn and use N+(v) rather than N(v).

Figure 3.11 illustrates the strong component algorithm (SCA). The complexity of SCAis O(|V |+ |A|).

Question 3.3.5 Using SCA find the strong components of a digraph D = (V,A) withV = a, b, c, d, e and A = ab, ba, ac, cd, dc, de.

Solution: When we start from a, DFS-A finds the following ”acyclic” ordering: a, c, d, e, b.The converse D′ of D has the same vertices as D and arc set ab, ba, ca, cd, dc, ed. Finally,DFS-CC applied to D′ with vertex ordering a, c, d, e, b finds three strong components ofD. They have vertices a, b, c, d and e.

Question 3.3.6 Using SCA find the strong components and strong component digraphsof digraphs H and R of Figure 3.10.

3.4 Application: Solving the 2-Satisfiability Problem (extramaterial)

In this section we deal with a problem that is not a problem on graphs, but it has appli-cations to several problems on graphs, in particular when we want to decide whether agiven undirected graph has an orientation with certain properties. We will show how tosolve this problem efficiently using the algorithm for strong components of digraphs fromthe previous section.

Page 56: Algorithms and Complexity II

56 CHAPTER 3. DIRECTED GRAPHS

A boolean variable x is a variable that can assume only two values 0 and 1. Thesum of boolean variables x1 + x2 + . . . + xk is defined to be 1 if at least one of the xi’s is1 and 0 otherwise. The negation x of a boolean variable x is the variable that assumesthe value 1− x. Hence x = x. Let X be a set of boolean variables. For every x ∈ X thereare two literals, over x, namely x itself and x. A clause C over a set of boolean variablesX is a sum of literals over the variables from X. The size of a clause is the number ofliterals it contains. For example, if u, v, w are boolean variables with values u = 0, v = 0and w = 1, then C = (u+ v + w) is a clause of size 3, its value is 1 and the literals in Care u, v and w. An assignment of values to the set of variables X of a boolean expressionis called a truth assignment. If the variables are x1, . . . , xk, then we denote a truthassignment by t = (t1, . . . , tk). Here it is understood that xi will be assigned the value tifor i = 1, . . . , k.

The 2-satisfiability problem, also called 2-SAT, is the following problem. LetX = x1, . . . , xk be a set of boolean variables and let C1, . . . , Cr be a collection ofclauses, all of size 2, for which every literal is over X. Decide if there exists a truthassignment t = (t1, . . . , tk) to the variables in X such that the value of every clause willbe 1. This is equivalent to asking whether or not the boolean expression F = C1 ∗ . . . ∗Cpcan take the value 1. Depending on whether this is possible or not, we say that Fis satisfiable or unsatisfiable. Here ‘∗’ stands for boolean multiplication, that is,1 ∗ 1 = 1, 1 ∗ 0 = 0 ∗ 1 = 0 ∗ 0 = 0. For a given truth assignment t = (t1, . . . , tk) and literalq we denote by q(t) the value of q when we use the truth assignment t (i.e. if q = x3 andt3 = 1, then q(t) = 1− 1 = 0)

To illustrate the definitions, let X = x1, x2, x3 and let C1 = (x1+x3), C2 = (x2+x3),C3 = (x1+x3) and C4 = (x2+x3). Then it is not difficult to check that F = C1∗C2∗C3∗C4

is satisfiable and that taking x1 = 0, x2 = 1, x3 = 1 we obtain F = 1.

If we allow more than 2 literals per clause then we obtain the more general problemSatisfiability (also called SAT), which is much harder to solve than 2-SAT. Below we willshow how to reduce 2-SAT to the problem of finding the strong components in a certaindigraph. We shall also show how to find a satisfying truth assignment if one exists.

Let C1, . . . , Cr be clauses of size 2 such that the literals are taken among the variablesx1, . . . , xk and their negations and let F = C1∗· · · ∗Cr be an instance of 2-SAT. Constructa digraph DF as follows: Let V (DF ) = x1, . . . , xk, x1, . . . , xk (i.e. DF has two verticesfor each variable, one for the variable and one for its negation). For every choice ofp, q ∈ V (DF ) such that some Ci has the form Ci = (p+ q), A(DF ) contains an arc from pto q and an arc from q to p (recall that x = x). See Figure 3.12 for examples of a 2-SATexpressions and the corresponding digraphs. The first expression is satisfiable, the secondis not.

Lemma 3.4.1 If DF has a (p, q)-path, then it also has a (q, p)-path. In particular, if p, qbelong to the same strong component in DF , then p, q belong to the same strong component

Page 57: Algorithms and Complexity II

3.4. APPLICATION: SOLVING THE 2-SATISFIABILITY PROBLEM (EXTRAMATERIAL)57

x2

x1

(a)

x3

x2

x1

x3

x2

x1

x3

x2

x1

x3

(b)

Figure 3.12: The digraph DF is shown for two instances of 2-SAT. In (a) F = (x1 + x3) ∗(x2 + x3) ∗ (x1 + x3) ∗ (x2 + x3) and in (b) F = (x1 + x2) ∗ (x1 + x2) ∗ (x2 + x3) ∗ (x2 + x3)

in DF .

Lemma 3.4.2 If DF contains a path from p to q, then, for every satisfying truth assign-ment t, p(t) = 1 implies q(t) = 1.

The following is an easy corollary of Lemma 3.4.1 and Lemma 3.4.2.

Corollary 3.4.3 If t is a satisfying truth assignment, then for every strong component D′

of DF and every choice of distinct vertices p, q ∈ V (D′) we have p(t) = q(t). Furthermorewe also have p(t) = q(t).

Lemma 3.4.4 F is satisfiable if and only if for every i = 1, 2, . . . , k, no strong componentof DF contains both the variable xi and its negation xi.

The lemmas above imply the following algorithm for solving 2-SAT.

2-SAT SolverInput: An instance F = C1 ∗ C2 ∗ · · · ∗ Cp of 2-SAT; the set R of all variables of F andtheir negations.Output: F is satisfiable or not. If F satisfiable, a truth assignment satisfying F .

1. Construct DF = (V,A) as follows: V := R; A := ∅; for i from 1 to p do A :=A ∪ (x, y), (y, x), where x, y are the literals in Ci.

Page 58: Algorithms and Complexity II

58 CHAPTER 3. DIRECTED GRAPHS

2. Find strong components D1, D2, . . . , Ds of D (the components are in acyclic order, i.e.,there is no arc from Dj to Di if i < j).

3. sat := 1; for i from 1 to p do if Di contains a variable and its negation then sat := 0;if sat = 1 then ’R is satisfiable’ else ’R is unsatisfiable’

4. if sat = 1 do

for each literal r ∈ R do r := 2; for i from p downto 1 do

for each literal r in Di do if r=2 then r := 1; r := 0

Proposition 3.4.5 Let p be the number of clauses and q be the number of variables of2-SAT. We can solve 2-SAT in time O(p+ q).

Proof: It is enough to prove that 2-SAT Solver can run in time O(p+ q). Since R has 2qliterals and, in the end of Step 1, A contains 2p edges, we need O(p+ q) time to constructDF = (V,A) in Step 1. Notice that DF has 2q vertices and 2p arcs. Thus, using SCA wecan perform Step 2 in time O(p+ q). It is easy to see the remaining two steps also run intime O(p+ q). QED

Question 3.4.6 Using 2-SAT Solver, determine which instance of 2-SAT in Figure 3.12is satisfiable and find its satisfying truth assignment.

Question 3.4.7 Using 2-SAT Solver, determine whether the following instance of 2-SATis satisfiable and, if it is, find its satisfying truth assignment: (x+y)∗(x+y)∗(y+z)∗(x+z).

3.5 Solutions

Question 3.1.8

There two alternatives: (a) replace max by min and use the same formula; (b) replacethe sigh of all weights of the digraph and use the longest path algorithm for the newweighted digraph.

Question 3.1.11

One acyclic ordering is s, a, b, c, d, e, f, t (there are others).

Page 59: Algorithms and Complexity II

3.5. SOLUTIONS 59

Let l(x) be the length of a longest path from s to a vertex x. Determine l(x) for allvertices in the order of the acyclic ordering.

l(s) = 0.

l(a) = l(s) + w(s, a) = 3.

l(b) = l(a) + w(ab) = 3 + 1 = 4.

l(c) = maxl(s) + w(sc), l(a) + w(ac) = max−2, 1 = 1.

l(d) = maxl(b) + w(bd), l(c) + l(cd) = max0, 6 = 6.

l(e) = 4. l(f) = 10.

l(t) = maxl(b) + w(bt), l(d) + w(dt), l(f) + w(fd) = 15.

The length of a longest path from s to t is 15.

Page 60: Algorithms and Complexity II

60 CHAPTER 3. DIRECTED GRAPHS

Page 61: Algorithms and Complexity II

Chapter 4

Colourings of Graphs,Independent Sets and Cliques

4.1 Basic definitions of vertex colourings

A vertex colouring of a graph G = (V,E) is an assignment that assigns a colour to everyvertex of G. A proper vertex colouring assigns different colours to end-vertices of alledges. For a positive integer k, a vertex k-colouring is a colouring that uses exactly kdifferent colours.

For example, three vertex colourings of a graph are depicted in Figure 4.1. The firstcolouring (the left hand side one) is not a proper vertex colouring since there we have twoedges with end-vertices of the same colour. The second and third colourings are properand they are a vertex 4-colouring and vertex 3-colouring.

A graph is said to be vertex k-colourable if it has a proper vertex k-colouring. So,the graph in Figure 4.1 is vertex 4- and 3-colourable. The chromatic number of a graphG, χ(G) = 3, is the minimum k such that G is vertex k-colourable.

To see that the chromatic number of the graph H in Figure 4.1 is 3, it is enoughto observe that H is vertex 3-colourable and H has no proper vertex 2-colouring as H

3

1

2 13

1

2

1 3 2

3 2 1

4 32

1

2

Figure 4.1: Three vertex colourings of a graph

61

Page 62: Algorithms and Complexity II

62CHAPTER 4. COLOURINGS OF GRAPHS, INDEPENDENT SETS AND CLIQUES

Figure 4.2: Bipartite graphs

contains (as a subgraph) K3, the complete graph on 3 vertices and χ(K3) = 3. Recall thata complete graph Kn on n vertices have vertices adjacent to each other. The vertices ofa complete bipartite graph Kn,m can be partitioned into two sets, partite sets V1 and V2,such that every edge has one end-vertex in V1 and the other in V2 and every vertex of V1is adjacent to each vertex of V2.

It is clear that χ(Kn) = n. Also, for complete χ(Kn,m) = 2 since every vertex fromone partite set can get colour 1 and every vertex from the other partite set can get colour2 and we obtain a proper vertex 2-colouring.

4.2 Bipartite graphs and digraphs

A graph G is bipartite if its vertices can be partitioned into two sets, partite sets V1 andV2, such that every edge has one end-vertex in V1 and the other in V2. Clearly, a completebipartite graph is a bipartite graph.

Let G be a bipartite graph with partite sets V1, V2. If we assign colour 1 to everyvertex in V1 and colour 2 to every vertex in V2, we will get a proper vertex 2-colouringof G. Thus, χ(G) ≤ 2. (If G has no edges, then χ(G) = 1, and if G has an edge, thenχ(G) = 2.) Moreover, it is easy to see that if χ(G) ≤ 2, then G is bipartite. Thus, theclass of bipartite graphs coincides with the class of graphs of chromatic number at most2.

The following is a characterization of bipartite graphs in terms of cycle lengths.

Theorem 4.2.1 A graph is bipartite if and only if it has no cycle of odd length.

Thus, the graph in Figure 4.1 is not bipartite (it has cycles of length 3). On the otherhand, graphs in Figure 4.2 are all bipartite. Every tree is bipartite as a tree has no cycles(in particular, cycles of odd lengths !).

Question 4.2.2 What is the minimum number of edges that we need to delete from thegraph H of Figure 4.1 to make it bipartite?

Page 63: Algorithms and Complexity II

4.2. BIPARTITE GRAPHS AND DIGRAPHS 63

(c)

(a)

(b)

Figure 4.3: Non-bipartite graphs

UG(D)D

Figure 4.4: A digraph D and its underlying graph

Solution: The graph H is not bipartite as it has cycles of odd lengths. If we delete thetwo ’diagonal’ edges, then we get the left hand side graph of Figure 4.2, i.e., a bipartitegraph. So, if we delete the two edges from H, it will become bipartite. On the otherhand, deletion of only one edge in H will leave H with at least one cycle of length 3 (oddlength). Thus, 2 is the required minimum number.

Question 4.2.3 What is the minimum number of edges that we need to delete from eachof the graphs in of Figure 4.3 to make it bipartite?

The underlying graph of a digraph D (denoted by UG(D)) is obtained from D byreplacing arcs by (undirected) edges and eliminating parallel edges. See Figure 4.4.

A digraph is called bipartite if its underlying graph is bipartite. One may think thatthe following claim is true: a digraph is bipartite if and only if it has no (directed) cyclesof odd length. However, this claim is wrong. Indeed, a digraph L with vertices v1, v2, v3and arcs v1v2, v2v3, v1v3 has no cycles (i.e., acyclic), but UG(L) is K3 and thus is notbipartite. However, the claim is true for strongly connected digraphs.

Theorem 4.2.4 A strongly connected digraph D is bipartite if and only if D has no cycleof odd length.

Page 64: Algorithms and Complexity II

64CHAPTER 4. COLOURINGS OF GRAPHS, INDEPENDENT SETS AND CLIQUES

4.3 Periods of digraphs and Markov chains (extra material)

Theorem 4.2.4 has a generalizations, which is of importance to applications. To state thegeneralization, we need some additional definitions. The greatest common divisor ofa set S of positive integers is the largest positive integer k that divides every integer fromS. Foe example, the greatest common divisor of 6, 4, 8, 10 is 2 and the greatest commondivisor of 6, 9 is 3.

The period, p(D), of a strongly connected digraph D is the greatest common divisorof the lengths of cycles in D. Since D is strongly connected, it has a cycle, so p(D) isdefined. If D is bipartite, then p(D) ≥ 2.

Theorem 4.3.1 If a strongly connected digraph D = (V,A) has period p ≥ 2, then V canbe partitioned into sets V1, V2, . . . , Vp such that every arc with the initial end-vertex in Vihas the terminal end-vertex in Vi+1 for every i = 1, 2, . . . , p, where Vp+1 = V1.

Consider an algorithm, by Balcer and Veinott, to compute p(D) for a strongly con-nected digraph D. If, for a vertex x of d+(x) ≥ 2, we contract all out-neighbours of x anddelete any parallel arcs obtained, then the resulting digraph has the same period as theoriginal digraph by Theorem 4.3.1. Repeating this iteration, we will finally obtain a cycleC. The length of C is the desired period. For example, see Figure 4.5.

Question 4.3.2 Find p(H) and p(L) for the following two digraphs using the Balcer-Veinott algorithm. The digraph H is given by V (H) = a, b, c, d, A(H) = ab, bc, cd, da, adand the digraph L is given by V (L) = a, b, c, d, e, f, A(L) = ab, bc, cd, da, be, ef, fb.

Theorem 4.3.1 and the above algorithm are of interest due to applications. Let usconsider one of them. It is on so-called Markov chains, but we will speak on some similarprocess. Suppose we have n water containers S1, S2, . . . , Sn. A container Si has pi fractionof water in it, pi ≤ 1. At every stage, some fraction pij of water in container Si is put intocontainer Sj (for all i, j). We are interested to know how the water will be distributedafter a very large number of stages. To find out that one constructs a digraph D withvertices S1, S2, . . . , Sn and arcs SiSj for all i, j such that pij > 0. If p(D) = 1, then aftera sufficiently large number of stages, the water distribution will be almost stationary, i.e.,every container will have a certain fraction of water all the time (with possibly very smallvariations). If p(D) ≥ 2, then the water will be moved cyclically (after a large number ofstages) and the cycle will have length p(D).

Page 65: Algorithms and Complexity II

4.4. COMPUTING CHROMATIC NUMBER 65

ad

be

cf

a b

c

d

e

f

g

h

b

c

e

f

g

had

c

f

g

had

be

g

h h

cf

adg

be

cf

adg

beh

Figure 4.5: Illustrating the Balcer-Veinott algorithm

4.4 Computing chromatic number

We already know that χ(Kn) = n and χ(Kn,m) = 2. If n is even, then cycle Cn is abipartite graph. So, χ(Cn) = 2. If n is odd, Cn is not bipartite. Hence, χ(Cn) ≥ 3.However, we can colour all vertices of Cn but one with colours 1 and 2 without creating anedges with end-vertices of the same colour (colour vertices along the cycle 1,2,1,2, etc.).Assign colour 3 to the remaining vertex. Clearly, we get a proper vertex 3-colouring ofCn. Hence, χ(Cn) = 3 when n is odd.

Let us consider another class of graph called wheels. Wheel Wn consists of cycle Cn−1and an extra vertex that is adjacent with every vertex in the cycle. See Figure 4.6.

W5W7 W4

Figure 4.6: Wheels

Page 66: Algorithms and Complexity II

66CHAPTER 4. COLOURINGS OF GRAPHS, INDEPENDENT SETS AND CLIQUES

Since the extra vertex requires the extra colour, we have χ(Wn) = 3 if n is odd andχ(Wn) = 4 if n is even.

G

a

b

cd

ef

g

H

x

y

z u

v

wR

a

b

c

de

f

Figure 4.7: Undirected graphs

Question 4.4.1 Find the chromatic numbers of the graphs in Figure 4.7. Justify youranswers.

Solution: χ(G) = 4 since χ(G′) = 4, where G′ is the subgraph of G induced by a, b, c, d.A 4-coloring of G′ can be extended to a proper 4-colouring of G.

χ(H) = 3 since c(w) = c(y) = 1, c(v) = c(u) = 2, c(x) = c(z) = 3 is a proper3-colouring of H and H contains K3.

χ(R) = 4 since χ(R− f) = 3 (odd cycle) and f must have the fourth colour.

Question 4.4.2 A lecture timetable is to be drawn up. Since some students wish to attendseveral lectures, certain lectures must not coincide. The asterisks in the following tableshow which pairs of lectures cannot coincide. How many periods are needed to timetableall five lectures?

lec. a b c d e

a - * - * *

b * - * - *

c - * - * *

d * - * - *

e * * * * -

Solution: We represent this situation by a graph G with vertices a, b, c, d, e. Two verticesof G are adjacent if the corresponding lectures cannot coincide. We see that G = W5.We know that χ(W5) = 3. Thus, the vertices of G can be partitioned into sets V1, V2, V3such that no edge has end-vertices in the same Vi. That means lectures in each of Vi cancoincide. Thus, we need 3 periods of time to schedule all five lectures.

Page 67: Algorithms and Complexity II

4.5. GREEDY COLOURING AND INTERVAL GRAPHS 67

4.5 Greedy colouring and interval graphs

For a graph G with vertices ordered (in some order) v1, v2, . . . , vn, the greedy colouringprocedure assigns colours 1, 2, . . . to the vertices in such a way that the vertices areconsidered in the above order and vi receives the smallest colour distinct from its alreadycoloured neighbours.

We illustrate the procedure using the wheel W6 with V (W6) = v1, . . . , v6 andE(W6) = v1v2, v2v5, v5v6, v6v4, v4v1 ∪ v3vi : i 6= 3. We colour vertices in the fol-lowing order: v1, v2, . . . , v6. We have color(v1) = 1. color(v2) = 2 since v2 has v1 as aneighbour. color(v3) = 3 since v1, v2 are neighbours of v3. color(v4) = 2 since v1 is aneighbour of v4, but v2 is not. color(v5) = 1 as v1 is not a neighbour of v5. color(v6) = 4as it has neighbours of colours 1,2 and 3. Thus, the colouring uses 4 colours. [4 marks]We know that χ(W6) = 4 (recall that χ(C5) = 3 and the central vertex requires an extracolour). Thus, the obtained colouring is optimal.

However, very often the greedy procedure does not produce an optimal colouring.For example, consider C6 with vertices 1, 2, . . . 6 and edges 12, 23, 34, 45, 56, 61. Let v1 =1, v2 = 4, v3 = 2, v4 = 3, v5 = 5, v6 = 6. Then the greedy procedure gives colour 1 to bothvertices 1 and 4, colour 2 to vertices 2 and 5 and colour 3 to vertices 3 and 6. However,χ(C6) = 2. Thus, the colouring obtained by the greedy procedure is not optimal.

Theorem 4.5.1 Let ∆(G) be the maximum degree of a vertex in a connected graph G. IfG is not regular (i.e., not every degree is ∆(G)), then χ(G) ≤ ∆(G). If G is regular, thenχ(G) ≤ ∆(G) + 1.

Proof: Let G be non-regular and let x be a vertex of G with degree d(x) < ∆(G). UseBFS to find distances from x to the rest of the vertices. Let l be the longest distance fromx to a vertex in G. Order vertices of G v1, . . . , vn such that first we list vertices distance lfrom x, than distance l − 1 from x, etc.

Suppose we have coloured all vertices v1, . . . , vi−1 with colours 1, 2, . . . ,∆(G) using thegreedy algorithm and we want to colour vi, i < n. Since dist(x, vi) > 0, among neighboursof vi at most d(vi) − 1 ≤ ∆(G) − 1 have been coloured (since there is a vertex vj on ashortest path from x to vi and, clearly, j > i). Thus, the greedy algorithm can choosea colour for vi among 1, 2, . . . ,∆(G) that is not used for any neighbour of vi in the setv1, . . . , vi−1 of already coloured vertices.

It remains to consider the case of v1. Since d(x) < ∆(G), the greedy algorithm canfind a colour for v1 among 1, 2, . . . ,∆(G) that is not used for any neighbour of v1. QED

In fact, a slightly stronger theorem holds:

Theorem 4.5.2 (Brook’s Theorem) Let ∆(G) be the maximum degree of a vertex ina connected graph G. If G is not complete and G is not an odd cycle, then χ(G) ≤ ∆(G).

Page 68: Algorithms and Complexity II

68CHAPTER 4. COLOURINGS OF GRAPHS, INDEPENDENT SETS AND CLIQUES

ea c

b d e

a b

c

d

Figure 4.8: Intervals and interval graph

Consider the following register problem. To speed up arithmetic computations com-putations the values of variables are stored in fast registers. The number of registers isrestricted, so we want to use them economically. For every variable we have a ”time”interval when the variable is used (between the first and last uses). If two variables havenon-intersecting time intervals, we can allocate the same register to the two variables.

To find the minimum number of registers needed, we construct a graphG whose verticescorrespond to the variables and two vertices are adjacent if the corresponding two timeintervals intersect. The chromatic number of G equals the minimum number of registerssince variables corresponding to the vertices of the same colour have non-intersectingintervals.

Similar graphs, called interval graphs, appear in other applications. A graph G isinterval if there is a set of intervals corresponding to the vertices of G such that twovertices of G are adjacent if and only if the corresponding intervals intersect. See Figure4.8. Not every graph is interval.

Question 4.5.3 Prove that K2,2 is not an interval graph.

Let K2,2 has vertices x1, x2, y1, y2 and edges xiyj , where 1 ≤ i, j ≤ 2. Assume that K2,2 isinterval, i.e., there are four intervals I1, I2, J1, J2 corresponding x1, x2, y1, y2 respectively.This means that every Ik intersects with every Js, but I1 and I2 do not intersect, nor doJ1 and J2. It is easy to check that this is not possible.

Question 4.5.4 Prove that every Kn is an interval graph.

Question 4.5.5 Construct the interval graph H for the following family of intervals:

[1, 3], [2, 4], [1, 5], [3.5, 6], [3.5, 7].

For a graph G, let ω(G) denotes the number of vertices in a largest complete subgraphof G. For example, ω(Kn) = n, ω(Kn,m) = 2. Clearly, χ(G) ≥ ω(G) since the vertices of acomplete subgraph of G require different colours in any proper vertex colouring.

Page 69: Algorithms and Complexity II

4.6. EDGE COLOURINGS (EXTRA MATERIAL) 69

Theorem 4.5.6 Let G be an interval graph. Then χ(G) = ω(G). Moreover, a propervertex ω(G)-colouring can be obtained by the greedy colouring procedure provided the ver-tices of G are ordered v1, v2, . . . , vn in the increasing order of the left end-points of theirintervals.

Question 4.5.7 Prove that the chromatic number of the graph H constructed in Question4.5.5 equals 4 without producing the actual vertex colouring. Find a proper vertex 4-colouring of H.

4.6 Edge colourings (extra material)

Sometimes, we need to colour edges rather than vertices in a graph G. Such a colouringis called a proper edge colouring if no two edges of G with the same end-vertex areassigned the same colour. The minimum number of colours in a proper edge colouring ofG is its chromatic index denoted by χ′(G). If ∆(G) is the maximum degree of a vertexin G, then it is easy to see that χ′(G) ≥ ∆(G).

Question 4.6.1 Consider Kn,n with vertices x1, x2, . . . , xn, y1, y2, . . . , yn and edges xiyj(1 ≤ i, j ≤ n). Prove that χ′(Kn,n) = n. Hint: See Figure 4.9.

Thus, χ′(Kn,n) = ∆(Kn,n). It is easy to see that χ′(K3) = 3 = ∆(K3)+1. The followingtheorem is the main result on the chromatic index.

Theorem 4.6.2 (Vising) If ∆(G) is the maximum degree of a vertex in G, then ∆(G) ≤χ′(G) ≤ ∆(G) + 1.

4.7 Independent Sets and Cliques

Given a graph G = (V,E) a set X ⊆ V is an independent set X of G if no edge containstwo vertices from X. In other words X ⊆ V and for any two vertices in X, there is noedge connecting them. For example, a, c and b, d are independent sets in the graphG of Fig. 4.10. The set a, d, e is an independent set in graph H of Fig. 4.10.

Suppose that you are organising a cruise, where none of the guests are allowed to knoweach other. You have a number of applicants, who want to go on your cruise, and youknow exactly which of the applicants know each other. Finally you want as many peopleon your cruise as possible. How do you decide who to invite on the cruise?

Page 70: Algorithms and Complexity II

70CHAPTER 4. COLOURINGS OF GRAPHS, INDEPENDENT SETS AND CLIQUES

1

1

23

2 3

1

3

2

Figure 4.9: Optimal K3,3 edge colouring

Hopefully after a little thought, you realise that this is not an easy problem if you haveseveral thousand applicants, and many of them know each other. In fact the problem isequivalent to finding a maximum independent set in a graph, i.e., an independent setwith maximum number of vertices. If you build a graph where each node correspondsto an applicant, and you add an edge between two nodes, if they know each other. Nowany independent set corresponds to a set of applicants, where nobody knows each other.So if we could find a maximum independent set in a graph, then we also solve our cruiseproblem.

1

F

ab

cd

e

f

HG

a b

c d

e f

a b

cd

e

f

g

Figure 4.10: Graphs

Question 4.7.1 Find the cardinality of a maximum independent set in the graph H of

Page 71: Algorithms and Complexity II

4.7. INDEPENDENT SETS AND CLIQUES 71

Fig. 4.10.

Solution: Observe that S = a, d, e is an independent set in H. To show that S is amaximum independent set in H, we will prove that H contains no independent set with4 vertices. Suppose H does contain an independent set I with 4 vertices. Notice that Icannot contain c as it has three neighbours, all of which we cannot eliminate by deletingonly two vertices from V (H) (to obtain I). Thus, c 6∈ I. Observe that H− c is a path andno vertex of this path can be deleted to eliminate all edges.

Question 4.7.2 Find the cardinality of a maximum independent set in the graphs G andF of Fig. 4.10.

The Cartesian product of graphs G and H, written G×H, is the graph with vertexset V (G)× V (H) = (u, v) : u ∈ V (G), v ∈ V (H) in which (u, v) is adjacent to (u′, v′)if and only if (1) u = u′ and vv′ ∈ E(H) or (2) v = v′ and uu′ ∈ E(G).

Question 4.7.3 Show that K2 ×K2 is isomorphic to C4.

Solution: Let the first K2 has vertices 1, 2 and edge 12 and the second K2 has vertices1′, 2′ and edge 1′2′. Then V (K2 ×K2) = (1, 1′), (1, 2′), (2, 1′), (2, 2′). By the definitionwe have the following edges: (1, 1′)(1, 2′), (1, 2′)(2, 2′), (2, 2′)(2, 1′), (2, 1′)(1, 1′). No otheredges are there, so K2 ×K2 is a cycle with 4 vertices.

Question 4.7.4 Show that K2 ×K3 is isomorphic to the graph H of Fig. 4.10.

The cartesian product provides a link between the independent sets and chromaticnumbers as follows:

Theorem 4.7.5 A graph G with n vertices has a vertex m-colouring if and only if G×Km

has an independent set with n vertices.

A set X of vertices of a graph G is a clique if any two distinct vertices of G are linkedby an edge of G. Observe that X is a clique in G if and only if X is an independent set inthe compliment of G, G.

Question 4.7.6 Find the cardinality of a maximum clique in each graph of Fig. 4.10.

Let ω(G) be the number of vertices in a largest clique of graph G. Observe thatω(G) ≤ χ(G) for each graph G (the vertices of clique require different colours in anyproper vertex colouring of G). The graphs in which for every induced subgraph H wehave ω(H) = χ(H) are called perfect. These graphs are very important; they includeinterval graphs and bipartite graphs.

Page 72: Algorithms and Complexity II

72CHAPTER 4. COLOURINGS OF GRAPHS, INDEPENDENT SETS AND CLIQUES

Question 4.7.7 Prove that a bipartite graph is perfect.

Page 73: Algorithms and Complexity II

Chapter 5

Matchings in graphs

5.1 Matchings in (general) graphs

In a graph G, a matching is a collection of edges with no common end-vertices. Edgesaf, be, cd form a matching in graph R in Figure 5.1; af, bf, cd is not a matching as edgesaf and bf have a common vertex, f. All matchings of graph H in Figure 5.1 consist ofonly one edge.

A matching M in a graph G is called maximum if M contains the maximum possiblenumber of edges in a matching of G. In a graph G with n vertices (n is even), a matchingis called perfect if it contains n/2 edges. For example, af, be, cd is a perfect matchingin graph R of Figure 5.1. Clearly, H in Figure 5.1 has no perfect matching. We will seebelow that Q has no perfect matching either.

It is natural to ask when a graph has a perfect matching. Tutte was the first to answer

R

a

b

c

d

e

fH

a b

cd

e

Q

x

y

Figure 5.1: Matchings in graphs: R has a perfect matching, H has only matchings of size1, Q has no perfect matching

73

Page 74: Algorithms and Complexity II

74 CHAPTER 5. MATCHINGS IN GRAPHS

this question in 1947. Let S be a set of vertices in a graph G. If we delete S from G weget the graph G− S with connectivity components; some of these components have evennumber of vertices, the others odd number of vertices. Let o(G − S) be the number ofcomponents with odd number of vertices. Suppose that G has a perfect matching M . Forevery connected components R of G−S, M may have only (|V (R)|− 1)/2 edges within Rand thus at least one vertex of R must have a ’matching’ (in M) vertex outside of R, whichcan only belong to S (all edges going outside of R have an end-vertex in S). Since thesevertices of S must be distinct, o(G−S) ≤ |S|. This inequality is a necessary condition fora graph G = (V,E) to have a perfect matching (for every S ⊆ V ). Tutte proved that thiscondition is also sufficient.

Theorem 5.1.1 (Tutte) A graph G = (V,E) has a perfect matching if and only if o(G−S) ≤ |S| for every S ⊆ V.

This theorem can be used to show that graph H in Figure 5.1 has no perfect matching.Indeed, if we delete vertex e from H (S = e and, thus, |S| = 1), we get four componentswith odd number of vertices. Let S = x, y for graph Q in Figure 5.1. Observe thato(Q− S) = 4 > 2 = |S| and, thus, by Tutte’s theorem Q has no perfect matching.

To see that a matching is maximum or not, one can use the following result by Berge,1957. For a matching M in a graph G, a path P is M -augmenting if edges of P alternatebetween edges in M and not in M and if the end-vertices of P are not end-vertices of edgesin M.

Theorem 5.1.2 (Berge) A matching M in a graph G is maximum if and only if G hasno M -augmenting path.

For example, matching ad, be in graph R of Figure 5.1 is not maximum since pathcebdaf is an augmenting path. If we delete the edges of the matching from the path, thenwe get a larger matching ce, bd, af. Thus, this theorem can be used to increase the sizeof matching. For bipartite graphs, the corresponding procedure is considered in the nextsection.

Question 5.1.3 Find a maximum matching in Q of Figure 5.1.

5.2 Matchings in bipartite graphs

In this section we consider bipartite graphs. Recall that the set of vertices V of a bipartitegraph G = (V,E) can be partitioned into two sets X and Y such that every edge of G hasone end-vertex in X and the other in Y. The set of neighbours of a vertex x ∈ X (i.e.,

Page 75: Algorithms and Complexity II

5.2. MATCHINGS IN BIPARTITE GRAPHS 75

R

a

b

c

d

a’

b’

c’

d’

a

b

c

d

e

a’

b’

c’

d’

e’H

Q

a

b

c

d

a’

b’

c’

d’

Figure 5.2: Bipartite graphs

vertices of Y that are adjacent with x) is denoted by N(x). For a set Z ⊆ X, N(X) is theunion of N(x) for all x ∈ X.

Theorem 5.2.1 (Hall) A bipartite graph G with partite sets X and Y (|X| ≤ |Y |) has amatching of size |X| if and only if |N(Z)| ≥ |Z| for every Z ⊆ X.

In Figure 5.2, graph R has no perfect matching by Hall’s theorem. Indeed, N(a, b, c) =a′, b′ and, thus, |N(a, b, c)| = 2 < 3 = |a, b, c|.

Hall’s theorem allows to check whether a bipartite graph has a perfect matching, inparticular, but it does not allow to find such a matching. Thus, we consider an algorithmbased on augmenting paths that allows to find a maximum matching in a bipartite graph.

In each iteration, the algorithms starts from a current matching M , builds an aug-menting tree and tries to use the tree to increase the size of matching by deleting someedges of M and adding more edges.

Suppose we have a matching M in a bipartite graph G. If M includes (as end-verticesof its edges) all vertices of G, we cannot obviously find a larger matching. Let x be avertex of G that is not in M . We build a tree T from x (as a root) by first taking x as theonly vertex of T , but then adding every neighbour y of x together with the edge xy to T .If one of the neighbours z is not in M , we increase M by adding edge xz. If all neighboursare in M , we add to T all edges from M with end-vertices in N(x). This adds to T newvertices L (the other end-vertices of the edges in M). We now add to T all neighbours ofvertices in L, that are not in T yet, together with one edge per neighbour that connectsthe neighbour with a vertex in L. If one of the vertices in the last set of added verticesis not in M , we can construct an augmenting path, delete the corresponding edge of M

Page 76: Algorithms and Complexity II

76 CHAPTER 5. MATCHINGS IN GRAPHS

and add to M two edges instead. If all last added vertices are in M we add to T thecorresponding edges in M and proceed as above.

If at any stage we add a vertex z 6= x which is not in M , we can obtain an augmentingpath from x to z. The path P is the unique path from x to z in the current T (it is uniqueas T is a tree). By deleting from M all edges that are in P and adding to M all edges ofP that are not in M , we will obtain a matching larger than M.

The above described iteration either finds an augmenting path and thus increases thesize of the current matching or fails to find an augmenting path when already all verticesare in T. The last possibility implies that the current matching is maximum and thealgorithm stops.

One can start the algorithm from a trivial matching, i.e., an edge in G. However,to have less iterations, it is advisable to start from a matching produced by a simplealgorithm such as the greedy algorithm. The greedy algorithm for matchings starts froman edge and adds the first possible edge to the current matching. Of course, the greedyalgorithm normally does not produce a maximum matching.

To illustrate the algorithm, let us consider graph H in Figure 5.2. The greedy algo-rithm produces matching M = aa′, bc′, cd′. We build tree T starting from d. The onlyneighbour of d is c′, so c′ and dc′ are added to T. Since c′ is in M , c′b ∈ M and b areadded to T. Then a′ and ba′ are added to T . Then a and a′a ∈ M are added to T. Thenb′ and ab′ are added to T . Then c and b′c are added to T . Then d′ and cd′ are added toT. Since in d′ is not in M , we finish building T and consider the augmenting path thatstarts in d and ends in d′ (this path is always unique as T is a tree): dcba′ab′cd′. Nowwe delete all edges of M in the path and add the remaining edges of the path to M . Weobtain ab′, ba′, cd′, dc′. The last matching is maximum as it is perfect.

Question 5.2.2 Find a maximum matchings in graph Q of Figure 5.2. Start from thematching ba′, cb′, de′, ec′.

Question 5.2.3 Find a maximum matchings in graph R of Figure 5.2. Start from thematching aa′, db′.

Question 5.2.4 Find a maximum matching in a graph G with V (G) = 1, 2, . . . , 5, 1′, 2′, . . . , 5′and E(G) = 11′, 21′, 24′, 25′, 32′, 34′, 35′, 43′, 45′, 51′, 52′, 53′.

Question 5.2.5 Six workers, A, B, C, D, E and F, are to be assigned to five tasks, α,β, γ, δ and ε. For safety reasons, task α must be done by two people working together.Worker A can perform tasks α and δ, B asks α and β, C tasks γ and ε, D tasks α, β andε, E task γ only, F tasks β and δ. Each worker can perform only one task.

(a) Find a way to assign the workers to the tasks such that all tasks will be carried out.

Page 77: Algorithms and Complexity II

5.3. APPLICATION OF MATCHINGS 77

(b) Suppose that C can no longer do task ε for health reasons. Explain why it is nolonger possible to carry out all tasks.

5.3 Application of matchings

Solving systems of linear equations by the Gauss elimination algorithm, one needs firstto arrange the equations in the system such that the coefficients of diagonal variables arenon-zero. This can achieved by using the algorithm for finding a maximum matching in abipartite graph.

For convenience, we will consider an equivalent problem: given a square matrix A =[aij ] rearrange its columns such that every diagonal entry aii becomes non-zero.

We illustrate a general solution to this problem by the following example. Considermatrix

A =

1 1 0 01 0 1 00 1 0 10 0 1 0

.

We see that all diagonal entries of A but a11 are zero. To find rearrangement of columns ofA that makes all diagonal entries non-zero, construct a bipartite graph B(A) correspondingto A. Let the partite sets of B(A) be 1, 2, 3, 4 and 1′, 2′, 3′, 4′. Edge ij′ is in B(A) ifand only if aij 6= 0. It is easy to check that B(A) is actually graph H in Figure 5.2, wherea is 1, b is 2, c is 3 and d is 4.

We need to find a perfect matching inB(A). We already know thatM = 12′, 21′, 34′, 43′is a perfect matching in B(A). This matching tells us that, because of edge 12′, the firstcolumn of A will be replaced by column 2, because of 21′ column 2 will be replaced column1, column 3 by column 4, and column 4 by column 3.

As a result we get matrix

A′ =

1 1 0 00 1 0 11 0 1 00 0 0 1

.

Question 5.3.1 For the following matrix

A =

1 0 0 0 01 1 0 1 00 1 0 1 00 0 1 0 11 0 1 0 0

,

Page 78: Algorithms and Complexity II

78 CHAPTER 5. MATCHINGS IN GRAPHS

find a column rearrangement which puts A in a zero-free diagonal form. Hint: use theresult of Question 5.2.2.

Question 5.3.2 For the following matrix

A =

1 0 0 0 01 0 0 1 10 1 0 1 10 0 1 0 11 1 1 0 0

,

find a column rearrangement which puts A in a zero-free diagonal form. Hint: use theresult of Question 5.2.4.

Question 5.3.3 Let G be a bipartite graph with vertices

V (G) = 1, 2, 3, 4, 5, 1′, 2′, 3′, 4′, 5′

and edgesE(G) = 12′, 13′, 15′, 21′, 22′, 31′, 43′, 52′, 54′.

Starting from the matching M = 12′, 21′, 43′, 54′ of G, find a maximum matching of G.Using this matching, find a column permutation that puts the matrix

A =

0 1 1 0 11 1 0 0 01 0 0 0 00 0 1 0 00 1 0 1 0

in a 0-free-diagonal form. Write down the 0-free-diagonal matrix.

5.4 Solutions

Question 5.3.3

Let M = 12′, 21′, 43′, 54′ To increase M , we grow an alternating tree with root 5′.The tree will give us an augmenting path 5′12′21′3. Thus, we can replace 12′, 21′ in M by15′, 22′, 31′, which gives us a perfect matching

M∗ = 15′, 22′, 31′, 43′, 54′.

To find the column permutation notice that B(A) = G.

Page 79: Algorithms and Complexity II

5.4. SOLUTIONS 79

M∗ shows that the required permutation replaces col. 1 by col. 5, col. 2 by col. 2,col. 3 by col. 1, col. 4 by col. 3, and col. 5 by col. 4.

As a result, we get the matrix

A∗ =

1 1 0 1 00 1 1 0 00 0 1 0 00 0 0 1 00 1 0 0 1

.

Page 80: Algorithms and Complexity II

80 CHAPTER 5. MATCHINGS IN GRAPHS

Page 81: Algorithms and Complexity II

Chapter 6

Euler trails and Hamilton cycles ingraphs

6.1 Euler trails in multigraphs

In this section we study undirected and directed multigraphs, i.e., generalizations of undi-rected and directed graphs in which parallel edges and arcs are allowed. A trail T indirected or undirected multigraph G is called Euler if T is closed (i.e., starts and ends atthe same vertex) and includes every arc or edge of G (exactly once as T is a trail).

When you are asked to draw a figure without taking pen off the paper, you draw anEuler trail in the corresponding multigraph G. If G has no Euler trail, you cannot drawG without taking pen off the paper.

Euler was the first to characterize (undirected) multigraphs with Euler trails. Again,here some obvious necessary conditions are also sufficient conditions. This was the veryfirst theorem in graph theory.

Theorem 6.1.1 A multigraph G has an Euler trail if and only if G is connected and everyvertex in G has even degree.

This theorem allows to conclude, in particular, that any connected multigraph whosevertices are all of degree 4 has an Euler trail. Suppose that a connected multigraph G hasall vertices but two x, y of even degree. By adding a new edge xy, we make the degreesof all vertices even, i.e., the graph becomes Euler, i.e., it has an Euler trail. Similarly, ifwe have a connected multigraph H with 2k vertices x1, x2, . . . , x2k of odd degree, then atleast k edges, say, x1x2, x3x4 . . . , x2k−1x2k are needed to make H Euler. Clearly, k is theminimum such number.

81

Page 82: Algorithms and Complexity II

82 CHAPTER 6. EULER TRAILS AND HAMILTON CYCLES IN GRAPHS

R

a

b

c

d

a’

b’

c’

d’

a

b

c

d

e

a’

b’

c’

d’

e’

H

Q

a

b

c

d

a’

b’

c’

d’

Figure 6.1: Multigraphs

Question 6.1.2 What is the minimum number of edges to add to multigraphs in Figure6.1 to make them Euler.

Question 6.1.3 Let G be a connected graph. Prove that we can orient edges of G to forma digraph D in which |d+(x)− d−(x)| ≤ 1 for each vertex x.

Solution: Let 2k be the number of vertices of G of odd degree. Add k edges to G betweenvertices of odd degree to obtain a multigraph G∗ in which all vertices have even degree.Let F be the set of added edges. By Theorem 6.1.1, G∗ has an Euler trail T . Now westart from the first vertex of T and traverse T in arbitrary direction and each time whenwe visit an edge xy from x to y we orient xy as an arc (x, y). Each edge will be orientedand we will obtain a digraph D∗ with the same Euler trail T .

Clearly, the in-degree and out-degree of each vertex z in D∗ are the same (we enter zas many time as we leave it). Now delete all oriented edges of F from D∗. We obtain adigraph D. Since a vertex z of D∗ is incident to at most one edge of F , either its in-degreein D is the same as in D∗ or it drops by 1. Similarly, the out-degree of z in D is the same asin D∗ or it drops by 1. Moreover, if the in-degree drops by 1, then the out-degree remainsthe same and vise versa. So, in D, we have either d+(z) = d−(z) or |d+(z)− d−(z)| = 1.

In bioinformatics, researchers use so-called edge-coloured multigraphs (for example,to study chromosome arrangements in a cell). An edge-coloured multigraph is an

Page 83: Algorithms and Complexity II

6.1. EULER TRAILS IN MULTIGRAPHS 83

2

12

1

2

1

2

1

2

1

Figure 6.2: Haggkvist’s transformation.

undirected multigraph in which every edge has a colour. The colours will be integers1,2,. . .. In bioinformatics, we are interested in proper coloured Euler trails. An Eulertrail T is called proper coloured (PC) if any two consecutive edges of T have differentcolours. For a vertex x of G and a colour i, di(x) denotes the number of edges of colour iincident to x.

Theorem 6.1.4 (Kotzig’s Theorem) An edge-coloured multigraph G has a properly colouredEuler trail if and only if G is connected, each vertex of G is of even degree, and for everyvertex x and every colour i, di(x) ≤

∑j 6=i dj(x).

Question 6.1.5 Let G be a connected graph and each vertex degree of G is even. Provethat we can assign colours 1,2,3 to the edges of G such that G has a PC Euler trail. Isthe same true when only two colours are assigned?

Solution: By Theorem 6.1.1, G has an Euler trail T . Colour edges of T using colours 1and 2 alternatively. This way all consecutive pairs of edges will get different colours partfrom, possibly, the last and the first edges of T (if the last edges is coloured 1). In thiscase, recolour the last edge in colour 3. Now, T becomes a PC trail.

We cannot restrict ourselves to colours 1 and 2 only. For example, consider the cycleof length 3, two of whose edges will have the same colour whatever the colouring.

Theorem 6.1.1 is a special case of Kotzig’s Theorem (use a new colour for each edge ofG). The following characterization of directed multigraphs with Euler trails (i.e., Eulerdirected multigraphs) generalizes the previous characterization for the undirected case.It is also a special case of Kotzig’s Theorem. To see that replace every arc (x, y) of Dby edges xz and zy of colours 1 and 2, respectively, where z is a new vertex, different foreach arc (x, y). In general, such a transformation is called Haggkvist’s transformation. Itis illustrated in Figure 6.2.

Theorem 6.1.6 A directed multigraph D has an Euler trail if and only if the underlyinggraph UG(D) is connected and, for every vertex x of D, d+(x) = d−(x).

Page 84: Algorithms and Complexity II

84 CHAPTER 6. EULER TRAILS AND HAMILTON CYCLES IN GRAPHS

5

1

F

ab

cd

e

f12

3

4

7

6

9

1

1

11

1

2

12

2

HG

2

a b

c d

e f

a b

cd

e

f

g

1

1

1

12

3 4

5

6

6

6

Figure 6.3: Weighted graphs

In particular, if a digraph H is k-regular, i.e., d+(x) = d−(x) = k for every vertex xin H, then H is Euler.

6.2 Chinese Postman Problem

Suppose a mail carrier traverses all edges in a road network (a connected graph G), startingand ending at the same vertex. The edges of G have non-negative weights representingdistance or time. We’d like to find a closed walk of minimum total length that uses all theedges of G. This is called the Chinese Postman Problem due to the fact it was firstintroduced by the Chinese mathematician Guan Meigu (1962).

Let us analyze the problem. If all vertex degrees of G are even, then G has an Eulertrail and, thus, any Euler trail is a solution to the problem (we do not need to take anyedge more than once). Suppose that G has two vertices x, y of odd degree. Then we canfind a shortest path P between x and y (for example, using Dijkstra’s algorithm) and bydoubling edges of G belonging to P , we get a new multigraph G∗ all of whose degrees areeven. It is possible to prove that an Euler trail of G∗, viewed as a closed walk in G, is asolution to the problem in this case.

Question 6.2.1 Solve the Chinese Postman Problem for the graph F in Figure 6.3.

Solution: The graph F has only two vertices of odd degree: a and b. The shortest pathbetween a and b is adb. Thus, we double edges ad and db in F to obtain a new multigraphF ∗. The trail gabefbdcbdadg is its Euler trail, which, as a closed walk in F , is a solutionto the Chinese Problem for F .

Page 85: Algorithms and Complexity II

6.2. CHINESE POSTMAN PROBLEM 85

Space for a figure of F ∗.

Question 6.2.2 Solve the Chinese Postman Problem for the graph H in Figure 6.3.

Now assume that G has 2k vertices of odd degree (every graph has even number ofvertices of odd degree, see Chapter 1). Then we can find a shortest path between everypair of these vertices and form a complete graph K on the 2k vertices such that the weightw(xy) of an edge xy of K is the distance between x and y in G. Now we find a minimum-weight perfect matching F in K and, for each edge uv of F , double all edges of G that areused in a shortest path between u and v in G. As a result we obtain a new multigraphG∗ whose Euler trail, viewed as a closed walk in G, is a solution to the Chinese PostmanProblem for G.

Question 6.2.3 Solve the Chinese Postman Problem for the graph G in Figure 6.3.

Solution: The graph G has four vertices of odd degree: a, c, e, f . We need to finddistances between them. By inspection, we find:

dist(a, c) = 6, dist(a, e) = 4, dist(a, f) = 9, dist(c, f) = 5, dist(c, e) = 9, dist(e, f) =8.

In the complete graph K on vertices a, c, e, f , edges ae and cf of weights dist(a, e) = 4and dist(c, f) = 5 form a lightest perfect matching of weight 4+5 = 9. The correspondingshortest paths are ae and cbf . Thus, we double the edges ae, cb and bf in G to form anew multigraph G∗. The trail daeabcbefbfcd is an Euler trail in G∗, which is a solutionto the Chinese Postman Problem for G.

Space for figures of K and G∗.

Question 6.2.4 Solve the Chinese Postman Problem for a graph G withV (G) = a, b, c, d, e, E(G) = ab, ac, ad, bc, bd, cd, de in which each edge is of weight 1.

Page 86: Algorithms and Complexity II

86 CHAPTER 6. EULER TRAILS AND HAMILTON CYCLES IN GRAPHS

6.3 Hamilton cycles

A cycle in directed or undirected graph G is Hamilton if it includes all vertices of G. Un-like multigraphs with Euler trails, there is no a characterization of graphs with Hamiltoncycles. Moreover, the problem to check whether a graph has a Hamilton cycle is computa-tionally intractable (more precisely, it is NP-complete) and thus there is no fast algorithmfor checking whether a graph has a Hamilton cycle.

There are only some sufficient conditions to guarantee that a graph has a Hamiltoncycle. In particular,

Theorem 6.3.1 (Dirac’s Theorem) A graph with n vertices in which every vertex is ofdegree at least n/2 has a Hamilton cycle.

In particular, Km,m has a Hamilton cycle. Of course, there are many graphs withHamilton cycle that do not satisfy this condition. In particular, consider a graph G onn ≥ 5 vertices consisting of a cycle. Clearly, G has a Hamilton cycle, but it does notsatisfy the conditions of Dirac’s Theorem. The following theorem is stronger than Dirac’sTheorem (see Question 6.3.3):

Theorem 6.3.2 (Ore’s Theorem) Let G be a graph with n vertices in which, for every pairx, y of distinct non-adjacent vertices, we have d(x) + d(y) ≥ n. Then G has a Hamiltoncycle.

Question 6.3.3 Derive Dirac’s Theorem from Ore’s Theorem.

There are better sufficient conditions that involve more graphs, but they will not bestudied in this course. There are also sufficient conditions for digraphs to have a Hamiltoncycle. Analogously to Ore’s Theorem, we have the following:

Theorem 6.3.4 (Meyniel’s Theorem) Let D be a strong digraph of order n ≥ 2. Ifd(x) + d(y) ≥ 2n − 1 for all pairs of non-adjacent vertices in D, then D has a Hamiltoncycle.

Question 6.3.5 Derive Ore’s Theorem from Meyniel’s Theorem Theorem.

Solution: Consider a graph G on n vertices satisfying the conditions of Ore’s Theorem,i.e., d(x) + d(y) ≥ n for each pair x, y of non-adjacent vertices. Let D be a digraphobtained from G by replacing every edge xy with two arcs (x, y) and (y, x). In D, we haved(x) + d(y) ≥ 2n for all pairs x, y of non-adjacent vertices (as every edge is replaced bytwo arcs). By Meyniel’s Theorem, D has a Hamilton cycle. So, G has a Hamilton cycle.

Page 87: Algorithms and Complexity II

6.4. TRAVELLING SALESMAN PROBLEM 87

h

ab

c

d

e

f

g

Figure 6.4: An example for DTH

6.4 Travelling Salesman Problem

In the Travelling Salesman Problem (TSP), we are given Kn with non-negative weightsassigned to its edges. We need to find a Hamilton cycle of Kn of minimum weight (theweight of a cycle is the sum of weights of its edges).

TSP is even more difficult than the problem to check whether a graph G has a Hamiltoncycle. Indeed, we can transform the latter into TSP by considering Kn with the samevertices as G in which an edge xy is of weight 0 if it belongs to G and of weight 1 if itdoes not belong to G. Clearly, G has a Hamilton cycle if and only if Kn with the weightsassigned above has a Hamilton cycle of weight 0 (which is of minimum weight, of course).

Consider a minimum-weight Hamilton cycle C in Kn with non-negative weights as-signed to its edges. If we delete an edge from C, we will get a Hamilton path P , which isa spanning tree in Kn. P is not necessarily a minimum weight spanning tree, but clearlythe weight of a minimum-weight spanning tree gives a lower bound for the weight of aminimum-weight Hamilton cycle C in Kn.

Since TSP is very difficult, there are some heuristic algorithms (heuristics, for short)for finding some ’good’ solution rather than optimal one. We consider one such heuristiccalled the double tree heuristic (DTH).

The heuristic starts from finding a minimum weight spanning tree T in Kn (withweights on edges). Then, it doubles every edge of T , which results in a multigraph M.This multigraph is connected and has only vertices of even degree. So, M is Euler. Wefind an Euler trail R in M . (Notice that R includes all vertices of Kn.) Then we consider Rand delete all repeated vertices from it (apart from the first and last). We get a Hamiltoncycle H. DTH is illustrated in Figure 6.4.

Suppose that a minimum cost spanning tree is given in the left hand side of the figure.After doubling its edges, we get the graph in the right hand side of the figure. We find an

Page 88: Algorithms and Complexity II

88 CHAPTER 6. EULER TRAILS AND HAMILTON CYCLES IN GRAPHS

Euler trail (as a sequence of vertices for simplicity):

acbcdfdedghgdca.

After deleting vertex repetitions, we get a tour T = acbdfegha.

If TSP satisfies the triangle inequality, i.e. the weight w(xy) + w(yz) ≥ w(xz), thenit is not difficult to show that the solution found by DTH is always at most twice theoptimal one.

Page 89: Algorithms and Complexity II

Chapter 7

NP-completeness

In this chapter we shall consider TSP from the computational complexity point of view.There is strong reason to believe that the best possible algorithm for finding the absolutebest, or optimum, Hamilton cycle for TSP is to try all possible permuations and pick theone that yields the minimal value of the cost function. This is disappointing because thereare factorially many permutations. We shall look at the nature of this factorial explosionand very briefly at a variety of approaches for finding good rather than optimal solution.

7.1 Why is arranging objects hard?

Consider TSP in the following lay person formulation: The salesman is given a mapcontaining several cities and routes between them of varying lengths. The salesman’s taskis to work out an itinerary which visits every city but which takes the minimum distance.

A naive approach to this problem is to randomly pick a first city to start at, thenrandomly pick another city, and add the route from the first city to this city to theitinerary being constructed. Now pick another city at random, and add the route fromthe second city to this city to the itinerary. Continue until an itinerary has been derivedwhich includes all the cities. The problem is that this may not be the shortest distance.

7.2 Brute force optimisation

Although the naive algorithm above is likely to produce a very inefficient route, it does atleast have the advantage of being fast. It grows linearly with the number of cities on themap. Using this as the basic method, an optimal itinerary could be found by constructingall possible itineraries and selecting the one that yields the shortest distance. However,

89

Page 90: Algorithms and Complexity II

90 CHAPTER 7. NP-COMPLETENESS

there are n! ways of selecting the n cities (because the first city can be any of the n cities,and the second city can be any of the remaining n− 1 and so on).

Assume we have a computer that can execute an entire itinerary construction in 0.1s.How large a map can we cope with using the exhaustive optimisation algorithm of simplytrying every ordering and selecting the best one? Cosmologists believe that the age of theuniverse since the big bang is about 1.6× 1017s, so the number of tries must certainly beless than 1.6× 1018 since we can perform 101 tries per second. Unfortunately the factorialfunction grows very rapidly, and 20! is about 2.4× 1018 so even if we had all the time inthe world we could only check maps with up to 19 cities. More realistically we would likethe process to complete in less than ten minutes, which limits the optimisation algorithmto a maximum of 6,000 attempts. This restricts us to 7 cities because 7! is 5,040.

This analysis makes for depressing reading. We have deliberately selected a near-trivialconstruction algorithm that runs quickly at the cost of producing a poor itinerary, andyet to optimise the route requires factorial time, limiting us to the optimisation of trivialmaps. Even if we had a computer that was 10 times faster we could only make 60,000checks, which is only sufficient to cope with eight cities since 8! is 40,320. This is animportant result because it means that speeding up the machine by an order of magnitudeonly increases the maximum allowable map size by one city. If this is the best availablealgorithm, then we would be justified in claiming that route planning was completelyintractable for non-trivial maps.

7.3 How to spot an explosive algorithm

The details of particular implementations are washed out of the complexity analysis byconsidering the proportional increase in number of steps and memory locations requiredas the size of the problem grows. If for instance the number of locations required quadru-ples when we double the number of cities to be visited then the space complexity ofthe algorithm is proportional to the square of the number of inputs. Such an algorithmis called quadratic. Our naive routing algorithm has a time complexity that grows asthe factorial of the number of cities, and is therefore a factorial algorithm.

If an algorithm requires 15N2 steps to finish a problem with N inputs then the run-time T is proportional to N2. Imagine that this algorithm required a large number of32-bit additions to be performed. A 16-bit computer executing the same algorithm mighthave to execute two steps to perform each addition, but even so the number of steps wouldstill be at worst 30N2. In both cases T ∝ N2, or in the terminology of complexity analysisT is of the order of N2 or T = O(N2). Clearly here, the constant of proportionality is aproperty of the implementation not just the algorithm.

It turns out that algorithms that are factorial (or O(N !)) or exponential (O(2N )) areso expensive that even the fastest computer can not handle problems with more than

Page 91: Algorithms and Complexity II

7.3. HOW TO SPOT AN EXPLOSIVE ALGORITHM 91

10–15 inputs in a reasonable time. This is surprising, so we will consider the behaviour ofexponential functions further.

7.3.1 Exponential functions

When asked to make estimates, humans tend to think linearly. As a simple example,consider this problem

Take a piece of paper and fold it in two. Then fold it in two again. Continueuntil a total of twenty folds have been made. How thick will the resultingassembly be?

Most people guess somewhere around the thickness of a large paperback book, say3–4cm. However, the real answer is about 100 metres. A 500 page paperback novel is2.5cm thick, so each leaf of paper must be about 0.1mm thick (since there two pages to aleaf). Every time the pile of paper is folded the stack doubles in thickness, so the thicknessin metres is

10−4 × 2× 2× . . .× 2 = 10−4 × 220 = 104.9m

This startling result can easily be made more startling still. The choice of twenty foldswas simply an arbitrary small integer. How about 42 folds? Well in that case the thicknesswill be 242× 10−4m, or about 400,000km. I chose 42 folds because a 400,000km is roughlythe distance from the earth to the moon – truly an astronomical distance.

These answers are surprising because everyday observable processes usually grow at aconstant rate and we are conditioned to think in terms of this linear expansion. Exponen-tial effects are rare but some of them work in our favour. In a sea port, the largest of shipscan be tethered by a few turns of a mooring rope around a bollard. It turns out that thefriction between the rope and the bollard goes up exponentially with the length of the ropeturned around the bollard, so only a short length is required to hold an enormous massin place. However, usually exponentials cause nasty surprises. There is a story about amedieval philosopher who performed a great service for his king and in return was allowedto ask for his heart’s desire. The philosopher asked for a chessboard, with a single grainof wheat on the first square, two grains on the second, four on the third and so on. Thisseemed to the king (who had a linear mind) a reasonable request so he granted it. Thestory did not record the philosopher’s fate after the king had discovered the implicationsof this decision.

7.3.2 Good algorithms

We have already seen that algorithms that require factorial or exponential numbers ofsteps will take so long to execute as to be useless for problem sizes greater than about

Page 92: Algorithms and Complexity II

92 CHAPTER 7. NP-COMPLETENESS

ten or fifteen, whereas polynomial time algorithms present far more reasonable demandsfor computer time and space. We will follow common practice in calling polynomial timealgorithms good. Formally, a good algorithm A is an algorithm such that TA = O(nα)for some α ≥ 0 where TA is the worst case time complexity. Algorithms that have worsethan polynomial behaviour are rather loosely called exponential even when they are notstrictly, as in the case of our factorial naive routing algorithm.

It is worth noting that for some applications ‘bad’ algorithms may be best. For in-stance, for a problem of size 20, given a choice between an exponential algorithm and apolynomial algorithm with T = O(N5) we would choose the exponential because 220 is lessthan 205. More subtly, the constant of proportionality in an order relation is not solelya property of the implementation. Sometimes algorithms showing quite small polynomialbehaviour are known to exhibit very large constants of proportionality. Whilst there willalways be some value of N above which an exponential algorithm will take longer thanthis polynomial algorithm, that value of N might be greater than any real problem inpractice.

7.4 Tractable problems

Analysing a single algorithm is useful and can be used to demonstrate that one algorithmis better than another, but this kind of complexity analysis does not tell us if a givenalgorithm is the best possible solution to a particular problem. Rather remarkably, itis possible to make general statements about certain problem domains that tell us whatthe performance of the best possible algorithm will be even when we do not know whatthat algorithm is. More ominously, analysis can demonstrate that for certain problems nopolynomial time solution exists. There are even problems for which the lower bound onthe time complexity not just exponential but doubly exponential (2N

N). In fact there are

problems where the lower bound on time complexity is of the form

2222...

where the number of 2’s can be arbitrarily large depending on the size of the input problem.It should be clear that these problems therefore admit to no algorithm that could be usefulon any computer.

When discussing problems (as opposed to particular algorithms for solving those prob-lems) we talk about the problem domain. A problem domain is tractable if there is agood, or polynomial time, algorithm for solving Π. The class of tractable problems iscalled P. The following problems are in P: Graph Connectivity Problem: give agraph G, check that G is connected; Digraph Connectivity Problem: give a digraphD, check that D is strongly connected; Perfect Bipartite Matching Problem: givena graph G, check whether G has a perfect matching.

Page 93: Algorithms and Complexity II

7.5. THE CLASS NP 93

Question 7.4.1 Give further examples of tractable problems.

7.5 The class NP

We are interested in combinatorial optimisation problems. Such problems feature alarge space of possible configurations (such as possible itineraries) and a cost function,such as distance. A good solution to the problem minimises the distance.

A decision problem is a mapping from a set of inputs to one of the two outcomes YESor NO. In other words, a decision problem is the union of some inputs called instancessome of which are YES-instances and the rest are NO-instances. For example, everyinstance of the decision version of TSP consists of the number of cities N , all distancesbetween distinct cities and some threshold L. The instances having a Hamilton cycle oflength at most L are YES-instances. The rest of the instances are NO-instances.

In fact, every optimisation problem has a corresponding decision problem formed byasking if there is a solution configuration of the optimisation problem that is better thansome threshold. The decision problem has boolean output and a result that can be easilychecked: if a configuration with less wire than the threshold is found it can be demonstratedto fulfill the requirement in polynomial time – simply by adding up the lengths of allthe wires in the circuit. If on the other hand we were required to demonstrate that aparticular configuration contained less wire than any other configuration we wouldhave to generate all possible configurations, which as we have seen requires factorial time.Hence this device of introducing a threshold does not reduce the time required to finda configuration that meets the requirement, but it does allow us to demonstrate that aconfiguration, once found, is valid in polynomial time.

7.5.1 Minimisation and backtracking

The process of testing all possible itineraries of the naive routing algorithm could be viewedas a process of backtracking. We have N bins and N cities. We allocate all the cities to abin in an arbitrary way, and then rip-up part of the arrangement and try it another way.In fact, as we allocate cities to bins we have a choice of all of the remaining cities. This iswhat gives us the N × (N − 1)× (N − 2) . . . = N ! behaviour of the algorithm.

Now imagine that we had an unlimited supply of computers. We could use one com-puter to decide which of the N cities to allocate to the first bin, and then create an arrayof N −1 computers to decide which of the remaining cities should go into the next bucket.Then each N − 1 branch would pass its information down to N − 2 computers below itwhich would decide in parallel which of the N − 3 remaining cities to allocate to the nextbucket. This machine will in fact form a tree of computers N levels deep but up to N !computers wide at the lowest level. We have traded time complexity for space (parallelism)

Page 94: Algorithms and Complexity II

94 CHAPTER 7. NP-COMPLETENESS

complexity.

There will be at least one path through the tree that corresponds to a minimal con-figuration of the problem. Imagine, instead of this rapidly branching tree of computersa single computer with the magical power to guess the right answer correctly. At eachbranch of the tree this computer would take the branch that leads to the correct result.It is clear that this computer could find the correct result in only N steps, that is inpolynomial time.

This magical (and sadly unrealisable) machine is called a non-deterministic machine.A non-deterministic algorithm for some decision problem Π is an algorithm that isallowed to make an ‘ideal’ guess at each stage and thus solves the decision problem inpolynomial time.

Not all problems have polynomial solutions even on non-deterministic machines, butthe set of problems that do have these polynomial solutions using non-deterministic ma-chines is called NP.

It is not known if problems in NP have polynomial algorithms, that is whether P =NP, but there is good reason to suppose that P 6= NP. Very remarkably there is a setof problems in NP that can be transformed into each other in polynomial time. This setis called the NP-complete problems. Since they are all convertible to each other inpolynomial time, if any one of the several thousand presently known such problems hasa polynomial time algorithm then they all have. However, so far no-one has ever foundone so the suspicion is that they are all intractable. On the other hand, exponential lowertime bounds have not been proven for any of them either.

Since NP-complete problems are believed to be intractable, we cannot guarantee find-ing an exact solution to every relatively moderate instance of a minimization NP-complete problem in practical time. However, heuristics may be able to approach a globalminimum in polynomial time using well chosen rules of thumb.

7.5.2 Examples of NP-complete problems

In this section we will give several examples of NP-complete problems. If we want toprove that a problem, say A, is NP-complete then we have to do the following two steps.

(i) Prove that A belongs to the class NP.

(ii) Show that if we could solve A in polynomial time then we could also solve some otherNP-complete problem in polynomial time.

In order to do (ii) above, we take an arbitrary instance of a problem that is known tobe NP-complete, and then transform it into an instance of A, using at most a polynomial

Page 95: Algorithms and Complexity II

7.5. THE CLASS NP 95

number of steps in the process. We then need to show that the answer to this instance ofA directly gives us the answer of the original NP-complete problem. We will later givesome examples of how to do (ii).

In order to do (i) above, we need to show that A belongs to the class NP. We willgive some examples of how to do that later, when we have an alternative definition of theclass NP.

If we instead of showing that A is NP-complete just wanted to show that A wasNP-hard, then we just needed to do (ii) above. This means that an NP-hard problemdoesn’t have to lie in the class NP, but it does have the property that if we could solveit in polynomial time, then we can solve every problem in NP in polynomial time.

We will however mainly be interested in NP-complete problems. We now give a fewexamples of such problems:

(1) Hamilton Path Problem: given a graph G, check whether G has a Hamiltonpath, i.e., a sequence of all vertices v1v2 . . . vn such that vi is adjacent to vi+1 for eachi < n;

(2) 3-SAT (recall the main terms of Section 3.4) The 3-Satisfiability Problem,also called 3-SAT, is the following problem. Let X = x1, . . . , xk be a set of booleanvariables and let C1, . . . , Cr be a collection of clauses, all of size 3, for which every literalis over X. Decide if there exists a truth assignment t = (t1, . . . , tk) to the variables in Xsuch that the value of every clause will be 1. This is equivalent to asking whether or notthe boolean expression F = C1 ∗ . . . ∗Cp can take the value 1. Depending on whether thisis possible or not, we say that F is satisfiable. Notice that 2-SAT is polynomial timesolvable, but 3-SAT is not (unless P = NP, which the vast majority believe is not true);

(3) 3-Colouring given a graphG, check whether χ(G) ≤ 3. Notice that 2-Colouringis polynomial time solvable, but 3-Colouring is not (unless P = NP, which the vastmajority believe is not true);

(4) Subset Sum: given a set N = n1, n2, . . . , nk of integers and an integer l, checkwhether there is a subset of N in which the sum of its elements equals l.

7.5.3 Alternative definition on the class NP

We can now give an alternative definition of the class NP, which we then will use to showthat the four problems mentioned in the previous section are all in the class NP.

A decision problem A lies in NP if and only if we can verify that the answer is YES,when this is the case, in polynomial time. In other words, if we know that the answer isYES (and know what the solution is), then we can prove (to anyone who does not knowthe answer) that the answer is YES in polynomial time.

Page 96: Algorithms and Complexity II

96 CHAPTER 7. NP-COMPLETENESS

We will now give a few examples of how to use this definition.

Hamilton Path Problem

If we have a Hamilton path, then we can give the corresponding sequence of vertices tosomeone who doesn’t know that the answer is YES, and then he/she can in polynomialtime verify that it in fact is a Hamilton path. So, Hamilton Path Problem belongs toNP.

Note that if the answer is NO, then it is not known if one could convince someone ofthis in polynomial time. We cannot just check all possible sequences of vertices, as thiscouldn’t be done in polynomial time. But this doesn’t matter as we only have to considerthe case when the answer is YES.

However this means that it is not known whether the decision problem No HamiltonPath belongs to NP. Note that this in some sense is the ’complement’ of HamiltonPath, as if one answers YES the other answers NO, and visa versa.

3-SAT

If we know that the answer is YES, and have the solution (i.e., the corresponding truthassignments), then we can convince anybody that the answer is YES, by giving them thetruth assignment. Then one can in polynomial time check that they in fact make the giveninstance of 3-SAT TRUE. So, 3-SAT belongs to NP.

Independent Set, Clique and 3-Colouring

As above if we do have an independent set of size at least k, then we give this set toanybody, and they can in polynomial time convince themselves of the fact that it is anindependent set of size at least k. So Independent Set belongs to NP. Similarly, forClique and 3-Colouring.

Subset sum

Again we just illustrate which integers sum up to l. This can then be checked in polynomialtime. So Subset sum belongs to NP.

For example, consider the set S = 5, 12, 22, 33, 35, 41, 55, 74, 99, 102, 124, 133, 167, 175, 189and the number l = 618. Then is may not be so easy to decide if there is a subset of Sthat sums up to 618 (as S is not too large it can be done, but if S had 10000 elements,then these problems are very difficult). However if we know the result (and any other

Page 97: Algorithms and Complexity II

7.6. PROVING THAT A PROBLEM IS NP-COMPLETE 97

information we may need), then we can easily convince someone that the answer is YES,as we simply give them the set 12, 22, 55, 74, 99, 167, 189, and they can then quickly (inpolynomial time) convince themselves that the answer is in fact YES, by checking thatthe set is a subset of S, and that the elements sum up to 618.

Question 7.5.1 Hamilton Cycle Problem: given a graph G, check whether G has aHamilton cycle. Show that the Hamilton Cycle Problem belongs to the class NP ofdecision problems.

7.6 Proving that a problem is NP-complete

Above we discussed that some decision problems are NP-complete. There are manybooks and research articles where thousands of problems are listed to be NP-complete.Nevertheless, once you encounter a new decision problem you may want to prove that theproblem is NP-complete. (This, among other things, will give you an excuse to use aheuristic rather than a polynomial time algorithm to solve the problem.)

There are a few ways to prove NP-completeness of a new decision problem. The key tothe proof is to choose the right decision problem that is already known to be NP-complete.Consider the following examples:

(1) Colouring: given a graph G and an integer k, check whether χ(G) ≤ k. Clearly, 3-Colouring is a special case of Colouring when k is always equal 3. 3-Colouringis known to be NP-complete. So, if we could solve Colouring in polynomial time,we surely could solve 3-Colouring is polynomial time, too. Thus, Colouringis NP-hard. To prove that it is NP-complete, it suffices to observe that givenan assignment of k or less colours to the vertices of G, we can check whether thisassignment is a proper vertex colouring in polynomial time.

(2) Independent Set Problem: given a graph G and an integer k, check whether Ghas an independent set with at least k vertices. Theorem 4.7.5 asserts the following:A graph G with n vertices has a vertex m-colouring if and only if the Cartesianproduct G × Km has an independent set with n vertices. Thus, if we could solveIndependent Set Problem in polynomial time, we could also solve Colouringin polynomial time. Hence, Independent Set Problem is NP-hard. Clearly, itis in NP.

(3) Hamilton Cycle Problem: given a graph G, check whether G has a Hamiltoncycle. We assume that we know that Hamilton Path Problem is NP-complete.Consider a graph H, an instance of Hamilton Path Problem. Add to H a newvertex x adjacent to each vertex in H. Observe that the new graph L has a Hamilton

Page 98: Algorithms and Complexity II

98 CHAPTER 7. NP-COMPLETENESS

cycle if and only if H has a Hamilton path. Thus, if we could solve Hamilton CycleProblem in polynomial time, we could also solve the NP-complete HamiltonPath Problem in polynomial time. Hence, Hamilton Cycle Problem is NP-hard. Clearly, it is in NP.

(4) TSP: given a complete graph with weights on its edges and an integer l, check whetherthe graph has a Hamilton cycle of total weight at most l. Consider a graph G. Giveweight 0 to all edges of G, join all non-adjacent vertices of G by new edges andassign weight 1 to each of them. Let H be the obtained weighted complete graph.Set l = 0. Observe that H has a Hamilton cycle of weight 0 if and only if G has aHamilton cycle. Thus, if we could solve TSP in polynomial time, we could also solvethe NP-complete Hamilton Cycle Problem in polynomial time. Hence, TSP isNP-hard. Clearly, it is in NP.

Question 7.6.1 The Satisfiability Problem, also called SAT, is the following prob-lem. Let X = x1, . . . , xk be a set of boolean variables and let C1, . . . , Cr be a collectionof clauses, of any size, for which every literal is over X. Decide if there exists a truthassignment t = (t1, . . . , tk) to the variables in X such that the value of every clause willbe 1. Prove that SAT is NP-complete.

Question 7.6.2 Clique Problem: given a graph G and an integer k, check whether Ghas a clique with at least k vertices. Prove that Clique Problem is NP-complete usingIndependent Set Problem.

Question 7.6.3 A set X of vertices of a graph G is called a vertex cover if any edge ofG has a vertex in X.

(1) Let G = (V,E) be a graph and let X be a vertex cover of G. Prove that V −X isan independent set of G.

(2) Let G = (V,E) be a graph and let I be an independent set of G. Prove that V − Iis a vertex cover of G.

Solution:

(1) Let u, v ∈ V −X. By the definition of a vertex cover, uv 6∈ E. Hence, V −X is anindependent set.

(2) Let uv ∈ E. Since u and v cannot both be in I, at least one of them is in V − I.Hence, V − I is a vertex cover.

Question 7.6.4 Vertex Cover Problem: given a graph G and an integer l, checkwhether G has a vertex cover with at most l vertices. Prove that Vertex Cover Problemis NP-complete. Hint: use the result of the previous question (both (1) and (2)).

Page 99: Algorithms and Complexity II

7.7. PROCEEDING IN THE FACE OF INTRACTABLE PROBLEMS 99

7.7 Proceeding in the face of intractable problems

Faced with such an explosive run-time requirement the following options present them-selves:

1. Get a faster computer.

2. Get a better algorithm.

3. Relax the quality requirement. Rather than searching for the best solution, accept‘good’ solutions which may be found without searching the entire set of solutions.

As already discussed, option 1 is likely to yield at best marginal improvements foralgorithms with explosive run-times, although it could help a great deal if, say, the algo-rithm had linear run-time behaviour. Option 2 is ideal, but discovering new algorithms isdifficult, and for example in the case of many real VLSI layout problems it can be shownwith a high degree of confidence that all algorithms solving the problem (even ones not yetdiscovered) will have explosive run-time behaviour. In the case of problems which haveonly explosive algorithms then option 3 must be adopted.

It is an observable fact that humans can optimise seven city maps in less than fiveminutes, and that humans can produce good layout for much larger maps in much lessthan factorial time. Of course, all this really means is that the human designer is notslavishly attempting to construct all possible itineraries, but is somehow short circuitingthe process by only testing plausible ones. These short circuits are called heuristics andact as rules of thumb for finding good solutions.

There is no guarantee that the solution found by a heuristic is the best one available,and indeed in certain pathological circumstances a heuristic may perform very badly. It isvery important to understand the strengths and weaknesses of any heuristic layout toolsthat you use and not to treat them as black boxes that will always generate good results.

Heuristics can take several forms. They may be a guiding principle that is itselfdeterministic and always yields the same result. On the other hand, the algorithm mayattempt to guess the answer by making sensible predictions as to the correct way forward.In order to illustrate some of the best known heuristics, we will consider the following wellknown combinatorial minimisation problem.

7.8 TSP Heuristics

We can now return to the travelling salesman problem (TSP).

Consider TSP with only nine cities. Even for such a problem to find a minimum weight

Page 100: Algorithms and Complexity II

100 CHAPTER 7. NP-COMPLETENESS

Hamilton cycle requires nearly an hour on a 25MHz 80486. We observed earlier that usinga faster computer will allow us to increase the size of the solvable TSP only slightly.

There are many TSP heuristics (see CS3490 blue book and [GP2002]). Two is of themare especially simple:

(1) In Nearest Neighbour (NN) Heuristic we start from an arbitrary initial vertexand go to the nearest one (in terms of the weight), from the second to to a new nearestone, etc. We are not allowed to return to already visited vertices before all vertices havebeen visited. We return from the last visited vertex to the initial one.

(2) The greedy heuristic is a similar algorithm to NN, except we don’t always main-tain a path, but instead maintain a number of disjoint paths. We start from an empty setof edges S (forming a collection of paths each consisting of just one vertex) and continuein the greedy manner, i.e., by adding to S the lightest edge, then the lightest edge amongthe remaining ones as long as a collection paths is maintained. Once we have a Hamiltonpath, we add the edge between its extreme vertices and we stop.

(3) The double tree heuristic was considered in Section 6.4.

Question 7.8.1 Consider an example of an airline that wants to offer connections be-tween the cities Beijing, London, Mexico City, New York, Paris and Tokyo. The distancesin miles between each pair of cities is given in the following table:

B L MC NY P T

B – 5074 7753 6844 5120 1307L 5074 – 5558 3469 214 5959MC 7753 5558 – 2090 5725 7035NY 6844 3469 2090 – 3636 6757P 5120 214 5725 3636 – 6053T 1307 5959 7035 6757 6053 –

The problem is described by an edge-weighted complete graph with vertices B,L,MC,NY,Pand T (see Fig. 7.1). Find six Hamilton cycles using NN and starting from each of thecities. Compare the lengths of the cycles.

Question 7.8.2 Find a Hamilton cycle in the graph of Fig. 7.1 using the greedy heuristic.

Page 101: Algorithms and Complexity II

7.8. TSP HEURISTICS 101

MC

NY

L

P

T

B

7750

7040

5730

5560

2090

3470

3640

6760

6840

5070

5960

210

5120

6050

1307

Figure 7.1: An edge-weighted complete graph

Page 102: Algorithms and Complexity II

102 CHAPTER 7. NP-COMPLETENESS

References

There is a large number of books on graph theory and algorithms and related topics.Here is a very short list of them. They were all mentioned in Chapters 1-7.

[BG2000] J. Bang-Jensen and G. Gutin, Digraphs: Theory, Algorithms and Applica-tions, Springer, London, 2000.

[CLR1990] T.H. Cormen, C.E. Leiserson and R.L. Rivest, Introduction to Algorithms.,MIT Press, Cambridge MA, 1990.

[GY1999] J. Gross and J. Yellen, Graph theory and its applications, CRC Press, 1999.

[GP2002] The Traveling Salesman Problem and its Variations (G. Gutin and A. Pun-nen, eds.), Kluwer, Dordrecht, 2002.