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++
#include <bits/stdc++.h>
using namespace std;
void solve(vector< int >& v, int n, string& s,
vector<vector< int > >& vv)
{
if (v.size() == n)
{
vv.push_back(v);
return ;
}
for ( int i = 0; i < n; i++)
{
if (s[i] != '1' )
{
s[i] = '1' ;
v.push_back(i + 1);
solve(v, n, s, vv);
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
import java.io.*;
class GFG
{
static int level = - 1 ;
static void print( int perm[], int N)
{
for ( int i = 0 ; i < N; i++)
System.out.print( " " + perm[i]);
System.out.println();
}
static void AlexanderBogomolyn( int perm[],
int N, int k)
{
level = level + 1 ;
perm[k] = level;
if (level == N)
print(perm, N);
else
for ( int i = 0 ; i < N; i++)
if (perm[i] == 0 )
AlexanderBogomolyn(perm, N, i);
level = level - 1 ;
perm[k] = 0 ;
}
public static void main (String[] args)
{
int i, N = 3 ;
int perm[] = new int [N];
AlexanderBogomolyn(perm, N, 0 );
}
}
|
Python3
def printn(perm, N):
for i in range (N):
print ( " " ,perm[i], sep = " ", end = " ")
print ()
level = [ - 1 ]
def AlexanderBogomolyn(perm, N, k):
level[ 0 ] = level[ 0 ] + 1
perm[k] = level[ 0 ]
if (level[ 0 ] = = N):
printn(perm, N)
else :
for i in range (N):
if (perm[i] = = 0 ):
AlexanderBogomolyn(perm, N, i)
level[ 0 ] = level[ 0 ] - 1
perm[k] = 0
return
N = 3
perm = [ 0 ] * N
AlexanderBogomolyn(perm, N, 0 )
|
C#
using System;
class GFG
{
static int level = -1;
static void print( int []perm,
int N)
{
for ( int i = 0; i < N; i++)
Console.Write( " " + perm[i]);
Console.WriteLine();
}
static void AlexanderBogomolyn( int []perm,
int N, int k)
{
level = level + 1;
perm[k] = level;
if (level == N)
print(perm, N);
else
for ( int i = 0; i < N; i++)
if (perm[i] == 0)
AlexanderBogomolyn(perm, N, i);
level = level - 1;
perm[k] = 0;
}
public static void Main ()
{
int N = 3;
int []perm = new int [N];
AlexanderBogomolyn(perm, N, 0);
}
}
|
Javascript
<script>
let level = -1;
function print(perm, N)
{
for (let i = 0; i < N; i++)
document.write( " " + perm[i]);
document.write( "<br/>" );
}
function AlexanderBogomolyn(perm, N, k)
{
level = level + 1;
perm[k] = level;
if (level == N)
print(perm, N);
else
for (let i = 0; i < N; i++)
if (perm[i] == 0)
AlexanderBogomolny(perm, N, i);
level = level - 1;
perm[k] = 0;
}
let i, N = 3;
let perm = Array.from({length: N}, (_, i) => 0);
AlexanderBogomolny(perm, N, 0);
</script>
|
Output
1 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.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...