Skip to main content
Log in

Space Efficient Linear Time Algorithms for BFS, DFS and Applications

  • Published:
Theory of Computing Systems Aims and scope Submit manuscript

Abstract

Research on space efficient graph algorithms, particularly for st-connectivity, has a long history including the celebrated polynomial time, O(lg n) bits1 algorithm in undirected graphs by Reingold J. JACM. 55(4) (2008), and polynomial time, \(n/2^{\Theta (\sqrt {\lg n})}\) bits algorithm in directed graphs by Barnes et al. SICOMP. 27(5), 1273–1282 (1998). Recent works by Asano et al. ISAAC (2014) and Elmasry et al. STACS (2015), reconsidered classical fundamental graph algorithms focusing on improving the space complexity. Elmasry et al. gave, among others, an implementation of breadth first search (BFS) in a graph G with n vertices and m edges, taking the optimal O(m + n) time using O(n) bits improving the naïve O(n lg n) bits implementation. Similarly, Asano et al. provided space efficient implementations for performing depth first search (DFS) in a graph G. We continue this line of work focusing on improving the space requirement for implementing a few classical graph algorithms. Our first result is a simple data structure that can maintain any subset S of a universe of u elements using just u + o(u) bits and supports 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). It can also enumerate all elements present currently in the set in no particular order in O(k + 1) time where k is the number of elements currently belonging to the set. While this data structure supports a weaker set of operations than that of Elmasry et al. STACS (2015), it is simple, more space efficient and is sufficient to support a BFS implementation optimally in O(m + n) time using at most 2n + o(n) bits. Later, we further improve the space requirement of BFS to at most n lg 3 + o(n) bits albeit with a slight increase in running time to O(m lg nf(n)) time where f(n) is any extremely slow growing function of n, and the o term in the space is a function of f(n). We also discuss a similar time-space tradeoff result for finding a minimum weight spanning tree of a weighted (bounded by polynomial in n) undirected graph using n + O(n/f(n)) bits and O(m lg nf(n)) time, for any function f(n) such that 1 ≤ f(n) ≤ n. For DFS in a graph G, we provide an implementation taking O(m + n) time and O(n lg m/n) bits. This partially answers at least for sparse graphs, a question asked by Asano et al. ISAAC (2014) whether DFS can be performed in O (m + n) time and using O(n) bits, and also simultaneously improves the DFS result of Elmasry et al. STACS (2015). Using our DFS algorithm and other careful implementations, we show how one can also test for biconnectivity, 2-edge connectivity, and find cut vertices and bridges of a given undirected graph within the same time and space bounds; earlier classical linear time algorithms for these problems used Ω(n lg n) bits of space.

This is a preview of subscription content, log in via an institution to check access.

Access this article

Price excludes VAT (USA)
Tax calculation will be finalised during checkout.

Instant access to the full article PDF.

Fig. 1
Fig. 2

Similar content being viewed by others

Notes

  1. Since our initial version of the paper, Hagerup and Kammer [35] have independently reported a similar structure with u + o(u) bits for the data structure. See Section 1.3 for more details.

  2. The data structure requires O (lg n) precomputed word constants, thus, the second term isO(lg 2n).

  3. We thank Szymon Grabowski for bringing [32] to our attention.

  4. Note that to implement this step, we only require the select0 operation in Theorem 2.

References

  1. Aho, A.V., Hopcroft, J.E., Ullman, J.D.: The design and analysis of computer algorithms. Addison-Wesley, Boston (1974)

    MATH  Google Scholar 

  2. Arora, S., Barak, B.: Computational complexity - a modern approach. Cambridge University Press, Cambridge (2009)

    Book  MATH  Google Scholar 

  3. Asano, T., Buchin, K., Buchin, M., Korman, M., Mulzer, W., Rote, G., Schulz, A.: Reprint of: Memory-constrained algorithms for simple polygons. Comput. Geom. 47(3), 469–479 (2014)

    Article  MathSciNet  MATH  Google Scholar 

  4. Asano, T., Izumi, T., Kiyomi, M., Konagaya, M., Ono, H., Otachi, Y., Schweitzer, P., Tarui, J., Uehara, R.: Depth-first search using O(n) bits. In: 25th ISAAC, LNCS, vol. 8889, pp 553–564 (2014)

  5. Asano, T., Kirkpatrick, D.G., Nakagawa, K., Watanabe, O.: Ȯ( n)-space and polynomial-time algorithm for planar directed graph reachability. In: 39th MFCS LNCS, vol. 8634, pp 45–56 (2014)

  6. Asano, T., Mulzer, W., Rote, G., Wang, Y.: Constant-work-space algorithms for geometric problems. JoCG 2(1), 46–68 (2011)

    MathSciNet  MATH  Google Scholar 

  7. Banerjee, N., Chakraborty, S., Raman, V.: Improved space efficient algorithms for BFS, DFS and applications. In: 22nd COCOON, vol. 9797, pp 119–130. Springer, LNCS (2016)

  8. Banerjee, N., Chakraborty, S., Raman, V., Roy, S., Saurabh, S.: Time-space tradeoffs for dynamic programming in trees and bounded treewidth graphs. In: 21st COCOON, vol. 9198, pp 349–360. Springer, LNCS (2015)

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

    Article  MathSciNet  MATH  Google Scholar 

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

    Article  MathSciNet  MATH  Google Scholar 

  11. Batagelj, V., Zaversnik, M.: An O(m) algorithm for cores decomposition of networks. CoRR, arXiv:0310049 (2003)

  12. Brandes, U.: Eager st-ordering. In: ESA, pp 247–256 (2002)

  13. Brodal, G.S., Chaudhuri, S., Radhakrishnan, J.: The randomized complexity of maintaining the minimum. Nord. J. Comput. 3(4), 337–351 (1996)

    MathSciNet  MATH  Google Scholar 

  14. Brodnik, A., Carlsson, S., Demaine, E.D., Munro, J.I., Sedgewick, R.: Resizable arrays in optimal time and space. In: 6th WADS, LNCS, vol. 1663, pp 37–48 (1999)

  15. Brȯnnimann, H., Chan, T.M.: Space-efficient algorithms for computing the convex hull of a simple polygonal line in linear time. Comput. Geom. 34(2), 75–82 (2006)

    Article  MathSciNet  Google Scholar 

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

  17. Chakraborty, S., Jo, S., Satti, S.R.: Improved space-efficient linear time algorithms for some classical graph problems. In: 15th CTW (2017)

  18. Chakraborty, S., Raman, V., Satti, S.R.: Biconnectivity, chain decomposition and st-numbering using O(n) bits. In: 27th ISAAC, pp 22:1–22:13 (2016)

  19. Chakraborty, S., Raman, V., Satti, S.R.: Biconnectivity, st-numbering and other applications of DFS using O(n) bits. J. Comput. Syst. Sci. 90, 63–79 (2017)

    Article  MathSciNet  MATH  Google Scholar 

  20. Chakraborty, S., Satti, S.R.: Space-efficient algorithms for maximum cardinality search, stack BFS, queue BFS and applications. In: 23rd COCOON, pp 87–98 (2017)

  21. Chan, T.M., Munro, J.I., Raman, V.: Selection and sorting in the ”restore” model. In: 25th SODA, pp 995–1004 (2014)

  22. Clark, D.: Compact pat trees. PhD thesis. University of Waterloo, Canada (1996)

    Google Scholar 

  23. Cormen, T.H., Leiserson, C.E., Rivest, R.L., Stein, C.: Introduction to algorithms, 3rd edn. MIT Press, Cambridge (2009)

    MATH  Google Scholar 

  24. Darwish, O., Elmasry, A.: Optimal time-space tradeoff for the 2d convex-hull problem. In: 22nd ESA, pp 284–295 (2014)

  25. Dodis, Y., Patrascu, M., Thorup, M.: Changing base without losing space. In: 42nd STOC, pp 593–602 (2010)

  26. Ellen, F.: Constant time operations for words of length w. Unpublished Manuscript

  27. Elmasry, A., Hagerup, T., Kammer, F.: Space-efficient basic graph algorithms. In: 32nd STACS, pp 288–301 (2015)

  28. Elmasry, A., Juhl, D.D., Katajainen, J., Satti, S.R.: Selection from read-only memory with limited workspace, vol. 554, pp 64–73 (2014)

  29. Elmasry, A., Kammer, F.: Space-efficient plane-sweep algorithms. In: 27th ISAAC, pp 30:1–30:13 (2016)

  30. Eppstein, D., Loffler, M., Strash, D: Listing all maximal cliques in large sparse real-world graphs. ACM Journal of Experimental Algorithmics, 18 (2013)

  31. Frederickson, G.N.: Upper bounds for time-space trade-offs in sorting and selection. J. Comput. Syst. Sci. 34(1), 19–26 (1987)

    Article  MathSciNet  MATH  Google Scholar 

  32. Fredriksson, K., Kilpelȧinen, P.: Practically efficient array initialization. Softw. Pract. Exper. 46(4), 435–467 (2016)

    Article  Google Scholar 

  33. Gabow, H.N.: Path-based depth-first search for strong and biconnected components. Inf. Process. Lett. 74(3-4), 107–114 (2000)

    Article  MathSciNet  MATH  Google Scholar 

  34. Gupta, A., Hon, W., Shah, R., Vitter, J.S.: A framework for dynamizing succinct data structures. In: 34th ICALP, pp 521–532. Proceedings, Wroclaw (2007)

  35. Hagerup, T., Kammer, F.: Succinct choice dictionaries. CoRR, arXiv:1604.06058 (2016)

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

    Article  MathSciNet  MATH  Google Scholar 

  37. Kammer, F., Kratsch, D., Laudahn, M.: Space-efficient biconnected components and recognition of outerplanar graphs. In: 41st MFCS, pp 56:1–56:14 (2016)

  38. Mehlhorn, K.: Data structures and algorithms 1: sorting and searching, volume 1 of eatcs monographs on theoretical computer science. Springer, Berlin (1984)

    Book  Google Scholar 

  39. Mortensen, C.W., Pagh, R., Patrascu, M.: On dynamic range reporting in one dimension. In: 37th STOC, pp 104–111 (2005)

  40. Munro, J.I.: Tables. In: FSTTCS LNCS, vol. 1180, pp 37–42 (1996)

  41. Munro, J.I., Paterson, M.: Selection and sorting with limited storage. Theor. Comput. Sci. 12, 315–323 (1980)

    Article  MathSciNet  MATH  Google Scholar 

  42. Munro, J.I., Raman, V.: Selection from read-only memory and sorting with minimum data movement. Theor. Comput. Sci. 165(2), 311–323 (1996)

    Article  MathSciNet  MATH  Google Scholar 

  43. Muthukrishnan, S.: Data streams: Algorithms and applications. Found. Trends Theor. Comput. Sci. 1(2) (2005)

  44. Poyias, A., Puglisi, S.J., Raman, R.: Compact dynamic rewritable (cdrw) arrays To appear in 19th ALENEX (2017)

  45. Raman, R., Raman, V., Rao, S.S.: Succinct dynamic data structures. In: 7th WADS, LNCS 2125, pp 426–437 (2001)

  46. Reingold, O.: Undirected connectivity in log-space. J. ACM 55(4), 17:1–17:24 (2008)

    Article  MathSciNet  MATH  Google Scholar 

  47. Schmidt, J.M.: Structure and constructions of 3-connected graphs. PhD thesis, Free University of Berlin, Berlin (2011)

    Google Scholar 

  48. Schmidt, J.M.: A simple test on 2-vertex- and 2-edge-connectivity. Inf. Process. Lett. 113(7), 241–244 (2013)

    Article  MathSciNet  MATH  Google Scholar 

  49. Tarjan, R.E.: Depth-first search and linear graph algorithms. SIAM J. Comput. 1(2), 146–160 (1972)

    Article  MathSciNet  MATH  Google Scholar 

  50. Tarjan, R.E.: A note on finding the bridges of a graph. Inf. Process. Lett. 2 (6), 160–161 (1974)

    Article  MathSciNet  MATH  Google Scholar 

  51. Willard, D.E.: Log-logarithmic worst-case range queries are possible in space theta(n). Inf. Process. Lett. 17(2), 81–84 (1983)

    Article  MATH  Google Scholar 

Download references

Author information

Authors and Affiliations

Authors

Corresponding author

Correspondence to Sankardeep Chakraborty.

Additional information

We use lg to denote logarithm to the base 2.

Some of these results were announced in preliminary form in the proceedings of 22nd International Computing and Combinatorics Conference (COCOON 2016), Springer LNCS volume 9797, pages 119-130 [7], and 27th International Symposium on Algorithms and Computation (ISAAC 2016), LIPIcs, volume 64, pages 22:1–22:13 [18]. More specifically, this paper contains the linear time algorithms for DFS and its applications, announced in [7] and [18], along with the linear time algorithms for BFS and afew other graph problems, announced in [7].

Rights and permissions

Reprints and permissions

About this article

Check for updates. Verify currency and authenticity via CrossMark

Cite this article

Banerjee, N., Chakraborty, S., Raman, V. et al. Space Efficient Linear Time Algorithms for BFS, DFS and Applications. Theory Comput Syst 62, 1736–1762 (2018). https://doi.org/10.1007/s00224-017-9841-2

Download citation

  • Published:

  • Issue Date:

  • DOI: https://doi.org/10.1007/s00224-017-9841-2

Keywords

Navigation