问题
For example I have this set k=5
of elements [1,2,3,4,5]
and I want all permutations of length n=2
.
1,2
1,3
1,4
1,5
2,1
etc etc.
Thing is i can't use STL, external math libraries etc.
I've been sitting on this problem for about 3 days now and im about to go crazy. What i tried is generating all permutations of all the elements using Heap's algorithm, and then all the permutations of n elements where contained in the first n numbers of all k-permutations and i could just truncate and delete duplicates, but then the complexity is way too high(n!)
I know the problem has a good solution as I've seen this being done with extra modules/libraries in questions about permutating strings
extra info : I only need this to brute force an unbalanced assignment problem, and Hungarian algorithm seems way too long when i'm aloud to "brute-force" the problem. My approach didn't come close to the allowed execution time because when i have an array of for example size 8x3, my algorithm needs 8! comparisons when it definitely could be optimized to a much smaller number.
Also its my first post here after 3 years of CS, i rage-quit this problem like a million times so i admitted defeat and decided ask you stackoverflow gods for help :>
回答1:
I think you can do it in two steps, first, generate combination of k elements out of a set of n, then print permutation of each combination. I tested this code and works fine:
#include <iostream>
using namespace std;
void printArr(int a[], int n, bool newline = true) {
for (int i=0; i<n; i++) {
if (i > 0) cout << ",";
cout << a[i];
}
if (newline) cout << endl;
}
// Generating permutation using Heap Algorithm
void heapPermutation(int a[], int n, int size) {
// if size becomes 1 then prints the obtained permutation
if (size == 1) {
printArr(a, n);
return;
}
for (int i=0; i<size; i++) {
heapPermutation(a, n, size-1);
// if size is odd, swap first and last element, otherwise swap ith and last element
swap(a[size%2 == 1 ? 0 : i], a[size-1]);
}
}
// Generating permutation using Heap Algorithm
void heapKPermutation(int a[], int n, int k, int size) {
// if size becomes 1 then prints the obtained permutation
if (size == n - k + 1) {
printArr(a + n - k, k);
return;
}
for (int i=0; i<size; i++) {
heapKPermutation(a, n, k, size-1);
// if size is odd, swap first and last element, otherwise swap ith and last element
swap(a[size%2 == 1 ? 0 : i], a[size-1]);
}
}
void doKCombination(int a[], int n, int p[], int k, int size, int start) {
int picked[size + 1];
for (int i = 0; i < size; ++i) picked[i] = p[i];
if (size == k) {
// We got a valid combination, use the heap permutation algorithm to generate all permutations out of it.
heapPermutation(p, k, k);
} else {
if (start < n) {
doKCombination(a, n, picked, k, size, start + 1);
picked[size] = a[start];
doKCombination(a, n, picked, k, size + 1, start + 1);
}
}
}
// Generate combination of k elements out of a set of n
void kCombination(int a[], int n, int k) {
doKCombination(a, n, nullptr, k, 0, 0);
}
int main()
{
int a[] = {1, 2, 3, 4, 5};
cout << "n=1, k=1, a=";
printArr(a, 1);
kCombination(a, 1, 1);
cout << "n=2, k=1, a=";
printArr(a, 2);
kCombination(a, 2, 1);
cout << "n=3, k=2, a=";
printArr(a, 3);
kCombination(a, 3, 2);
cout << "n=5, k=2, a=";
printArr(a, 5);
kCombination(a, 5, 2);
return 0;
}
The result is:
n=1, k=1, a=1
1
n=2, k=1, a=1,2
2
1
n=3, k=2, a=1,2,3
2,3
3,2
1,3
3,1
1,2
2,1
n=5, k=2, a=1,2,3,4,5
4,5
5,4
3,5
5,3
3,4
4,3
2,5
5,2
2,4
4,2
2,3
3,2
1,5
5,1
1,4
4,1
1,3
3,1
1,2
2,1
回答2:
In practice, you have k
possibilities for the first value.
Then, once you have selected this first value, the problem is to generate all permutations with n-1
and k-1
parameters.
This lead to a rather simple recursive implementation. There may be faster methods. However, it is clearly faster than your algorithm.
#include <iostream>
#include <algorithm>
bool Pnk (int n, int k, int *a, int iter, int offset) {
if (n == 0) {
return false;
}
bool check = true;
int index = 0;
std::swap (a[iter], a[iter+offset]);
while (check) {
if (n-1 == 0) {
for (int i = 0; i <= iter; ++i) {
std::cout << a[i] << " ";
}
std::cout << "\n";
}
check = Pnk (n-1, k-1, a, iter + 1, index);
index++;
}
std::swap (a[iter], a[iter+offset]);
return offset != k-1;
}
void Pnk0 (int n, int k, int *a) {
int offset = 0;
while (Pnk (n, k, a, 0, offset)) {
offset++;
}
}
int main () {
int length = 3;
const int size = 4;
int a[size] = {1, 2, 3, 4};
Pnk0 (length, size, a);
}
回答3:
If you don't care about output being in lexicographic order here's a fairly straightforward implementation.
using namespace std;
void perm(int* a, int n, int k, int i)
{
if(i == 0)
{
for(int j=n; j<n+k; j++) cout << a[j] << " ";
cout << endl;
return;
}
for(int j=0; j<n; j++)
{
swap(a[j], a[n-1]);
perm(a, n-1, k, i-1);
swap(a[j], a[n-1]);
}
}
Test (OnlineGDB):
int n = 4, k = 2;
int a[] = {1,2,3,4};
perm(a, n, k, k);
Output:
4 1
2 1
3 1
1 2
4 2
3 2
1 3
2 3
4 3
1 4
2 4
3 4
回答4:
I don't know if n is always 2, but if it is and you don't mind performance, here's some """working""" code:
#include <iostream>
#include <tuple>
#include <vector>
std::vector<std::tuple<int, int> >
get_tuples_from_vector (const std::vector<int> elements)
{
std::vector<std::tuple<int, int> > tuples;
for (int i = 0; i < elements.size(); ++i)
{
for (int j = i + 1; j < elements.size(); ++j)
{
tuples.push_back({elements[i], elements[j]});
}
}
return tuples;
}
std::vector<std::tuple<int, int> >
generate_permutations (const std::vector<int>& elements)
{
if (elements.size() == 0 || elements.size() < 2)
{
return {};
}
std::vector<std::tuple<int, int> > tuples
{
get_tuples_from_vector(elements)
};
std::vector<std::tuple<int, int> > permutations;
for (const auto& tuple: tuples)
{
permutations.push_back(
{ std::get<0>(tuple), std::get<1>(tuple) }
);
permutations.push_back(
{ std::get<1>(tuple), std::get<0>(tuple) }
);
}
return permutations;
}
void print_vector(std::vector<std::tuple<int, int> > elements)
{
for (const auto& element: elements)
{
std::cout << "[ "
<< std::get<0>(element)
<< ", " << std::get<1>(element)
<< "]\n";
}
std::cout << std::endl;
}
int main(int argc, char const *argv[])
{
print_vector(generate_permutations({1, 2, 3, 4, 5}));
}
来源:https://stackoverflow.com/questions/61592209/how-to-generate-all-permutations-of-lenght-n-from-a-set-of-k-elements