Depth-First-Search(DFS) Explained With Visualization
Published in
2 min readJun 15, 2020
DFS Overview
The Depth First Search(DFS) is the most fundamental search algorithm used to explore the nodes and edges of a graph. It runs with time complexity of O(V+E), where V
is the number of nodes, and E
is the number of edges in a graph.
DFS
is often used as a building block in other algorithms; it can be used to:
- A naive solution for any searching problem.
- Finding connected components or strongly connected components.
- Topological sorting.
- Finding the bridges of a graph.
- Detect cycles in a graph.
DFS Visualization on Maze
The source is at the position of left-up, and the target is the position of right-bottom.
As we can see, DFS explores as far as possible along each branch before backtracking:
The maze is generated by disjoint set.
The recursive implementation
#include <list>
#include <vector>
#include <iostream>
using namespace std;