Help with permutations

Mar 26, 2017 at 11:48pm
Hello! So I have been solving some programming tasks and I came up to a program that requires me to test some easy cases on all permutations of a given 5x5 matrix. Lets say that matrix looks as:
1 2 3 4 5
2 3 4 5 1
3 4 5 1 2
4 5 1 2 3
5 1 2 3 4
So permutations allowed are only to swap rows (between themselves) and columns (also between themselves).
example: (I will swap rows 0 and 1 and columns 0 and 1)
3 2 4 5 1
2 1 3 4 5
4 3 5 1 2
5 4 1 2 3
1 5 2 3 4

I really do not know how to do this.. Can anyone help please? :)
Mar 27, 2017 at 11:21am
a program that requires me to test some easy cases on all permutations

it is not quite clear (at least to me) what exactly are the requirements of the program. does your quote mean that you are given the initial state and the final state of a 2D matrix and you have to check if it's possible to go from initial -> final only by swapping rows and cols (which, off the top of my head, I think should be possible) or do you mean you need an algorithm to swap matrix row and cols?
in any case, whatever may be the requirement(s) consider using a std::vector<std::vector<int>> as the holding container so that you can use all the standard library facilities with the container/matrix
Last edited on Mar 27, 2017 at 11:21am
Mar 27, 2017 at 2:26pm
Oh, sorry for my bad english. I need an algorithm that makes all permutations of rows of a matrix and all permutations of columns of that matrix combined, so for 3x3 matrix for instance i get all 12 permutations. From:
123
231
312
one can get 12 permutations because of 3! Permutations of rows and 3! Permutations of columns giving (3!)**2=12

So for 5x5 matrix there would be (5!)**2=14400 permutations possible to be obtained with before mentioned moves.

And what I need is an algorithm that makes all of these permutations. Thanks in advance, I hope that I expressed myself properly this time :)

P.S. that quote really means that with those matrixes, when generated there is not much work to be done. It implies that this is the most difficult part of the program and I know what I will do with generated matrixes because there is some simple testing to be done with them. (Summing some values which are collected on all matrixes)
Mar 27, 2017 at 5:47pm
Here's an outline of the steps for the permuatation of the rows:
std::vector<std::vector<int>> - the 2D matrix that has the given numbers
extract row and col information from the matrix
initialize vector 0, 1, ..., row size - 1 with std::iota to run permutations of the number of rows:
http://en.cppreference.com/w/cpp/algorithm/iota
run std::next_permutation on this vector
http://en.cppreference.com/w/cpp/algorithm/next_permutation
declare another std::vector<std::vector<int> - capture each permuation order and push_back into this 2D vector
finally one last 3D std::vector<std::vector<std::vector<int>>> that will have the various row permuation vectors
in the program below i've tried to comment within the program as well on the above lines:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#include <iostream>
#include <algorithm>    // std::next_permutation, std::iota
#include <vector>

int main ()
{
    std::vector<std::vector<int>> myVec{{1,2,3, 8}, {2,3,1,5},{3,1,2,2}};//given 2D matrix
    std::vector<int> rowNum(myVec.size());//extract row information
    std::iota(rowNum.begin(), rowNum.end(), 0);//to capture the permuation of the number of rows ...
    //http:en.cppreference.com/w/cpp/algorithm/iota
    std::vector<std::vector<int>> rowCombos{};//... and the actual permutations are saved in this vector

    do
    {
        {
            std::vector<int> tempVec{};
            for (size_t i = 0; i < myVec.size(); ++i)
            {
                tempVec.push_back(rowNum[i]);
            }
            rowCombos.push_back(tempVec);
            tempVec.clear();//http://en.cppreference.com/w/cpp/container/vector/clear
            }
    }   while ( std::next_permutation(rowNum.begin(),rowNum.end()));
    //http://en.cppreference.com/w/cpp/algorithm/next_permutation

    std::vector<std::vector<std::vector<int>>> threeDVec{};//the 3D vector that'll save the verious permuations by row of the 2d vector
    for (const auto& elem : rowCombos)
    {
        std::vector<std::vector<int>> tempVec{};//a temporary holding vector of the various 2d vector permutations until push_back into the 3D vector
        for (const auto& elemI : elem)
        {
            tempVec.push_back(myVec[elemI]);
        }
        threeDVec.push_back(tempVec);
        tempVec.clear();
    }
    for (const auto& elem1 : threeDVec)//now print out the 3D vector
    {
        for (const auto& elem2: elem1)
        {
            for (const auto& elem3 : elem2)
            {
                std::cout << elem3 << " ";
            }
            std::cout << "\n";
        }
        std::cout << "\n";
    }
}
/* output
1 2 3 8
2 3 1 5
3 1 2 2

1 2 3 8
3 1 2 2
2 3 1 5

2 3 1 5
1 2 3 8
3 1 2 2

2 3 1 5
3 1 2 2
1 2 3 8

3 1 2 2
1 2 3 8
2 3 1 5

3 1 2 2
2 3 1 5
1 2 3 8

*/

OK, so now the question remains how are we going to swap the cols? well I think one way might be to transpose the vector first so that the cols become rows and then apply the above program and transpose the vectors back! and a good way to transpose a 2D vector can be found here: http://stackoverflow.com/questions/6009782/how-to-pivot-a-vector-of-vectors, particularly the second answer from user Bjorn Pollex

edit: if
(3!)**2=12
I'm wondering how can it be that
(5!)**2=14400
? shouldn't it be 240?
Last edited on Mar 27, 2017 at 5:54pm
Topic archived. No new replies allowed.