All lessons

10. Graphs

DFS traversal

0 of 5 activities0%

Reading 1

Path-oriented exploration

Open

DFS explores as far as possible along a branch before backtracking.

Recursive DFS is natural; iterative uses a stack.

Use for connectivity, cycle detection (with colors/states), topological ideas, components.

void dfs(int u){
  vis[u]=true;
  for(int v:g[u]) if(!vis[v]) dfs(v);
}

Check 2

Structure

Open

Iterative DFS uses a

Fill in 3

Mark

Open

To avoid reprocessing nodes, DFS uses a

Try it 4

DFS recurse

Open

Visit all from 0.

main.cpp
Loading editor…
Output will appear here.

Assignment 5

Component size on path

Open

Read n. In a path of n nodes, component size is n — print n.

main.cpp
Loading editor…