Shortest Path
3/17/2005 2:08 AM
Outline and Reading (§6.4) Reachability (§6.4.1)
Directed Graphs
BOS
ORD
Directed DFS Strong connectivity
JFK
Transitive closure (§6.4.2)
SFO
LAX
DFW
The Floyd-Warshall Algorithm
MIA
Directed Acyclic Graphs (DAG’s) (§6.4.4) Directed Graphs
Topological Sorting
1
Directed Graphs
2
E
Digraphs
Digraph Properties C
A digraph is a graph whose edges are all directed
E
D
B A
Directed Graphs
ics23
ics51
ics53
ics52
We can specialize the traversal algorithms (DFS and BFS) to digraphs by traversing edges only along their direction In the directed DFS algorithm, we have four types of edges
ics161 ics131
ics141
ics121
ics171
Directed Graphs
discovery edges back edges forward edges cross edges
4
5
E D C B
A directed DFS starting at a vertex s determines the vertices reachable from s
The good life
ics151
Directed Graphs
Directed DFS
Scheduling: edge (a,b) means task a must be completed before b can be started ics22
A
If G is simple, m < n(n-1). If we keep in-edges and out-edges in separate adjacency lists, we can perform listing of of the sets of in-edges and out-edges in time proportional to their size.
3
Digraph Application
ics21
Each edge goes in one direction: Edge (a,b) goes from a to b, but not b to a.
C
one-way streets flights task scheduling
B
A graph G=(V,E) such that
Short for “directed graph”
Applications
D
Directed Graphs
A
6
1
Shortest Path
3/17/2005 2:08 AM
Strong Connectivity
Reachability
Each vertex can reach all other vertices
DFS tree rooted at v: vertices reachable from v via directed paths E
E
D F
A
d
E
D
B
e
C
F
A
7
Directed Graphs
Strong Connectivity Algorithm a
G:
g
c
If there’s a w not visited, print “no”. d
Let G’ be G with edges reversed. Perform a DFS from v in G’.
e
a
g
c a
G’:
g
c d
Running time: O(n+m).
d
e
Directed Graphs
e
9
D
E
B C
G
D
E
B C A
We can perform DFS starting at each vertex
A
{f,d,e,b}
Directed Graphs
Computing the Transitive Closure
Transitive Closure
{a,c,g}
b
f
b
f
Directed Graphs
Maximal subgraphs such that each vertex can reach all other vertices in the subgraph Can also be done in O(n+m) time using DFS, but is more complicated (similar to biconnectivity).
b
f
If there’s a w not visited, print “no”. Else, print “yes”.
Given a digraph G, the transitive closure of G is the digraph G* such that 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 to v The transitive closure provides reachability information about a digraph
8
Strongly Connected Components
Pick a vertex v in G. Perform a DFS from v in G.
b
f
B
Directed Graphs
g
c
C
C A
a
D
10
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.
O(n(n+m))
Alternatively ... Use dynamic programming: the Floyd-Warshall Algorithm
G* 11
Directed Graphs
12
2
Shortest Path
3/17/2005 2:08 AM
Floyd-Warshall Transitive Closure
Floyd-Warshall’s Algorithm
Idea #1: Number the vertices 1, 2, …, n. Idea #2: Consider paths that use only vertices numbered 1, 2, …, k, as intermediate vertices: Uses only vertices numbered 1,…,k (add this edge if it’s not already in)
i
j
Uses only vertices numbered 1,…,k-1 k
Uses only vertices numbered 1,…,k-1
Directed Graphs
Algorithm FloydWarshall(G) Input digraph G Output transitive closure G* of G i←1 for all v ∈ G.vertices() G0=G denote v as vi Gk has a directed edge (vi, vj) i←i+1 if G has a directed path from G0 ← G vi to vj with intermediate for k ← 1 to n do vertices in the set {v1 , …, vk} Gk ← Gk − 1 We have that Gn = G* for i ← 1 to n (i ≠ k) do for j ← 1 to n (j ≠ i, k) do In phase k, digraph Gk is computed from Gk − 1 if Gk − 1.areAdjacent(vi, vk) ∧ Gk − 1.areAdjacent(vk, vj) Running time: O(n3), if ¬Gk.areAdjacent(vi, vj) assuming areAdjacent is O(1) Gk.insertDirectedEdge(vi, vj , k) (e.g., adjacency matrix) return Gn
Floyd-Warshall’s algorithm numbers the vertices of G as v1 , …, vn and computes a series of digraphs G0, …, Gn
13
Floyd-Warshall Example
v7
Directed Graphs
14
Floyd-Warshall, Iteration 1
BOS ORD
v4
ORD JFK
v2 SFO
v1
v4 JFK
v2
v6
LAX
v6
SFO
DFW
LAX
v3
v1
MIA
DFW
v3 MIA
v5
v5
Directed Graphs
15
Floyd-Warshall, Iteration 2
v7
Directed Graphs
16
Floyd-Warshall, Iteration 3
BOS ORD
v4
ORD
v2
v4 JFK
v2
v6
SFO
v1
v7 BOS
JFK
LAX
v7 BOS
v6
SFO
DFW
LAX
v3
v1
MIA
DFW
v3 MIA
v5 Directed Graphs
v5 17
Directed Graphs
18
3
Shortest Path
3/17/2005 2:08 AM
Floyd-Warshall, Iteration 4
v7
Floyd-Warshall, Iteration 5
v4
ORD
JFK
SFO
v1
JFK
v2
v6
LAX
v4
ORD
v2
v6
SFO
DFW
DFW
LAX
v3
v3
v1
MIA
MIA
v5
v5
Directed Graphs
19
Floyd-Warshall, Iteration 6
v7
Directed Graphs
20
Floyd-Warshall, Conclusion
BOS
JFK
JFK
v2
v6
SFO
v1
v4
ORD
v2
v7 BOS
v4
ORD
LAX
v7 BOS
BOS
v6
SFO
DFW
DFW
LAX
v3
v3
v1
MIA
MIA
v5
v5
Directed Graphs
21
DAGs and Topological Ordering A directed acyclic graph (DAG) is a digraph that has no directed cycles 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 Example: in a task scheduling digraph, a topological ordering a task sequence that satisfies the v2 precedence constraints Theorem A digraph admits a topological v1 ordering if and only if it is a DAG Directed Graphs
D
Topological Sorting wake up
1
A typical student day
2 study computer sci.
C A
DAG G D
B C
v4
E
22
Number vertices, so that (u,v) in E implies u < v
E
B
A
Directed Graphs
v5
4 7 play
nap
Topological ordering of G 23
5 more c.s.
8 write c.s. program
9 make cookies for professors
v3
3
eat
6 work out
10 sleep
11 dream about graphs
Directed Graphs
24
4
Shortest Path
3/17/2005 2:08 AM
Algorithm for Topological Sorting
Simulate the algorithm by using depth-first search
Note: This algorithm is different than the one in Goodrich-Tamassia
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.incidentEdges(v) if getLabel(e) = UNEXPLORED w ← opposite(v,e) if getLabel(w) = UNEXPLORED setLabel(e, DISCOVERY) 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 e ∈ G.edges() setLabel(e, UNEXPLORED) for all v ∈ G.vertices() if getLabel(v) = UNEXPLORED topologicalDFS(G, v)
Method 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
Running time: O(n + m). How…? Directed Graphs
Topological Sorting Algorithm using DFS
O(n+m) time. 25
Topological Sorting Example
Directed Graphs
26
Topological Sorting Example
9 Directed Graphs
27
Topological Sorting Example
Directed Graphs
28
Topological Sorting Example
7 8
8 9 Directed Graphs
9 29
Directed Graphs
30
5
Shortest Path
3/17/2005 2:08 AM
Topological Sorting Example
Topological Sorting Example
6
6 7
5 7
8
8 9
9
Directed Graphs
31
Topological Sorting Example
Directed Graphs
32
Topological Sorting Example 3
4 6
4 6
5 7
5 7
8
8 9
9
Directed Graphs
33
Topological Sorting Example
Directed Graphs
34
Topological Sorting Example
2
2
3
1
3 4 6
4 6
5 7
5 7
8
8 9 Directed Graphs
9 35
Directed Graphs
36
6