Proving That Equal DFS and BFS Trees Force a Graph to Be a Tree

Claim: if a graph G produces the same DFS and BFS tree, then G is a tree.

The underlying observation. Work through any graph containing a cycle and you will never get identical BFS and DFS trees from it. Conversely, for a graph with no cycles, the two traversals always agree. That is the fact the proof rests on.

Proof by contradiction. Let T be the tree produced by the BFS and DFS traversals of G, and suppose for contradiction that G has at least one edge more than T.

Adding a single extra edge to a tree necessarily creates a cycle — that is a basic property of trees, since every pair of vertices in T is already joined by exactly one path, and the new edge closes a second one. So G would contain a cycle.

But by the observation established above, no graph containing a cycle can yield the same DFS and BFS tree. Our assumption therefore produces a contradiction.

Hence G cannot have more edges than T. Since T is a spanning tree of G, G cannot have fewer either. Therefore G = T, and G is a tree. QED.

One clarification worth making explicit, because it is where this argument is often misread: the claim is not that every graph yields identical DFS and BFS trees. It is that when they coincide, the graph must be acyclic. A connected graph in which the two traversals agree has exactly n-1 edges and is, by definition, a tree.