Skip to content
Related Articles

Related Articles

Improve Article

Alexander Bogomolny’s UnOrdered Permutation Algorithm

  • Difficulty Level : Medium
  • Last Updated : 01 Jun, 2021

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. 
 

  1. It initializes the value of current level and permutes the remaining values to the higher levels.
  2. As the assigning action of the values reaches the highest level, it prints the permutation obtained.
  3. 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




// 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.

Python3




# Python3 program to implement Alexander
# Bogomolny’s UnOrdered Permutation Algorithm
 
# A function to prthe permutation.
def printn(perm, N):
    for i in range(N):
        print(" ",perm[i], sep = "", end = "")
    print()
     
# A function implementing Alexander Bogomolyn
# 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 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.

Javascript




<script>
 
// Javascript program to implement
// Alexander Bogomolny UnOrdered
// Permutation Algorithm
 
let level = -1;
   
// A function to print
// the permutation.
function prlet(perm, N)
{
    for (let i = 0; i < N; i++)
        document.write(" " + perm[i]);
    document.write("<br/>");
}
   
// A function implementing
// Alexander Bogomolyn algorithm.
function AlexanderBogomolyn(perm, N, k)
{
   
    // Assign level to
    // zero at start.
    level = level + 1;
    perm[k] = level;
   
    if (level == N)
        prlet(perm, N);
    else
        for (let 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 program
 
    let i, N = 3;
    let perm = Array.from({length: N}, (_, i) => 0);
    AlexanderBogomolyn(perm, N, 0);
     
</script>

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 write.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.
 

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.




My Personal Notes arrow_drop_up
Recommended Articles
Page :