, Volume 78, Issue 4, pp 1151–1181 | Cite as

How to Sort by Walking and Swapping on Paths and Trees

  • Daniel Graf


Consider a graph G with n vertices. On each vertex we place a box. The n vertices and n boxes are each numbered from 1 to n, and initially shuffled according to a permutation \(\pi \). A single robot is given the task to sort these boxes. In every step, the robot can walk along an edge of the graph and can carry at most one box at a time. At a vertex, it may swap the box placed there with the box it is carrying. How many steps does the robot need to sort all the boxes? We present efficient algorithms that construct such a shortest sorting walk if G is a path or a tree, and we show that the problem is \(\mathcal {NP}\)-complete for planar graphs. If we minimize the number of swaps in addition to the number of walking steps, it is \(\mathcal {NP}\)-complete even if G is a tree.


Physical sorting Shortest sorting walk Warehouse reorganization Robot scheduling Robot transportation problem Permutation properties Ensemble motion planning 



I want to thank Kateřina Böhmová and Peter Widmayer for many interesting and helpful discussions as well as the anonymous reviewers for their comments. I acknowledge the support of SNF Project 200021L_156620.


  1. 1.
    Atallah, M.J., Kosaraju, S.R.: Efficient solutions to some transportation problems with applications to minimizing robot arm travel. SIAM J. Comput. 17(5), 849–869 (1988)MathSciNetCrossRefzbMATHGoogle Scholar
  2. 2.
    Bender, M.A., Farach-Colton, M., Mosteiro, M.A.: Insertion sort is \(\cal{O}(n \log n)\). Theory Comput. Syst. 39(3), 391–397 (2006)MathSciNetCrossRefzbMATHGoogle Scholar
  3. 3.
    Bóna, M.: A survey of stack-sorting disciplines. Electron. J. Comb. 9(2), 16 (2003)MathSciNetzbMATHGoogle Scholar
  4. 4.
    Bulteau, L., Fertin, G., Rusu, I.: Pancake flipping is hard. J. Comput. Syst. Sci. 81, 1556–1574 (2015)Google Scholar
  5. 5.
    Cayley, A.: Note on the theory of permutations. Lond. Edinb. Dublin Philos. Mag. J. Sci. 34(232), 527–529 (1849)Google Scholar
  6. 6.
    Demaine, E.D., Demaine, M.L., Fox-Epstein, E., Hoang, D.A., Ito, T., Ono, H., Otachi, Y., Uehara, R., Yamada, T.: Polynomial-time algorithm for sliding tokens on trees. In: Proceedings of the 25th International Symposium on Algorithms and Computation, ISAAC 2014, Jeonju, Korea, December 15–17, 2014, pp. 389–400. Springer, Berlin (2014)Google Scholar
  7. 7.
    Demuth, H.B.: Electronic data sorting. Ph.D. thesis, Department of Electrical Engineering, Stanford University (1956)Google Scholar
  8. 8.
    Edmonds, J.: Optimum branchings. J. Res. Natl. Bur. Stand. B 71(4), 233–240 (1967)MathSciNetCrossRefzbMATHGoogle Scholar
  9. 9.
    Elizalde, S., Winkler, P.: Sorting by placement and shift. In: Proceedings of the Twentieth Annual ACM-SIAM Symposium on Discrete Algorithms, SODA 2009, New York, NY, USA, January 4–6, 2009, pp. 68–75. SIAM (2009)Google Scholar
  10. 10.
    Frederickson, G.N.: A note on the complexity of a simple transportation problem. SIAM J. Comput. 22(1), 57–61 (1993)Google Scholar
  11. 11.
    Frederickson, G.N., Guan, D.: Ensemble motion planning in trees. In: Proceedings of the 30th Annual Symposium on Foundations of Computer Science, FOCS 1989, Research Triangle Park, North Carolina, USA, 30 October–1 November 1989, pp. 66–71. IEEE (1989)Google Scholar
  12. 12.
    Frederickson, G.N., Guan, D.: Preemptive ensemble motion planning on a tree. SIAM J. Comput. 21(6), 1130–1152 (1992)MathSciNetCrossRefzbMATHGoogle Scholar
  13. 13.
    Frederickson, G.N., Guan, D.: Nonpreemptive ensemble motion planning on a tree. J. Algorithms 15(1), 29–60 (1993)MathSciNetCrossRefzbMATHGoogle Scholar
  14. 14.
    Frederickson, G.N., Hecht, M.S., Kim, C.E.: Approximation algorithms for some routing problems. In: Proceedings of the 17th Annual Symposium on Foundations of Computer Science, FOCS 1976, Houston, Texas, USA, 25–27 October 1976, pp. 216–227. IEEE (1976)Google Scholar
  15. 15.
    Gates, W.H., Papadimitriou, C.H.: Bounds for sorting by prefix reversal. Discrete Math. 27(1), 47–57 (1979)MathSciNetCrossRefzbMATHGoogle Scholar
  16. 16.
    Gatto, M., Maue, J., Mihalák, M., Widmayer, P.: Shunting for dummies: an introductory algorithmic survey. In: Ravindra K.A., Rolf H.M.Ö., Christos D.Z., (eds.) Robust and Online Large-Scale Optimization: Models and Techniques for Transportation Systems, Lecture Notes in Computer Science, pp. 310–337. Springer, Berlin (2009)Google Scholar
  17. 17.
    Graf, D.: How to sort by walking on a tree. In: Proceesings of the 23rd Annual European Symposium on Algorithms, ESA 2015, Patras, Greece, September 14–16, 2015, pp. 643–655. Springer, Berlin (2015)Google Scholar
  18. 18.
    Graf, D.: Sorting and scheduling algorithms for robotic warehousing systems. Master’s thesis, ETH Zürich (2015)Google Scholar
  19. 19.
    Guan, D.J.: Routing a vehicle of capacity greater than one. Discrete Appl. Math. 81(1), 41–57 (1998)MathSciNetCrossRefzbMATHGoogle Scholar
  20. 20.
    Haddon, B.K.: Cycle-sort: a linear sorting method. Comput. J. 33(4), 365–367 (1990)CrossRefGoogle Scholar
  21. 21.
    Hearn, R.A.: The complexity of sliding block puzzles and plank puzzles. In: Cipra, B., Demaine, E.D., Demaine, M.L., Rodgers, T. (eds.) Tribute to a Mathemagician, pp. 173–183. AK Peters, Wellesley, Mass (2005)Google Scholar
  22. 22.
    Ihler, E., Reich, G., Widmayer, P.: Class Steiner trees and VLSI-design. Discrete Appl. Math. 90(1), 173–194 (1999)MathSciNetCrossRefzbMATHGoogle Scholar
  23. 23.
    Itai, A., Papadimitriou, C.H., Szwarcfiter, J.L.: Hamilton paths in grid graphs. SIAM J. Comput. 11(4), 676–686 (1982)MathSciNetCrossRefzbMATHGoogle Scholar
  24. 24.
    Jerrum, M.R.: The complexity of finding minimum-length generator sequences. Theor. Comput. Sci. 36, 265–289 (1985)MathSciNetCrossRefzbMATHGoogle Scholar
  25. 25.
    Kaplan, H., Shamir, R., Tarjan, R.E.: A faster and simpler algorithm for sorting signed permutations by reversals. SIAM J. Comput. 29(3), 880–892 (2000)MathSciNetCrossRefzbMATHGoogle Scholar
  26. 26.
    Karp, R.: Two combinatorial problems associated with external sorting. In: Proceedings of the Courant Computer Science Symposium on Combinatorial Algorithms, pp. 17–29 (1972)Google Scholar
  27. 27.
    Karp, R.M.: A simple derivation of Edmonds’ algorithm for optimum branchings. Networks 1(3), 265–272 (1971)MathSciNetCrossRefzbMATHGoogle Scholar
  28. 28.
    Knuth, D.E.: The Art of Computer Programming: Fundamental Algorithms, vol. 1. Addison-Wesley, Reading (1973)zbMATHGoogle Scholar
  29. 29.
    Knuth, D.E.: The Art of Computer Programming: Sorting and Searching, vol. 3. Addison-Wesley, Reading (1973)zbMATHGoogle Scholar
  30. 30.
    Miltzow, T., Narins, L., Okamoto, Y., Rote, G., Thomas, A., Uno, T.: Approximation and hardness of token swapping. In: Proceedings of the 24th Annual European Symposium on Algorithms, ESA 2016, Aarhus, Denmark, August 22–26, 2016, Leibniz International Proceedings in Informatics (LIPIcs), vol. 57, pp. 66:1–66:15 (2016)Google Scholar
  31. 31.
    Reich, G., Widmayer, P.: Beyond Steiner’s problem: a VLSI oriented generalization. In: Proceedings of the 15th International Workshop on Graph-Theoretic Concepts in Computer Science, WG ’89, Castle Rolduc, The Netherlands, June 14-16, 1989, pp. 196–210. Springer, Berlin (1990)Google Scholar
  32. 32.
    Tarjan, R.: Sorting using networks of queues and stacks. J. ACM 19(2), 341–346 (1972)MathSciNetCrossRefzbMATHGoogle Scholar
  33. 33.
    Tarjan, R.E.: Finding optimum branchings. Networks 7(1), 25–35 (1977)MathSciNetCrossRefzbMATHGoogle Scholar
  34. 34.
    Umans, C.M.: An algorithm for finding Hamiltonian cycles in grid graphs without holes. Bachelor’s thesis, Williams College, Department of Mathematics (1996)Google Scholar
  35. 35.
    Wyttenbach, A.: Bike loft. (2015)
  36. 36.
    Yamanaka, K., Demaine, E.D., Ito, T., Kawahara, J., Kiyomi, M., Okamoto, Y., Saitoh, T., Suzuki, A., Uchizawa, K., Uno, T.: Swapping labeled tokens on graphs. In: Proceedings of the 7th International Conference on Fun with Algorithms, FUN 2014, Lipari Island, Sicily, Italy, July 1–3, 2014, pp. 364–375. Springer, Berlin (2014)Google Scholar
  37. 37.
    Zwick, U.: Directed minimum spanning trees. (2013)

Copyright information

© Springer Science+Business Media New York 2017

Authors and Affiliations

  1. 1.Department of Computer ScienceETH ZürichZurichSwitzerland

Personalised recommendations