pptx - Math/CS

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