Nepedia
Back to Discover
Fluent Reader
🔢

Depth-first search

math Maturity 7-9 Next article →

Imagine you are exploring a vast, branching cave system. You decide to pick one tunnel and walk as far as you can.

Depth-First-Search
Depth-First-Search

You do not stop to look at every side tunnel right away. Instead, you push forward until you hit a dead end. When that happens, you backtrack to the last place where you had a choice. This way of searching is called a depth-first search. It is a smart way to explore structures called graphs. A graph is a collection of points connected by lines.

graph.traversal.example
graph.traversal.example

To make this search work, the computer needs a special tool. It uses something called a stack to remember the path it has taken. A stack works like a pile of trays in a cafeteria. You add new points to the top and take them off from the top too. This helps the search know exactly how to backtrack when it hits a wall. If the search does not remember where it has been, it might get stuck. It could walk in a circle forever and never find the exit.

Depth-First-Search
Depth-First-Search

People have studied this idea for a very long time. In the 1800s, a French mathematician named Charles Pierre Trémaux looked at this method. He was a telegraph engineer who lived from 1859 to 1882. He used these steps as a strategy to solve tricky mazes.

His work helped people understand how to navigate paths. Today, we use his ideas to help computers solve many different puzzles.

Computers use this search for many important jobs. They might use it to crawl through the huge web. Sometimes the web is so big it is almost infinite. In those cases, the search only goes to a certain depth. This means it only goes so far down a branch before it stops. This helps the computer save memory and disk space.

graph.traversal.example
graph.traversal.example

It also helps scientists study how different species of living things are related. They use it to build trees that show how life changes.

When a search is finished, it leaves behind a special map. This map is called a spanning tree. This tree helps us group the lines of the graph into different types. Some lines are called forward edges because they point to a descendant. Other lines are called back edges because they point to an ancestor. There are also cross edges that do neither.

Tree edges
Tree edges

By looking at these edges, we can understand the shape of the whole graph.

407 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.