14. Graphs
CAUTION - CHAPTER UNDER CONSTRUCTION!
This chapter was last updated on April 21, 2025.
Graphs are discrete mathematical structures that have many applications in a diversity of fields including chemistry, network analysis, algorithms, and social sciences.
Key terms and concepts covered in this chapter:
-
Graphs
-
Undirected graphs
-
Directed graphs
-
Weighted graphs
-
-
Creating new graphs from old graphs
-
Subgraphs
-
Unions and intersections of graphs
-
-
Graph isomorphism
-
Graph coloring
-
Connectivity of graphs
-
Eulerian circuits
-
Hamiltonian circuits
-
Shortest-path problems
-
Dijkstra’s algorithm
-
Traveling Salesperson Problem (TSP)
-
-
14.1. Introduction and Basic Definitions
A graph consists of a set of vertices (also called nodes ) and a set of edges, where each edge connects either two different vertices or one vertex to itself.
-
For each edge, its endpoints are the vertices that it connects. The edge is said to be incident with each endpoint, and to connect the endpoints.
-
If an edge has only one endpoint, it is called a loop.
-
If two or more edges connect the same endpoints (or endpoint if the edges are loops), the graph is called a multigraph. Two edges that have the same endpoints are called parallel edges.
-
Two vertices are adjacent if they are the endpoints of at least one edge. Adjacent vertices are also called neighbors.
-
The degree of a vertex \(v\) is the number of edges in the graph \(G\) containing the vertex \(v.\)
-
An isolated vertex is a vertex that is not an endpoint of any edge.
-
A simple graph is a graph that has no loops and does not have two or more edges that connect the same endpoints.
14.2. Simple Graphs
A simple graph is a graph that has no loops and no parallel edges (that is, no two edges can connect the same endpoints.) This means that each edge is determined by its two distinct endpoints. This allows us to give a relatively simple but formal set-theoretic definition of "simple graph". This formal definition can be useful if you need to implement a simple graph in code. Graphs discussed in this textbook are assumed to be simple unless stated otherwise.
14.2.1. A Formal Definition
A simple graph \(G=\left(V,\ E\right)\) is an ordered pair consisting of a set \(V\) of objects called vertices (also called nodes ) and a set \(E\) of objects called edges . An edge \(e\in\ E\) is a set of the form \(e=\left\{x,y\right\}\), where the vertices \(x\) and \(y\) are two different elements of \(V\). The two vertices \(x\) and \(y\) in the edge \(e=\left\{x,y\right\}\) are said to be adjacent or connected or neighbors , and \(x\) and \(y\) are called the endpoints of the edge \(e\).
Notice that the sum of all the degrees \(d\left(A\right)+\ d\left(B\right)\ +\ d\left(C\right)+\ \ d\left(D\right)\ \ +d\left(E\right)+\ d\left(F\right) + d\left(G\right)=14\), which is is twice the number of edges, \(2 \cdot \left|E\right|=7\). In fact, this is true for any undirected graph with finitely many edges as long as any loops are counted twice. This result is called the Handshaking Theorem. A formal proof of the Handshaking Lemma can be written using mathematical induction on the predicate \(P(n):\) "If graph \(G\) has \(n\) edges, then the sum of the degrees of the vertices of \(G\) is equal to \(2n.\)"
A useful consequence of this to keep in mind is that the sum of the degrees of a graph is always even. |
14.3. Directed Graphs
The main focus of this chapter will be undirected simple graphs, but we will briefly discuss directed graphs in this section.
A directed graph (or digraph ) is a graph in which the edges are directed from one vertex to another vertex. Each edge has an initial vertex \(u\) and a terminal index \(v;\) the edge is drawn as an arrow pointing from \(u\) to \(v.\)
The out-degree of a vertex \(w\) is the number of edges that have \(w\) as the initial index. The in-degree of a vertex \(w\) is the number of edges that have \(W\) as the terminal index.
14.3.1. Simple Directed Graphs
We can give a formal set-theoretic definition of simple directed graph as well. To indicate the directed edges, ordered pairs of vertices are used instead of 2-element sets.
A simple directed graph \(G=\left(V,\ E\right)\) is an ordered pair consisting of a set \(V\) of objects called vertices (or nodes ) and a set \(E\) of objects called edges . A directed edge \(e\in\ E\) is an ordered pair of the form \(e=\left(x,y\right)\), where the vertices \(x\) and \(y\) are two different elements of \(V\). Vertex \(x\) is the initial vertex of \(e\) and vertex \(y\) is the terminal vertex of edge \(e\).
14.4. Examples of Simple Graphs
In this section presents several classes of graphs.

The complete graph \(K_n\) is the simple graph with \(n\) vertices such that any two vertices are adjacent, that is, every pair of vertices are the endpoints of an edge. The image shows \(K_{4},\) the complete graph on 4 vertices. Click here to see images of \(K_{n}\) for the positive integers that are less than or equal to \(12.\)

The n-cube \(Q_{n}\) can be described as the graph that has vertex set consisting of the \(2^{n}\) bitstrings of length \(n,\) and edges such that two vertices are adjacent if and only if the bitstrings differ in exactly one bit position. The image shows the three graphs \(Q_{1},\) \(Q_{2},\) and \(Q_{3};\) these graphs can be used as a way to represent the power sets of sets that have \(1,\) \(2,\) and \(3\) elements, respectively. Notice that \(Q_{2}\) can be drawn as a square and that \(Q_{3}\) can be represented as a cube in \(3\)-dimensional space (or by a drawing of a cube in a \(2\)-dimensional plane.)
A bipartite graph is a simple graph whose set of vertices can be partitioned into two disjoint nonempty sets such that every vertex is in exactly one of the two sets and every edge has one endpoint in each of the two sets. One way to think of a bipartite graph is that each vertex can be assigned one of two colors so that every edge must connect vertices of different colors. Notice that \(Q_{1},\) \(Q_{2},\) and \(Q_{3}\) are all examples of bipartite graphs (Question: Is \(Q_{n}\) a bipartite graph for every natural number \(n?\) Why or why not?)

This image shows the graph \(K_{2,3}\) and is another example of a bipartite graph.
Notice that \(K_{2,3}\) has an additional property:
Every
pair of vertices \(\{a, b \}\) with \(a\) in the set of \(2\) "upper" vertices and \(b\) in the set of \(3\) "lower" vertices are the endpoints of an edge. A bipartite graph that has this additional property is called a
complete bipartite graph.
In general, the symbol \(K_{m,n}\) represents the complete bipartite graph that has two disjoint sets of vertices, one of cardinality \(|m|\) and the other of cardinality \(|n|,\) such that every pair of vertices that come from the different sets are joined by an edge. Notice that \(Q_{1} = K_{1,1}\) and \(Q_{2} = K_{2,2}\) are complete bipartite graphs, but that \(Q_{3}\) is not a complete bipartite graph because, for example, there is no edge joining \(000\) and \(111.\)
NOTE: The phrase
"complete bipartite"
needs to be read as a single term used to indicate that a bipartite graph has all the edges it can possibly have. For example, \(K_{2,3}\) is a bipartite graph such that if you tried to enlarge it by inserting an additional edge into the graph, that edge would join either the \(2\) "upper" vertices, \(2\) of the "lower" vertices, or \(2\) vertices that are already joined; in this sense, \(K_{2,3}\) is "complete" as a bipartite graph. \(K_{2,3}\) is not a "complete graph" in the sense of the earlier example in this section. In fact, since a "complete graph" must contain an edge for every pair of distinct vertices, the only graph that can be both a "complete graph" and a "complete bipartite graph" is \(Q_{1} = K_{2} = K_{1,1}.\) Mathematicians recycle and reuse a lot of words… .
14.5. Representing Simple Graphs
In addition to the vertex-edge drawing, a simple graph can be represented in other ways that are more useful for computing.
First, recall that if \(u\) is a vertex of a simple graph, then vertex \(v\) is said to be adjacent to \(u\) if and only if \(\{u, v \}\) are the endpoints of an edge of the graph.
One way to represent a simple graph is by using an adjacency list. This list can be written as a table, where each row has two columns. In each row, the entry in the first column is a single vertex \(v\) and the entry in the second column is a list of all vertices of the graph that are adjacent to \(v.\)
Another way to represent a simple graph is by using an adjacency matrix. The adjacency matrix of a simple graph represents the graph in table form, and contains an entry for each pair of vertices. For each vertex of the graph, there is a row and also a column. If vertices \(u\) and \(v\) are adjacent (that is, connected by some edge), then the adjacency matrix will contain a \(1\) in the position that corresponds to the row for \(u\) and the column for \(v,\) otherwise the matrix contains a \(0\) at that postion. The next example may help make this more clear.
14.6. Weighted Graphs
In some applications, each edge of a graph has a weight, which is some nonnegative number. The weight could represent the physical distance between the two endpoint nodes, or could represent the cost to travel or transmit data between the endpoint nodes.
You can use an adjacency matrix to describe a weighted graph, but instead of using a \(1\) to represent that there is an edge between two vertices you place the the weight of the edge in the correct position of the adjacency matrix, as shown in the following example.
14.7. Creating New Graphs From Old Graphs
Given a set of one or more graphs, there are several ways to create new graphs using the graphs in the set.
14.7.1. Subgraphs
Given a simple graph \(G,\) you can form a
subgraph
\(H\) by choosing a subset of the vertices of \(G\) along with a subset of the edges of \(G\) such that each edge has endpoints in the set of vertices you chose. That is, \(H\) is a subgraph of \(G\) if \(H\) is a graph such that every vertex of \(H\) is a vertex of \(G\) and every edge of \(H\) is a vertex of \(G.\)
More formally, \(H = (V_{H}, E_{H})\) is a subgraph of \(G = (V,E)\) if and only if all three of the following statements are True: \(V_{H} \subseteq V,\) \(E_{H} \subseteq E,\) and for every edge \(e \in E_{H}\) the endpoints of \(e\) are in \(V_{H}.\)
If \(v\) is a vertex of \(G,\) we denote by \(G-v\) , the subgraph obtained from \(G\) by removing the vertex \(v\) along with all edges in \(E\) that have \(v\) as an endpoint.
The image shows a graph \(G\), and the subgraph \(G-d\) formed by removing the vertex \(d\).

In the same way, you can obtain a subgraph by removing multiple vertices along with the edges associated with the removed vertices. The subgraph obtained is called the subgraph induced by removing those vertices.
14.7.2. Unions and Intersections Of Graphs
Given two simple graphs \(G_{1}\) and \(G_{2}\), you can form the union of the graphs by taking the union of the two sets of vertices to get a new set of vertices, and taking the union of the two sets of edges to get a new set of edges. Notice that any edge that is in both graphs will only appear once in the new graph because you took the union of the sets of edges, that is, you can’t create parallel edges by forming the union.
In the same way, you can form the intersection of two simple graphs by taking the intersection of the two sets of vertices to get a new set of vertices, and taking the intersection of the two sets of edges to get a new set of edges.
14.8. Graph Isomorphism
Recall that a graph is determined by its set of vertices and how those vertices are connected by edges, but not the drawing you use to represent the graph.
Sometimes, different graphs may be essentially the same graph, as in the next example.
A one-to-one correspondence between the set of vertices of two simple graphs that preserves adjacency is called a graph isomorphism, and the two graphs are said to be isomorphic. Informally, you can think of two isomorphic graphs as a pair of graphs where one graph can be relabeled and/or reshaped to obtain the other graph (That is, the two graphs are the same graph but have drawings that are labeled and/or shaped differently.)
14.9. Graph Coloring
In some contexts, it can be useful to partition either the set of vertices or the set of edges of a graph into disjoint subsets to make it easier to understand the graph and the network it represents. This act of partitioning is usually referred to as "coloring" since using different colors can make it easy to see and interpret the properties of the partition when the graph is drawn. Notice that you could instead create the partition by assigning labels like "group 1," "group 2," and so on, to each vertex (or edge.)

For example, the image shows a graph called the Petersen graph with its vertex set partitioned into 3 subsets so that each edge’s endpoints are in two different subsets of the partition (That is, each edge’s endpoints have different colors.)
Image credit:
"Petersen_graph_3-coloring.svg"
by Д.Ильин. The copyright holder of this work has released this work into the public domain. This applies worldwide.
The next example discusses an application of vertex coloring.

In other contexts, it is more appropriate to use edge coloring. That is, each edge of the graph is assigned a color so that the set of edges is partitioned into disjoint subsets.
For example, the graph in the image shows that the complete bipartite graph \(K_{4,4}\) can be partitioned as a union of 3 disjoint graphs called
forests
(Forests are defined later in this textbook, in the
Trees
chapter.)
Image credit:
"K44 arboricity.svg"
by David Eppstein. The copyright holder of this work has released this work into the public domain. This applies worldwide.
14.10. Connectivity, Eulerian Graphs, and Hamiltonian Graphs
A walk on a graph \(G=\left(V,E\right)\) is a finite, non-empty, alternating sequence of vertices and edges of the form, \(v_0e_1v_1e_2\ldots e_nv_n\), with vertices \(v_i\in V\) and edges \(e_i\in E\).
We will focus on simple graphs. For simple graphs, there is at most one edge joining adjacent vertices, so we can omit the edges from the sequence and instead write \(v_0v_1\ldots v_n.\)
-
A trail is a walk that does not repeat an edge. That is, all edges in a trail are distinct.
-
A path is a trail that does not repeat a vertex.
-
The distance between two vertices, \(u\) and \(v\), denoted \(d(u,v)\), is the number of edges in a shortest path connecting them.
-
A cycle is a non-empty trail in which the only repeating vertices are the beginning and ending vertices, \(v_0=v_n\).
A graph is connected if there is a path from each vertex to every other vertex.
14.10.1. Eulerian Graphs
An Euler path on a graph is a path that uses each edge of the graph exactly once.
An Euler circuit (also called an Eulerian trail ) is a closed trail containing each edge of the graph \(G\) exactly once and returning to the start vertex. A graph with an Euler circuit is considered Eulerian or is said to be an Eulerian graph .
In the following, the first graph is Eulerian. The sequence of edges \(e_1 e_2 e_3 e_4 e_5 e_6 e_7\) describes an Euler circuit. The second graph is not an Eulerian graph. Convince yourself of this fact by looking at all necessary trails or closed trails.

The following are useful characterizations of graphs with Euler circuits and Euler paths and are due to Leonhard Euler
14.10.2. Hamiltonian Graphs
A cycle in a graph \(G\), is said to be a Hamiltonian cycle if every vertex, except for the starting and ending vertex, is visited exactly once.
A graph is Hamiltonian , or said to be a Hamiltonian graph , if it contains a Hamiltonian cycle.
The following graph is Hamiltonian and shows a Hamiltonian cycle \(ABCDA\), highlighted, while the second graph is not Hamiltonian.

While we have the Euler Theorem to tell us which graphs are Eulerian or not, there are no comparable simple criteria to determine if graphs are Hamiltonian or not. We do have the following sufficient criterion due to Paul Dirac.
14.11. Finding A Shortest Path: Dijkstra’s Algorithm
COMING SOON!
14.11.1. Traveling Salesperson Problem (TSP)
COMING SOON!
14.12. Additional topics will be added to this chapter soon!
-
Traveling Salesperson Problem (TSP)
-
Algorithms for Graphs
-
Shortest-path algorithms (Dijkstra’s and Floyd’s algorithms)
-
Transitive closure (Floyd’s algorithm)
-
Topological sort
MORE TO COME!