Improved Space Efficient Algorithms for BFS, DFS and Applications

  • Niranka BanerjeeEmail author
  • Sankardeep Chakraborty
  • Venkatesh Raman
Conference paper
Part of the Lecture Notes in Computer Science book series (LNCS, volume 9797)


Recent work by Elmasry et al. (STACS 2015) and Asano et al. (ISAAC 2014), reconsidered classical fundamental graph algorithms focusing on improving the space complexity. Elmasry et al. gave, among others, implementations of breadth first search (BFS) and depth first search (DFS) in a graph on n vertices and m edges, taking \(O(m+n)\) time using O(n) and \(O(n \lg \lg n)\) bits of space respectively improving the naive \(O(n \lg n)\)(We use \(\lg \) to denote logarithm to the base 2.) bits implementation. We continue this line of work focusing on space.

Our first result is a simple data structure that can maintain any subset S of a universe of n elements using \(n+o(n)\) bits and support in constant time, apart from the standard insert, delete and membership queries, the operation findany that finds and returns any element of the set (or outputs that the set is empty). Using this we give a BFS implementation that takes \(O(m+n)\) time using at most \(2n+o(n)\) bits. Later, we further improve the space requirement of BFS to at most \(1.585n + o(n)\) bits albeit with a slight increase in running time to \(O(m \lg n f(n))\) time where f(n) is any extremely slow growing function of n. These improve the space by a constant factor from earlier representations.

We demonstrate the use of our data structure by developing another data structure using it that can represent a sequence of n non-negative integers \(x_1, x_2, \ldots x_n\) using at most \(\sum _{i=1}^n x_i + 2n + o(\sum _{i=1}^n x_i+n)\) bits and, in constant time, determine whether the i-th element is 0 or decrement it otherwise. We use this data structure to output the vertices of a
  • directed acyclic graph in topological sorted order in \(O(m+n)\) time and \(O(m+n)\) bits, and

  • graph with degeneracy d in degeneracy order in O(nd) time using O(nd) bits.

We also discuss an algorithm for finding a minimum weight spanning tree of a weighted undirected graph using at most \(n+o(n)\) bits.

For DFS we give an \(O(m+n)\) bits implementation for finding a chain decomposition of a connected undirected graph, and to find cut vertices, bridges and maximal two connected subgraphs of a connected graph. We also provide a O(n) bits implementations for finding strongly connected components of a directed graph, to output the vertices of a directed acyclic graph in a topologically sorted manner, and to find a sparse biconnected subgraph of a biconnected graph. These improve the space required for earlier implementations from \(\varOmega (n \lg n)\) bits.


Undirected Graph Input Graph Depth First Search Black Vertex Minimum Span Tree Problem 
These keywords were added by machine and not by the authors. This process is experimental and the keywords may be updated as the learning algorithm improves.



The authors thank Saket Saurabh for suggesting the exploration of algorithms using \(O(m+n)\) bits for DFS and Anish Mukherjee for helpful discussions.


  1. 1.
    Arora, S., Barak, B.: Computational Complexity - A Modern Approach. Cambridge University Press, Cambridge (2009)CrossRefzbMATHGoogle Scholar
  2. 2.
    Asano, T., et al.: Depth-first search using O(n) bits. In: Ahn, H.-K., Shin, C.-S. (eds.) ISAAC 2014. LNCS, vol. 8889, pp. 553–564. Springer, Heidelberg (2014)Google Scholar
  3. 3.
    Asano, T., Kirkpatrick, D., Nakagawa, K., Watanabe, O.: \(\widetilde{O}(\sqrt{n})\)-space and polynomial-time algorithm for planar directed graph reachability. In: Ésik, Z., Csuhaj-Varjú, E., Dietzfelbinger, M. (eds.) MFCS 2014, Part II. LNCS, vol. 8635, pp. 45–56. Springer, Heidelberg (2014)Google Scholar
  4. 4.
    Asano, T., Mulzer, W., Rote, G., Wang, Y.: Constant-work-space algorithms for geometric problems. JoCG 2(1), 46–68 (2011)MathSciNetGoogle Scholar
  5. 5.
    Banerjee, N., Chakraborty, S., Raman, V., Roy, S., Saurabh, S.: Time-space tradeoffs for dynamic programming algorithms in trees and bounded treewidth graphs. In: Xu, D., Du, D., Du, D. (eds.) COCOON 2015. LNCS, vol. 9198, pp. 349–360. Springer, Heidelberg (2015)CrossRefGoogle Scholar
  6. 6.
    Barba, L., Korman, M., Langerman, S., Sadakane, K., Silveira, R.I.: Space-time trade-offs for stack-based algorithms. Algorithmica 72(4), 1097–1129 (2015)MathSciNetCrossRefzbMATHGoogle Scholar
  7. 7.
    Barnes, G., Buss, J., Ruzzo, W., Schieber, B.: A sublinear space, polynomial time algorithm for directed s-t connectivity. SICOMP 27(5), 1273–1282 (1998)MathSciNetCrossRefzbMATHGoogle Scholar
  8. 8.
    Batagelj, V., Zaversnik, M.: An \({\rm O}(m)\) algorithm for cores decomposition of networks. CoRR cs.DS/0310049 (2003)Google Scholar
  9. 9.
    Brandes, U.: Eager \(st\)-ordering. In: Möhring, R.H., Raman, R. (eds.) ESA 2002. LNCS, vol. 2461, pp. 247–256. Springer, Heidelberg (2002)CrossRefGoogle Scholar
  10. 10.
    Brodnik, A., Carlsson, S., Demaine, E.D., Munro, J.I., Sedgewick, R.D.: Resizable arrays in optimal time and space. In: Dehne, F., Gupta, A., Sack, J.-R., Tamassia, R. (eds.) WADS 1999. LNCS, vol. 1663, pp. 37–48. Springer, Heidelberg (1999)CrossRefGoogle Scholar
  11. 11.
    Chakraborty, D., Pavan, A., Tewari, R., Vinodchandran, N.V., Yang, L.: New time-space upperbounds for directed reachability in high-genus and H-minor-free graphs. In: FSTTCS, pp. 585–595 (2014)Google Scholar
  12. 12.
    Clark, D.: Compact pat trees. Ph.D. thesis, University of Waterloo, Canada (1996)Google Scholar
  13. 13.
    Cormen, T.H., Leiserson, C.E., Rivest, R.L., Stein, C.: Introduction to Algorithms, 3rd edn. MIT Press, Cambridge (2009)zbMATHGoogle Scholar
  14. 14.
    Darwish, O., Elmasry, A.: Optimal time-space tradeoff for the 2D convex-hull problem. In: Schulz, A.S., Wagner, D. (eds.) ESA 2014. LNCS, vol. 8737, pp. 284–295. Springer, Heidelberg (2014)Google Scholar
  15. 15.
    Elmasry, A., Hagerup, T., Kammer, F.: Space-efficient basic graph algorithms. In: 32nd STACS, pp. 288–301 (2015)Google Scholar
  16. 16.
    Eppstein, D., Loffler, M., Strash, D.: Listing all maximal cliques in large sparse real-world graphs. ACM J. Exp. Algorithmics 18, 1–3 (2013)Google Scholar
  17. 17.
    Frederickson, G.N.: Upper bounds for time-space trade-offs in sorting and selection. J. Comput. Syst. Sci. 34(1), 19–26 (1987)MathSciNetCrossRefzbMATHGoogle Scholar
  18. 18.
    Gupta, A., Hon, W.-K., Shah, R., Vitter, J.S.: A framework for dynamizing succinct data structures. In: Arge, L., Cachin, C., Jurdziński, T., Tarlecki, A. (eds.) ICALP 2007. LNCS, vol. 4596, pp. 521–532. Springer, Heidelberg (2007)CrossRefGoogle Scholar
  19. 19.
    Hagerup, T., Kammer, F.: Succinct choice dictionaries. CoRR abs/1604.06058 (2016)Google Scholar
  20. 20.
    Hon, W.K., Sadakane, K., Sung, W.K.: Succinct data structures for searchable partial sums with optimal worst-case performance. Theor. Comput. Sci. 412(39), 5176–5186 (2011)MathSciNetCrossRefzbMATHGoogle Scholar
  21. 21.
    Munro, J.I.: Tables. In: Chandru, V., Vinay, V. (eds.) FSTTCS 1996. LNCS, vol. 1180, pp. 37–42. Springer, Heidelberg (1996)CrossRefGoogle Scholar
  22. 22.
    Munro, J.I., Paterson, M.: Selection and sorting with limited storage. Theor. Comput. Sci. 12, 315–323 (1980)MathSciNetCrossRefzbMATHGoogle Scholar
  23. 23.
    Munro, J.I., Raman, V.: Selection from read-only memory and sorting with minimum data movement. Theor. Comput. Sci. 165(2), 311–323 (1996)MathSciNetCrossRefzbMATHGoogle Scholar
  24. 24.
    Reingold, O.: Undirected connectivity in log-space. J. ACM 55(4), 1–17 (2008)MathSciNetCrossRefzbMATHGoogle Scholar
  25. 25.
    Schmidt, J.M.: A simple test on 2-vertex- and 2-edge-connectivity. Inf. Process. Lett. 113(7), 241–244 (2013)MathSciNetCrossRefzbMATHGoogle Scholar

Copyright information

© Springer International Publishing Switzerland 2016

Authors and Affiliations

  • Niranka Banerjee
    • 1
    Email author
  • Sankardeep Chakraborty
    • 1
  • Venkatesh Raman
    • 1
  1. 1.The Institute of Mathematical SciencesCIT CampusChennaiIndia

Personalised recommendations