Albert Chan http://www.scs.carleton.ca/~achan School of Computer Science, Carleton University COMP 2002/2402 Introduction to Data Structures and Data Types Version 03.s 11-1 Graphs • Introductions • DFS • BFS • Biconnectivity • DiGraphs • MST • Shortest Paths Albert Chan http://www.scs.carleton.ca/~achan School of Computer Science, Carleton University COMP 2002/2402 Introduction to Data Structures and Data Types Version 03.s 11-2 Graph • A graph is a pair (V, E), where – V is a set of nodes, called vertices – E is a collection of pairs of vertices, called edges – Vertices and edges are positions and store 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 ORD PVD MIA DFW SFO LAX LGA HNL 849 802 1387 1743 1843 1099 1120 1233 337 2555 142
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Albert Chanhttp://www.scs.carleton.ca/~achan
School of Computer Science, Carleton UniversityCOMP 2002/2402 Introduction to Data Structures and Data Types
School of Computer Science, Carleton UniversityCOMP 2002/2402 Introduction to Data Structures and Data Types
Version 03.s11-2
Graph• A graph is a pair (V, E), where
– V is a set of nodes, called vertices– E is a collection of pairs of vertices, called edges– Vertices and edges are positions and store 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
routeORD PVD
MIADFW
SFO
LAX
LGA
HNL
849
802
13871743
1843
10991120
1233
337
2555
142
Albert Chanhttp://www.scs.carleton.ca/~achan
School of Computer Science, Carleton UniversityCOMP 2002/2402 Introduction to Data Structures and Data Types
Version 03.s11-3
ORD PVDflight
AA 1206
ORD PVD849miles
Edge Types• Directed edge
– ordered pair of vertices (u,v)– first vertex u is the origin– second vertex v is the destination– e.g., a flight
• Undirected edge– unordered pair of vertices (u,v)– e.g., a flight route
• Directed graph– all the edges are directed– e.g., route network
• Undirected graph– all the edges are undirected– e.g., flight network
Albert Chanhttp://www.scs.carleton.ca/~achan
School of Computer Science, Carleton UniversityCOMP 2002/2402 Introduction to Data Structures and Data Types
Algorithm BFS(G)Input graph GOutput labeling of the edges
and partition of thevertices of G
for all u ∈ G.vertices()setLabel(u, UNEXPLORED)
for all e ∈ G.edges()setLabel(e, UNEXPLORED)
for all v ∈ G.vertices()if getLabel(v) = UNEXPLORED
BFS(G, v)
Albert Chanhttp://www.scs.carleton.ca/~achan
School of Computer Science, Carleton UniversityCOMP 2002/2402 Introduction to Data Structures and Data Types
Version 03.s11-29
Example
CB
A
E
D
discovery edgecross edge
A visited vertexA unexplored vertex
unexplored edge
L0
L1
F
CB
A
E
D
L0
L1
F
CB
A
E
D
L0
L1
F
Albert Chanhttp://www.scs.carleton.ca/~achan
School of Computer Science, Carleton UniversityCOMP 2002/2402 Introduction to Data Structures and Data Types
Version 03.s11-30
Example (cont.)
CB
A
E
D
L0
L1
F
CB
A
E
D
L0
L1
FL2
CB
A
E
D
L0
L1
FL2
CB
A
E
D
L0
L1
FL2
Albert Chanhttp://www.scs.carleton.ca/~achan
School of Computer Science, Carleton UniversityCOMP 2002/2402 Introduction to Data Structures and Data Types
Version 03.s11-31
Example (cont.)
CB
A
E
D
L0
L1
FL2
CB
A
E
D
L0
L1
FL2
CB
A
E
D
L0
L1
FL2
Albert Chanhttp://www.scs.carleton.ca/~achan
School of Computer Science, Carleton UniversityCOMP 2002/2402 Introduction to Data Structures and Data Types
Version 03.s11-32
PropertiesNotation
Gs: connected component of sProperty 1
BFS(G, s) visits all the vertices andedges of Gs
Property 2The discovery edges labeled byBFS(G, s) form a spanning tree Tsof Gs
Property 3For each vertex v in Li
– The path of Ts from s to v has iedges
– Every path from s to v in Gs has atleast i edges
CB
A
E
D
L0
L1
FL2
CB
A
E
D
F
Albert Chanhttp://www.scs.carleton.ca/~achan
School of Computer Science, Carleton UniversityCOMP 2002/2402 Introduction to Data Structures and Data Types
Version 03.s11-33
Analysis• Setting/getting a vertex/edge label takes O(1) time• Each vertex is labeled twice
– once as UNEXPLORED– once as VISITED
• Each edge is labeled twice– once as UNEXPLORED– once as DISCOVERY or CROSS
• Each vertex is inserted once into a sequence Li• Method incidentEdges is called once for each vertex• BFS runs in O(n + m) time provided the graph is represented by the
adjacency list structure– Recall that ΣΣΣΣv deg(v) = 2m
Albert Chanhttp://www.scs.carleton.ca/~achan
School of Computer Science, Carleton UniversityCOMP 2002/2402 Introduction to Data Structures and Data Types
Version 03.s11-34
Applications
• Using the template method pattern, we can specialize theBFS traversal of a graph G to solve the following problemsin O(n + m) time– Compute the connected components of G– Compute a spanning forest of G– Find a simple cycle in G, or report that G is a forest– Given two vertices of G, find a path in G between them with the
minimum number of edges, or report that no such path exists
Albert Chanhttp://www.scs.carleton.ca/~achan
School of Computer Science, Carleton UniversityCOMP 2002/2402 Introduction to Data Structures and Data Types
School of Computer Science, Carleton UniversityCOMP 2002/2402 Introduction to Data Structures and Data Types
Version 03.s11-36
CB
A
E
D
L0
L1
FL2
CB
A
E
D
F
DFS BFS
DFS vs. BFS (cont.)
• Back edge (v,w)– w is an ancestor of v in the tree
of discovery edges
• Cross edge (v,w)– w is in the same level as v or in
the next level in the tree ofdiscovery edges
Albert Chanhttp://www.scs.carleton.ca/~achan
School of Computer Science, Carleton UniversityCOMP 2002/2402 Introduction to Data Structures and Data Types
Version 03.s11-37
Separation Edges and Vertices• Definitions
– Let G be a connected graph– A separation edge of G is an edge whose removal disconnects G– A separation vertex of G is a vertex whose removal disconnects G
• Applications– Separation edges and vertices represent single points of failure in a network and are
critical to the operation of the network• Example
– DFW, LGA and LAX are separation vertices– (DFW,LAX) is a separation edge
ORD PVD
MIADFW
SFO
LAX
LGA
HNL
Albert Chanhttp://www.scs.carleton.ca/~achan
School of Computer Science, Carleton UniversityCOMP 2002/2402 Introduction to Data Structures and Data Types
Version 03.s11-38
ORDPVD
MIADFW
SFO
LAX
LGAHNL
Biconnected Graph
• Equivalent definitions of a biconnected graph G– Graph G has no separation edges and no separation vertices– For any two vertices u and v of G, there are two disjoint simple
paths between u and v (i.e., two simple paths between u and v thatshare no other vertices or edges)
– For any two vertices u and v of G, there is a simple cyclecontaining u and v
• Example
Albert Chanhttp://www.scs.carleton.ca/~achan
School of Computer Science, Carleton UniversityCOMP 2002/2402 Introduction to Data Structures and Data Types
Version 03.s11-39
ORD PVD
MIADFW
SFO
LAX
LGA
HNLRDU
Biconnected Components• Biconnected component of a graph G
– A maximal biconnected subgraph of G, or– A subgraph consisting of a separation edge of G and its end vertices
• Interaction of biconnected components– An edge belongs to exactly one biconnected component– A nonseparation vertex belongs to exactly one biconnected component– A separation vertex belongs to two or more biconnected components
• Example of a graph with four biconnected components
Albert Chanhttp://www.scs.carleton.ca/~achan
School of Computer Science, Carleton UniversityCOMP 2002/2402 Introduction to Data Structures and Data Types
Version 03.s11-40
Equivalence Classes• Given a set S, a relation R on S is a set of ordered pairs of elements of S, i.e., R is a
subset of S×S• An equivalence relation R on S satisfies the following properties
– Reflexive: (x,x) ∈ R– Symmetric: (x,y) ∈ R ⇒ (y,x) ∈ R– Transitive: (x,y) ∈ R ∧ (y,z) ∈ R ⇒ (x,z) ∈ R
• An equivalence relation R on S induces a partition of the elements of S into equivalenceclasses
• Example (connectivity relation among the vertices of a graph):– Let V be the set of vertices of a graph G– Define the relation
C = {(v,w) ∈ V×V such that G has a path from v to w}– Relation C is an equivalence relation– The equivalence classes of relation C are the vertices in each connected component of graph G
Albert Chanhttp://www.scs.carleton.ca/~achan
School of Computer Science, Carleton UniversityCOMP 2002/2402 Introduction to Data Structures and Data Types
Version 03.s11-41
ab
g
cj
de
f
i
Equivalence classes of linked edges:{a} {b, c, d, e, f} {g, i, j}
ab
g
cj
de
f
i
Link Relation• Edges e and f of connected graph
G are linked if– e = f, or– G has a simple cycle containing
e and f• Theorem:The link relation on the
edges of a graph is anequivalence relation
– Proof Sketch:– The reflexive and symmetric
properties follow from thedefinition
– For the transitive property,consider two simple cyclessharing an edge
Albert Chanhttp://www.scs.carleton.ca/~achan
School of Computer Science, Carleton UniversityCOMP 2002/2402 Introduction to Data Structures and Data Types
Version 03.s11-42
Link Components• The link components of a connected graph G are the equivalence classes of
edges with respect to the link relation• A biconnected component of G is the subgraph of G induced by an
equivalence class of linked edges• A separation edge is a single-element equivalence class of linked edges• A separation vertex has incident edges in at least two distinct equivalence
classes of linked edge
ORD PVD
MIADFW
SFO
LAX
LGA
HNLRDU
Albert Chanhttp://www.scs.carleton.ca/~achan
School of Computer Science, Carleton UniversityCOMP 2002/2402 Introduction to Data Structures and Data Types
Version 03.s11-43
a
bg
cj
d
e
f
i
Auxiliary graph B
DFS on graph G
a
db
c
eh i
jf
h
g
i
Auxiliary Graph• Auxiliary graph B for a connected
graph G– Associated with a DFS traversal of
G– The vertices of B are the edges of G– For each back edge e of G, B has
edges (e,f1), (e,f2) , …, (e,fk),where f1, f2, …, fk are thediscovery edges of G that form asimple cycle with e
– Its connected componentscorrespond to the the linkcomponents of G
Albert Chanhttp://www.scs.carleton.ca/~achan
School of Computer Science, Carleton UniversityCOMP 2002/2402 Introduction to Data Structures and Data Types
Version 03.s11-44
Auxiliary graph BDFS on graph G
Auxiliary Graph (cont.)
• In the worst case, the number of edges of the auxiliarygraph is proportional to nm
Albert Chanhttp://www.scs.carleton.ca/~achan
School of Computer Science, Carleton UniversityCOMP 2002/2402 Introduction to Data Structures and Data Types
Version 03.s11-45
Proxy GraphAlgorithm proxyGraph(G)
Input connected graph GOutput proxy graph F for GF ←←←← empty graphDFS(G, s) { s is any vertex of G}for all discovery edges e of G
F.insertVertex(e)setLabel(e, UNLINKED)
for all vertices v of G in DFS visit orderfor all back edges e = (u,v)
F.insertVertex(e)repeat
f ←←←← discovery edge with dest. uF.insertEdge(e,f,∅∅∅∅)if f getLabel(f) = UNLINKED
setLabel(f, LINKED)u ←←←← origin of edge f
elseu ←←←← v { ends the loop }
until u = vreturn F
a
bg
cj
d
e
f
i
Proxy graph F
DFS on graph G
a
db
c
eh i
jf
h
g
i
Albert Chanhttp://www.scs.carleton.ca/~achan
School of Computer Science, Carleton UniversityCOMP 2002/2402 Introduction to Data Structures and Data Types
Version 03.s11-46
a
bg
cj
d
e
f
i
Proxy graph F
DFS on graph G
a
db
c
eh i
jf
h
g
i
Proxy Graph (cont.)• Proxy graph F for a connected graph G
– Spanning forest of the auxiliary graph B– Has m vertices and O(m) edges– Can be constructed in O(n + m) time– Its connected components (trees)
correspond to the the link componentsof G
• Given a graph G with n vertices and medges, we can compute the following inO(n + m) time
– The biconnected components of G
– The separation vertices of G– The separation edges of G
Albert Chanhttp://www.scs.carleton.ca/~achan
School of Computer Science, Carleton UniversityCOMP 2002/2402 Introduction to Data Structures and Data Types
Version 03.s11-47
A
C
E
B
D
Digraphs
• A digraph is a directed graph whose edges are all directed• Applications
– one-way streets– flights– task scheduling
Albert Chanhttp://www.scs.carleton.ca/~achan
School of Computer Science, Carleton UniversityCOMP 2002/2402 Introduction to Data Structures and Data Types
Version 03.s11-48
A
C
E
B
D
Directed DFS
• We can specialize the traversalalgorithms (DFS and BFS) todigraphs by traversing edgesonly along their direction
• In the directed DFS algorithm,we have four types of edges
– discovery edges– back edges– forward edges– cross edges
• A directed DFS starting a avertex s determines the verticesreachable from s
Albert Chanhttp://www.scs.carleton.ca/~achan
School of Computer Science, Carleton UniversityCOMP 2002/2402 Introduction to Data Structures and Data Types
Version 03.s11-49
B
A
D
C
E
B
A
D
C
E
G
G*
Transitive Closure
• Given a digraph G, the transitiveclosure of G is the digraph G* suchthat
– G* has the same vertices as G– if G has a directed path from u to v (u
≠ v), G* has a directed edge from u tov
• The transitive closure providesreachability information about adigraph
• We can compute the transitive closurein time O(n(n + m)) by repeatedapplications of directed DFS
Albert Chanhttp://www.scs.carleton.ca/~achan
School of Computer Science, Carleton UniversityCOMP 2002/2402 Introduction to Data Structures and Data Types
Version 03.s11-50
Algorithm FloydWarshall(G)Input digraph GOutput transitive closure G* of Gi ← 1for all v ∈ G.vertices()
denote v as vii ← i + 1
G0 ← Gfor k ← 1 to n do
Gk ← Gk −−−− 1 for i ← 1 to n (i ≠≠≠≠ k) do
for j ← 1 to n (j ≠≠≠≠ i, k) doif Gk −−−− 1.areAdjacent(vi, vk) ∧∧∧∧
Gk −−−− 1.areAdjacent(vk, vj) if ¬¬¬¬Gk.areAdjacent(vi, vj)