Nepedia
Back to Discover
Advanced Reader
🔢

Depth-first search

math Maturity 7-9 Next article →

Depth-first search, or DFS, is a fundamental algorithm used to explore or traverse data structures known as trees and graphs. A graph is a collection of points, called vertices, connected by lines, called edges. The core logic of DFS is to start at a specific point, often called the root, and travel as deep as possible along a single branch before turning back. This process of turning back is known as backtracking. By following this method, the algorithm can systematically visit the different parts of a complex network.

graph.traversal.example
graph.traversal.example

To manage this exploration, the algorithm requires extra memory to keep track of its progress. It typically uses a data structure called a stack. A stack follows a specific order where the last item added is the first one removed. This is essential for backtracking because it allows the algorithm to remember the exact path it took to reach a dead end. When the search reaches a point with no new directions, it pops the last location off the stack to return to a previous junction. Without this memory, a search might enter an infinite loop, circling the same points forever without ever reaching new areas.

Depth-First-Search
Depth-First-Search

There are different ways to implement this search depending on the needs of the computer. A recursive implementation uses a function that calls itself to move deeper into the graph. An iterative implementation, however, uses a manual stack to manage the vertices. These two methods can actually visit neighbors in different orders. For example, in a specific graph with nodes A, B, D, F, E, C, and G, a recursive search might visit them in one sequence, while an iterative search might follow a different path.

graph.traversal.example
graph.traversal.example

History shows that these ideas have been used for solving physical puzzles for a long time. In the 19th century, a French mathematician and telegraph engineer named Charles Pierre Trémaux investigated DFS. He lived from 1859 to 1882 and studied these methods as a strategy for solving mazes. His work helped define how one might navigate through complex, branching paths. Today, his legacy lives on in how we program computers to handle digital networks.

In theoretical computer science, the efficiency of DFS is measured by time and space complexity. The time it takes to complete a search is linear, meaning it depends on the number of vertices and edges in the graph. The space needed depends on how much information the computer must store. In some cases, such as web-crawling or artificial intelligence, the graph might be too large or even infinite. To prevent the computer from running out of memory or disk space, engineers often use a limited-depth search. This version only searches to a specific depth limit. This makes the space complexity proportional to that limit rather than the size of the entire graph.

Depth-First-Search
Depth-First-Search

When a DFS is finished, the paths it took form a structure called a spanning tree. This tree allows us to categorize the edges of the original graph into three distinct types. Forward edges point from a node to one of its descendants in the tree. Back edges point from a node back to one of its ancestors. Cross edges are those that do neither. If the original graph is undirected, all edges will be either tree edges or back edges.

Tree edges
Tree edges

DFS is also used to create specific orderings of vertices. A preordering lists vertices in the order they were first visited. A postordering lists them in the order they were last visited. These orderings are useful for organizing data, such as expression trees in Polish notation. In directed acyclic graphs, a reverse postordering can produce a topological sorting. This is a way of lining up tasks so that every dependency is respected.

If-then-else-control-flow-graph
If-then-else-control-flow-graph

Because of its versatility, DFS serves as a building block for many advanced tasks. It is used to find connected components and to identify bridges in a graph. Scientists also use it to determine how closely related different species are in a phylogenetic tree. It can even be used to test for planarity, which asks if a graph can be drawn without edges crossing. Whether it is solving a puzzle or mapping the web, DFS remains a vital tool in the world of mathematics and computing.

708 words

Where will your curiosity go next?

Keep exploring at your chosen reading level.

🖼️ Images & Media (6)
File:Depth-First-Search.gif
Depth-First-Search.gif
File:graph.traversal.example.svg
graph.traversal.example.svg
File:Tree edges.svg
Tree edges.svg
File:If-then-else-control-flow-graph.svg
If-then-else-control-flow-graph.svg
File:Graph.traversal.example.svg
Graph.traversal.example.svg
MAZE 30x20 DFS.ogv
More to explore

What is Nepedia?

A free, ad-free encyclopedia for children. Every article is written at five reading levels, so the same page works for a five-year-old and a fifteen-year-old — use the level switcher above to see this one change. No account needed to read.