Switch language한국어
Back to the list

TOP 25 ALGORITMOS | Depth First Search (DFS)

TL;DR AI

Key summary

2 min read
  1. DFS starts from a given source and explores all vertices reachable from that source.

  2. An adjacency list is created for the graph; edges are added bidirectionally so the graph is treated as undirected.

  3. A visited array prevents revisiting vertices, and a dfsRec function traverses adjacent unvisited vertices recursively.

  4. Example setup: V = 5 with edges [[1,2], [1,0], [2,0], [2,3], [2,4]]; loops may exist in graphs.

  5. Time and space complexity are O(V+A) with auxiliary space used; advantage: time-bounded search and linear memory; disadvantage: a finite graph can produce an infinite solution.

Read the original