SFO
CHAPTER 14
GRAPH ALGORITHMS
LAX
ORD
DFW
ACKNOWLEDGEMENT: THESE SLIDES ARE ADAPTED FROM SLIDES PROVIDED WITH
DATA STRUCTURES AND ALGORITHMS IN JAVA, GOODRICH, TAMASSIA AND
GOLDWASSER (WILEY 2016)
GRAPH
โข A graph is a pair ๐บ = (๐, ๐ธ), where
โข
โข
โข
๐ is a set of nodes, called vertices
๐ธ is a collection of pairs of vertices, called edges
Vertices and edges can store arbitrary elements
โข Example:
โข
โข
A vertex represents an airport and stores the three-letter airport code
An edge represents a flight route between two airports and stores the mileage of the route
SFO
PVD
ORD
LGA
HNL
LAX
DFW
MIA
APPLICATIONS
โข Electronic circuits
โข Printed circuit board
โข Integrated circuit
โข Transportation networks
โข Highway network
โข Flight network
โข Computer networks
โข Local area network
โข Internet
โข Web
โข Databases
โข Entity-relationship diagram
TERMINOLOGY
EDGE AND GRAPH TYPES
โข Edge Types
โข Graph Types
โข Directed edge
โข
โข
โข
โข
โข Directed graph (Digraph)
โข all the edges are directed
โข e.g., route network
ordered pair of vertices (๐ข, ๐ฃ)
first vertex ๐ข is the origin/source
second vertex ๐ฃ is the destination/target
โข all the edges are undirected
โข e.g., flight network
e.g., a flight
โข Undirected edge
โข Weighted graph
โข unordered pair of vertices (๐ข, ๐ฃ)
โข e.g., a flight route
โข all the edges are weighted
โข Weighted edge
โข Numeric label associated with edge
(๐ข, ๐ฃ)
flight
ORD AA 1206
๐ข
802 miles
โข Undirected graph
DFW
๐ฃ
flight
ORD
DFW
route
๐ข
๐ฃ
802 miles
TERMINOLOGY
VERTICES AND EDGES
โข
End points (or end vertices) of an edge
โข
โข
Edges incident on a vertex
โข
โข
โข
๐ has degree 5
Parallel (multiple) edges
โข
โข
๐ and ๐ are adjacent
Degree of a vertex
โข
โข
๐, ๐, and ๐ are incident on ๐
Adjacent vertices
โข
U
b
d
h
X
c
e
W
j
Z
i
g
f
Y
โ and ๐ are parallel edges
Self-loop
โข
a
๐ and ๐ are the endpoints of ๐
V
๐ is a self-loop
Note: A graph with no parallel edges or self loops are
said to be simple. Unless otherwise stated, you should
assume all graphs discussed are simple
TERMINOLOGY
VERTICES AND EDGES
โข
Outgoing edges of a vertex
โข
โข
โข
โข
โข
๐ has in-degree 3
Out-degree of a vertex
โข
d
๐ has out-degree 2
e
W
g
f
Y
j
Z
X
e, g, and ๐ are incoming edges of ๐
In-degree of a vertex
h
b
โ and ๐ are the outgoing edges of ๐
Incoming edges of a vertex
โข
V
i
TERMINOLOGY
PATHS
โข Path
โข
โข
โข
โข
Sequence of alternating vertices and edges
Begins with a vertex
Ends with a vertex
Each edge is preceded and followed by its endpoints
โข Simple path
โข Path such that all its vertices and edges are distinct
โข Examples
โข ๐1 = ๐, ๐, ๐, โ, ๐ is a simple path
โข ๐2 = ๐, ๐, ๐, ๐, ๐, ๐, ๐, ๐, ๐, ๐, ๐ is a path that is not simple
a
U
c
V
b
d
P2
P1
X
e
W
g
f
Y
h
Z
TERMINOLOGY
CYCLES
โข Cycle
โข Circular sequence of alternating vertices and edges
โข Each edge is preceded and followed by its endpoints
โข Simple cycle
โข Cycle such that all its vertices and edges are distinct
โข Examples
a
U
c
โข ๐ถ1 = ๐, ๐, ๐, ๐, ๐, ๐, ๐, ๐, ๐, ๐, ๐ is a simple cycle
โข ๐ถ2 = ๐, ๐, ๐, ๐, ๐, ๐, ๐, ๐, ๐, ๐, ๐, ๐, ๐ is a cycle that is not simple
โข A digraph is called acyclic if it does not contain any cycles
V
b
d
C2
X
e
C1
g
W
f
h
Y
Z
EXERCISE ON TERMINOLOGY
1.
2.
3.
4.
5.
6.
7.
8.
Number of vertices?
Number of edges?
What type of the graph is it?
Show the end vertices of the edge with largest weight
Show the vertices of smallest degree and largest degree
Show the edges incident to the vertices in the above question
Identify the shortest simple path from HNL to PVD
Identify the simple cycle with the most edges
SFO
PVD
ORD
LGA
HNL
LAX
DFW
MIA
EXERCISE
PROPERTIES OF UNDIRECTED GRAPHS
โข Property 1 โ Total degree
ฮฃ๐ฃ ๐๐๐ ๐ฃ =?
โข Property 2 โ Total number of edges
โข In an undirected graph with no selfloops and no multiple edges
๐ โค ๐๐๐๐๐ ๐ต๐๐ข๐๐?
๐ฟ๐๐ค๐๐ ๐ต๐๐ข๐๐? โค ๐
โข Notation
โข ๐
โข ๐
โข deg(๐ฃ)
number of vertices
number of edges
degree of vertex v
Example
๏ฎ ๐ =?
๏ฎ ๐ =?
๏ฎ deg ๐ฃ =?
A graph with given number of
vertices (4) and maximum
number of edges
EXERCISE
PROPERTIES OF UNDIRECTED GRAPHS
โข Property 1 โ Total degree
ฮฃ๐ฃ ๐๐๐ ๐ฃ = 2๐
โข Property 2 โ Total number of edges
โข In an undirected graph with no self-loops and
โข Notation
โข ๐
โข ๐
โข deg(๐ฃ)
number of vertices
number of edges
degree of vertex v
no multiple edges
๐(๐ โ 1)
2
0โค๐
๐โค
Proof: Each vertex can have degree at most
๐โ1
Example
๏ฎ ๐ = 4
๏ฎ ๐ = 6
๏ฎ deg ๐ฃ = 3
A graph with given number of
vertices (4) and maximum
number of edges
EXERCISE
PROPERTIES OF DIRECTED GRAPHS
โข
โข
Property 1 โ Total in-degree and outdegree
ฮฃ๐ฃ ๐๐ โ deg(๐ฃ) =?
ฮฃ๐ฃ ๐๐ข๐ก โ deg ๐ฃ =?
โข
Notation
โข
โข
โข
๐
number of vertices
๐
number of edges
deg(๐ฃ)
degree of vertex v
Property 2 โ Total number of edges
โข
In an directed graph with no self-loops
and no multiple edges
๐ โค ๐๐๐๐๐๐ต๐๐ข๐๐?
๐ฟ๐๐ค๐๐๐ต๐๐ข๐๐? โค ๐
Example
๏ฎ ๐ =?
๏ฎ ๐ =?
๏ฎ deg ๐ฃ =?
A graph with given number of
vertices (4) and maximum
number of edges
EXERCISE
PROPERTIES OF DIRECTED GRAPHS
โข
โข
Property 1 โ Total in-degree and outdegree
ฮฃ๐ฃ ๐๐ โ deg(๐ฃ) = ๐
ฮฃ๐ฃ ๐๐ข๐ก โ deg ๐ฃ = ๐
โข
Notation
โข
โข
โข
๐
number of vertices
๐
number of edges
deg(๐ฃ)
degree of vertex v
Property 2 โ Total number of edges
โข
In an directed graph with no self-loops
and no multiple edges
๐โค๐ ๐โ1
0โค๐
Example
๏ฎ ๐ = 4
๏ฎ ๐ = 12
๏ฎ deg ๐ฃ = 6
A graph with given number of
vertices (4) and maximum
number of edges
TERMINOLOGY
CONNECTIVITY
โข Given two vertices ๐ข and ๐ฃ, we say ๐ข
๐ข
reaches ๐ฃ, and that ๐ฃ is reachable from
๐ข, if there exists a path from ๐ข to ๐ฃ. In
an undirected graph reachability is
symmetric
โข A graph is connected if there is a path
๐ฃ
Connected graph
๐ข and ๐ฃ are reachable
๐ข
๐ฃ
between every pair of vertices
โข A digraph is strongly connected if there
every pair of vertices is reachable
Connected digraph
๐ข and ๐ฃ are not mutually reachable
TERMINOLOGY
SUBGRAPHS
โข A subgraph ๐ป of a graph ๐บ is a graph
Subgraph
whose vertices and edges are subsets of ๐บ,
respectively
โข A spanning subgraph of ๐บ is a subgraph
Spanning subgraph
that contains all the vertices of ๐บ
โข A connected component of a graph ๐บ is
a maximal connected subgraph of ๐บ
Non connected graph with two
connected components
TERMINOLOGY
TREES AND FORESTS
โข A forest is a graph without cycles
โข A (free) tree is connected forest
โข This definition of tree is different from
Tree
the one of a rooted tree
โข The connected components of a
forest are trees
Forest
SPANNING TREES AND FORESTS
โข
A spanning tree of a connected graph
is a spanning subgraph that is a tree
โข
A spanning tree is not unique unless the
graph is a tree
โข
Spanning trees have applications to the
design of communication networks
Graph
Spanning tree
GRAPH ADT
โข Vertices and edges are
lightweight objects and
store elements
โข Although the ADT is
specified from the graph
object, we often have
similar functions in the
Vertex and Edge objects
EXERCISE ON ADT
1.
2.
3.
4.
5.
outgoingEdges(๐๐๐)
incomingEdges(๐๐๐)
6.
7.
8.
9.
insertVertex(๐๐โ)
insertEdge(๐๐๐, ๐๐ฃ๐, 1200)
removeVertex(๐๐๐)
removeEdge( ๐๐๐ค, ๐๐๐ )
outDegree(๐๐๐)
endVertices( ๐๐๐, ๐๐๐ )
opposite(๐๐๐ค, ๐๐๐ค, ๐๐๐ )
SFO
PVD
ORD
LGA
HNL
LAX
DFW
MIA
PVD
ORD
EDGE LIST STRUCTURE
Edge List
{ORD, PVD, 849}
{ORD, DFW, 802}
LGA
Vertex List
ORD
LGA
{LGA, PVD, 142}
PVD
{LGA, MIA, 1099}
DFW
{DFW, LGA, 1387}
{DFW, MIA, 1120}
DFW
MIA
MIA
โข An edge list can be stored in a list or a
map/dictionary (e.g. hash table)
โข Vertex object
โข element
โข reference to position in vertex sequence
โข Edge object
โข
โข
โข
โข
element
origin vertex object
destination vertex object
reference to position in edge sequence
EXERCISE
EDGE LIST STRUCTURE
โข Construct the edge list for the following graph
x
u
w
y
z
v
ASYMPTOTIC PERFORMANCE
EDGE LIST STRUCTURE
โข ๐ vertices, ๐ edges
โข No parallel edges
โข No self-loops
Space
Edge List
Edge List
?
getEdge ๐ข, ๐ฃ ,
outDegree ๐ฃ ,
outgoingEdges ๐ฃ
?
insertVertex(๐ฅ),
insertEdge(๐ข, ๐ฃ, ๐ค),
removeEdge(๐)
?
removeVertex(๐ฃ)
?
Vertex List
{ORD, PVD, 849}
ORD
{ORD, DFW, 802}
LGA
{LGA, PVD, 142}
PVD
{LGA, MIA, 1099}
DFW
{DFW, LGA, 1387}
MIA
{DFW, MIA, 1120}
ASYMPTOTIC PERFORMANCE
EDGE LIST STRUCTURE
โข ๐ vertices, ๐ edges
โข No parallel edges
โข No self-loops
Space
Edge List
Edge List
๐(๐ + ๐)
getEdge ๐ข, ๐ฃ ,
outDegree ๐ฃ ,
outgoingEdges ๐ฃ
๐(๐)
insertVertex(๐ฅ),
insertEdge(๐ข, ๐ฃ, ๐ค),
removeEdge(๐)
๐(1)
removeVertex(๐ฃ)
๐(๐)
Vertex List
{ORD, PVD, 849}
ORD
{ORD, DFW, 802}
LGA
{LGA, PVD, 142}
PVD
{LGA, MIA, 1099}
DFW
{DFW, LGA, 1387}
MIA
{DFW, MIA, 1120}
PVD
ORD
LGA
ADJACENCY LIST STRUCTURE
DFW
Adjacency List
ORD {ORD, PVD} {ORD, DFW}
LGA {LGA, PVD} {LGA, MIA} {LGA, DFW}
โข
Adjacency Lists associate
vertices with their edges
(in addition to edge list!)
โข
Each vertex stores a list of
incident edges
PVD {PVD, ORD} {PVD, LGA}
DFW {DFW, ORD} {DFW, LGA} {DFW, MIA}
MIA {MIA, LGA} {MIA, DFW}
MIA
โข
โข
List of references to incident
edge objects
Augmented edge object
โข
Stores references to associated
positions in incident adjacency
lists
EXERCISE
ADJACENCY LIST STRUCTURE
โข Construct the adjacency list for the following graph
x
u
a
y
z
v
ASYMPTOTIC PERFORMANCE
ADJACENCY LIST STRUCTURE
โข ๐ vertices, ๐ edges
โข No parallel edges
โข No self-loops
Space
getEdge ๐ข, ๐ฃ
Adjacency List
Edge List
ORD {ORD, PVD}
{ORD, DFW}
LGA
{LGA, PVD}
{LGA, MIA}
PVD
{PVD, ORD}
{PVD, LGA}
DFW {DFW, ORD}
{DFW, LGA}
{LGA, DFW}
?
?
outDegree ๐ฃ ,
insertVertex(๐ฅ),
insertEdge(๐ข, ๐ฃ, ๐ค),
removeEdge(๐)
?
outgoingEdges(๐ฃ),
removeVertex(๐ฃ)
?
MIA
{MIA, LGA}
{MIA, DFW}
{DFW, MIA}
ASYMPTOTIC PERFORMANCE
ADJACENCY LIST STRUCTURE
โข ๐ vertices, ๐ edges
โข No parallel edges
โข No self-loops
Space
getEdge ๐ข, ๐ฃ
outDegree ๐ฃ ,
insertVertex(๐ฅ),
insertEdge(๐ข, ๐ฃ, ๐ค),
removeEdge(๐)
outgoingEdges(๐ฃ),
removeVertex(๐ฃ)
Adjacency List
Edge List
ORD {ORD, PVD}
{ORD, DFW}
LGA
{LGA, PVD}
{LGA, MIA}
PVD
{PVD, ORD}
{PVD, LGA}
DFW {DFW, ORD}
{DFW, LGA}
{LGA, DFW}
๐(๐ + ๐)
๐ min deg ๐ฃ , deg ๐ข
๐(1)
๐ deg ๐ฃ
MIA
{MIA, LGA}
{MIA, DFW}
{DFW, MIA}
ADJACENCY MAP STRUCTURE
โข We can store augmenting incidence structures in maps, instead of lists. This is
called an adjacency map.
โข What would this do to the complexities?
โข If it is implemented as a hash table?
โข If it is implemented as a red-black tree?
ADJACENCY MATRIX STRUCTURE
0
1
2
3
4
0
โ
โ
{0, 2}
{0, 3}
โ
1
โ
โ
{1, 2}
{1, 3}
{1, 4}
2
{0, 2}
{1, 2}
โ
โ
โ
3
{0, 3}
{1, 3}
โ
โ
{3, 4}
4
โ
{1, 4}
โ
{3, 4}
โ
2:PVD
0:ORD
1:LGA
3:DFW
4:MIA
โข Adjacency matrices store references
to edges in a table
(in addition to the edge list)
โข Augment vertices with integer keys
(often done in all graph
implementations!)
EXERCISE
ADJACENCY MATRIX STRUCTURE
โข Construct the adjacency matrix for the following graph
x
u
a
y
z
v
ASYMPTOTIC PERFORMANCE
ADJACENCY MATRIX STRUCTURE
โข ๐ vertices, ๐ edges
โข No parallel edges
โข No self-loops
Space
outDegree ๐ฃ ,
outgoingEdges ๐ฃ
0
1
2
3
4
0
โ
โ
{0, 2}
{0, 3}
โ
1
โ
โ
{1, 2}
{1, 3}
{1, 4}
?
2
{0, 2}
{1, 2}
โ
โ
โ
?
3
{0, 3}
{1, 3}
โ
โ
{3, 4}
4
โ
{1, 4}
โ
{3, 4}
โ
Edge List
getEdge(๐ข, ๐ฃ),
insertEdge(๐ข, ๐ฃ, ๐ค),
removeEdge(๐)
?
insertVertex(๐ฅ),
removeVertex(๐ฃ)
?
ASYMPTOTIC PERFORMANCE
ADJACENCY MATRIX STRUCTURE
โข ๐ vertices, ๐ edges
โข No parallel edges
โข No self-loops
Space
outDegree ๐ฃ ,
outgoingEdges ๐ฃ
0
1
2
3
4
0
โ
โ
{0, 2}
{0, 3}
โ
1
โ
โ
{1, 2}
{1, 3}
{1, 4}
๐(๐2 )
2
{0, 2}
{1, 2}
โ
โ
โ
๐ ๐
3
{0, 3}
{1, 3}
โ
โ
{3, 4}
4
โ
{1, 4}
โ
{3, 4}
โ
Edge List
getEdge(๐ข, ๐ฃ),
insertEdge(๐ข, ๐ฃ, ๐ค),
removeEdge(๐)
๐(1)
insertVertex(๐ฅ),
removeVertex(๐ฃ)
๐ ๐2
ASYMPTOTIC PERFORMANCE
โข ๐ vertices, ๐ edges
โข No parallel edges
โข No self-loops
Edge
List
Adjacency
List
Adjacency
Matrix
Space
๐(๐ + ๐)
๐(๐ + ๐)
๐ ๐2
outgoingEdges(๐ฃ)
๐(๐)
๐ deg ๐ฃ
๐ ๐
getEdge(๐ข, ๐ฃ)
๐ ๐
๐ min deg ๐ฃ , deg ๐ค
๐ 1
insertEdge(๐ข, ๐ฃ, ๐ค),
eraseEdge(๐)
๐(1)
๐(1)
๐ 1
insertVertex(๐ฅ)
๐ 1
๐ 1
๐(๐2 )
removeVertex(๐ฃ)
๐(๐)
๐ deg ๐ฃ
๐ ๐2
© Copyright 2026 Paperzz