Finding and Counting Permutations via CSPs

Authors Benjamin Aram Berendsohn, László Kozma, Dániel Marx

Thumbnail PDF


  • Filesize: 0.52 MB
  • 16 pages

Document Identifiers

Author Details

Benjamin Aram Berendsohn
  • Institut für Informatik, Freie Universität Berlin, Germany
László Kozma
  • Institut für Informatik, Freie Universität Berlin, Germany
Dániel Marx
  • Max Planck Institute for Informatics, Saarland Informatics Campus, Saarbrücken, Germany


An earlier version of the paper contained a mistake in the analysis of the algorithm for Theorem 2. We thank Günter Rote for pointing out the error. This work was prompted by the Dagstuhl Seminar 18451 "Genomics, Pattern Avoidance, and Statistical Mechanics". The second author thanks the organizers for the invitation and the participants for interesting discussions.

Cite AsGet BibTex

Benjamin Aram Berendsohn, László Kozma, and Dániel Marx. Finding and Counting Permutations via CSPs. In 14th International Symposium on Parameterized and Exact Computation (IPEC 2019). Leibniz International Proceedings in Informatics (LIPIcs), Volume 148, pp. 1:1-1:16, Schloss Dagstuhl – Leibniz-Zentrum für Informatik (2019)


Permutation patterns and pattern avoidance have been intensively studied in combinatorics and computer science, going back at least to the seminal work of Knuth on stack-sorting (1968). Perhaps the most natural algorithmic question in this area is deciding whether a given permutation of length n contains a given pattern of length k. In this work we give two new algorithms for this well-studied problem, one whose running time is n^{k/4 + o(k)}, and a polynomial-space algorithm whose running time is the better of O(1.6181^n) and O(n^{k/2 + 1}). These results improve the earlier best bounds of n^{0.47k + o(k)} and O(1.79^n) due to Ahal and Rabinovich (2000) resp. Bruner and Lackner (2012) and are the fastest algorithms for the problem when k in Omega(log{n}). We show that both our new algorithms and the previous exponential-time algorithms in the literature can be viewed through the unifying lens of constraint-satisfaction. Our algorithms can also count, within the same running time, the number of occurrences of a pattern. We show that this result is close to optimal: solving the counting problem in time f(k) * n^{o(k/log{k})} would contradict the exponential-time hypothesis (ETH). For some special classes of patterns we obtain improved running times. We further prove that 3-increasing and 3-decreasing permutations can, in some sense, embed arbitrary permutations of almost linear length, which indicates that an algorithm with sub-exponential running time is unlikely, even for patterns from these restricted classes.

Subject Classification

ACM Subject Classification
  • Theory of computation → Data structures design and analysis
  • Theory of computation → Pattern matching
  • permutations
  • pattern matching
  • constraint satisfaction
  • exponential time


  • Access Statistics
  • Total Accesses (updated on a weekly basis)
    PDF Downloads


  1. Shlomo Ahal and Yuri Rabinovich. On Complexity of the Subpattern Problem. SIAM J. Discrete Math., 22(2):629-649, 2008. URL:
  2. M.H. Albert and M.S. Paterson. Bounds for the growth rate of meander numbers. Journal of Combinatorial Theory, Series A, 112(2):250-262, 2005. URL:
  3. Michael Albert and Mireille Bousquet-Mélou. Permutations sortable by two stacks in parallel and quarter plane walks. Eur. J. Comb., 43:131-164, 2015. URL:
  4. Michael H. Albert, Robert E. L. Aldred, Mike D. Atkinson, and Derek A. Holton. Algorithms for Pattern Involvement in Permutations. In Proceedings of the 12th International Symposium on Algorithms and Computation, ISAAC '01, pages 355-366, London, UK, 2001. Springer-Verlag. URL:
  5. Michael H. Albert, Cheyne Homberger, Jay Pantone, Nathaniel Shar, and Vincent Vatter. Generating permutations with restricted containers. J. Comb. Theory, Ser. A, 157:205-232, 2018. URL:
  6. Michael H. Albert, Marie-Louise Lackner, Martin Lackner, and Vincent Vatter. The Complexity of Pattern Matching for 321-Avoiding and Skew-Merged Permutations. Discrete Mathematics & Theoretical Computer Science, 18(2), 2016. URL:
  7. David Aldous and Persi Diaconis. Longest Increasing Subsequences: From Patience Sorting to the Baik-Deift-Johansson Theorem. Bull. Amer. Math. Soc, 36:413-432, 1999. Google Scholar
  8. David Arthur. Fast Sorting and Pattern-avoiding Permutations. In Proceedings of the Fourth Workshop on Analytic Algorithmics and Combinatorics, ANALCO 2007, pages 169-174, 2007. URL:
  9. Omri Ben-Eliezer and Clément L. Canonne. Improved Bounds for Testing Forbidden Order Patterns. In Proceedings of the Twenty-Ninth Annual ACM-SIAM Symposium on Discrete Algorithms, SODA 2018, pages 2093-2112, 2018. URL:
  10. Donald J. Berndt and James Clifford. Using Dynamic Time Warping to Find Patterns in Time Series. In Proceedings of the 3rd International Conference on Knowledge Discovery and Data Mining, AAAIWS'94, pages 359-370. AAAI Press, 1994. URL:
  11. Hans L. Bodlaender. A partial k-arboretum of graphs with bounded treewidth. Theoretical Computer Science, 209(1):1-45, 1998. URL:
  12. Miklós Bóna. A survey of stack-sorting disciplines. The Electronic Journal of Combinatorics, 9(2):1, 2003. Google Scholar
  13. Miklós Bóna. Combinatorics of Permutations. CRC Press, Inc., Boca Raton, FL, USA, 2004. Google Scholar
  14. Miklós Bóna. A Walk Through Combinatorics: An Introduction to Enumeration and Graph Theory. World Scientific, 2011. URL:
  15. Prosenjit Bose, Jonathan F. Buss, and Anna Lubiw. Pattern Matching for Permutations. Inf. Process. Lett., 65(5):277-283, 1998. URL:
  16. Karl Bringmann, László Kozma, Shay Moran, and N. S. Narayanaswamy. Hitting Set for Hypergraphs of Low VC-dimension. In 24th Annual European Symposium on Algorithms, ESA 2016, August 22-24, 2016, Aarhus, Denmark, pages 23:1-23:18, 2016. URL:
  17. Marie-Louise Bruner and Martin Lackner. The computational landscape of permutation patterns. Pure Mathematics and Applications, 24(2):83-101, 2013. Google Scholar
  18. Marie-Louise Bruner and Martin Lackner. A Fast Algorithm for Permutation Pattern Matching Based on Alternating Runs. Algorithmica, 75(1):84-117, 2016. URL:
  19. Laurent Bulteau, Romeo Rizzi, and Stéphane Vialette. Pattern Matching for k-Track Permutations. In Costas Iliopoulos, Hon Wai Leong, and Wing-Kin Sung, editors, Combinatorial Algorithms, pages 102-114, Cham, 2018. Springer International Publishing. Google Scholar
  20. Parinya Chalermsook, Mayank Goswami, László Kozma, Kurt Mehlhorn, and Thatchaphol Saranurak. Pattern-Avoiding Access in Binary Search Trees. In IEEE 56th Annual Symposium on Foundations of Computer Science, FOCS 2015, pages 410-423, 2015. URL:
  21. Maw-Shang Chang and Fu-Hsing Wang. Efficient algorithms for the maximum weight clique and maximum weight independent set problems on permutation graphs. Information Processing Letters, 43(6):293-295, 1992. URL:
  22. Hubie Chen. A rendezvous of logic, complexity, and algebra. ACM Comput. Surv., 42(1):2:1-2:32, 2009. URL:
  23. Marek Cygan, Lukasz Kowalik, and Arkadiusz Socala. Improving TSP Tours Using Dynamic Programming over Tree Decompositions. In 25th Annual European Symposium on Algorithms, ESA 2017, pages 30:1-30:14, 2017. URL:
  24. Rina Dechter and Judea Pearl. Tree clustering for constraint networks. Artificial Intelligence, 38(3):353-366, 1989. URL:
  25. Rodney G. Downey and Michael R. Fellows. Parameterized Complexity. Monographs in Computer Science. Springer, 1999. URL:
  26. Vida Dujmovic, David Eppstein, and David R. Wood. Structure of Graphs with Locally Restricted Crossings. SIAM J. Discrete Math., 31(2):805-824, 2017. URL:
  27. Pál Erdős and G. Szekeres. A combinatorial problem in geometry. Compositio Mathematica, 2:463-470, 1935. URL:
  28. Fedor V. Fomin, Serge Gaspers, Saket Saurabh, and Alexey A. Stepanov. On Two Techniques of Combining Branching and Treewidth. Algorithmica, 54(2):181-207, 2009. URL:
  29. Fedor V. Fomin and Kjartan Høie. Pathwidth of cubic graphs and exact algorithms. Information Processing Letters, 97(5):191-196, 2006. URL:
  30. Jacob Fox. Stanley-Wilf limits are typically exponential. CoRR, abs/1310.8378, 2013. URL:
  31. Jacob Fox and Fan Wei. Fast property testing and metrics for permutations. Combinatorics, Probability and Computing, pages 1-41, 2018. Google Scholar
  32. Eugene C. Freuder. Complexity of K-tree Structured Constraint Satisfaction Problems. In Proceedings of the Eighth National Conference on Artificial Intelligence - Volume 1, AAAI'90, pages 4-9. AAAI Press, 1990. URL:
  33. Sylvain Guillemot and Dániel Marx. Finding small patterns in permutations in linear time. In Proceedings of the Twenty-Fifth Annual ACM-SIAM Symposium on Discrete Algorithms, SODA 2014, pages 82-101, 2014. URL:
  34. Sylvain Guillemot and Stéphane Vialette. Pattern Matching for 321-Avoiding Permutations. In Yingfei Dong, Ding-Zhu Du, and Oscar Ibarra, editors, Algorithms and Computation, pages 1064-1073. Springer Berlin Heidelberg, 2009. Google Scholar
  35. Kurt Hoffmann, Kurt Mehlhorn, Pierre Rosenstiehl, and Robert E. Tarjan. Sorting Jordan sequences in linear time using level-linked search trees. Information and Control, 68(1-3):170-184, 1986. Google Scholar
  36. Louis Ibarra. Finding pattern matchings for permutations. Information Processing Letters, 61(6):293-295, 1997. URL:
  37. Vít Jelínek and Jan Kyncl. Hardness of Permutation Pattern Matching. In Proceedings of the Twenty-Eighth Annual ACM-SIAM Symposium on Discrete Algorithms, SODA 2017, pages 378-396, 2017. URL:
  38. Eamonn J. Keogh, Stefano Lonardi, and Bill Yuan-chi Chiu. Finding surprising patterns in a time series database in linear time and space. In Proceedings of the Eighth ACM SIGKDD 2002 International Conference on Knowledge Discovery and Data Mining, pages 550-556, 2002. URL:
  39. Sergey Kitaev. Patterns in Permutations and Words. Monographs in Theoretical Computer Science. An EATCS Series. Springer, 2011. URL:
  40. Donald E. Knuth. The Art of Computer Programming, Volume I: Fundamental Algorithms. Addison-Wesley, 1968. Google Scholar
  41. Adam Marcus and Gábor Tardos. Excluded permutation matrices and the Stanley-Wilf conjecture. J. Comb. Theory, Ser. A, 107(1):153-160, 2004. URL:
  42. Dániel Marx. Can You Beat Treewidth? Theory of Computing, 6(1):85-112, 2010. URL:
  43. Both Neou, Romeo Rizzi, and Stéphane Vialette. Permutation Pattern matching in (213, 231)-avoiding permutations. Discrete Mathematics & Theoretical Computer Science, Vol. 18 no. 2, Permutation Patterns 2015, 2017. URL:
  44. Ilan Newman, Yuri Rabinovich, Deepak Rajendraprasad, and Christian Sohler. Testing for Forbidden Order Patterns in an Array. In Proceedings of the Twenty-Eighth Annual ACM-SIAM Symposium on Discrete Algorithms, SODA 2017, pages 1582-1597, 2017. URL:
  45. Pranav Patel, Eamonn Keogh, Jessica Lin, and Stefano Lonardi. Mining Motifs in Massive Time Series Databases. In In Proceedings of IEEE International Conference on Data Mining ICDM’02, pages 370-377, 2002. Google Scholar
  46. Vaughan R. Pratt. Computing Permutations with Double-ended Queues, Parallel Stacks and Parallel Queues. In Proceedings of the Fifth Annual ACM Symposium on Theory of Computing, STOC '73, pages 268-277. ACM, 1973. URL:
  47. Pierre Rosenstiehl. Planar permutations defined by two intersecting Jordan curves. In Graph Theory and Combinatorics, pages 259-271. London, Academic Press, 1984. URL:
  48. Pierre Rosenstiehl and Robert E. Tarjan. Gauss codes, planar hamiltonian graphs, and stack-sortable permutations. Journal of Algorithms, 5(3):375-390, 1984. URL:
  49. Raimund Seidel. A New Method for Solving Constraint Satisfaction Problems. In Proceedings of the 7th International Joint Conference on Artificial Intelligence, IJCAI 1981, pages 338-342, 1981. URL:
  50. Rodica Simion and Frank W. Schmidt. Restricted Permutations. European Journal of Combinatorics, 6(4):383-406, 1985. URL:
  51. N. J. A. Sloane. The Encyclopedia of Integer Sequences, Sequence A073028. Google Scholar
  52. S. M. Tanny and M. Zuker. On a unimodal sequence of binomial coefficients. Discrete Mathematics, 9(1):79-89, 1974. URL:
  53. Robert E. Tarjan. Sorting Using Networks of Queues and Stacks. J. ACM, 19(2):341-346, April 1972. URL:
  54. Edward P. K. Tsang. Foundations of constraint satisfaction. Computation in cognitive science. Academic Press, 1993. Google Scholar
  55. Vincent Vatter. Permutation Classes. In Miklós Bóna, editor, Handbook of Enumerative Combinatorics, chapter 12. Chapman and Hall/CRC, New York, 2015. Preprint at URL:
  56. V. Yugandhar and Sanjeev Saxena. Parallel algorithms for separable permutations. Discrete Applied Mathematics, 146(3):343-364, 2005. URL:
Questions / Remarks / Feedback

Feedback for Dagstuhl Publishing

Thanks for your feedback!

Feedback submitted

Could not send message

Please try again later or send an E-mail