English

BFS versus DFS for random targets in ordered trees

Data Structures and Algorithms 2024-04-09 v1 Discrete Mathematics Combinatorics Probability

Abstract

Consider a search from the root of an ordered tree with nn 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 nn 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 nn, 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 λ0.789004\lambda\approx 0.789004, such that in expectation BFS is asymptotically faster than DFS if and only if λn\ell\leq \lambda\sqrt{n}. Furthermore, we find the asymptotic average time complexity of BFS in the given setting for any class of Galton\unicodex2013\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.

Keywords

Cite

@article{arxiv.2404.05664,
  title  = {BFS versus DFS for random targets in ordered trees},
  author = {Stoyan Dimitrov and Martin Minchev and Yan Zhuang},
  journal= {arXiv preprint arXiv:2404.05664},
  year   = {2024}
}

Comments

32 pages

R2 v1 2026-06-28T15:47:46.456Z