2
$\begingroup$

I have a simple theoretical question in very basic algorithms, as the title mentions, is it always true that the depth of BFS is $\leq$ DFS?

From what I understand, the tricky part here is the possible cycles in the graph. Even though that I believe that the depth of BFS will always be less or equal to the depth of DFS.

In each iteration of BFS, from what I understand, the depth might grow by one, but DFS's grows in each vertex it can not reach, sometimes above the maximal value of BFS.

So, is it always true that the depth of BFS is $\leq$ DFS?

$\endgroup$

1 Answer 1

4
$\begingroup$

A result about BFS is that what you call the depth of the BFS of a node $v$ is the length of a shortest path from the source to $v$.

Assume the exists an instance where the depth of the DFS $<$ BFS. Then we have a path from the source to $v$ that has length strictly smaller (the path taken by the DFS to reach $v$) than the length of a shortest path from the source to $v$ (the path taken by the BFS), which is impossible.

This argument by contradiction shows that depth BFS $\leq$ DFS, for any graph.

$\endgroup$
1
  • $\begingroup$ thank you for your answer $\endgroup$
    – hps13
    Commented Jul 10, 2019 at 8:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.