Table of Contents
Fetching ...

BFS versus DFS for random targets in ordered trees

Stoyan Dimitrov, Martin Minchev, Yan Zhuang

TL;DR

This paper analyzes the average-case efficiency of BFS versus DFS when locating a uniformly random target at a fixed level $\ell$ in ordered trees with $n$ edges. It derives exact formulas for the total BFS and DFS scores using lattice-path bijections and Brownian excursion results, and develops a generating-function framework linked to Fibonacci polynomials to extract these quantities. A central finding is a unique threshold $\lambda\approx 0.789004$ such that BFS is asymptotically faster when $\ell \le \lambda\sqrt{n}$, and it extends the analysis to Galton–Watson trees while introducing a truncated DFS variant that outperforms both BFS and DFS when the level is known. These results connect discrete tree-search problems to continuum limits and offer precise guidance for algorithm choice in level-constrained tree searches.

Abstract

Consider a search from the root of an ordered tree with $n$ edges to some target node at a fixed distance $\ell$ from that root. We compare the average time complexity of the breadth-first search (BFS) and depth-first search (DFS) algorithms, when the target node is selected uniformly at random among all nodes at level $\ell$ in the ordered trees with $n$ edges. Intuition suggests that BFS should have better average performance when $\ell$ is small, while DFS must have an advantage when $\ell$ is large. But where exactly is the threshold, as a function of $n$, and is it unique? We obtain explicit formulas for the expected number of steps of both BFS and DFS, by using results on the occupation measure of Brownian excursions, as well as a combinatorial proof of an identity related to lattice paths. This allows us to show that there exists a unique constant $λ\approx 0.789004$, such that in expectation BFS is asymptotically faster than DFS if and only if $\ell\leq λ\sqrt{n}$. Furthermore, we find the asymptotic average time complexity of BFS in the given setting for any class of Galton$\unicode{x2013}$Watson trees, including binary trees and ordered trees. Finally, we introduce the truncated DFS algorithm, which performs better than both BFS and DFS when $\ell$ is known in advance, and we find a formula evaluating the average time complexity of this algorithm.

BFS versus DFS for random targets in ordered trees

TL;DR

This paper analyzes the average-case efficiency of BFS versus DFS when locating a uniformly random target at a fixed level in ordered trees with edges. It derives exact formulas for the total BFS and DFS scores using lattice-path bijections and Brownian excursion results, and develops a generating-function framework linked to Fibonacci polynomials to extract these quantities. A central finding is a unique threshold such that BFS is asymptotically faster when , and it extends the analysis to Galton–Watson trees while introducing a truncated DFS variant that outperforms both BFS and DFS when the level is known. These results connect discrete tree-search problems to continuum limits and offer precise guidance for algorithm choice in level-constrained tree searches.

Abstract

Consider a search from the root of an ordered tree with edges to some target node at a fixed distance from that root. We compare the average time complexity of the breadth-first search (BFS) and depth-first search (DFS) algorithms, when the target node is selected uniformly at random among all nodes at level in the ordered trees with edges. Intuition suggests that BFS should have better average performance when is small, while DFS must have an advantage when is large. But where exactly is the threshold, as a function of , and is it unique? We obtain explicit formulas for the expected number of steps of both BFS and DFS, by using results on the occupation measure of Brownian excursions, as well as a combinatorial proof of an identity related to lattice paths. This allows us to show that there exists a unique constant , such that in expectation BFS is asymptotically faster than DFS if and only if . Furthermore, we find the asymptotic average time complexity of BFS in the given setting for any class of GaltonWatson trees, including binary trees and ordered trees. Finally, we introduce the truncated DFS algorithm, which performs better than both BFS and DFS when is known in advance, and we find a formula evaluating the average time complexity of this algorithm.
Paper Structure (12 sections, 19 theorems, 129 equations, 7 figures, 3 tables)

This paper contains 12 sections, 19 theorems, 129 equations, 7 figures, 3 tables.

Key Result

Theorem 1.1

The total number of nodes at level $\ell$ among trees in $\mathcal{T}_{n}$ is equal to

Figures (7)

  • Figure 1: The $\mathop{\mathrm{bfsScore}}\nolimits$ and $\mathop{\mathrm{dfsScore}}\nolimits$ of each node in a tree with $9$ edges.
  • Figure 2: The target node is selected uniformly at random among the five circled nodes at level $\ell=2$, when we have $n=3$ edges.
  • Figure 3: A Dyck path that is an image of an ordered tree under the folklore bijection. The $\mathop{\mathrm{dfsScore}}\nolimits$ of the tree node corresponding to the point marked with a cross is equal to the number of $U$ steps in the prefix of the path ending at that point.
  • Figure 4: The decomposition $P=P'P"$ of the paths crossing $y=l$ counted by the two sides of Equation \ref{['eq:paths']}.
  • Figure 5: The node $x$ in the tree $T$ has a corresponding node $x'$ in the tree $T'$, and the dfsScores of $x$ and $x'$ are related. In this way, almost all nodes on a given level among the trees with $n$ edges can be paired up.
  • ...and 2 more figures

Theorems & Definitions (38)

  • Theorem 1.1: Dershowitz--Zaks dersh
  • Theorem 1.2
  • Lemma 2.1: krat
  • Theorem 2.2
  • proof
  • Theorem 3.1
  • Remark 3.2
  • proof : Proof of Theorem \ref{['thm: totalB']}
  • proof : Proof of Theorem \ref{['th:threshold']}
  • Theorem 4.1: flajSed
  • ...and 28 more