Overview of DFS and BFS
Traversal a “path”
• Generally we search a “path” to find an
answer.
– The path can as simple as tree structure to some
more complex as circuit (a path with cycles into)
– It can be a directed or undirected graph as well.
Order of traversal.
• Most of the time, we use a preorder or
inorder traversal
– Preorder: visit each node before its children
– Inorder: visit left subtree, node, right subtree (for
binary trees only)
– Postorder: vist each node after its children
Traversal example
• If we were to enter this
maze from the top,
which direction should
we try each time.
– Left?
– Right?
BFS and DFS
• DFS is depth first search
– Start down one path
• Example 1, 2, 3, 4,5, 6, 7, etc.
• BFS is breath first search
– Search down the “levels”
• 1, 2,7,8, 3,6,9,12
Uses
• Either can be used in most cases.
– Find the depth of the tree (4)
– Find short branch (1)
– Find a path from node 1 to node 10
– The maze, find the exit.
– Many others as well.
Dfs and backtracking.
• Using a recursive version, it allows you to
backtrack.
– Using the maze below, say we go down first
• We’ll fail to find an exit. So we would backup until we
get back to start and choose to go right.
• BFS doesn’t need
Backtracking.
Why?
DFS and BFS
• Visiting all nodes or not following cycles
– We would need to make the node that we visit so
that go on infinitely.
implementation
• There a number of implementations
– Simplest (likely least efficient too)
– Dfs (Node) {
if (visit (Node) ==“answer”)
done
else
mark current node as visited.
foreach node connected below this node {
dfs(new node to search)
//if no nodes are the answer, it falls back to try another node
– Initial call: Dfs(start point);
Implementation (2)
• Dfs with a stack (works with cycles as well)
1. Push starting node onto stack
2. Pop stack
1. Check to make sure node has not be visited
2. If visited, pop again, until find unvisited node
3. Visit node.
3. Mark node visited and push “adjacent” unvisited
nodes onto the stack (order matters here).
4. Go to 2.
Implementations (3)
• BFS search
– Harder to implement a “simple” version, because you
need to kept track of the “levels”
– Easiest way is using queue
1. Insert start node on queue
2. Pop front node on queue. Visit node
3. If not the answer, mark node as visited
4. push all push “adjacent” unvisited nodes onto back
of queue.
5. Go to 2.
“Edge” costs.
• So far the cost between nodes, has had a cost
of 1.
– If we have edges, then that can change the order.
– Short path can be found with dfs and bfs but
which path we take is based on the edge cost as
the order. Ie take the path with a cost of 1 first,
then 2, then 3, etc…
Iterative deepening
• Iterative deepening
– Use dfs, but only to a depth and call again with
next depth, …, until solution is found.
• Depth 1, then 2, 3, 4, … until solution is found or max
depth is reach.
– Can look a lot like bfs, except it has “cut off” point.
Graph representation – undirected
graph
Adjacency list
ref. Introduction to Algorithms by Thomas
Cormen
Adjacency matrix
Graph representation – directed
graph
Adjacency list
ref. Introduction to Algorithms by Thomas
Cormen
Adjacency matrix
© Copyright 2026 Paperzz