Depth First Search🎃

Tehleel Mir
1 min readMar 9, 2022

Depth-first search is a graph algorithm used in topological sorting, scheduling problems, cycle detection in graphs, and solving puzzles with only one solution, such as a maze or a sudoku puzzle.

Time complexity
The time complexity for the depth-first search is O(V+E). Where v is the number of vertices and e number of edges.

Explanation
Depth-first search is almost the same algorithm as breadth-first search and even both algorithms have the same time complexity, the only difference between these two is that breadth-first search algorithms check nodes/vertices level-wise and the depth-first search goes directly into the depth of the graph.
In depth-first search, there are also two main operations happing

  • checking all the vertices/nodes in the graph
  • checking all the edges of that node
  • so thus O(V+E) is the time complexity of this graph.

Ps: in breadth-first search, I have already written a huge explanation about the time complexity of that graph and the same can be applied here as well.

In the below coding example I have used an undirected graph, while as in breadth-first search algorithm I have used the directed graph as an example. But the graph coordinates are the same in both examples.

Code

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

No responses yet

Write a response