TOP 25 ALGORITMOS | Depth First Search (DFS)

TL;DR AI
2 min readKey summary
DFS starts from a given source and explores all vertices reachable from that source.
An adjacency list is created for the graph; edges are added bidirectionally so the graph is treated as undirected.
A visited array prevents revisiting vertices, and a dfsRec function traverses adjacent unvisited vertices recursively.
Example setup: V = 5 with edges [[1,2], [1,0], [2,0], [2,3], [2,4]]; loops may exist in graphs.
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.
