Graphs

Problem shape

Beginner
A
B
C
D
E
Core idea

Explore as far as possible before backtracking.

Step 1 / 5
Problem shape
Current lesson

Depth-First Search Lab

Graphs Beginner

Depth-First Search

Welcome to the Depth-First Search algorithm lab. Explore as far as possible before backtracking. Use it when: Use for connected components, cycle checks, topological ideas, and tree traversal. The time complexity is O(V + E), and the space complexity is O(V). The pseudocode is: dfs(node) mark node for next in graph[node] if not seen next dfs(next) As you practice, track the state that changes and the condition that makes the algorithm stop.

Time

O(V + E)

Space

O(V)

Pseudocode

dfs(node)
  mark node
  for next in graph[node]
    if not seen next
      dfs(next)
Open deeper structure lesson
1.0x