Algorithms
DFS: Depth-First Search
Цели урока
- Understand the difference between DFS and BFS
- Implement both recursive and iterative DFS
- Apply DFS for path finding and cycle detection
- Understand discovery/finish times
Предварительные знания
- Stack
- Graph representation
How do you escape a maze without a map? Walk forward until a dead end, then back to the last fork. This intuitive method is the foundation of DFS, one of two fundamental graph traversal algorithms.
- Compilers: module dependency analysis
- Games: maze generation
- AI: game trees (minimax)
- Web crawlers: following links depth-first
Trémaux's maze rule and the Hopcroft-Tarjan era
The depth-first idea predates computers. In the 19th century the French engineer Charles Pierre Trémaux worked out a rule for solving mazes by hand: mark every passage on each pass, never take a fully marked passage twice, and back up once the fresh corridors run out. Édouard Lucas published the method in his Récréations Mathématiques around 1882. That is DFS with edge marking, decades before any machine could run it. The algorithm became central to computer science in the early 1970s, when John Hopcroft and Robert Tarjan turned DFS into a precision tool. Their 1973 work showed that a single DFS pass, augmented with discovery and finish times, solves a whole family of graph problems in linear time: biconnected components, bridges and articulation points, and planarity testing. Tarjan's 1972 strongly connected components algorithm and the topological sort by finish time both fall straight out of the same traversal. What had been a maze trick became the backbone of graph theory in practice.