Alexander Bogomolny’s UnOrdered Permutation Algorithm
Last Updated :
06 Sep, 2022
Alexander Bogomolny'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 the current level and permutes the remaining values to higher levels.
- As the assigning action of the values reaches the highest level, it prints the permutation obtained.
- This approach is recursively implemented to obtain all possible permutations.
C++
// C++ program to implement
// Alexander Bogomolny UnOrdered
// Permutation Algorithm
#include <bits/stdc++.h>
using namespace std;
// A function implementing
// Alexander Bogomolny algorithm.
void solve(vector<int>& v, int n, string& s,
vector<vector<int> >& vv)
{
// if all the numbers are added
// to the vector
if (v.size() == n)
{
// Append the vector
// to the answer
vv.push_back(v);
return;
}
for (int i = 0; i < n; i++)
{
// if the number is not taken
if (s[i] != '1')
{
s[i] = '1';
v.push_back(i + 1);
// Recursive call to the function
solve(v, n, s, vv);
// Backtrakking step
s[i] = '0';
v.pop_back();
}
}
}
int main()
{
int n = 3;
vector<int> v;
vector<vector<int>> vv;
string s;
for (int i = 0; i < n; i++)
s += '0';
solve(v, n, s, vv);
for (int i = 0; i < vv.size(); i++)
{
for (int j = 0; j < vv[i].size(); j++)
{
cout << vv[i][j] << " ";
}
cout << endl;
}
}
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 Bogomolny 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.
Python3
# Python3 program to implement Alexander
# Bogomolny’s UnOrdered Permutation Algorithm
# A function to print permutation.
def printn(perm, N):
for i in range(N):
print(" ",perm[i], sep = "", end = "")
print()
# A function implementing Alexander Bogomolny
# algorithm.
level = [-1]
def AlexanderBogomolyn(perm, N, k):
# Assign level to zero at start.
level[0] = level[0] + 1
perm[k] = level[0]
if (level[0] == N):
printn(perm, N)
else:
for i in range(N):
# 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[0] = level[0] - 1
perm[k] = 0
return
# Driver code
N = 3
perm = [0]*N
AlexanderBogomolyn(perm, N, 0)
# This code is contributed by SHUBHAMSINGH10
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 Bogomolny 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.
JavaScript
<script>
// Javascript program to implement
// Alexander Bogomolny UnOrdered
// Permutation Algorithm
let level = -1;
// A function to print
// the permutation.
function print(perm, N)
{
for (let i = 0; i < N; i++)
document.write(" " + perm[i]);
document.write("<br/>");
}
// A function implementing
// Alexander Bogomolny algorithm.
function AlexanderBogomolyn(perm, N, k)
{
// Assign level to
// zero at start.
level = level + 1;
perm[k] = level;
if (level == N)
print(perm, N);
else
for (let i = 0; i < N; i++)
// Assign values
// to the array
// if it is zero.
if (perm[i] == 0)
AlexanderBogomolny(perm, N, i);
// Decrement the level
// after all possible
// permutation after
// that level.
level = level - 1;
perm[k] = 0;
}
// driver program
let i, N = 3;
let perm = Array.from({length: N}, (_, i) => 0);
AlexanderBogomolny(perm, N, 0);
</script>
Output1 2 3
1 3 2
2 1 3
2 3 1
3 1 2
3 2 1
Time Complexity: O(N*N!), where N is the given integer.
Auxiliary Space: O(N*N!), for storing all the permutations of the first N natural numbers.
Similar Reads
Heap's Algorithm for generating permutations Heap's algorithm is used to generate all permutations of n objects. The idea is to generate each permutation from the previous permutation by choosing a pair of elements to interchange, without disturbing the other n-2 elements. Following is the illustration of generating all the permutations of n g
6 min read
Print all permutations in sorted (lexicographic) order Given a string s, print all unique permutations of the string in lexicographically sorted order.Examples:Input: "ABC"Output: ["ABC", "ACB", "BAC", "BCA", "CAB", "CBA"]Explanation: All characters are distinct, so there are 3! = 6 unique permutations, printed in sorted order.Input: "AAA"Output: ["AAA"
15+ min read
Permutation of a Linked List Given a singly linked list of distinct integers, the task is to generate all possible permutations of the linked list elements. Examples: Input: Linked List: 1 -> 2 -> 3Output: 1 -> 2 -> 31 -> 3 -> 22 -> 1 -> 32 -> 3 -> 13 -> 1 -> 23 -> 2 -> 1Input: Linked L
11 min read
Print k different sorted permutations of a given array Given an array arr[] containing N integers, the task is to print k different permutations of indices such that the values at those indices form a non-decreasing sequence. Print -1 if it is not possible. Examples: Input: arr[] = {1, 3, 3, 1}, k = 3 Output: 0 3 1 2 3 0 1 2 3 0 2 1 For every permutatio
8 min read
BogoSort or Permutation Sort BogoSort also known as permutation sort, stupid sort, slow sort, shotgun sort or monkey sort is a particularly ineffective algorithm one person can ever imagine. It is based on generate and test paradigm. The algorithm successively generates permutations of its input until it finds one that is sorte
7 min read