0

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...

Luca Cappelletti
  • 2,485
  • 20
  • 35
  • The max size of queue = max number of nodes in a level for all levels – Abhinav Mathur May 04 '21 at 09:40
  • Hello @AbhinavMathur, could you further explain what you mean? Of course, once the tree is computed for a given root one can know the maximal size of the heap, but at that point is useless information. Changing the root will change the maximal size of the heap. If you are referring to the max node degree instead, that is incorrect: a graph containing multiple nodes with max node degree can easily have a queue with a maximum size greater than max node degree. – Luca Cappelletti May 04 '21 at 10:15
  • I'm not talking about the max node degree. The maximum number of nodes in a level for a undirected graph will be the same irrespective of the root – Abhinav Mathur May 04 '21 at 10:35
  • How do you define the number of nodes in a level and how do you compute it? – Luca Cappelletti May 04 '21 at 11:08
  • Look at https://www.geeksforgeeks.org/count-number-nodes-given-level-using-bfs/ – Abhinav Mathur May 04 '21 at 11:13
  • Thank you @AbhinavMathur, but those are not constant with different roots. Picture executing BFS search on a star graph. Depending on if you start on the centre of the star you would get max level size = N -1, while the maximum starting on one of the leafs of the star would be N - 2. You can skew arbitrarily and get significantly skewed results. – Luca Cappelletti May 04 '21 at 12:55
  • The answer only changes by ```+-1``` depending on the root, but generally speaking the theoretical worst case will always be ```O(N)``` memory – Abhinav Mathur May 04 '21 at 15:26
  • Can you provide any theorem that proves that the maximal size of queue only changes `+/- 1`? If I had for instance multiple nodes connected, each one connected to other max-degree number of nodes the queue would grow bigger than the number of nodes in the given layer. Of course the theoretical worst case will always be O(N), but the point of the question was to get a stricter lower bound. – Luca Cappelletti May 05 '21 at 08:30
  • Another simple case where that would break down is when the graph is not connected. – Luca Cappelletti May 05 '21 at 08:50

0 Answers0