Graph Algorithms
Timothy Vismor
June 11, 2011
Abstract
๎e network structure of many physical systems is represented by mathematical entities known as graphs. ๎is document examines several of the
computational algorithms of graph theory most relevant to network analysis.
Copyright © 1990 - 2011 Timothy Vismor
CONTENTS
CONTENTS
Contents
1 Graph Nomenclature
1.1 Subgraph . . . . . . . . . . .
1.2 Directed Graph . . . . . . . .
1.3 Undirected Graph . . . . . .
1.4 Paths and Connected Graphs
1.5 Cyclic Graph . . . . . . . . .
1.6 Acyclic Graph . . . . . . . .
.
.
.
.
.
.
5
5
5
6
7
7
7
2 Modeling Graphs
2.1 Representing Graphs as Lists . . . . . . . . . . . . . . . . . . .
2.2 Representing Graphs as Adjaceny Matrices . . . . . . . . . . .
2.3 Representing Sparse Graphs as Adjaceny Lists . . . . . . . . .
9
10
10
11
3 Abstract Data Types for Graphs
3.1 Adjacency List . . . . . .
3.2 Reduced Graph . . . . . .
3.3 List . . . . . . . . . . . .
3.4 Mapping . . . . . . . . .
13
13
14
14
15
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
4 Creating Adjacency Lists
16
5 Depth First Search
5.1 Recursive Depth First Search . .
5.2 Non-recursive Depth First Search
5.3 Depth First Spanning Trees . . .
5.4 Depth First Traversal . . . . . . .
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
17
18
19
20
20
6 Graph Structure Analysis
6.1 Connected Components of a Graph . . . . . . .
6.2 Isolated Vertex Detection . . . . . . . . . . . .
6.3 Cycle Detection . . . . . . . . . . . . . . . . .
6.3.1 Detecting Cycles in Undirected Graphs
6.3.2 Detecting Cycles in Directed Graphs . .
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
21
22
22
22
23
24
7 Determining the Degree of Each Vertex
2
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
25
LIST OF FIGURES
LIST OF ALGORITHMS
8 Vertex Elimination
8.1 Eliminating a Single Vertex . . . . . . . . . .
8.2 Eliminating Many Vertices . . . . . . . . . . .
8.3 Initializing Minimum Degree Vertex Tracking
8.4 Maintaining the Reduced Graph . . . . . . .
8.5 Querying the Reduced Graph State . . . . . .
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
26
26
27
28
29
30
Example of a Directed Graph . . . . . . . . . .
Example of an Undirected Graph . . . . . . . .
Example of Cycles in a Digraph . . . . . . . . .
Example of an Acyclic Digraph . . . . . . . . .
Example of a Directed Tree . . . . . . . . . . .
Example of a Rooted Free Tree . . . . . . . . .
Adjacency List of Directed Graph in Figure 1 .
Adjacency List of Undirected Graph in Figure 2
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
6
7
8
8
9
9
12
12
Vertex Degree Summary for Figure 1 . . . . . . . . . . . . . .
6
List of Figures
1
2
3
4
5
6
7
8
List of Tables
1
List of Algorithms
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
Create Adjacency List of Undirected Graph . . . . . . . . . .
Map Vertices To Labeling Order . . . . . . . . . . . . . . . .
Create Ordered Adjacency List . . . . . . . . . . . . . . . .
Depth First Search . . . . . . . . . . . . . . . . . . . . . . .
Recursive Depth First Search . . . . . . . . . . . . . . . . .
Depth First Search With Recursion Removed . . . . . . . . .
Non-recursive DFS Vertex Visitation . . . . . . . . . . . . .
Depth First Traversal . . . . . . . . . . . . . . . . . . . . . .
Extract the Connected Components of a Graph . . . . . . .
Isolated Vertex Detection . . . . . . . . . . . . . . . . . . .
Cycle Detection in Undirected Graphs . . . . . . . . . . . .
Cycle Detection in Directed Graphs . . . . . . . . . . . . . .
Determine Vertex Degree Using Adjacency List . . . . . . .
Determine Vertex Degree of Directed Graph Using Edges . .
Determine Vertex Degree of Undirected Graph Using Edges
3
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
16
16
17
18
18
19
19
21
22
23
23
24
25
25
26
LIST OF ALGORITHMS
16
17
18
19
20
21
LIST OF ALGORITHMS
Eliminate a Vertex from a Graph . . . . . . .
Initialize Minimum Degree Vertex Tracking .
Increase the Degree of a Vertex . . . . . . . .
Decrease the Degree of a Vertex . . . . . . . .
Remove a Vertex from the Reduced Graph . .
Determine if a Vertex is in the Reduced Graph
4
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
27
28
29
29
30
30
1 GRAPH NOMENCLATURE
1 Graph Nomenclature
Data structures and algorithms derived from graph theory form the basis for
many network analysis techniques. A brief review of the most relevant aspects
of computational graph theory follows. An excellent introduction to the subject is found in Aho, Hopcroft, and Ullman(1983) [1]. Horowitz and Sahni
(1978) [2] cover similar material in a more disjointed fashion. Even (1980) [3]
provides a more theoretical examination of the subject.
๎e graph ๐บ = (๐, ๐ธ) consists of a nite set of vertices ๐(๐บ) = {๐ฃ1 , ๐ฃ2 , โฏ , ๐ฃ๐ }
and a nite set of edges ๐ธ(๐บ) = {๐1 , ๐2 , โฏ , ๐๐ }.
Each edge corresponds to a pair of vertices. If edge ๐ corresponds to the
vertex pair (๐ฃ, ๐ค), then ๐ is incident upon vertices ๐ฃ and ๐ค. ๎e number of
vertices in ๐(๐บ) is indicated by |๐|. ๎e number of edges in ๐ธ(๐บ) is indicated by
|๐ธ|.
A labeling (or ordering) of the graph ๐บ is a mapping of the set ๎ฝ1, 2, โฏ , ||๐ ||๎พ
onto ๐(๐บ).
๎e usual convention for drawing a graph, is to represent each vertex as a
dot and each edge as a line connecting two dots. If the graph is directed, an
arrow is superimposed on each edge to show its orientation.
1.1 Subgraph
A graph ๐บ๎
= ๎ท๐ ๎
, ๐ธ ๎
๎ธ is a subgraph of ๐บ if the following conditions apply.
โข ๐ ๎
๎ท๐บ๎
๎ธ โ ๐(๐บ).
โข ๐ธ ๎
(๐บ๎
) consists of edges (๐ฃ, ๐ค) in ๐ธ(๐บ) where both ๐ฃ and ๐ค are in ๐ ๎
(๐บ๎
).
If ๐ธ ๎
(๐บ๎
) consists of all the edges in ๐ธ(๐บ) for which the second condition
holds, then ๐บ๎
is an induced subgraph of ๐บ. An induced subgraph of ๐บ that is
not a proper subset of any other connected subgraph of ๐บ is called a connected
component of ๐บ.
1.2 Directed Graph
If the edges of ๐บ are ordered pairs, then ๐บ is a directed graph. In a directed
graph, (๐ฃ, ๐ค) โ (๐ค, ๐ฃ).
A directed graph is often referred to as a digraph. If edge ๐ of a digraph
is represented by (๐ฃ, ๐ค), then ๐ is an edge from ๐ฃ to ๐ค. Vertex ๐ค is adjacent
to vertex ๐ฃ. Vertex ๐ฃ is not adjacent to vertex ๐ค unless the edge (๐ค, ๐ฃ) also
5
1.3 Undirected Graph
1 GRAPH NOMENCLATURE
2
4
1
6
3
5
Figure 1: Example of a Directed Graph
exists in ๐บ. ๎e number of vertices adjacent to vertex ๐ฃ is the degree of ๐ฃ. Indegree indicates the number of edges incident upon ๐ฃ. Out-degree indicates the
number of edges emanating from ๐ฃ.
Figure 1 depicts a directed graph with six vertices and ten edges. In the
gure, the arrowheads indicate the direction of the edge.
Table 1 summarizes the degree of each vertex in Figure 1.
Table 1: Vertex Degree Summary for Figure 1
Vertex
Degree
In-Degree
Out-Degree
1
2
3
4
5
6
2
4
4
4
3
3
1
1
2
3
2
1
1
3
2
1
1
2
1.3 Undirected Graph
If the edges of ๐บ are unordered pairs, ๐บ is a undirected graph. In an undirected
graph, (๐ฃ, ๐ค) = (๐ค, ๐ฃ). If edge ๐ of an undirected graph is represented by (๐ฃ, ๐ค),
then ๐ฃ is adjacent to ๐ค and ๐ค is adjacent to ๐ฃ. ๎e terminology โundirected
graphโ is somewhat cumbersome. In this document, undirected graphs are often
referred to as just โgraphsโ.
Figure 2 depicts an undirected graph with six vertices and eight edges. ๎e
fact that the graph is undirected is indicated by the absence of directionality
arrows.
6
1.4 Paths and Connected Graphs
1 GRAPH NOMENCLATURE
2
4
1
6
3
5
Figure 2: Example of an Undirected Graph
1.4 Paths and Connected Graphs
A path is a sequence of edges, e.g. (๐ฃ1 , ๐ฃ2 ), (๐ฃ2 , ๐ฃ3 ), โฏ , (๐ฃ๐โ1 , ๐ฃ๐ ). ๎is path connects vertices ๐ฃ1 and ๐ฃ๐ . ๎e path may also be represented by the vertex sequence ๐ฃ1 , ๐ฃ2 , ๐ฃ3 , โฏ , ๐ฃ๐โ1 , ๐ฃ๐ . ๎e path begins at vertex ๐ฃ1 passes through vertices ๐ฃ2 , ๐ฃ3 , โฏ , ๐ฃ๐โ1 and ends at vertex ๐ฃ๐ .
๎e number of edges that comprise a path is its length. A path is simple if
all of the vertices on a path are distinct (with the possible exception of the rst
and last vertices).
If some path connects each pair of vertices in ๐บ, then ๐บ is a connected graph.
Connected digraphs are classi ed as either weakly connected or strongly connected. A strongly connected digraph has a directed path that connects all the
vertices in the graph. All the vertices of a strongly connected digraph must have
an in-degree of at least one.
A weakly connected digraph has has an undirected path that connects all the
vertices in the graph. All the vertices of a weakly connected digraph must have
either an in-degree or out-degree of at least one.
1.5 Cyclic Graph
A cycle is a simple path that begins and ends at the same vertex and has a length
of at least one. We refer to any graph that contains a cycle as a cyclic graph.
Figure 3 illustrates the cycles that are present in the directed graph of Figure 1.
1.6 Acyclic Graph
A directed graph with no cycles is called directed acyclic graph or a DAG for
short. An acyclic digraph has at least one vertex with an out-degree of zero.
7
1.6 Acyclic Graph
1 GRAPH NOMENCLATURE
2
4
1
6
3
5
Figure 3: Example of Cycles in a Digraph
2
4
1
6
3
5
Figure 4: Example of an Acyclic Digraph
Figure 4 shows a variant of the directed graph of Figure 1 that contains no
cycles. You will observe that vertex 4 has an out-degree of zero.
A directed tree is a connected DAG with the following properties:
โข ๎ere is one vertex, called the root, which no edges enter.
โข All vertices except the root have one entering edge.
โข ๎ere is a unique path from each vertex to the root.
A DAG consisting of one or more trees is called a forest. If the graph
๐น = (๐, ๐ธ) is a forest and the edge (๐ฃ, ๐ค) is in ๐ธ(๐น), vertex ๐ฃ is the parent of
๐ค and vertex ๐ค is the child of ๐ฃ. If there is a path from ๐ฃ to ๐ค, then vertex ๐ฃ
is an ancestor of ๐ค and vertex ๐ค is a descendent of ๐ฃ. A vertex with no proper
descendants is a leaf. A vertex ๐ฃ and its descendants form a subtree of ๐น . ๎e
vertex ๐ฃ is the root of this subtree. ๎e depth of vertex ๐ฃ is the length of the
path from the root to ๐ฃ. ๎e height of vertex ๐ฃ is the length of the longest path
from ๐ฃ to a leaf. ๎e height of a tree is the height of its root. ๎e level of vertex
๐ฃ is its depth subtracted from the height of the tree.
Figure 5 depicts a directed tree. Its root is vertex 1. Its leaves are the set of
vertices ๐ฟ(๐บ) = {3, 4, 6, 8, 9}.
8
2
MODELING GRAPHS
1
2
3
5
4
6
7
8
9
Figure 5: Example of a Directed Tree
1
2
3
5
4
6
7
8
9
Figure 6: Example of a Rooted Free Tree
An undirected, connected, acyclic graph is called a free tree or an undirected
tree. A rooted free tree is a free tree in which one vertex has been designated
as the root. A directed tree is converted into a rooted free tree by discarding
the orientation of the edges. A rooted free tree is converted into a directed tree
by orienting each edge away from the root. ๎e terminology which applies to
directed trees also applies to rooted free trees.
Figure 6 depicts the directed tree of Figure 5 converted into a rooted free
tree.
2 Modeling Graphs
A number of di๏ฌerent data structures provide useful representations of a graph
๐บ. Di๏ฌerent representations of ๐บ often lend themselves to speci c applications.
Indeed, the e๏ฌciency of graph algorithms often relies on the manner in which
a graph is represented. For this reason, it may prove necessary to transform
a graph from one representation to another to implement a complex series of
graph operations. It can be shown that converting ๐บ between representations
requires no more than ๐ ๎ท|๐|2 ๎ธ operations.
9
2.1 Representing Graphs as Lists
2
MODELING GRAPHS
2.1 Representing Graphs as Lists
๎e most obvious data structure for representing a graph arises directly from its
de nition. ๐บ is described by two simple lists:
โข A vertex list ๐(๐บ), and
โข An edge list ๐ธ(๐บ) which represents each edge as a pair of vertices.
๎is data structure often forms the basis of the interface between the user
and graph algorithms. ๎e reasons are twofold:
โข For many people, it is the most โnaturalโ way to describe the connections
in a graph.
โข It is a relatively e๏ฌcient way to represent sparse graphs.
As an example, consider the directed graph of Figure 1. It has six vertices.
Its vertex list is simply
๐(๐บ)๐๐๐๐๐๐โ = {1, 2, 3, 4, 5, 6}
๎e directed graph in Figure 1 has ten edges. Its edge list follows.
๐ธ(๐บ)๐๐๐๐๐๐โ = {(1, 3), (2, 1)(2, 3), (2, 4), (3, 2), (3, 5), (4, 6), (5, 4), (6, 4), (6, 5)}
๎e corresponding undirected graph shown in Figure 2 has the same six vertices.
๐(๐บ)๐ข๐๐๐๐๐๐๐ก๐๐ = {1, 2, 3, 4, 5, 6}
However, it has 16 edges.
๐ธ(๐บ)๐ข๐๐๐๐๐๐๐ก๐๐ = {(1, 2), (1, 3), (2, 1)(2, 3), (2, 4), (3, 1), (3, 2), (3, 5),
(4, 2), (4, 5), (4, 6), (5, 3), (5, 4), (5, 6), (6, 4), (6, 5)}
2.2 Representing Graphs as Adjaceny Matrices
An alternate representation of ๐บ is known as an adjacency matrix. In this data
structure, a |๐| × |๐| matrix ๐ is established such that
๐๐๐ =
1
๎ฏ0
when vertex ๐ is adjacent to vertex ๐
when vertex ๐ is not adjacent to vertex ๐
10
(1)
2.3 Representing Sparse Graphs as Adjaceny Lists
2
MODELING GRAPHS
๎e storage required to implement an adjacency matrix is proportional to |๐|2 .
When ๐บ is represented as an adjacency matrix, the best time complexity you
can expect for graph algorithms is ๐ ๎ท|๐|2 ๎ธ. ๎is is the time required to access
each element of the matrix exactly one time.
Once again consider the directed graph of Figure 1. Its adjacency matrix
follows.
๐ด๐๐๐๐๐๐โ
โ
โ
โ
=โ
โ
โ
โ
โ
0
1
0
0
0
0
0
0
1
0
0
0
1
1
0
0
0
0
0
1
0
0
1
1
0
0
1
0
0
1
0
0
0
1
0
0
โ
โ
โ
โ
โ
โ
โ
โ
๎e adjacency matrix of the undirected version of the graph (Figure 2) is as
follows.
๐ด๐ข๐๐๐๐๐๐๐ก๐๐
โ
โ
โ
=โ
โ
โ
โ
โ
0
1
1
0
0
0
1
0
1
1
0
0
1
1
0
0
1
0
0
1
0
0
1
1
0
0
1
1
0
1
0
0
0
1
1
0
โ
โ
โ
โ
โ
โ
โ
โ
Observe that the adjacency matrix of an undirected graph is symmetric.
2.3 Representing Sparse Graphs as Adjaceny Lists
An adjacency list is a data structure for representing adjacency relationships in
sparse graphs. ๎e adjacency list of ๐บ is actually a set of |๐| linked lists. Each
vertex ๐ฃ is the head of a list. Vertices adjacent to ๐ฃ form the body of each list. If
a vertex is not adjacent to any other vertices its list is empty.
๎e storage required to implement an adjacency list is proportional to |๐| +
|๐ธ|. When ๐บ is represented as an adjacency list, the best time complexity you
can expect for graph algorithms is ๐ (|๐| + |๐ธ|). ๎e time required to access each
vertex and each edge exactly one time. An algorithm whose time complexity is
๐ (|๐| + |๐ธ|) is sometimes referred to as linear in the size of ๐บ.
Figure 7 depicts the adjacency list for the directed graph in Figure 1.
Figure 8 depicts the adjacency list for the undirected graph in Figure 2.
11
2.3 Representing Sparse Graphs as Adjaceny Lists
Vertex
2
MODELING GRAPHS
Adjacent Vertices
1
3
eol
2
1
3
3
2
5
eol
4
6
eol
5
4
eol
6
4
5
eol
4
eol
Figure 7: Adjacency List of Directed Graph in Figure 1
Vertex
Adjacent Vertices
1
2
3
2
1
3
3
2
5
4
2
5
6
eol
4
eol
5
6
eol
3
4
6
eol
4
5
eol
eol
Figure 8: Adjacency List of Undirected Graph in Figure 2
12
3 ABSTRACT DATA TYPES FOR GRAPHS
3 Abstract Data Types for Graphs
Graph algorithms in this document are described using an abstract data type
paradigm. ๎at is, data sets and operators are speci ed, but the actual data
structures used to implement them remain unde ned. Any data structure that
e๏ฌciently satis es the constraints imposed in this section is suited for the job.
All signals emitted by operators de ned in this section are used to navigate
through data, not to indicate errors. Error processing is intentionally omitted
from the algorithms in this document. ๎e intent is to avoid clutter that obscures the nature of the algorithms.
3.1 Adjacency List
An adjacency list, ๐ด, is a data type for representing adjacency relationships of the
sparse graph ๐บ = (๐, ๐ธ). Adjacency lists are described in more detail in Section
2.3 of this document.
An adjacency list is typically stored in a dynamic data structure that identi es the edge from vertex ๐ to vertex ๐ as an ordered pair of vertex labels (๐, ๐).
Descriptive information is usually associated with each edge.
More speci cally, the following operations are supported on an adjacency
list ๐ด:
โข Insert adds an arbitrary edge (๐, ๐) to ๐ด. If edge (๐, ๐) is not already in
the list, insert signals a successful insertion.
โข Get retrieves an arbitrary edge (๐, ๐) from ๐ด. When edge (๐, ๐) is in ๐ด, get
signals a successful lookup.
โข Scan permits sequential access to all edges incident upon vertex ๐. Vertex
scans are bounded. More speci cally, a vertex scan nds all edges (๐, ๐)
such that ๐๐๐๐ โค ๐ โค ๐๐๐๐ฅ . When scan nds edge (๐, ๐), it returns ๐ .
When a vertex scan has exhausted all entries in its range, a nished signal
is emitted.
A scan has two support operations: push and pop. A push suspends
the scan at its current position. A pop resumes a suspended scan. ๎e
push and pop operations permit scans to be nested.
โข Put updates the information associated with an arbitrary edge (๐, ๐) in ๐ด.
๎e algorithms assume that read operations (get and scan) make edge
information available in a bu๏ฌer (this bu๏ฌer is usually denoted by the symbol
13
3.2 Reduced Graph
3 ABSTRACT DATA TYPES FOR GRAPHS
๐). Update operations (insert and put) modify the description of an edge
based on the current contents of the communication bu๏ฌer.
Algorithms for creating adjacency lists are examined in Section 4.
3.2 Reduced Graph
A reduced graph, ๐บ๎
= (๐ ๎
, ๐ธ ๎
), is a data structure that supports the systematic
elimination of all vertices from the graph ๐บ = (๐, ๐ธ). ๎e vertices of the reduced graph are denoted as ๐ ๎
(๐บ๎
) and its edges as ๐ธ ๎
(๐บ๎
). A crucial attribute
of the reduced graph is e๏ฌcient identi cation of the vertex in ๐ ๎
(๐บ๎
) with the
minimum degree.
A reduced graph supports the following operations:
โข Increase_degree increases the degree of vertex ๐ฃ in ๐ ๎
(๐บ๎
) by one.
โข Decrease_degree decreases the degree of vertex ๐ฃ in ๐ ๎
(๐บ๎
) by one.
โข Remove excises vertex ๐ฃ from $๐ ๎
(๐บ๎
) .
โข In_graph tests to see whether vertex ๐ฃ is in ๐ ๎
(๐บ๎
).
โข Minimum_degree nds the vertex ๐ฃ in ๐ ๎
(๐บ๎
) with the smallest degree.
๎e topic of vertex elimination and e๏ฌcient techniques for facilitating the
elimination of many vertices are examined in detail in Section 8 of this document.
3.3 List
A simple list ๐ฟ is an ordered set of elements. If the set {๐1 , โฏ , ๐๐ , ๐๐+1 , โฏ , ๐๐ }
represents ๐ฟ, then the list contains ๐ elements. Element ๐1 is the rst item on
the list and ๐๐ is the last item on the list. Element ๐๐ precedes ๐๐+1 and element
๐๐+1 follows ๐๐ . Element ๐๐ is at position ๐ in ๐ฟ. Descriptive information may
accompany each item on a list. Lists associated with graph algorithms support
the following operations:
โข Link adds an element ๐ฅ to a list at position ๐. Inserting element ๐ฅ position
๐ results in an updated list: {๐1 , โฏ , ๐๐โ1 , ๐ฅ, ๐๐ , ๐๐+1 , โฏ , ๐๐ } An insertion at
position ๐๐๐ appends ๐ฅ to the end of the list.
โข Unlink removes the element at position ๐ from the list. Deleting element
๐ results in the list {๐1 , โฏ , ๐๐โ1 , ๐๐+1 , โฏ , ๐๐ }.
14
3.4 Mapping
3 ABSTRACT DATA TYPES FOR GRAPHS
โข Find looks for an element on the list and returns its position. If the
element is not a member of the list, ๐๐๐ is returned.
โข First returns the position of the rst item on the list. When the list is
empty, ๐๐๐ is returned.
โข Next returns position ๐ + 1 on the list if position ๐ is provided. When ๐๐
is the last item on the list, ๐๐๐ is returned.
โข Previous returns position ๐ โ 1 on the list if position ๐ is provided. If ๐
is one, ๐๐๐ is returned.
A linked list refers to a list implementation that does not require its members to reside in contiguous storage locations. In this environment, an e๏ฌcient
implementation of the previous operator dictates the use of a doubly linked
list.
Communicating with a simple list is analogous to adjacency list communication. Read operations (๏ฌnd, ๏ฌrst, next, and previous) make list information available in a bu๏ฌer. Update operations (link, unlink) modify the
list based on the current contents of the bu๏ฌer.
If it is necessary to distinguish discrete elements in the communication bu๏ฌer,
the structure notation of the C programming language is used. For example,
consider a set of edges ๐ธ maintained in a list whose communication bu๏ฌer is
๐. Each edge in ๐ธ is de ned by the pair of vertices (๐ฃ, ๐ค) that constitute its
endpoints. In this situation, the endpoints of edge ๐ are referenced as ๐.๐ฃ and
๐.๐ค.
3.4 Mapping
A mapping ๐ relates elements of its domain ๐ to elements of its range ๐ as follows.
๐(๐) = ๐
A mapping resides in a data structure that supports two operations:
โข Map links an element ๐ in the range of ๐ to an arbitrary element ๐ in the
domain of ๐, i.e. sets ๐(๐) to ๐.
โข Evaluate evaluates the mapping ๐ for an arbitrary element ๐ in its domain, i.e. returns ๐(๐).
15
4 CREATING ADJACENCY LISTS
Algorithm 1: Create Adjacency List of Undirected Graph
๐ = first ( ๐ )
w h i l e ๐ โ ๐๐๐
insert ( ๐ด, ๐ฃ, โ๐๐๐๐๐ )
๐ = next ( ๐, ๐ )
๐ = first ( ๐ธ )
w h i l e ๐ โ ๐๐๐
insert ( ๐ด, ๐.๐ฃ, ๐.๐ค )
insert ( ๐ด, ๐.๐ค, ๐.๐ฃ )
๐ = next ( ๐ธ, ๐ )
Algorithm 2: Map Vertices To Labeling Order
๐=0
๐ = first ( ๐ )
w h i l e ๐ โ ๐๐๐
๐=๐+1
map ( ๐, ๐ฃ, ๐ )
๐ = next ( ๐, ๐ )
4 Creating Adjacency Lists
An undirected graph ๐บ = (๐, ๐ธ) is represented by the sets ๐(๐บ) and ๐ธ(๐บ). If
these sets are maintained as simple lists where the bu๏ฌer ๐ฃ communicates with
the vertex list and ๐ communicates with the edge list, Algorithm 1 creates an
adjacency list ๐ด for ๐บ.
๎e rst loop in Algorithm 1 creates a header for each vertex. ๎is operation
is not strictly necessary given the current data de nition; however, it is often
required in practical implementations.
You should observe that the algorithm inserts each edge in ๐ธ(๐บ) into the adjacency list twice: once in the stated direction and once in the reverse direction.
If ๐บ is a directed graph, the reverse insertion is inappropriate (i.e. omit the nal
insert statement in Algorithm 1). It should be apparent that the algorithm has
a time complexity of ๐ (|๐| + |๐ธ|) if all operators have time complexity of ๐(1).
It is often bene cial to create an adjacency list based on an alternate labeling
of the vertices of ๐บ. ๎is operation requires a mapping ๐ of the integers from
1 to |๐| onto the set ๐(๐บ). Algorithm 2 maps ๐(๐บ) to the order in which it is
enumerated.
16
5
DEPTH FIRST SEARCH
Algorithm 3: Create Ordered Adjacency List
๐=0
๐ = first ( ๐ )
w h i l e ๐ โ ๐๐๐
๐=๐+1
map ( ๐, ๐ฃ, ๐ )
insert ( ๐ด, ๐ฃ, โ๐๐๐๐๐ )
๐ = next ( ๐, ๐ )
๐ = first ( ๐ธ )
w h i l e ๐ โ ๐๐๐
๐ = evaluate ( ๐, ๐.๐ฃ )
๐ = evaluate ( ๐, ๐.๐ค )
insert ( ๐ด, ๐, ๐ )
insert ( ๐ด, ๐, ๐ )
๐ = next ( ๐ธ, ๐ )
Algorithm 3 combines these two procedures. It labels ๐(๐บ) according to its
order of enumeration and creates an adjacency list based on this labeling.
If map and evaluate have constant time complexity, the overall algorithm
retains its linear complexity.
Note. In a practical implementation, map will insert the pair (๐, ๐) into
a data structure that is optimized for searching and evaluate will look up
๐ in this data structure. E๏ฌcient operations of this sort have a time complexity of ๐ ๎ท log2 ๐ ๎ธ. ๎erefore, adjacency list creation will have complexity
๐ ๎ท |๐| log2 |๐| + |๐ธ| log2 |๐| ๎ธ.
5 Depth First Search
Given a graph ๐บ = (๐, ๐ธ) and a vertex ๐ โ ๐(๐บ), a depth- rst search nds all
the vertices ๐ฃ โ ๐(๐บ) for which there is a path from ๐ to ๐ฃ. In other words, a
depth- rst search nds the connected component of ๐บ that contains ๐. It does
so by starting at ๐ and systematically searching ๐บ until no more vertices can be
reached. After a vertex is reached by the search, it is referred to as visited. A
vertex is explored after all adjacent vertices are visited. ๎e vertex ๐ that anchors
the search is referred to as its root.
When a depth- rst search reaches vertex ๐ฃ, it immediately suspends its exploration of ๐ฃ and explores any unvisited vertex ๐ค adjacent to ๐ฃ. ๎e exploration
of ๐ฃ resumes only after the exploration of ๐ค has nished. ๎e order in which
17
5.1 Recursive Depth First Search
5
DEPTH FIRST SEARCH
Algorithm 4: Depth First Search
w h i l e next ( ๐ ) โ ๐๐๐๐๐ โ๐๐
map ( ๐พ, ๐ฃ, ๐ข๐๐ฃ๐๐ ๐๐ก๐๐ )
๐๐๐๐กโ = ๐๐๐ = 0
dfs ( ๐ )
Algorithm 5: Recursive Depth First Search
dfs ( ๐ฃ )
๐๐๐ = ๐๐๐ + 1
map ( ๐พ, ๐ฃ, ๐๐๐ )
map ( ๐ฟ, ๐ฃ, ๐๐๐๐กโ )
๐๐๐๐กโ = ๐๐๐๐กโ + 1
f o r all vertices ๐ค adjacent to ๐ฃ
i f evaluate ( ๐พ, ๐ค ) is ๐ข๐๐ฃ๐๐ ๐๐ก๐๐
map ( ๐, ๐ค, ๐ฃ )
dfs ( ๐ค )
๐๐๐๐กโ = ๐๐๐๐กโ โ 1
a depth- rst search visits a graph ๐บ is a generalization of the order in which a
preorder traversal visits the vertices of a tree. If ๐บ is a tree, a depth- rst search
produces a preorder traversal of ๐บ.
5.1 Recursive Depth First Search
Algorithm 4 performs a depth- rst search of ๐บ rooted at ๐. ๎e counters ๐๐๐
and ๐๐๐๐กโ are global. It relies upon the recursive procedure dfs. Algorithm 5
sketches the implementation of the dfs procedure.
Observe that Algorithm 5 produces three mappings as it performs the DFS:
the depth rst ordering ๐พ , the vertex depth map ๐ฟ , and the predecessor map
๐. ๎ese mappings provide useful information concerning the structure of the
graph. However, only ๐พ is actually required by the algorithm. It is initialized
with all vertices marked as ๐ข๐๐ฃ๐๐ ๐๐ก๐๐ and updated as each node is visited.
๎e time complexity of a depth- rst search is linear in the size of ๐บ, i.e.
๐ (|๐| + |๐ธ|). To achieve this linear time complexity, the implementation of the
adjacency list is crucial. More speci cally, some indication of the most recently
visited neighbor (call it vertex ๐ค) must be maintained for each active vertex
๐ฃ. ๎is permits the scan of ๐ฃโs adjacency list to resume at the point where it
18
5.2 Non-recursive Depth First Search
5
DEPTH FIRST SEARCH
Algorithm 6: Depth First Search With Recursion Removed
dfs ( ๐ )
visit ( ๐, ๐๐๐๐๐๐๐๐ก )
๐ฃ=๐
next edge
f o r all vertices ๐ค adjacent to ๐ฃ
i f evaluate ( ๐พ, ๐ค ) is ๐ข๐๐ฃ๐๐ ๐๐ก๐๐
๐๐๐๐กโ = ๐๐๐๐กโ + 1
visit ( ๐ค, ๐ฃ )
๐ฃ=๐ค
goto next edge
if ๐ฃโ ๐
๐ฃ = evaluate ( ๐, ๐ฃ )
๐๐๐๐กโ = ๐๐๐๐กโ โ 1
goto next edge
Algorithm 7: Non-recursive DFS Vertex Visitation
visit ( ๐ฃ, ๐๐๐๐๐๐๐๐ ๐ ๐๐ )
map ( ๐ฟ, ๐ฃ, ๐๐๐๐กโ )
map ( ๐, ๐ฃ, ๐๐๐๐๐๐๐๐ ๐ ๐๐ )
๐๐๐ = ๐๐๐ + 1
map ( ๐พ, ๐ฃ, ๐๐๐ )
was suspended when the exploration of ๐ค is nished. If you have to rescan the
adjacency list of ๐ฃ to pick up where you left o๏ฌ, linearity is lost.
5.2 Non-recursive Depth First Search
Since the procedure described in Section 5.1 has a depth of recursion of |๐| in the
worst case, Algorithm 5 may have unacceptable execution characteristics (such
as stack over ow) for large graphs. Algorithm 6 removes the recursion from the
dfs function.
๎e vertex visitation procedure visit is straightforward and is speci ed in
Algorithm 7.
Observe that vertex visitation (Algorithm 7) generates the three mappings
(๐พ , ๐ฟ , and ๐) that were produced by the recursive procedure. Note that the predecessor mapping ๐ is required by the unwound dfs procedure. It was not
19
5.3 Depth First Spanning Trees
5
DEPTH FIRST SEARCH
required by the recursive implementation since equivalent information is implicitly retained on the stack during recursion.
๎e computational complexity of this unwound procedure is similar to that
of the recursive implementation.
When implementing non-recursive depth rst search, donโt forget that the
dfs function of Algorithm 6 is imbedded in the overall procedure de ned by
Algorithm 4.
See Section 5.1 for more information on these issues.
5.3 Depth First Spanning Trees
Any edge (๐ฃ, ๐ค) that leads to the discovery of an unvisited vertex during a depthrst search is referred to as a tree edge of ๐บ. Collectively, the tree edges of ๐บ
form a depth- rst spanning tree of ๐บ. Tree edges are detected as follows:
โข In Algorithm 5, the recursive depth- rst search of Section 5.1, only the
tree edges will cause map(๐, ๐ค, ๐ฃ) to execute.
โข In Algorithm 6, the non-recursive depth- rst search of Section 5.2, only
tree edges cause visit(๐ค, ๐ฃ) to execute.
If ๐บ is a directed graph, an edge that connects a vertex to one of its ancestors
in the spanning tree is referred to as a back edge. An edge that connects a vertex
to one of its descendants in the spanning tree is referred to as a forward edge. If
๐ค is neither an ancestor nor descendant of ๐ฃ, then (๐ฃ, ๐ค) is called a cross edge.
If ๐บ is an undirected graph, the depth- rst spanning tree simpli es as follows:
โข Back edges and forward edges are not distinguished.
โข Cross edges do not exist.
๎erefore, only two types of edge are de ned for undirected graphs. We will
call them tree edges and back edges.
5.4 Depth First Traversal
Following the lead of Horowitz and Sahni (1978) [2], a distinction is made
between a depth- rst search (Sections 5.1 and 5.2) and a depth- rst traversal.
Given a graph ๐บ = (๐, ๐ธ), a series of depth- rst searches that visit all the vertices
in ๐(๐บ) is referred to as a depth- rst traversal. If ๐บ is a connected graph, a
20
6 GRAPH STRUCTURE ANALYSIS
Algorithm 8: Depth First Traversal
๐ = first ( ๐ )
w h i l e ๐ โ ๐๐๐
map ( ๐พ, ๐ฃ, ๐ข๐๐ฃ๐๐ ๐๐ก๐๐ )
๐ = next ( ๐, ๐ )
๐๐๐ = 0
๐ = first ( ๐ )
w h i l e ๐ โ ๐๐๐
i f evaluate ( ๐พ, ๐ฃ ) is ๐ข๐๐ฃ๐๐ ๐๐ก๐๐
๐๐๐๐กโ = 0
dfs ( ๐ฃ )
๐ = next ( ๐, ๐ )
single depth- rst search produces a depth- rst traversal. Otherwise, a depthrst traversal will discover all of the connected components of ๐บ.
Algorithm 8 determines a depth- rst traversal of ๐บ. It assumes the bu๏ฌer ๐ฃ
is used to communicate with the vertex list.
๎e time complexity of a depth- rst traversal is linear if dfs is linear.
6 Graph Structure Analysis
A depth- rst search can be used to preprocess a graph or it can be directly embedded in a more complex algorithm. In the latter case, the action taken when
a vertex is visited depends on the algorithm in which the search is embedded.
In the former case, the normal objective of the search is to gather information
about the structure of the graph (as seen from the vantage point of the search).
Recall that Algorithms 5 and 6 gathered three bits of structural information
as it proceeded:
โข ๎e depth- rst labeling ๐พ(๐ฃ) is the order in which ๐ฃ was visited during the
search. Depth- rst numbers range from 1 to |๐|. ๎e root is mapped to
one.
โข ๎e predecessor (or parent) mapping ๐(๐ฃ) identi es the vertex from which
๐ฃ was discovered during the search. ๎e root has no parent.
โข ๎e depth mapping ๐ฟ(๐ฃ) is the length of the path from ๐ฃ to its root in the
depth- rst spanning tree of ๐บ.
21
6.1 Connected Components of a Graph
6 GRAPH STRUCTURE ANALYSIS
Algorithm 9: Extract the Connected Components of a Graph
๐ = first ( ๐ )
w h i l e ๐ โ ๐๐๐
map ( ๐พ, ๐ฃ, ๐ข๐๐ฃ๐๐ ๐๐ก๐๐ )
๐ = next ( ๐, ๐ )
๐๐๐ = 0
๐ = first ( ๐ถ )
w h i l e ๐ โ ๐๐๐
i f evaluate ( ๐พ, ๐ฃ ) is ๐ข๐๐ฃ๐๐ ๐๐ก๐๐
๐๐๐๐กโ = 0
dfs ( ๐ฃ )
๐ = next ( ๐ถ, ๐ )
Subsequent sections provide algorithmic examples of structural information
about a graph that is obtained by making small alterations to the depth- rst
search and depth- rst traversal algorithms.
6.1 Connected Components of a Graph
If ๐ถ(๐บ) โ ๐(๐บ), a similar procedure (Algorithm 9) extracts a set of components
from ๐บ whose roots are de ned by ๐ถ(๐บ). It assumes that ๐ถ(๐บ) and ๐(๐บ) are
maintained in simple lists (Section 3.3 examines the list data type).
6.2 Isolated Vertex Detection
Isolated vertices are detected during a depth- rst traversal of a graph. ๎e detection scheme is based on the observation that an isolated vertex processed by dfs
increases the depth- rst number by exactly one. Algorithm 10 implements this
observation in the main loop of a depth- rst traversal (Algorithm 8). It assumes
the bu๏ฌer ๐ฃ provides communication with the vertex list.
Since the isolated vertex test is an O(1) operation, it does not increase the
complexity of a depth- rst traversal.
6.3 Cycle Detection
It can be shown that each back edge detected during a depth- rst search of the
graph ๐บ = (๐, ๐ธ) corresponds to a cycle in ๐บ. Recall from Section 5.3 that a
back edge connects a vertex to one of its ancestors in the spanning tree.
22
6.3 Cycle Detection
6
GRAPH STRUCTURE ANALYSIS
Algorithm 10: Isolated Vertex Detection
๐๐๐ = 0
๐ = first ( ๐ )
w h i l e ๐ โ ๐๐๐
i f evaluate ( ๐พ, ๐ฃ ) is ๐ข๐๐ฃ๐๐ ๐๐ก๐๐
๐๐๐๐กโ = 0
๐ = ๐๐๐
dfs ( ๐ฃ )
i f ๐๐๐ i s ๐ + 1
๏ฉ๏ณ๏ฏ๏ฌ๏ก๏ด๏ฅ๏ค ๏ถ๏ฅ๏ฒ๏ด๏ฅ๏ธ
๐ = next ( ๐, ๐ )
Algorithm 11: Cycle Detection in Undirected Graphs
next edge
f o r all vertices ๐ค adjacent to ๐ฃ
i f evaluate ( ๐พ, ๐ค ) is ๐ข๐๐ฃ๐๐ ๐๐ก๐๐
๐๐๐๐กโ = ๐๐๐๐กโ + 1
visit ( ๐ค, ๐ฃ )
๐ฃ=๐ค
else
๏ฃ๏น๏ฃ๏ฌ๏ฅ ๏ค๏ฅ๏ด๏ฅ๏ฃ๏ด๏ฅ๏ค
goto next edge
A minor modi cation of the depth- rst search will produce a test for cycles.
We will examine the case of cycle detection in undirected graphs rst. It is
simpler.
6.3.1 Detecting Cycles in Undirected Graphs
Algorithm 11 implements a cycle test for undirected graphs that is illustrated
on a fragment of Algorithm 6, the non-recursive dfs procedure.
Since the cycle test is an ๐(1) operation, it does not increase the complexity
of the depth- rst search.
23
6.3 Cycle Detection
6
GRAPH STRUCTURE ANALYSIS
Algorithm 12: Cycle Detection in Directed Graphs
next edge
f o r all vertices ๐ค adjacent to ๐ฃ
i f evaluate ( ๐พ, ๐ค ) is ๐ข๐๐ฃ๐๐ ๐๐ก๐๐
๐๐๐๐กโ = ๐๐๐๐กโ + 1
visit ( ๐ค, ๐ฃ )
๐ฃ=๐ค
else
i f evaluate ( ๐พ, ๐ฃ ) > evaluate ( ๐พ, ๐ค ) and
evaluate ( ๐พ, ๐ ) < evaluate ( ๐พ, ๐ค )
๏ฃ๏น๏ฃ๏ฌ๏ฅ ๏ค๏ฅ๏ด๏ฅ๏ฃ๏ด๏ฅ๏ค
goto next edge
6.3.2 Detecting Cycles in Directed Graphs
Testing for cycles in directed graphs is still a matter of looking for back edges.
However, the process is complicated by the fact that the edges of a digraph that
are not part of its spanning forest are not necessarily back edges either. ๎e
following observations permit the forward edges, cross edges, and back edges of
a depth- rst search to be distinguished e๏ฌciently.
It can be shown that the forward edges of a depth- rst search connect vertices with low depth- rst numbers to vertices with high depth- rst numbers.
๎erefore, an edge (๐ฃ, ๐ค) of ๐บ that is not part of its spanning forest is a forward
edge when ๐พ(๐ฃ) < ๐พ(๐ค).
Conversely, (๐ฃ, ๐ค) is a back edge or cross edge of ๐บ when ๐พ(๐ฃ) > ๐พ(๐ค).
Distinguishing between back edges and cross edges relies on the observation
that a cross edge connects the current spanning tree to a spanning tree that was
explored at an earlier stage of a depth- rst traversal. Since the root ๐ of the
current spanning tree must have a larger depth- rst number than any vertex in
any previously processed tree in the spanning forest, the condition ๐พ(๐) > ๐พ(๐ค)
must hold for a cross edge (๐ฃ, ๐ค).
๎erefore, an edge (๐ฃ, ๐ค) of ๐บ that is not part of its spanning forest is a back
edge when the following conditions apply: ๐พ(๐ฃ) > ๐พ(๐ค) and ๐พ(๐) < ๐พ(๐ค).
๎is observation leads to Algorithm 12 for detecting cycles in a directed
graph, it is a modi cation to Algorithm 6, the non-recursive depth- rst search.
Obviously, the cycle test does not increase the complexity of the algorithm.
24
7
DETERMINING THE DEGREE OF EACH VERTEX
Algorithm 13: Determine Vertex Degree Using Adjacency List
๐ = first ( ๐ )
w h i l e ๐ โ ๐๐๐
map ( ๐, ๐ฃ, 0 )
๐ = next ( ๐, ๐ )
๐ = first ( ๐ )
w h i l e ๐ โ ๐๐๐
f o r all vertices ๐ค adjacent to ๐ฃ
๐ = evaluate ( ๐, ๐ค ) +1
map ( ๐, ๐ฃ, ๐ )
๐ = next ( ๐, ๐ )
Algorithm 14: Determine Vertex Degree of Directed Graph Using Edges
๐ = first ( ๐ )
w h i l e ๐ โ ๐๐๐
map ( ๐, ๐ฃ, 0 )
๐ = next ( ๐, ๐ )
๐ = first ( ๐ธ )
w h i l e ๐ โ ๐๐๐
๐ = evaluate ( ๐, ๐.๐ฃ ) +1
map ( ๐, ๐.๐ฃ, ๐ )
๐ = next ( ๐ธ, ๐ )
7 Determining the Degree of Each Vertex
If the graph ๐บ = (๐, ๐ธ) is represented by its adjacency list ๐ด(๐บ) and vertex list
๐(๐บ), Algorithm 13 develops a mapping ๐ whose domain is ๐(๐บ) and whose
range is the degree of ๐(๐บ). It assumes the bu๏ฌer ๐ฃ provides communication
with the vertex list.
If ๐บ is a directed graph, represented by its vertex list ๐(๐บ) and edge list ๐ธ(๐บ),
Algorithm 14 maps each vertex in ๐(๐บ) to its degree. It is assumed that each
edge is represented by an ordered pair of vertices (๐ฃ, ๐ค) and the bu๏ฌer ๐ provides
communication with the edge list.
If ๐บ is an undirected graph, Algorithm 15 performs the same operation. It
is assumed that each edge is represented by an unordered pair of vertices (๐ฃ, ๐ค).
If all the elementary operations are ๐(1), creating ๐ is linear, i.e. has complexity ๐ (|๐| + |๐ธ|).
25
8
VERTEX ELIMINATION
Algorithm 15: Determine Vertex Degree of Undirected Graph Using Edges
๐ = first ( ๐ )
w h i l e ๐ โ ๐๐๐
map ( ๐, ๐ฃ, 0 )
๐ = next ( ๐, ๐ )
๐ = first ( ๐ธ )
w h i l e ๐ โ ๐๐๐
๐ = evaluate ( ๐, ๐.๐ฃ ) +1
map ( ๐, ๐.๐ฃ, ๐ )
๐ = evaluate ( ๐, ๐.๐ค ) +1
map ( ๐, ๐.๐ค, ๐ )
๐ = next ( ๐ธ, ๐ )
8 Vertex Elimination
Eliminating a vertex ๐ฃ from the graph ๐บ = (๐, ๐ธ) creates a reduced graph ๐บ๎
=
(๐ ๎
, ๐ธ ๎
) with the following characteristics:
โข ๎e set of vertices ๐ ๎
is the subset of ๐(๐บ) that does not contain ๐ฃ.
โข ๎e set of edges ๐ธ ๎
does not contain any edges that were incident upon ๐ฃ.
โข ๎e set of edges ๐ธ ๎
contains edges that connect all vertices in ๐(๐บ) that
were adjacent to ๐ฃ.
In other words, you get ๐บ๎
from ๐บ by
1. Removing vertex ๐ฃ from ๐(๐บ).
2. Removing all edges that were incident upon ๐ฃ from ๐ธ(๐บ).
3. Adding edges to ๐ธ(๐บ) that connect all the vertices that were adjacent to
๐ฃ.
8.1 Eliminating a Single Vertex
Assuming that the graph ๐บ is represented by a list of vertices ๐(๐บ) and a list of
edges ๐ธ(๐บ), Algorithm 16 eliminates vertex ๐ฃ from ๐บ. It assumes the bu๏ฌer ๐
provides communication with the edge list.
๎e operation adjacency_list creates an adjacency list from ๐(๐บ) and
๐ธ(๐บ). In Section 4, Algorithms 1 and Algorithm 3 were presented to solve this
26
8.2 Eliminating Many Vertices
8
VERTEX ELIMINATION
Algorithm 16: Eliminate a Vertex from a Graph
adjacency_list ( ๐, ๐ธ )
f o r all vertices ๐ค adjacent to ๐ฃ
f o r all vertices ๐ง adjacent to ๐ฃ
i f ๐ค โ ๐ง and find ( ๐ธ, ๐ค, ๐ง ) is ๐๐๐
๐.๐ฃ = ๐ค
๐.๐ค = ๐ง
link ( ๐ธ, 1 )
๐ = find ( ๐ธ, ๐ค, ๐ง )
unlink ( ๐ธ, ๐ )
๐ = find ( ๐, ๐ฃ )
unlink ( ๐, ๐ )
problem. New edges created during vertex elimination are arbitrarily inserted
at the beginning of the edge list. For the current purposes, the insertion point
is irrelevant.
8.2 Eliminating Many Vertices
๎e vertex elimination procedure outlined in Algorithm 16 of Section 8.1 is
primarily illustrative. When multiple vertices are eliminated, it is usually quite
ine๏ฌcient to create an adjacency list each time you want to eliminate a vertex
from a graph.
Many of the algorithms discussed in the companion document Matrix Algorithms1 require an e๏ฌcient vertex elimination model that supports the sequential elimination of all vertices ๐(๐บ) from the graph ๐บ = (๐, ๐ธ). ๎e procedures
are often constrained such that each stage of the process eliminates the minimum degree vertex ๐ฃ from the reduced vertex set ๐ ๎
(๐บ๎
). Additionally, the
vertex elimination procedures are often prohibited the modifying of the adjacency list of the graphโs ๐ด(๐บ). Recall from Section 3.2 that vertex elimination
data structures must also support the following operations:
โข Increase_degree increases the degree of vertex ๐ฃ by one.
โข Decrease_degree decreases the degree of vertex ๐ฃ by one.
โข Remove excises vertex ๐ฃ from $๐ ๎
(๐บ๎
) .
1
https://vismor.com/documents/network_analysis/matrix_algorithms/
27
8.3 Initializing Minimum Degree Vertex Tracking 8
VERTEX ELIMINATION
Algorithm 17: Initialize Minimum Degree Vertex Tracking
compute_degree ( ๐ด, ๐, ๐ )
๐ = first ( ๐ )
w h i l e ๐ โ ๐๐๐
๐ฅ=๐ฃ
link ( ๐ฟ, ๐๐๐ )
๐ = next ( ๐, ๐ )
merge_sort ( ๐ฟ, ๐ )
โข In_graph determines whether vertex ๐ฃ is in ๐ ๎
(๐บ๎
).
โข Minimum_degree nds the vertex ๐ฃ in ๐ ๎
(๐บ๎
) with the smallest degree.
Subsequent sections of this document examine the implementation of these
operations.
8.3 Initializing Minimum Degree Vertex Tracking
๎e following data structure is proposed to support e๏ฌcient tracking the minimum degree vertex during sequential vertex elimination:
โข A mapping ๐ between each vertex in ๐(๐บ) and its degree.
โข A list ๐ฟ which orders ๐ ๎
(๐บ๎
) by ascending degree.
Algorithm 17 initializes this data structure for the graph ๐บ based on its adjacency list ๐ด(๐บ) and vertex list ๐(๐บ). Observe that, per the comments in Section
8.2, ๐ด(๐บ) is unchanged by this operation.
๎e procedure compute_degree creates the vertex degree mapping ๐ based
on ๐ด(๐บ) and ๐(๐บ). Algorithm 13 is a candidate for compute_degree.
๎e procedure merge_sort sorts the list ๐ฟ based on the mapping ๐. Its
name suggests that a merge sort may be the appropriate sorting algorithm. ๎e
minimum number of comparisons required to sort a set of ๐ keys is ๐ ln ๐. ๎e
merge sort algorithm is ๐(๐ ln ๐) in both its best and worst case and is particularly suited for use with linked lists. Furthermore, merge sort does not su๏ฌer
from the potentially catastrophic worst case stack growth that is common in recursive sorting algorithms. ๎e depth of recursion of a merge sort is bounded by
log ๐. ๎is implies that sorting 1,000,000 items would require no more than
20 nested function calls. An e๏ฌcient implementation of merge sort needs at
most 4 to 8 bytes of local stack space for each level of recursion.
28
8.4 Maintaining the Reduced Graph
8
VERTEX ELIMINATION
Algorithm 18: Increase the Degree of a Vertex
increase_degree ( ๐ฃ )
๐ = evaluate ( ๐, ๐ฃ ) +1
map ( ๐, ๐ฃ, ๐ )
๐ = ๐ = find ( ๐ฟ, ๐ฃ )
w h i l e ๐ โ ๐๐๐ and ๐ > evaluate ( ๐, ๐ )
๐ = ๐ e x t ( ๐ฟ, ๐ )
link ( ๐ฟ, ๐, ๐ฃ )
unlink ( ๐ฟ, ๐ )
Algorithm 19: Decrease the Degree of a Vertex
decrease_degree ( ๐ฃ )
๐ = evaluate ( ๐, ๐ฃ ) โ1
map ( ๐, ๐ฃ, ๐ )
๐ = ๐ = find ( ๐ฟ, ๐ฃ )
w h i l e ๐ โ ๐๐๐ and ๐ < evaluate ( ๐, ๐ )
๐ = previous ( ๐ฟ, ๐ )
link ( ๐ฟ, ๐, ๐ฃ )
unlink ( ๐ฟ, ๐ )
8.4 Maintaining the Reduced Graph
With the vertex elimination data structure established, maintenance and query
operations are straightforward. Algorithm 18 implements increase_degree.
It assumes the list communication bu๏ฌer is ๐ฅ.
In words, Algorithm 18 increases the degree of ๐ฃ and scans the degree list ๐ฟ
in ascending order until the new position of ๐ฃ is located. It then adds ๐ฃ into ๐ฟ at
this position and deletes ๐ฃ from the old position. In the worst case, the loop in
Algorithm 18 is an ๐ ๎ท|๐ ๎
|๎ธ operation. ๎is degenerate case only occurs when
all vertices in ๐ ๎
(๐บ๎
) have the same degree and ๐ฃ is the rst item in ๐ฟ.
Algorithm 19 implements the decrease_degree operation. It decreases
the degree of ๐ฃ then scans the degree list ๐ฟ in descending order until the new
position of ๐ฃ is located. It then adds ๐ฃ into ๐ฟ at this position and deletes ๐ฃ from
the old position. Once again, Algorithm 19 is an ๐ ๎ท|๐ ๎
|๎ธ in the worse case. In
the average case, it is much closer ๐(1).
๎e remove operator (Algorithm 20) excises ๐ฃ from the degree list ๐ฟ and
makes sure its degree count is less than or equal to zero in the mapping ๐.
29
8.5 Querying the Reduced Graph State
REFERENCES
Algorithm 20: Remove a Vertex from the Reduced Graph
remove ( ๐ฃ )
๐ = find ( ๐ฟ, ๐ฃ )
unlink ( ๐ฟ, ๐ )
map ( ๐, ๐ฃ, 0 )
Algorithm 21: Determine if a Vertex is in the Reduced Graph
in_graph ( ๐ฃ )
i f evaluate ( ๐, ๐ฃ ) > 0
๐ฃ ๏ฉ๏ณ ๏ฉ๏ฎ ๏ง๏ฒ๏ก๏ฐ๏จ
else
๐ฃ ๏ฉ๏ณ ๏ฎ๏ฏ๏ด ๏ฉ๏ฎ ๏ง๏ฒ๏ก๏ฐ๏จ
8.5 Querying the Reduced Graph State
A couple of reduced graph state operations are also speci ed in Section 8.1.
๎eir implementations tend to fall out trivially from the data structures used
for minimum degree vertex tracking.
๎e in_graph operator is realized by the mapping ๐. If the degree of vertex ๐ฃ is greater than zero, then ๐ฃ is in ๐ ๎
(๐บ๎
). Algorithm 21 formalizes this
observation.
In a similar vein, the minimum_degree operation is realized by the list operation ๏ฌrst. By de nition, ๏ฌrst(๐ฟ) gets the minimum degree vertex. Recall
that the minimum degree vertex list ๐ฟ orders the vertices by ascending degree.
References
[1] A. Aho, J. Hopcroft, and J. Ullman, ๎e Design and Analysis of Computer
Algorithms, Addison-Wesley, Reading, Massachusetts, 1983. 5
[2] E. Horowitz and S. Sahni, Fundamentals of Computer Algorithms, Computer
Science Press, Rockville, Maryland, 1978. 5, 20
[3] S. Even, Graph Algorithms, Computer Science Press, Rockville, Maryland,
1980. 5
30
© Copyright 2026 Paperzz