Depth First Search

DFS Stack

Depth-First Search (DFS) is a fundamental algorithm used for traversing or searching through graph data structures. It explores as far as possible along each branch before backtracking, which means it goes deep into a graph or tree before exploring its siblings.DFS is particularly useful for tasks like pathfinding, cycle detection, and solving puzzles like mazes.

DFS(graph, node, visited):
    // Mark the current node as visited
    visited[node] = true

    // Visit all the neighbors of the current node
    for each neighbor in graph[node]:
        if not visited[neighbor]:
            DFS(graph, neighbor, visited)