All lessonsOpen Open Open Open Open
10. Graphs
DFS traversal
0 of 5 activities0%
Reading 1
Path-oriented exploration
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
Iterative DFS uses a
Fill in 3
Mark
Try it 4
DFS recurse
Visit all from 0.
main.cpp
Loading editor…
Output will appear here.
Assignment 5
Component size on path
Read n. In a path of n nodes, component size is n — print n.
main.cpp
Loading editor…