Lecture 5
DFS applications, Graph modeling
CSE 421 Autumn 2026
1
Previously…
2
BFS and DFS
3
BFS: Queue — first in, first out���DFS: Stack — first in, last out
BFS and DFS
4
Applications of BFS from previously:
Depth-first search (DFS)
“Recursive” version
5
DFS():
DFS()
Order of completion:
Spanning trees
6
Understanding the DFS spanning tree
(this is helpful for designing other algorithms based on DFS)
7
Let this be a spanning tree obtained via DFS. These are the edges we walked along.
Understanding the DFS spanning tree
(this is helpful for designing other algorithms based on DFS)
What do the edges not included in the spanning tree look like?
8
Let this be a spanning tree obtained via DFS. These are the edges we walked along.
Understanding the DFS spanning tree
What do the edges not included in the spanning tree look like?
9
(this is helpful for designing other algorithms based on DFS)
Let this be a spanning tree obtained via DFS. These are the edges we walked along.
Understanding the DFS spanning tree
What do the edges not included in the spanning tree look like?
10
All descendants of a given vertex
(this is helpful for designing other algorithms based on DFS)
Let this be a spanning tree obtained via DFS. These are the edges we walked along.
Understanding the DFS spanning tree
What do the edges not included in the spanning tree look like?
11
Def. A cross edge is an edge that connects a vertex to a vertex in another “branch”
All descendants of a given vertex
(this is helpful for designing other algorithms based on DFS)
(Two vertices are in different branches if they are not “descendant” of one another)
Understanding the DFS spanning tree
What do the edges not included in the spanning tree look like?
12
Def. A cross edge is an edge that connects a vertex to a vertex in another “branch”
All descendants of a given vertex
Similarly, this is also a cross edge, and is not present in the graph
(this is helpful for designing other algorithms based on DFS)
Understanding the DFS spanning tree
What do the edges not included in the spanning tree look like?
13
Def. A back edge is an edge that connects a vertex to an ancestor that is not it’s parent in the tree.
Yes it can exist!
Is there any other type of edge (other than cross and back edges)?
No, that’s all!
Q1: does a graph with a back edge contain a cycle?
Q2: does a graph with a cycle contain a back edge?
Yes!
Yes!
(this is helpful for designing other algorithms based on DFS)
(Because a tree has no cycles by definition, and back edges are the only other edges in the graph, as we have argued)
Understanding the DFS spanning tree
What do the edges not included in the spanning tree look like?
14
Def. A back edge is an edge that connects a vertex to an ancestor that is not it’s parent in the tree.
Is there any other type of edge (other than cross and back edges)?
No, that’s all!
Useful Fact: A graph contains a cycle iff it has a back edge.
This suggests the following algorithm to decide if a graph has a cycle:
Can also use DFS for bipartiteness testing:
Equivalently, color each vertex alternately red and blue and check if a back edge ever connects to same-color vertices
Recall: a graph is bipartite iff it contains an odd cycle
(this is helpful for designing other algorithms based on DFS)
Today
15
Directed graphs
16
Depth-first search on directed graphs
17
Depth-first search on directed graphs
18
Depth-first search on directed graphs
19
Depth-first search on directed graphs
20
DFS edge nomenclature (directed graphs)
21
DFS edge nomenclature (directed graphs)
22
Back edge�Connects vertex to its ancestor in DFS tree
DFS edge nomenclature (directed graphs)
23
Back edge�Connects vertex to its ancestor in DFS tree. Can exist.
DFS edge nomenclature (directed graphs)
24
Forward edge�Connects vertex to its descendant in DFS tree.
Back edge�Connects vertex to its ancestor in DFS tree. Can exist.
DFS edge nomenclature (directed graphs)
25
Forward edge�Connects vertex to its descendant in DFS tree.�Can exist.
Back edge�Connects vertex to its ancestor in DFS tree. Can exist.
DFS edge nomenclature (directed graphs)
26
Cross edge�Connects vertices across branches.
Back edge�Connects vertex to its ancestor in DFS tree. Can exist.
Forward edge�Connects vertex to its descendant in DFS tree.�Can exist.
DFS edge nomenclature (directed graphs)
27
Cross edge�Connects vertices across branches. Can only exist if high -> low.
Back edge�Connects vertex to its ancestor in DFS tree. Can exist.
Forward edge�Connects vertex to its descendant in DFS tree.�Can exist.
DFS edge nomenclature (directed graphs)
28
Tree edge�The edges in the DFS tree
Never-considered edge�The edges that are not even �considered during DFS
Q: How do we use this information to decide if a graph has a directed cycle?
Back edge�Connects vertex to its ancestor in DFS tree. Can exist.
Forward edge�Connects vertex to its descendant in DFS tree.�Can exist.
Cross edge�Connects vertices across branches. Can only exist if high -> low.
Same as for undirected graphs: a cycle exists iff there is back edge!
So run DFS and check if you encounter a back edge.
Finding strongly connected components (SCC)
29
Finding strongly connected components (SCC)
30
Shrinking a directed graph to a DAG
31
Directed acyclic graphs
32
Shrinking a directed graph to a DAG
33
Topological sorting
34
Input: A DAG
Output: A numbering of vertices such that all edges go from lower to higher.
Topological sorting
DFS():
If is not visited:
DFS()
35
Input: A DAG
Output: A numbering of vertices such that all edges go from lower to higher.
Topo-sort():
Small but key modification
Note that this returns them in reverse order of when they are added.
Topological sorting walkthrough
Run DFS
Run DFS
Run DFS
Run DFS
Run DFS
Run DFS
Run DFS
Topological sorting walkthrough
Run DFS
Run DFS
Run DFS
Run DFS
Run DFS
Run DFS
Run DFS
15
Topological sorting walkthrough
Run DFS
Run DFS
Run DFS
Run DFS
Run DFS
Run DFS
Run DFS
15
14
Topological sorting walkthrough
Run DFS
Run DFS
Run DFS
Run DFS
Run DFS
Run DFS
Run DFS
15
14
13
Topological sorting walkthrough
Run DFS
Run DFS
Run DFS
Run DFS
Run DFS
Run DFS
Run DFS
15
14
13
12
Run DFS
Run DFS
Topological sorting walkthrough
Run DFS
Run DFS
Run DFS
Run DFS
Run DFS
Run DFS
Run DFS
15
14
13
12
Run DFS
Run DFS
11
Topological sorting walkthrough
Run DFS
Run DFS
Run DFS
Run DFS
Run DFS
Run DFS
Run DFS
15
14
13
12
Run DFS
Run DFS
11
10
Topological sorting walkthrough
Run DFS
Run DFS
Run DFS
Run DFS
Run DFS
Run DFS
Run DFS
15
14
13
12
Run DFS
Run DFS
11
10
9
Topological sorting walkthrough
Run DFS
Run DFS
Run DFS
Run DFS
Run DFS
Run DFS
Run DFS
15
14
13
12
Run DFS
Run DFS
11
10
9
8
Run DFS
Run DFS
Run DFS
Run DFS
Topological sorting walkthrough
Run DFS
Run DFS
Run DFS
Run DFS
Run DFS
Run DFS
Run DFS
15
14
13
12
Run DFS
Run DFS
11
10
9
8
Run DFS
Run DFS
Run DFS
Run DFS
7
Topological sorting walkthrough
Run DFS
Run DFS
Run DFS
Run DFS
Run DFS
Run DFS
Run DFS
15
14
13
12
Run DFS
Run DFS
11
10
9
8
Run DFS
Run DFS
Run DFS
Run DFS
7
6
Topological sorting walkthrough
Run DFS
Run DFS
Run DFS
Run DFS
Run DFS
Run DFS
Run DFS
15
14
13
12
Run DFS
Run DFS
11
10
9
8
Run DFS
Run DFS
Run DFS
Run DFS
7
6
5
Topological sorting walkthrough
Run DFS
Run DFS
Run DFS
Run DFS
Run DFS
Run DFS
Run DFS
15
14
13
12
Run DFS
Run DFS
11
10
9
8
Run DFS
Run DFS
Run DFS
Run DFS
7
6
5
4
Topological sorting walkthrough
Run DFS
Run DFS
Run DFS
Run DFS
Run DFS
Run DFS
Run DFS
15
14
13
12
Run DFS
Run DFS
11
10
9
8
Run DFS
Run DFS
Run DFS
Run DFS
7
6
5
4
3
Run DFS
Run DFS
Topological sorting walkthrough
Run DFS
Run DFS
Run DFS
Run DFS
Run DFS
Run DFS
Run DFS
15
14
13
12
Run DFS
Run DFS
11
10
9
8
Run DFS
Run DFS
Run DFS
Run DFS
7
6
5
4
3
Run DFS
Run DFS
2
Topological sorting walkthrough
Run DFS
Run DFS
Run DFS
Run DFS
Run DFS
Run DFS
Run DFS
15
14
13
12
Run DFS
Run DFS
11
10
9
8
Run DFS
Run DFS
Run DFS
Run DFS
7
6
5
4
3
Run DFS
Run DFS
2
1
Why this works: if vertex terminates before vertex , then cannot be an ancestor of :
Topological sorting walkthrough
Run DFS
Run DFS
Run DFS
Run DFS
Run DFS
Run DFS
Run DFS
15
14
13
12
Run DFS
Run DFS
11
10
9
8
Run DFS
Run DFS
Run DFS
Run DFS
7
6
5
4
3
Run DFS
Run DFS
2
1
Remark: here, we ran the algorithm on a DAG to find a topological sort. One can also run the algorithm on an arbitrary directed graph to decide whether the graph is a DAG (and in that case output a topological sort) or not. One just needs a small modification:
(Convince yourself that this works!)
Summary - graph traversal applications
53
BFS
Shortest path (unweighted graphs)
DFS
Strongly connected components
Topological sort
Either one:
Bipartiteness testing (aka 2-coloring)
Connected components
Cycle detection
Takeaways
54