Springer Nature is making SARS-CoV-2 and COVID-19 research free. View research | View latest news | Sign up for updates

Necklaces, Convolutions, and X+Y


We give subquadratic algorithms that, given two necklaces each with n beads at arbitrary positions, compute the optimal rotation of the necklaces to best align the beads. Here alignment is measured according to the p norm of the vector of distances between pairs of beads from opposite necklaces in the best perfect matching. We show surprisingly different results for p=1, p even, and p=∞. For p even, we reduce the problem to standard convolution, while for p=∞ and p=1, we reduce the problem to (min,+) convolution and \((\operatorname {median},+)\) convolution. Then we solve the latter two convolution problems in subquadratic time, which are interesting results in their own right. These results shed some light on the classic sorting X+Y problem, because the convolutions can be viewed as computing order statistics on the antidiagonals of the X+Y matrix. All of our algorithms run in o(n 2) time, whereas the obvious algorithms for these problems run in Θ(n 2) time.

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

Fig. 1
Fig. 2
Fig. 3


  1. 1.

    “Tropical convolution” would also make sense, by direct analogy with tropical geometry, but we have never seen this terminology used in print.


  1. 1.

    Aloupis, G., Fevens, T., Langerman, S., Matsui, T., Mesa, A., Nuñez, Y., Rappaport, D., Toussaint, G.: Algorithms for computing geometric measures of melodic similarity. J. Comput. Music 30(3), 67–76 (2006)

  2. 2.

    Ardila, Y.J.P., Clifford, R., Iliopoulos, C.S., Landau, G.M., Mohamed, M.: Necklace swap problem for rhythmic similarity measures. Int. J. Comput. Methods 5(3), 351–363 (2008)

  3. 3.

    Baran, I., Demaine, E.D., Pǎtraşcu, M.: Subquadratic algorithms for 3SUM. Algorithmica 50(4), 584–596 (2008). Special issue of selected papers from the 9th Workshop on Algorithms and Data Structures, 2005

  4. 4.

    Bellman, R., Karush, W.: Mathematical programming and the maximum transform. J. Soc. Ind. Appl. Math. 10(3), 550–567 (1962)

  5. 5.

    Bernstein, D.J.: Fast multiplication and its applications. In: Buhler, J., Stevenhagen, P. (eds.) Algorithmic Number Theory. MSRI Publications, vol. 44 (2008)

  6. 6.

    Blum, M., Floyd, R.W., Pratt, V., Rivest, R.L., Tarjan, R.E.: Time bounds for selection. J. Comput. Syst. Sci. 7(4), 448–461 (1973)

  7. 7.

    Bussieck, M., Hassler, H., Woeginger, G.J., Zimmermann, U.T.: Fast algorithms for the maximum convolution problem. Oper. Res. Lett. 15(3), 133–141 (1994)

  8. 8.

    Cardinal, J., Kremer, S., Langerman, S.: Juggling with pattern matching. Theory Comput. Syst. 39(3), 425–437 (2006)

  9. 9.

    Chan, T.M.: All-pairs shortest paths with real weights in O(n 3/logn) time. Algorithmica 50, 236–243 (2008)

  10. 10.

    Chan, T.M.: More algorithms for all-pairs shortest paths in weighted graphs. SIAM J. Comput. 39, 2075–2089 (2010)

  11. 11.

    Clifford, P., Clifford, R.: Self-normalised distance with don’t cares. In: Ma, B., Zhang, K. (eds.) Combinatorial Pattern Matching. Lecture Notes in Computer Science, vol. 4580, pp. 63–70. Springer, Berlin (2007)

  12. 12.

    Clifford, P., Clifford, R.: Simple deterministic wildcard matching. Inf. Process. Lett. 101(2), 53–54 (2007)

  13. 13.

    Clifford, P., Clifford, R., Iliopoulos, C.: Fourier transform methods for δ and (δ,γ) matching and other measures of string similarity. Tech. Rep. TR-04-09, King’s College, London (2004)

  14. 14.

    Clifford, P., Clifford, R., Iliopoulos, C.: Faster algorithms for δ, γ-matching and related problems. In: Combinatorial Pattern Matching. Lecture Notes in Computer Science, vol. 3537, pp. 71–90. Springer, Berlin (2005)

  15. 15.

    Cohn, H., Kleinberg, R., Szegedy, B., Umans, C.: Group-theoretic algorithms for matrix multiplication. In: Proceedings of the 46th Annual IEEE Symposium on Foundations of Computer Science, pp. 379–388 (2005)

  16. 16.

    Colannino, J., Damian, M., Hurtado, F., Iacono, J., Meijer, H., Ramaswami, S., Toussaint, G.: An O(nlogn)-time algorithm for the restriction scaffold assignment. J. Comput. Biol. 13(4), 979–989 (2006)

  17. 17.

    Cole, R., Hariharan, R.: Verifying candidate matches in sparse and wildcard matching. In: Proceedings of the 34th Annual ACM Symposium on Theory of Computing, Montréal, Canada, pp. 592–601 (2002)

  18. 18.

    Cooley, J.W., Tukey, J.W.: An algorithm for the machine calculation of complex Fourier series. Math. Comput. 19, 297–301 (1965)

  19. 19.

    Cormen, T.H., Leiserson, C.E., Rivest, R.L., Stein, C.: Introduction to Algorithms, 2nd edn. MIT Press, Cambridge (2001)

  20. 20.

    Demaine, E.D., Mitchell, J.S.B., O’Rourke, J.: Problem 41: Sorting X+Y (pairwise sums). In: The Open Problems Project (2006). http://cs.smith.edu/~orourke/TOPP/P41.html

  21. 21.

    Demaine, E.D., O’Rourke, J.: Open problems from CCCG 2005. Proceedings of the 18th Canadian Conference on Computational Geometry, Kingston, Canada (2006)

  22. 22.

    Díaz-Báñez, J.M., Farigu, G., Gómez, F., Rappaport, D., Toussaint, G.T.: El compás flamenco: a phylogenetic analysis. In: Proceedings of BRIDGES: Mathematical Connections in Art, Music, and Science, Winfield, KS, pp. 61–70 (2004)

  23. 23.

    Erickson, J.: Lower bounds for linear satisfiability problems. Chic. J. Theor. Comput. Sci. 8 (1999)

  24. 24.

    Felzenszwalb, P.F., Huttenlocher, D.P.: Distance transforms of sampled functions. Tech. Rep. TR2004-1963, Faculty of Computing and Information Science, Cornell University (2004)

  25. 25.

    Fischer, M.J., Paterson, M.S.: String-matching and other products. In: Complexity of computation. SIAM-AMS Proceedings, vol. VII, pp. 113–125. Am. Math. Soc., New York (1974)

  26. 26.

    Frederickson, G.N., Johnson, D.B.: The complexity of selection and ranking in X+Y and matrices with sorted columns. J. Comput. Syst. Sci. 24(2), 197–208 (1982)

  27. 27.

    Fredman, M.L.: How good is the information theory bound in sorting? Theor. Comput. Sci. 1(4), 355–361 (1976)

  28. 28.

    Fredman, M.L.: New bounds on the complexity of the shortest path problem. SIAM J. Comput. 5(1), 83–89 (1976)

  29. 29.

    Gauss, C.F.: Werke, vol. 3. Königlichen Gesellschaft der Wissenschaften, Göttingen (1866)

  30. 30.

    Heideman, M.T., Johnson, D.H., Burrus, C.S.: Gauss and the history of the fast Fourier transform. Arch. Hist. Exact Sci. 34(3), 265–277 (1985)

  31. 31.

    Indyk, P.: Faster algorithms for string matching problems: Matching the convolution bound. In: Proceedings of the 39th Annual Symposium on Foundations of Computer Science, Palo Alto, CA, pp. 166–173 (1998)

  32. 32.

    Kalai, A.: Efficient pattern-matching with don’t cares. In: Proceedings of the 13th Annual ACM-SIAM Symposium on Discrete Algorithms, San Francisco, CA, pp. 655–656 (2002)

  33. 33.

    Maragos, P.: Differential morphology. In: Mitra, S., Sicuranza, G. (eds.) Nonlinear Image Processing, pp. 289–329. Academic Press, New York (2000)

  34. 34.

    Moreau, J.J.: Inf-convolution, sous-additivité, convexité des fonctions numériques. J. Math. Pures Appl., Neuv. Sér. 49, 109–154 (1970)

  35. 35.

    Rockafellar, R.T.: Convex Analysis. Princeton Mathematical Series. Princeton University Press, Princeton (1970)

  36. 36.

    Schönhage, A., Paterson, M., Pippenger, N.: Finding the median. J. Comput. Syst. Sci. 13(2), 184–199 (1976)

  37. 37.

    Steiger, W.L., Streinu, I.: A pseudo-algorithmic separation of lines from pseudo-lines. Inf. Process. Lett. 53(5), 295–299 (1995)

  38. 38.

    Strömberg, T.: The operation of infimal convolution. Diss. Math. 352, 58 (1996)

  39. 39.

    Toussaint, G.: The geometry of musical rhythm. In: Revised Papers from the Japan Conference on Discrete and Computational Geometry, Tokyo, Japan. Lecture Notes in Computer Science, vol. 3742, pp. 198–212 (2004)

  40. 40.

    Toussaint, G.T.: A comparison of rhythmic similarity measures. In: Proceedings of the 5th International Conference on Music Information Retrieval, Barcelona, Spain, pp. 242–245 (2004) A longer version appears as Technical Report SOCS-TR-2004.6, School of Computer Science, McGill University, August 2004

Download references


This work was initiated at the 20th Bellairs Winter Workshop on Computational Geometry held January 28–February 4, 2005. We thank the other participants of that workshop—Greg Aloupis, Justin Colannino, Mirela Damian-Iordache, Vida Dujmović, Francisco Gomez-Martin, Danny Krizanc, Erin McLeish, Henk Meijer, Patrick Morin, Mark Overmars, Suneeta Ramaswami, David Rappaport, Diane Souvaine, Ileana Streinu, David Wood, Godfried Toussaint, Remco Veltkamp, and Sue Whitesides—for helpful discussions and contributing to a fun and creative atmosphere. We particularly thank the organizer, Godfried Toussaint, for posing the problem to us. The last author would also like to thank Luc Devroye for pointing out the easy generalization of the 2 necklace alignment problem to p for any fixed even integer p.

Author information

Correspondence to Perouz Taslakian.

Additional information

In memory of our colleague Mihai Pǎtraşcu.

D. Bremner and T.M. Chan are supported by NSERC. E.D. Demaine and J. Iacono are supported in part by NSF grants CCF-0430849 and OISE-0334653 and by an Alfred P. Sloan Fellowship. F. Hurtado is supported in part by projects MICINN MTM2009-07242, Gen. Cat. DGR 2009SGR1040, and ESF EUROCORES programme EuroGIGA, CRP ComPoSe: MICINN Project EUI-EURC-2011-4306, for Spain.

Rights and permissions

Reprints and Permissions

About this article

Cite this article

Bremner, D., Chan, T.M., Demaine, E.D. et al. Necklaces, Convolutions, and X+Y . Algorithmica 69, 294–314 (2014). https://doi.org/10.1007/s00453-012-9734-3

Download citation


  • Necklace alignment
  • Cyclic swap distance
  • Convolution
  • Sorting X+Y
  • All pairs shortest paths