Upload
others
View
3
Download
0
Embed Size (px)
Citation preview
© 2015 Goodrich and Tamassia Directed Graphs 1
Directed Graphs JFK
BOS
MIA
ORD
LAXDFW
SFO
Presentation for use with the textbook, Algorithm Design and Applications, by M. T. Goodrich and R. Tamassia, Wiley, 2015
© 2015 Goodrich and Tamassia Directed Graphs 2
Digraphs q A digraph is a graph
whose edges are all directed n Short for “directed graph”
q Applications n one-way streets n flights n task scheduling
A
C
E
B
D
© 2015 Goodrich and Tamassia Directed Graphs 3
Digraph Properties
q A graph G=(V,E) such that n Each edge goes in one direction: n Edge (a,b) goes from a to b, but not b to a
q If G is simple, m < n⋅(n - 1) q If we keep in-edges and out-edges in separate
adjacency lists, we can perform listing of incoming edges and outgoing edges in time proportional to their size
A
C
E
B
D
© 2015 Goodrich and Tamassia Directed Graphs 4
Digraph Application q Scheduling: edge (a,b) means task a must be
completed before b can be started
The good life
cs141 cs131 cs121
cs53 cs52 cs51
cs46 cs22 cs21
cs161
cs151
cs171
© 2015 Goodrich and Tamassia Directed Graphs 5
Directed DFS q We can specialize the traversal
algorithms (DFS and BFS) to digraphs by traversing edges only along their direction
q In the directed DFS algorithm, we have four types of edges n discovery edges n back edges n forward edges n cross edges
q A directed DFS starting at a vertex s determines the vertices reachable from s
A
C
E
B
D
© 2015 Goodrich and Tamassia
The Directed DFS Algorithm
Directed Graphs 6
© 2015 Goodrich and Tamassia Directed Graphs 7
Reachability
q DFS tree rooted at v: vertices reachable from v via directed paths
A
C
E
B
D
F A
C
E D
A
C
E
B
D
F
© 2015 Goodrich and Tamassia Directed Graphs 8
Strong Connectivity q Each vertex can reach all other vertices
a
d
c
b
e
f
g
© 2015 Goodrich and Tamassia Directed Graphs 9
q Pick a vertex v in G q Perform a DFS from v in G
n If there’s a w not visited, print “no”
q Let G’ be G with edges reversed q Perform a DFS from v in G’
n If there’s a w not visited, print “no” n Else, print “yes”
q Running time: O(n+m)
Strong Connectivity Algorithm
G:
G’:
a
d
c
b
e
f
g
a
d
c
b
e
f
g
© 2015 Goodrich and Tamassia Directed Graphs 10
q Maximal subgraphs such that each vertex can reach all other vertices in the subgraph
q Can also be done in O(n+m) time using DFS, but is more complicated (similar to biconnectivity).
Strongly Connected Components
{ a , c , g }
{ f , d , e , b }
a
d
c
b
e
f
g
© 2015 Goodrich and Tamassia Directed Graphs 11
Transitive Closure q Given a digraph G, the
transitive closure of G is the digraph G* such that n G* has the same vertices
as G n if G has a directed path
from u to v (u ≠ v), G* has a directed edge from u to v
q The transitive closure provides reachability information about a digraph
B
A
D
C
E
B
A
D
C
E
G
G*
© 2015 Goodrich and Tamassia Directed Graphs 12
Computing the Transitive Closure q We can perform
DFS starting at each vertex n O(n(n+m))
If there's a way to get from A to B and from B to C, then there's a way to get from A to C.
Alternatively ... Use dynamic programming: The Floyd-Warshall Algorithm
© 2015 Goodrich and Tamassia Directed Graphs 13
Floyd-Warshall Transitive Closure q Idea #1: Number the vertices 1, 2, …, n. q Idea #2: Consider paths that use only
vertices numbered 1, 2, …, k, as intermediate vertices:
k
j
i
Uses only vertices numbered 1,…,k-1 Uses only vertices
numbered 1,…,k-1
Uses only vertices numbered 1,…,k (add this edge if it’s not already in)
© 2015 Goodrich and Tamassia Directed Graphs 14
Floyd-Warshall’s Algorithm: High-Level View q Number vertices v1 , …, vn q Compute digraphs G0, …, Gn
n G0=G n Gk has directed edge (vi, vj) if G has a directed
path from vi to vj with intermediate vertices in {v1 , …, vk}
q We have that Gn = G* q In phase k, digraph Gk is computed from Gk - 1 q Running time: O(n3), assuming areAdjacent is
O(1) (e.g., adjacency matrix)
© 2015 Goodrich and Tamassia
The Floyd-Warshall Algorithm
q The running time is clearly O(n3). Directed Graphs 15
© 2015 Goodrich and Tamassia Directed Graphs 16
Floyd-Warshall Example
JFK
BOS
MIA
ORD
LAXDFW
SFO
The image cannot be v2
The image cannot be v1
The image cannot be v3
The image cannot v4
The image cannot be v5
The image cannot v6
v7
© 2015 Goodrich and Tamassia Directed Graphs 17
Floyd-Warshall, Iteration 1
JFK
BOS
MIA
ORD
LAXDFW
SFO
The image cannot be v2
The image cannot be v1
The image cannot be v3
The image cannot v4
The image cannot be v5
The image cannot v6
v7
© 2015 Goodrich and Tamassia Directed Graphs 18
Floyd-Warshall, Iteration 2
JFK
BOS
MIA
ORD
LAXDFW
SFO
The image cannot be v2
The image cannot be v1
The image cannot be v3
The image cannot v4
The image cannot be v5
The image cannot v6
v7
© 2015 Goodrich and Tamassia Directed Graphs 19
Floyd-Warshall, Iteration 3
JFK
BOS
MIA
ORD
LAXDFW
SFO
The image cannot be v2
The image cannot be v1
The image cannot be v3
The image cannot v4
The image cannot be v5
The image cannot v6
v7
© 2015 Goodrich and Tamassia Directed Graphs 20
Floyd-Warshall, Iteration 4
JFK
BOS
MIA
ORD
LAXDFW
SFO
The image cannot be v2
The image cannot be v1
The image cannot be v3
The image cannot v4
The image cannot be v5
The image cannot v6
v7
© 2015 Goodrich and Tamassia Directed Graphs 21
Floyd-Warshall, Iteration 5
JFK
MIA
ORD
LAXDFW
SFO
The image cannot be v2
The image cannot be v1
The image cannot be v3
The image cannot v4
The image cannot be v5
The image cannot v6
v7BOS
© 2015 Goodrich and Tamassia Directed Graphs 22
Floyd-Warshall, Iteration 6
JFK
MIA
ORD
LAXDFW
SFO
The image cannot be v2
The image cannot be v1
The image cannot be v3
The image cannot v4
The image cannot be v5
The image cannot v6
v7BOS
© 2015 Goodrich and Tamassia Directed Graphs 23
Floyd-Warshall, Conclusion
JFK
MIA
ORD
LAXDFW
SFO
The image cannot be v2
The image cannot be v1
The image cannot be v3
The image cannot v4
The image cannot be v5
The image cannot v6
v7BOS
© 2015 Goodrich and Tamassia Directed Graphs 24
DAGs and Topological Ordering q A directed acyclic graph (DAG) is a
digraph that has no directed cycles q A topological ordering of a digraph
is a numbering v1 , …, vn
of the vertices such that for every edge (vi , vj), we have i < j
q Example: in a task scheduling digraph, a topological ordering a task sequence that satisfies the precedence constraints
Theorem A digraph admits a topological ordering if and only if it is a DAG
B
A
D
C
E
DAG G
B
A
D
C
E
Topological ordering of G
v1
v2
v3
v4 v5
© 2015 Goodrich and Tamassia Directed Graphs 25
write c.s. program
play
Topological Sorting q Number vertices, so that (u,v) in E implies u < v
wake up
eat
nap
study computer sci.
more c.s.
work out
sleep
dream about graphs
A typical student day 1
2 3
4 5
6
7
8
9
10 11
bake cookies
© 2015 Goodrich and Tamassia Directed Graphs 26
q Note: This algorithm is different than the one in the book
q Running time: O(n + m)
Algorithm for Topological Sorting
Algorithm TopologicalSort(G) H ← G // Temporary copy of G n ← G.numVertices() while H is not empty do
Let v be a vertex with no outgoing edges Label v ← n n ← n - 1 Remove v from H
© 2015 Goodrich and Tamassia Directed Graphs 27
Implementation with DFS q Simulate the algorithm by
using depth-first search q O(n+m) time.
Algorithm topologicalDFS(G, v) Input graph G and a start vertex v of G Output labeling of the vertices of G in the connected component of v setLabel(v, VISITED) for all e ∈ G.outEdges(v)
{ outgoing edges } w ← opposite(v,e) if getLabel(w) = UNEXPLORED { e is a discovery edge } topologicalDFS(G, w) else { e is a forward or cross edge }
Label v with topological number n n ← n - 1
Algorithm topologicalDFS(G) Input dag G Output topological ordering of G
n ← G.numVertices() for all u ∈ G.vertices() setLabel(u, UNEXPLORED) for all v ∈ G.vertices() if getLabel(v) = UNEXPLORED topologicalDFS(G, v)
© 2015 Goodrich and Tamassia Directed Graphs 28
Topological Sorting Example
© 2015 Goodrich and Tamassia Directed Graphs 29
Topological Sorting Example
9
© 2015 Goodrich and Tamassia Directed Graphs 30
Topological Sorting Example
8
9
© 2015 Goodrich and Tamassia Directed Graphs 31
Topological Sorting Example
7 8
9
© 2015 Goodrich and Tamassia Directed Graphs 32
Topological Sorting Example
7 8
6
9
© 2015 Goodrich and Tamassia Directed Graphs 33
Topological Sorting Example
7 8
5 6
9
© 2015 Goodrich and Tamassia Directed Graphs 34
Topological Sorting Example
7
4
8
5 6
9
© 2015 Goodrich and Tamassia Directed Graphs 35
Topological Sorting Example
7
4
8
5 6
3
9
© 2015 Goodrich and Tamassia Directed Graphs 36
Topological Sorting Example 2
7
4
8
5 6
3
9
© 2015 Goodrich and Tamassia Directed Graphs 37
Topological Sorting Example 2
7
4
8
5 6
1
3
9