Depth First Search (DFS) Algorithm in Python
11 December, 2022
11
11
0
Contributors
Overview
What is Depth First Search?
Finding an end point in the Maze using DFS.
Algorithm of DFS
1.
We add the node to the top of the stack.
2.
Marked it as visited.
3.
We check if this node has any adjacent nodes:
If it has adjacent nodes, check to make sure they have not been visited already, and then visit them.
We remove it from the stack if it has no adjacent nodes.
Implementation of DFS
Time & Space Complexity
Time:
Space:
Applications of DFS
•
Cycle Detection in graphs, Scheduling Problems, and Solving Puzzles with just one solution, such as a maze or a Sudoku puzzle, all use depth-first search.
•
Other uses involve Network Analysis, such as detecting if a graph is bipartite.
•
Depth-first searches are used in route mapping, scheduling, and finding spanning trees.
Wrap Up
Follow me on Twitter (@triposat).
python
graphs
develevate
datastructure
dfs