Graph Theory37 sections · 1633 units
Open in Course

DFS on Trees

A simpler graph

A tree is a special graph: fully connected, no cycles, exactly n1n-1 edges for nn vertices. DFS on trees is simpler than on general graphs. You do not need to worry about cycles destroying your algorithm.

However, in an undirected tree, there is still a technical "cycle" issue: if you are at vertex uu and came from vertex pp, there is an edge back to pp. Without tracking the parent, you would immediately recurse back. Solution: pass the parent vertex as a parameter and skip it when iterating neighbors.