One of the most common and simple operations executed on unweighted graphs is the Breadth-first search.
An aspect of the algorithm that is left to the practical implementation is how to implement the queue and especially, what capacity it should have. Specifically, for a given graph with N
nodes if one allocates a queue with capacity N
for sure there won't be a time where there is a need to re-allocate the queue because it has reached capacity, but if the N
value is high enough this might lead to an excessive RAM requirement if the graph has a structure that leads to little use for the queue (for instance filiform graphs full of tendrils), especially if the BFS only returns the maximum length (like needed in the computation of a graph diameter) or the total length (like needed in the computation of a Closeness centrality).
Is there any good paper on the expected optimal size for the queue in the BFS algorithm, based on some property of the given graph (even with pre-processing)?
If there is any, does it generalize for other shortest-path tree algorithms in contexts such as Dijkstra?
Making my specific implementation details a bit more concrete, I am working on a rust algorithm using as queue the VecDeque. I am not aware of any better queue for the purposes of the BFS, as there is no need for sorting etc...