Advanced DFS Topics (Parts 5-8)
This interactive review covers three advanced depth-first search topics critical for your exam:
Master DFS algorithm on directed graphs with discovery and finish times, DFS forest construction, and key theorems.
Learn to classify edges as tree, back, forward, or cross edges during and after DFS execution.
Understand DAGs and compute topological orderings using DFS with finish-time ordering.
Depth-First Search on directed graphs extends the basic DFS algorithm with additional properties and theorems that help analyze graph structure.
Vertex v is a descendant of u in the DFS forest if and only if at time d[u], there exists a white path from u to v (a path consisting entirely of white vertices).
For any two vertices u and v, exactly one of the following holds:
Vertex v is a proper descendant of u if and only if d[u] < d[v] < f[v] < f[u].
Question: Given a directed graph with vertices {s,t,u,v} and edges s→t, s→u, t→v, u→v, v→t, trace DFS starting from s. Show all discovery and finish times.
Question: Using the DFS from the interactive walkthrough, explain why y is a descendant of a using the White-Path Theorem.
Question: Given timestamps a:[1/8], b:[2/5], x:[6/7], determine the relationship between these vertices using the Parenthesis Theorem.
DFS can classify every edge in a directed graph into one of four types based on the color of the destination vertex when the edge is explored.
Edge (u,v) where v is WHITE when explored. These edges form the DFS forest.
Rule: color[v] = WHITE when examining (u,v)
Edge (u,v) where v is GRAY when explored. Points to an ancestor in DFS tree. Indicates a cycle!
Rule: color[v] = GRAY when examining (u,v)
Edge (u,v) where v is BLACK and v is a descendant of u. Points from ancestor to descendant (non-tree edge).
Rule: color[v] = BLACK and d[u] < d[v] < f[v] < f[u]
Edge (u,v) where v is BLACK but v is not a descendant of u. All other edges.
Rule: color[v] = BLACK and intervals disjoint
Question: Given a directed graph with DFS timestamps: a:[1/10], b:[2/5], c:[6/9], d:[3/4], e:[7/8]. Edges: (a,b), (a,c), (b,d), (c,e), (d,b). Classify each edge.
Question: Explain how to detect a cycle in a directed graph using DFS. Why do back edges indicate cycles?
Question: In an undirected graph, why are there only tree and back edges? Why can't forward or cross edges exist?
A DAG is a directed graph with no cycles. Many real-world problems naturally form DAGs:
A linear ordering of vertices such that for every directed edge (u,v), vertex u appears before vertex v in the ordering.
Example: If tasks have dependencies A→B, A→C, B→D, C→D, one valid ordering is: A, B, C, D (or A, C, B, D)
Key insight: If edge (u,v) exists, then f[u] > f[v] (u finishes after v). By ordering vertices by decreasing finish time, we ensure u appears before v.
For any edge (u,v) in a DAG:
Question: Given a new course DAG with courses {CS150, CS220, CS240, CS350} and prerequisites: CS150→CS220, CS150→CS240, CS220→CS350, CS240→CS350. Compute a topological sort showing the DFS process and final valid course sequence.
Question: If we add an edge CS400→CS100 to the course prerequisite graph, what happens? Why can't we perform topological sort? Explain in the context of course prerequisites.
Question: Prove that if edge (u,v) exists in a DAG (representing "u is prerequisite for v"), then u appears before v in any topological ordering. Use the course prerequisite context.