Progressively generates all the permutations of the given size.

You're viewing an older version of this page (#5556). View the current version.

View versions (2)

Interface

#include <codecogs/maths/combinatorics/permutations/permutation1.h>

using namespace Maths::Combinatorics::Permutations;

class Permutation1

Overview

This class progressively generates all the permutations of the given size, while also giving the possibility to return the rank and parity of the current permutation.

References

SUBSET, a C++ library of combinatorial routines, http://www.csit.fsu.edu/~burkardt/cpp_src/subset/subset.html

Example 1

#include <codecogs/maths/combinatorics/permutations/permutation1.h>
#include <iostream>
int main()
{
  Maths::Combinatorics::Permutations::Permutation1 P(3);
  std::cout << "The permutations of 3 elements:";
  std::cout << std::endl;
  for (int i = 0; i < 6; i++)
  {
    std::vector<int> alpha = P.getNext();
    for (int j = 0; j < alpha.size(); j++)
      std::cout << alpha[j] << " ";
    if (P.isEven())
      std::cout << "\tis even";
    else
      std::cout << "\tis odd";
    std::cout << "\t rank = " << P.getRank();
    std::cout << std::endl;
  }
  return 0;
}

Output

The permutations of 3 elements:
1 2 3   is even  rank = 1
2 1 3   is odd   rank = 2
3 1 2   is even  rank = 3
1 3 2   is odd   rank = 4
2 3 1   is even  rank = 5
3 2 1   is odd   rank = 6
GPL Licence — free for non commercial use. See Licence details.

Members of Permutation1

CONSTRUCTOR

Permutation1

Parameters

n
the size of the permutations to generate

CLASS METHOD

getNext

CLASS METHOD

getRank

CLASS METHOD

isEven

CLASS METHOD

init