DSC 40B

Problems tagged with "trees"

Problem #262

Tags: depth first search, lecture-13, trees

What is the time complexity of depth first search (DFS) when run on a tree with \(n\) nodes?

Solution

\(\Theta(n)\).

DFS takes \(\Theta(V + E)\) time, and a tree with \(n\) nodes has \(n - 1\) edges, so it takes \(\Theta(n + (n - 1)) = \Theta(n)\) time.