permutations
lexicographic order
algorithm
combinatorics
programming

Print all permutation in lexicographic order

Data Structures & Algorithms practice on Codemia

Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.

Practice algorithms

Introduction

Permutations are a fundamental concept in combinatorics and computer science, particularly in problems involving arrangements, ordering, and scheduling. Generating permutations in lexicographic order refers to creating permutations that are ordered based on the traditional alphabetical order (dictionary order) of their components. This approach is particularly useful when you need to systematically explore all possible configurations of a set, such as in backtracking algorithms or generating test cases.

Technical Explanation

Lexicographic Order

Lexicographic order generalizes the way words are alphabetically ordered in dictionaries. In this context, given two permutations A and B , A is considered less than B if during a left-to-right comparison, the first position where they differ has a lower value in A than in B .

Generating Permutations

The task of generating permutations in lexicographic order can be elegantly solved by using the algorithm that leverages the next-lexicographical permutation approach:

  1. Identify the longest non-increasing suffix: The rightmost element that is smaller than the element immediately after it denotes a pivot point.
  2. Find the successor to the identified pivot: This involves locating the smallest element in the suffix that is larger than the pivot and swapping it.
  3. Reverse the suffix: Following the swap, reverse the entire suffix beyond the pivot to achieve the next permutation in lexicographic order.

Example

Consider the sequence [1, 2, 3] :

  1. Start with [1, 2, 3] .
  2. Identify the longest non-increasing suffix: [3] . The pivot is 2 .
  3. Find the smallest element in the suffix larger than 2 , which is 3 , and swap them to get [1, 3, 2] .
  4. Reverse the suffix starting immediately after the pivot to keep the suffix as low as possible, although in this case, since it’s a single element, no reversal is needed.
  5. Continue this process for each next permutation:
    • [2, 1, 3]
    • [2, 3, 1]
    • [3, 1, 2]
    • [3, 2, 1]

Python Implementation

Here's a simple Python implementation of the above algorithm:

  • Avoid recalculating permutations from scratch; instead, generate the next permutation using in-place swaps.
  • Use iterative approaches instead of recursion to prevent stack overflow in deeper permutations.
  • Backtracking Algorithms: Exploring all configurations systematically can help in constraint satisfaction problems.
  • Testing and Validation: Permutations allow comprehensive coverage of input scenarios.
  • Cryptographic Variants: Generating all permutations can be used to analyze symmetric encryption schemes.

Related reading
Course
Intermediate
27 lessons
15 hours
DSA Fundamentals

Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Data Structures & Algorithms practice on Codemia

Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.

Practice algorithms

All Rights Reserved.