P[k]
for \(k < i\). (Inductive hypothesis/Recursion Fairy)
P[k]
paths to \(v_k\).P[i]
).See Figure 6.8.
DagvillePaths(G):
initialize an array P[1..n]
P[1] <- 1
TG <- TopologicalSort(G) // a=v_1, v_2, v_3, ..., v_n = z
for each vertex v in TG // or for i = 1..n
count <- 0
for each directed edge uv // scan each in-neighbor of v
count <- count + P[k] // v_k = u
P[i] <- count // v_i = v
return P[n] // v_n = z
Time:
CountPaths(G):
P[1..n] = all 0s
P[1] <- 1 ?? Otherwise everything is just going to be zero
sorted = TopologicalSort(G) #Finishes in O(V+E) time
for vertex in sorted: #Runs V times total
for edge in vertex edges: #Runs E times total
P[edge target] += P[vertex]
return P[n]
NumPaths(G):
G <- TopologicalSort(G)
P[1..n] // will be the number of paths that visit the ith node of G. Initialize to zero.
P[1] = 1; // there is only one path that visits the first node from the first node.
for i <- 1 up to n: // make P[i] equal to the sum of the number of paths of each parent node
for each edge in G[i]: // guranteed (i < edge) is true because of the topological ordering of G
increase P[edge] by P[i]
return P[n] // the only sink is guranteed to be last, since G is in topological order.
A directed graph \(G\) is called strongly connected if, for any vertices \(u,v \in V(G)\), there is a directed path from \(u\) to \(v\) and there is also a directed path from \(v\) to \(u\).
-poll "Is this graph strongly connected?" "Yes" "No" "There is no way to tell."
Let \(G\) be a directed graph.
See Figure 6.13.
See Figure 6.14.
Table #1 | Table #2 | Table #3 | Table #4 | Table #5 | Table #6 | Table #7 |
---|---|---|---|---|---|---|
Claire | Andrew | Josiah | Nathan | Jack | Kevin | Jordan |
James | Logan | Blake | Bri | Levi | Drake | John |
Talia | Isaac | Trevor | Kristen | Graham | Ethan | Grace |
Let \(G\) be a directed graph. Explain why \(\mbox{scc}(G)\) must be a DAG. (Hint: Prove by contradiction: what if \(\mbox{scc}(G)\) weren’t a DAG?)
Suppose \(D\) is a DAG. What is \(\mbox{scc}(D)\)?
Suppose \(v \in V(G)\) is the last vertex to finish in the call DFSAll(G)
. In other words, \(v\) is the root of the last DFS tree discovered in the depth-first forest. Explain why \(v\)’s strongly connected component must be a source in \(\mbox{scc}(G)\).
FindSinkComponentVertex(G)
FindSinkComponentVertex(G):
G' <- rev(G)
v_1...v_n <- a postordering of G' (using DFSAll(G'))
return v_n
Time: \(O(V+E)\)
StrongComponents(G):
count <- 0
while G is non-empty
C <- ∅
count <- count + 1
v <- FindSinkComponentVertex(G)
for all vertices w in reach(v)
w.label <- count
add w to C
remove C and its incoming edges from G
StrongComponents(G):
count <- 0
while G is non-empty
C <- ∅
count <- count + 1
v <- FindSinkComponentVertex(G)
for all vertices w in reach(v)
w.label <- count
add w to C
remove C and its incoming edges from G