3. Depth-First Search
Search “deeper” in the graph whenever possible
Edges are explored out of the most recently
discovered vertex v that still has unexplored edges
• After all edges of v have been explored, the search
“backtracks” from the parent of v
• The process continues until all vertices reachable from the
original source have been discovered
• If undiscovered vertices remain, choose one of them as a
new source and repeat the search from that vertex
• DFS creates a “depth-first forest”
1 2
5 4
3
4. Depth-first search: Directed graphs
The algorithm is essentially the same as for undirected graphs, the
difference residing in the interpretation of the word "adjacent".
In a directed graph, node w is adjacent to node v if the directed edge (v,
w) exists.
If (v, w) exists but (w, v) does not, then w is adjacent to v
but v is not adjacent to w.
With this change of interpretation the procedures dfs and search apply
equally well in the case of a directed graph.
23. Breadth-first search
23
• In graph theory, breadth-first
search (BFS) is a graph search
algorithm that begins at the
root node and explores all the
neighboring nodes.
• Then for each of those
nearest nodes, it explores
their unexplored neighbor
nodes, and so on, until it finds
the goal.