Graphs: an introduction
4.2 Subgraphs, components, adjacency matrix
4.2.2 Observation. Each component of any graph is connected, and a graph is connected if and only if it has a single component
Proof. Clearly, a connected graph has a single component. On the other hand, any two vertices x, y in the same component of a graph G can be connected by a walk. Any walk from x to y of the shortest
possible length must be a path. 2
Why did we choose the somewhat roundabout definition of compo-nents using walks, rather than using paths? We could define the com-ponents using the relation∼, where x∼ y if a path from x to y exists.
The above considerations show that∼ is in fact the same relation as
∼. But showing directly that ∼ is an equivalence is a bit messy, and the approach via walks seems cleaner.
4.2 Subgraphs, components, adjacency matrix 121 It is relatively easy to decide whether a given graph is connected, or to find the components. We aren’t going to describe such algorithms here; they can be found in almost any textbook on algorithms. They are usually presented as algorithms for searching a graph, or a maze.
One such algorithm is the so-called depth-first search.
Distance in graphs. Let G = (V, E) be a connected graph. We define the distance of two vertices v, v∈ V (G), denoted by dG(v, v), as the length of a shortest path from v to v in G.
Hence dGis a function, dG: V×V → R, and it is called the distance function or the metric of the graph G. The metric of G has the following properties:
1. dG(v, v)≥ 0, and dG(v, v) = 0 if and only if v = v;
2. (symmetry) dG(v, v) = dG(v, v) for any pair of vertices v, v; 3. (triangle inequality) dG(v, v)≤ dG(v, v) + dG(v, v) for any three
vertices v, v, v∈ V (G).
The validity of these statements can be readily checked from the defi-nition of the distance function dG(v, v). Each mapping d : V × V → R with properties 1–3 is called a metric on the set V , and the set V to-gether with such a mapping d is called a metric space. The distance function dG of a graph has, moreover, the following special properties:
4. dG(v, v) is a nonnegative integer for any two vertices v, v; 5. if dG(v, v) > 1 then there exists a vertex v, v = v = v, such
that dG(v, v) + dG(v, v) = dG(v, v).
Conditions 1–5 already characterize functions arising as distance functions of graphs with vertex set V (see Exercise 7).
Graph representations. We have seen representations of graphs by drawings, and also by writing out a list of vertices and edges.
Graphs can also be represented in many other ways. Some of them become particularly important if we want to store and manipulate graphs in a computer. A very basic and very common representation is by an adjacency matrix:
4.2.3 Definition. Let G = (V, E) be a graph with n vertices. Denote the vertices by v1, v2, . . . , vn(in some arbitrary order). The adjacency matrix of G, with respect to the chosen vertex numbering, is an n×n matrix AG= (aij)ni,j=1 defined by the following rule:
aij =
1 if{vi, vj} ∈ E 0 otherwise.
This is very similar to the adjacency matrix of a relation defined in Section 1.5. The adjacency matrix of a graph is always a symmetric
square matrix with entries 0 and 1, with 0s on the main diagonal.
Conversely, each matrix with these properties is the adjacency matrix of some graph.
Example. The graph G = 1
Let us emphasize that the adjacency matrix also depends on the chosen numbering of the vertices of a graph!
It might seem that we gain nothing new by viewing a graph as a matrix since the graph and the adjacency matrix both encode the same information. To illustrate that we can profit from this alterna-tive representation, let us show a simple connection between matrix multiplication and the graph metric.
4.2.4 Proposition. Let G = (V, E) be a graph with vertex set V = {v1, v2, . . . , vn} and let A = AGbe its adjacency matrix. Let Akdenote the kth power of the adjacency matrix (the matrices are multiplied as is usual in linear algebra, i.e. if we put B = A2, we have bij =
n
k=1aikakj). Let a(k)ij denote the element of the matrix Ak at position (i, j). Then a(k)ij is the number of walks of length exactly k from the vertex vi to the vertex vj in the graph G.
Proof. This is easy but very instructive. We proceed by induction on k. A walk of length 1 between two vertices means exactly that these vertices are connected by an edge, and hence for k = 1 the proposition just reformulates the definition of the adjacency matrix.
Next, let k > 1, and let vi, vj be two arbitrary vertices (possibly identical). Any walk of length k from vi to vj consists of an edge from vi to some neighbor v of vi and a walk of length k− 1 from v to vj:
4.2 Subgraphs, components, adjacency matrix 123
vi
v
vj
By the inductive hypothesis, the number of walks of length k− 1 from v to vj is a(kj−1). Hence the number of walks of length k from vi to vj
is
{vi,v}∈E(G)
a(kj−1)=
n
=1
aia(kj−1).
But this is exactly the element at position (i, j) in the product of the
matrices A and Ak−1, i.e. a(k)ij . 2
4.2.5 Corollary. The distance of any two vertices vi, vj satisfies dG(vi, vj) = min{k ≥ 0: a(k)ij = 0}.
This result has surprising applications. For instance, if one wants to find the distance for all pairs of vertices in a given graph, one can apply sophisticated algorithms for matrix multiplication (some of them are described in Aho, Hopcroft, and Ullman [11]) and some other ideas and get unexpectedly fast methods for computing the function dG. Ex-ercises 10 and 11 indicate other algorithmic applications.
Let us remark that the adjacency matrix is not always the best computer representation of a graph. Especially if a graph has few edges (much fewer than n
2
), it is usually better to store the list of neighbors for every vertex. For a fast implementation of certain algorithms, other, more complicated representations are used as well.
Exercises
1. Prove that the complement of a disconnected graph G is connected.
(The complement of a graph G = (V, E) is the graph (V,V
2
\ E).) 2. What is the maximum possible number of edges of a graph with n
vertices and k components?
3. CSDesign an algorithm that finds the decomposition of a given graph G into its components. (Try to get an algorithm which needs at most O(n + m) steps for a graph with n vertices and m edges.)
4. ∗Prove that a graph is bipartite if and only if it contains no cycle of odd length.
5. ∗Describe all graphs containing no path (not necessarily induced!) of length 3.
6. ∗Having solved the preceding exercise, describe all graphs containing no path of length 4.
7. Show that if a function d : V × V → {0, 1, 2, . . .} satisfies conditions 1–5 above then a graph G = (V, E) exists such that dG(v, v) = d(v, v) for any pair of elements of V .
8. Define the “diameter” and “radius” of a graph (in analogy with the intuitive meaning of these notions).
9. (a) Find a connected graph of n vertices for which each of the powers A1G, A2G, . . . of the adjacency matrix contains some zero elements.
(b) Let G be a graph on n vertices, A = AG its adjacency matrix, and Inthe n×n identity matrix (with 1s on the diagonal and 0s elsewhere).
Prove that G is connected if and only if the matrix (In+ A)n−1 has no 0s.
(c) Where are the 0s in the matrix (In+ A)n−1 if the graph G is not connected?
10. Show that a graph G contains a triangle (i.e. a K3) if and only if there exist indices i and j such that both the matrices AGand A2G have the entry (i, j) nonzero, where AG is the adjacency matrix of G.
Remark. In connection with algorithms for fast matrix multiplication, this observation gives the fastest known method for deciding whether a given graph contains a triangle, substantially faster than the obvious O(n3) algorithm.
11. ∗Let G be a graph. Prove the following formula for the number of cycles of length 4 in G (not necessarily induced):
1 8
trace
A4G
− 2|E(G)| − 4
v∈V (G)
degG(v) 2
.
Here A4G is the 4th power of the adjacency matrix, and trace A4G denotes the sum of the elements on the main diagonal of A4G. For the definition of degG(v), see the next section. Note that this gives an O(n3) algorithm for counting the number of cycles of length 4, or even a faster algorithm using algorithms for fast matrix multiplication.
12. Prove that G and Gare isomorphic if and only if a permutation matrix P exists such that
AG = P AGPT.
Here AG is the adjacency matrix of G and PT denotes the transposed matrix P . A matrix P is called a permutation matrix if its entries are 0 and 1 and each row and each column contain precisely one 1.