Skip to main content

Section7.1Basic Graph Theory

Objectives
  1. Learn the different definitions of graphs: directed/undirected, simple, and weighted graphs.
  2. Learn some of the common properties of graphs: complete, bipartite, planar, tree, Hamiltonian, Eulerian
  3. Recipe: find the adjacency matrix representation of a (vertex-ordered, weighted) graph.
  4. Formula: counting the number of k -step walks.
  5. Vocabulary: graph, vertex, edge, loop, walk, trail, circuit, cycle, tree, degree, component

Subsection7.1.1Different types of graphs

To study Google's PageRank algortihm later, we introduce some terminology regarding graphs. There are variations, included directed and undirected graphs, weighteds graphs, graphs with loops and multiple edges, or simple graphs where these are disallowed.

Here's an example of a graph:

ABCDe1e9e2e3e4e5e6e7e8e10
Definition

A (directed) graph is a pair (V,E) where V is a set of vertices and E assigns to each pair (v,w) of vertices a set of edges with source v and target w.

In the example, the vertex set is {A,B,C,D}, and we have E(A,B)={e1,e9}.

Note that an equivalent definition is to define a graph as a quadruple (V,E,s,t) where V is a set of vertices, E is a set of edges, and s,t:EV are the source and target functions. This is equivalent to the first definition because we can define the set of edges as the disjoint union of the sets E(v,w) for all pairs of vertices (v,w). Note also the definition allow multiple edges between two vertices, or even loops. These graphs are furthermore directed, because there doesn't have to be an edge from v to w if there is an edge from w to v. To be fully precise, this definition encompasses directed multigraphs.

To make sense of these possible restrictions, we need to define some more terms.

Definition

  • An undirected graph is a pair (V,E) where V is a set of vertices and E assigns to each unordered pair v,w of vertices a set of edges connecting v and w.
  • A (directed or undirected) graph is simple if it has no loops and no multiple edges between any (ordered or unordered) pair of vertices.

A simple directed graph can be represented as a pair (V,E) where V is a set of vertices and EV×V is a binary irreflexive relation on the edges. (Irreflexivity means that (v,v)B∈E for all vertices v, thus ensuring the absense of loops.)

Then a simple undirected graph corresponds to the case where the edge relation is symmetric (as well as irreflexive). Sometimes the word “graph” is taken to mean undirected graph, and then digraph is used to mean directed graph. So be careful to check the definitions when reading about graphs!

Let's look at some examples of the various types of graphs. Above we saw general (directed multi-)graph; if we remove the edge e9 and the loop e10, we get a simple directed graph:

ABCDe1e2e3e4e5e6e7e8

If we ignore the direction of the edges, we get a (non-simple!) undirected graph:

ABCDe1e2e3e4e5e6e7e8

To get a simple undirected graph, we can remove the edge e6 and the edge e7:

ABCDe1e2e3e4e5e8

Because we can never see too many examples of graphs (well, maybe!), let's look at some more famous ones. First up, complete graphs: The the complete graph Kn is the simple graph on n vertices, with all pairs of vertices connected by an edge, e.g., K5:

A0A1A2A3A4

Next, the line graph Ln is the simple graph on n ordered vertices, with edges between adjacent vertices, e.g., L5:

A0A1A2A3A4

Finally, the cycle graph Cn is the simple graph on n ordered vertices, with edges between adjacent vertices and an edge between the first and last vertex, e.g., C5:

A0A1A2A3A4

Subsection7.1.2Graph Terminology

Definition

The in-/out-degree of a vertex v in a directed graph is the number of edges entering/leaving v.

Definition

The degree of a vertex v in an undirected graph is the number of edges incident with v.

For example, in the complete graph Kn, the degree of each vertex is n1.

Definition

A walk in a graph is a sequence of vertices and edges v1,e1,v2,e2,...,vk such that eiE(vi,vi+1 for i=1,...,k1.

The length of a walk is the number of edges in it, i.e., k1. We allow k=0, in which case the walk consists of a single vertex and no edges. A walk is closed if the first and last vertices are the same, i.e., v1=vk.

Definition

A trail in a graph is a walk in which all edges are distinct.

For example, the walk A,e1,B,e4,C,e6,A,e2,C is a trail in the first graph above.

Definition

A path in a graph is a walk in which all vertices (and hence all the edges) are distinct.

Definition

A cycle in a graph is a closed walk in which all vertices are distinct, except the first and the last, which are equal.

For example, the walk A,e1,B,e4,C,e6,A is a cycle in the first graph above.

There are also famous special type of trails and cycles:

  • A circuit is a closed trail.
  • A Eulerian circuit is a closed trail that visits every edge exactly once. A graph is called Eulerian if it has an Eulerian circuit.
  • A Hamiltonian cycle is a closed path that visits every vertex exactly once. A graph is called Hamiltonian if it has a Hamiltonian cycle.

The name “Eulerian” comes from the mathematician Leonhard Euler, who studied the famous problem of the Seven Bridges of Königsberg in 1736. It boils down to the question of whether the graph below has an Eulerian circuit:

Here, the vertices are the land masses (top and bottom the two banks of the Pregel river, left and right the islands Kneiphof and Lomse), and the edges are the bridges. It turns out that there is no Eulerian circuit in this graph, because of the following theorem:

An undirected graph is connected if it is nonempty and there is a path between any two vertices, and a directed graph is strongly connected if it is nonempty and there is a (directed) path between any two vertices.

We don't give the proof of the Eulerian circuit theorem, but it boils down to the correctness of a greedy algorithm: Keep finding cycles (where all vertices have even degree, viz. 2, until no edges remain; then glue the cycles together into a circuit). In contrast to Eulerian circuits, Hamiltonian cycles are much harder to find. In fact, it is NP-complete to determine whether a given graph has a Hamiltonian cycle.

Subsection7.1.3Graph isomorphism

The same graph can be represented in many equivalent (isomorphic) ways. For example, it doesn't matter whether we use the set {0,1,3,4,5} or any other five element to represent the complete graph on five vertices. More formally, we introduce the notion of graph isomorphism:

Definition

An isomorphism between directed graphs (V,E),(VA,EA) is a bijection f:VVA between the vertex sets together with, for each pair (v,w) of vertices in V, a bijection gv,w:E(v,w)EA(f(v),f(w)).

For simple graphs, the condition on the edges can be simplified to saying that f preserves the edge relation, i.e., maps connected vertices to connected vertices in the sense that (v,w)E implies (f(v),f(w))EA.

Graphs provide a rich supply of difficult algorithmic problems in computer science. For example, the graph isomorphism problem is to determine whether two graphs are isomorphic. It is not known whether this problem is solvable in polynomial time (feasibly computable with a bounded amount of resources), or whether it is NP-complete (intractable). The subgraph isomorphism problem is to determine whether a simple graph G contains another graph H as a subgraph, i.e., whether there is an injection from the vertex set of H to the vertex set of G such that there is an edge between two vertices in H if and only if there is an edge between the corresponding vertices in G. This is NP-complete.

Subsection7.1.4Adjacency matrices

The main connection between graph theory and linear algebra is that we can represent graphs without multiple edges as adjacency matrices. This works by ordering the vertices as v1,v2,...,vn and forming the n×n matrix A whose i,j is one or zero, depending on whether there is an edge from vi to vj. For example, for the graph that began the section, if we relabel the vertices v1,v2,v3,v4, we get the graph,

v1v2V3v4

whose adjacency matrix is:

AEC0111001110001010BFD.

We can also accommodate graphs with multiple (yet finitely many) edges and loops by using a weighted adjacency matrix, where the (i,j) entry is the number of edges from vertex vi to vertex vj. For example, the adjacency matrix of the graph we started this section with is:

AEC0211001110001011BFD.

Note, however, that we loose some information, by replacing the finite sets E(vi,vj) with their size ai,j.

Not only are adjacency matrices a useful data structure for storing graphs, we can even link matrix multiplication to graph theoretic features, e.g.:

We can also represent undirected graphs as adjacency matrices, by using symmetric matrices. These have real eigenvalues, and we can learn a lot about the graph from the eigenvalues and eigenvectors. That is, however, is a topic for another day.

Adjacency matrices take on a special form when the graph is bipartite, i.e., the vertex set can be partitioned into two sets V1 and V2 such that all edges connect a vertex in V1 to a vertex in V2. In this case, the adjacency matrix can be written as a block matrix:

G0AB0H,

where A is the adjacency matrix of edges going from V1 and V2, while B is that of edges going from V2 to V1. For example, the complete bipartite graph on 3+3 vertices, i.e.,

has the symmetric adjacency matrix:

AEEEEEC000111000111000111111000111000111000BFFFFFD.

This is the smallest bipartite graph that is not planar, i.e., drawable in the plane without intersecting edges. But that is a topic for another day.