Applications of DFS
DFS(G)1. For each vertex 𝑢 ∈ 𝐺. 𝑉2. u.color = WHITE3. u.𝜋 = NIL4. time = 05. For each vertex 𝑢 ∈ 𝐺. 𝑉6. if u.color == WHITE7. DFS-Visit(G,u)
DFS-Visit(G,u)1. time = time + 12. u.d = time3. u.color = GRAY4. For each vertex 𝑣 ∈ 𝐺. 𝐴𝑑𝑗 𝑢5. if v.color == WHITE6. v.𝜋 = u7. DFS-Visit(G,v)8. u.color = BLACK9. time = time + 110. u.f = time
Each vertex has two timestamps:𝒖. 𝒅 : First discovered and GRAYed𝒖. 𝒇 : Finished and Blackened∀𝒖: 𝟏 ≤ 𝒖. 𝒅 < 𝒖. 𝒇 ≤ 𝟐𝒏
u
v
y
x
1 2 3 4 5 6 7 8 9 10 11 12
w
z
DFS(G)1. For each vertex 𝑢 ∈ 𝐺. 𝑉2. u.color = WHITE3. u.𝜋 = NIL4. time = 05. For each vertex 𝑢 ∈ 𝐺. 𝑉6. if u.color == WHITE7. DFS-Visit(G,u)
DFS-Visit(G,u)1. time = time + 12. u.d = time3. u.color = GRAY4. For each vertex 𝑣 ∈ 𝐺. 𝐴𝑑𝑗 𝑢5. if v.color == WHITE6. v.𝜋 = u7. DFS-Visit(G,v)8. u.color = BLACK9. time = time + 110. u.f = time
Thm 2: 𝑣. 𝑑, 𝑣. 𝑓 ⊂ 𝑢. 𝑑, 𝑢. 𝑓 iff v is a descendant of u in the DFS-forest, 𝑣. 𝑑, 𝑣. 𝑓 ∩ 𝑢. 𝑑, 𝑢. 𝑓 = ∅ iff u and v
are unrelated in the DFS-forest
DFS(G)1. For each vertex 𝑢 ∈ 𝐺. 𝑉2. u.color = WHITE3. u.𝜋 = NIL4. time = 05. For each vertex 𝑢 ∈ 𝐺. 𝑉6. if u.color == WHITE7. DFS-Visit(G,u)
DFS-Visit(G,u)1. time = time + 12. u.d = time3. u.color = GRAY4. For each vertex 𝑣 ∈ 𝐺. 𝐴𝑑𝑗 𝑢5. if v.color == WHITE6. v.𝜋 = u7. DFS-Visit(G,v)8. u.color = BLACK9. time = time + 110. u.f = time
Thm 3: 𝑣 is a descendant of 𝑢 in the DFS-forest iff when we invoke DFS-Visit(u) there is a white path from 𝑢to 𝑣
u
v
y
x
1 2 3 4 5 6 7 8 9 10 11 12
w
z
The green forward edge should point to x
Edge classification
• Tree edges: (u,v), DFS-Visit(v) was called from DFS-visit(u)
• Back edges: (u,v) such that v is an ancestor of u in the DFS-forest
• Forward edges: nontree edges (u,v) such that v is a descendant of u
• Cross edges: (u,v) such that 𝑣. 𝑓 < 𝑢. 𝑑
uv
Observe, among non-tree edges:1. (Only) backward edges go to a vertex with a later finish time2. (Only) forward edges go to a vertex with later discovery time
Is my directed graph acyclic ?
Thm 6: 𝐺 is acyclic iff DFS on 𝐺 does not produce back edges
This is actually Thm 5 if we want to be consistent with previous videos
Topological sort
• Input: 𝐺 = (𝑉, 𝐸) a directed, acyclic graph (DAG)
Topological sort
• Input: G=(V,E) a directed, acyclic graph
• Output: Ordering of the vertices such that if 𝑢, 𝑣 ∈ 𝐸 then 𝑢precedes 𝑣
1
3
564
2
0 6543210
Recall:
Observe, among non-tree edges:1. (Only) backward edges go to a vertex with a later finish time2. (Only) forward edges go to a vertex with later discovery time
Topological sort
• Lets run DFS
1/
Topological sort
• Lets run DFS
2/
1/
Topological sort
• Lets run DFS
3/
2/
1/
Topological sort
• Lets run DFS
4/3/
2/
1/
Topological sort
• Lets run DFS
4/53/
2/
1/
Topological sort
• Lets run DFS
4/53/6
2/
1/
Topological sort
• Lets run DFS
4/53/6
2/7
1/
Topological sort
• Lets run DFS
4/53/6
2/7
1/
Topological sort
• Lets run DFS
4/53/6
2/7
1/8
Topological sort
• Lets run DFS
9/14
10/13
11/12
4/53/6
2/7
1/8
Topological sort
• Order the vertices by reverse finishing times
9/14
10/13
11/12
4/53/6
2/7
1/8
Topological sort
• Order the vertices by reverse finishing times
9/14 10/13 11/12 4/53/62/71/8
9/14
10/13
11/12
4/53/6
2/7
1/8