Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ All combinations of a vector

Lets say we have some numbers from 0 to n, we want to scramble those in size s and want to see every possible combination.

So the number of permutations happens to be equal to s! * n!/(s!*(n-s)!).

Example with n = 3 and s = 3:

0 1 2 | 0 1 3 | 0 2 1 | 0 2 3 | 0 3 1 | 0 3 2 | 1 0 2 | 1 0 3 | 1 3 2 
1 2 3 | 1 2 0 | 1 3 0 | 2 0 1 | 2 1 0 | 2 0 3 | 2 3 0 | 2 3 1 | 2 1 3
            3 0 1 | 3 1 0 | 3 0 2 | 3 2 0 | 3 1 2 | 3 2 1

Is there a smooth way using boost/stl to achieve this?

like image 534
NaCl Avatar asked Sep 13 '14 21:09

NaCl


2 Answers

Here is code using the link that T.C. referred to in the comments (http://howardhinnant.github.io/combinations.html):

#include "../combinations/combinations"
#include <iostream>
#include <vector>

int
main()
{
    std::vector<int> v{0, 1, 2, 3};
    typedef std::vector<int>::iterator Iter;
    for_each_permutation(v.begin(), v.begin()+3, v.end(),
        [](Iter f, Iter l)
        {
            for (; f != l; ++f)
                std::cout << *f << ' ';
            std::cout << "| ";
            return false;
        }
    );
    std::cout << '\n';
}

0 1 2 | 0 2 1 | 1 0 2 | 1 2 0 | 2 0 1 | 2 1 0 | 0 1 3 | 0 3 1 | 1 0 3 | 1 3 0 | 3 0 1 | 3 1 0 | 0 2 3 | 0 3 2 | 2 0 3 | 2 3 0 | 3 0 2 | 3 2 0 | 1 2 3 | 1 3 2 | 2 1 3 | 2 3 1 | 3 1 2 | 3 2 1 | 

One significant advantage of this library over std::next_permutation is that the elements that are being permuted need not be sorted, nor even need be less-than-comparable. For example:

#include "../combinations/combinations"
#include <iostream>
#include <vector>

enum class color
{
    red,
    green,
    blue,
    cyan
};

std::ostream&
operator<< (std::ostream& os, color c)
{
    switch (c)
    {
    case color::red:
        os << "red";
        break;
    case color::green:
        os << "green";
        break;
    case color::blue:
        os << "blue";
        break;
    case color::cyan:
        os << "cyan";
        break;
    }
    return os;
}

int
main()
{
    std::vector<color> v{color::blue, color::red, color::cyan, color::green};
    typedef std::vector<color>::iterator Iter;
    for_each_permutation(v.begin(), v.begin()+3, v.end(),
        [](Iter f, Iter l)
        {
            for (; f != l; ++f)
                std::cout << *f << ' ';
            std::cout << "| ";
            return false;
        }
    );
    std::cout << '\n';
}

blue red cyan | blue cyan red | red blue cyan | red cyan blue | cyan blue red | cyan red blue | blue red green | blue green red | red blue green | red green blue | green blue red | green red blue | blue cyan green | blue green cyan | cyan blue green | cyan green blue | green blue cyan | green cyan blue | red cyan green | red green cyan | cyan red green | cyan green red | green red cyan | green cyan red |

like image 75
Howard Hinnant Avatar answered Nov 15 '22 05:11

Howard Hinnant


LIVE DEMO

#include <algorithm>
#include <vector>
#include <iterator>
#include <iostream>

void dfs(int depth, int s, int i, std::vector<int>& c, const std::vector<int>& v)
{
    if (depth == s)
    {
        do
        {
            std::copy(c.begin(), c.end(), std::ostream_iterator<int>(std::cout, " "));
            std::cout << "| ";
        }
        while (std::next_permutation(c.begin(), c.end()));
    }
    else
    {
        for (int j = i + 1; j < (int)v.size(); ++j)
        {
            c.push_back(v[j]);
            dfs(depth + 1, s, j, c, v);
            c.pop_back();
        }
    }
}

int main()
{
    std::vector<int> v{ 0, 1, 2, 3 };
    std::sort(v.begin(), v.end());
    v.erase(std::unique(v.begin(), v.end()), v.end());    
    std::vector<int> c;
    const int length = 3;
    dfs(0, length, -1, c, v);
}

Output:

0 1 2 | 0 2 1 | 1 0 2 | 1 2 0 | 2 0 1 | 2 1 0 | 0 1 3 | 0 3 1 | 1 0 3 |
1 3 0 | 3 0 1 | 3 1 0 | 0 2 3 | 0 3 2 | 2 0 3 | 2 3 0 | 3 0 2 | 3 2 0 |
1 2 3 | 1 3 2 | 2 1 3 | 2 3 1 | 3 1 2 | 3 2 1
like image 37
Piotr Skotnicki Avatar answered Nov 15 '22 05:11

Piotr Skotnicki