Alexander Bogomolny’s UnOrdered Permutation Algorithm
The Alexander Bogomolyn’s algorithm is used to permute first N natural numbers.
Given the value of N we have to output all the permutations of numbers from 1 to N.
Examples:
Input : 2
Output : 1 2
2 1
Input : 3
Output : 1 2 3
1 3 2
2 1 3
3 1 2
2 3 1
3 2 1
The idea is to maintain an array to store the current permutation. A static integer level variable is used to define these permutations.
- It initializes the value of current level and permutes the remaining values to the higher levels.
- As the assigning action of the values reaches to the highest level, it prints the permutation obtained.
- This approach is recursively implemented to obtain all possible permutations.
C++
// CPP program to implement Alexander // Bogomolny’s UnOrdered Permutation Algorithm #include <iostream> using namespace std; // A function to print the permutation. void print(int perm[], int N) { for (int i = 0; i < N; i++) cout << " " << perm[i]; cout << "\n"; } // A function implementing Alexander Bogomolyn // algorithm. void AlexanderBogomolyn(int perm[], int N, int k) { static int level = -1; // Assign level to zero at start. level = level + 1; perm[k] = level; if (level == N) print(perm, N); else for (int i = 0; i < N; i++) // Assign values to the array // if it is zero. if (perm[i] == 0) AlexanderBogomolyn(perm, N, i); // Decrement the level after all possible // permutation after that level. level = level - 1; perm[k] = 0; } // Driver Function int main() { int i, N = 3; int perm[N] = { 0 }; AlexanderBogomolyn(perm, N, 0); return 0; } |
chevron_right
filter_none
Java
// Java program to implement // Alexander Bogomolny UnOrdered // Permutation Algorithm import java.io.*; class GFG { static int level = -1; // A function to print // the permutation. static void print(int perm[], int N) { for (int i = 0; i < N; i++) System.out.print(" " + perm[i]); System.out.println(); } // A function implementing // Alexander Bogomolyn algorithm. static void AlexanderBogomolyn(int perm[], int N, int k) { // Assign level to // zero at start. level = level + 1; perm[k] = level; if (level == N) print(perm, N); else for (int i = 0; i < N; i++) // Assign values // to the array // if it is zero. if (perm[i] == 0) AlexanderBogomolyn(perm, N, i); // Decrement the level // after all possible // permutation after // that level. level = level - 1; perm[k] = 0; } // Driver Code public static void main (String[] args) { int i, N = 3; int perm[] = new int[N]; AlexanderBogomolyn(perm, N, 0); } } // This code is contributed by anuj_67. |
chevron_right
filter_none
C#
// C# program to implement // Alexander Bogomolny UnOrdered // Permutation Algorithm using System; class GFG { static int level = -1; // A function to print // the permutation. static void print(int []perm, int N) { for (int i = 0; i < N; i++) Console.Write(" " + perm[i]); Console.WriteLine(); } // A function implementing // Alexander Bogomolyn algorithm. static void AlexanderBogomolyn(int []perm, int N, int k) { // Assign level to // zero at start. level = level + 1; perm[k] = level; if (level == N) print(perm, N); else for (int i = 0; i < N; i++) // Assign values // to the array // if it is zero. if (perm[i] == 0) AlexanderBogomolyn(perm, N, i); // Decrement the level // after all possible // permutation after // that level. level = level - 1; perm[k] = 0; } // Driver Code public static void Main () { int N = 3; int []perm = new int[N]; AlexanderBogomolyn(perm, N, 0); } } // This code is contributed // by anuj_67. |
chevron_right
filter_none
Output:
1 2 3 1 3 2 2 1 3 3 1 2 2 3 1 3 2 1
This article is contributed by Vineet Joshi. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Recommended Posts:
- Count unordered pairs (i,j) such that product of a[i] and a[j] is power of two
- Inverse Permutation
- K difference permutation
- Largest permutation after at most k swaps
- Permutation and Combination in Python
- Number of distinct permutation a String can have
- How to find Lexicographically previous permutation?
- Combination and Permutation Practice Questions | Set 1
- Find n-th lexicographically permutation of a string | Set 2
- Number of moves required to guess a permutation.
- Lexicographically smallest permutation of {1, .. n} such that no. and position do not match
- Find the good permutation of first N natural numbers
- Count Derangements (Permutation such that no element appears in its original position)
- Lexicographically smallest permutation with distinct elements using minimum replacements
- Stack Permutations (Check if an array is stack permutation of other)
Improved By : vt_m



