0

I have a set of 30 nodes, each node can connect to one and only one at a time. Moreover, one node cannot connect to another specific one (say, the one with and id= id+15, e.g. 1-16, 2-17... the rule is not relevant, I can change the association node-number). What I am looking for is a matrix where each row is a possible set of connections where all nodes are connected to another and that each nodes connected to all other nodes (except the forbidden ones). It would look like this (read it in pairs)

[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;
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  1   30;
1   4   2   5   3   6   7   10  8   11  9   12  13  16  14  17  15  18  19  22  20  23  21  24  25  28  26  29  27  30;
4   7   5   8   6   9   10  13  11  14  12  15  16  19  17  20  18  21  22  25  23  26  24  27  28  1   2   29  3   30;
...
]

I was trying to develop this by collecting all the possible non-repeated combination (binomial 30 over 2 => 435) with nchoosek but now I am dealing with the problem of placing all the pairs that I have in a 29-by-15 pairs matrix without repetition. This would allow me to respect the constraint easily by removing the forbidden pairs from the output of nchoosek.

I am sure this is a known graph problem but I was not able to find anything about that. Does anyone know to implement it?

Aim is to describe the timeline of point-to-point connections between 30 nodes. Since each one cannot connect to a particular other, the total number of possible connections is 420 (435 - 15 forbidden connections), for a total of 28 time slots (rows) containing 15 pairs (30 column).

Edit2: A further way could be to generate a 30x30 matrix having as row all possible combinations of numbers from 1 to 30 but with the constraint that pairs shall not be repeated (not in order). E.g. the following are possible valid vectors (limited to 12 elements instead of 30), however the second is discarded since couple [2 1]is equivalent to couple [1 2]. Don't know how to deal with the constraint, though.

[1 2 3 4 5 6 7 8 9 10 11 12]
[2 1 3 4 5 6 7 8 9 10 11 12]
RockeJoe
  • 1
  • 1

2 Answers2

0

You can do that:

clear   all
%creation of vector
v       = 1:30;
%creation of combinations
[p1,p2] = meshgrid(v,v);
A       = [p1(:) p2(:)];
B       = fliplr(A);
%selection of unique combinations
[~,ind] = ismember(A,B,'rows');
ind     = ind > (1:length(ind))';
A       = A(ind,:);
obchardon
  • 10,614
  • 1
  • 17
  • 33
  • This is the same you can achieve with: `nchoosek(30,2)` – RockeJoe Oct 02 '15 at 14:01
  • ok so i missed the question, why did you not apply the rules directly on your vector (435x2) ? – obchardon Oct 02 '15 at 14:11
  • I do, and I have a resulting 420x2 vector (435 pairs minus the 15 forbidden). My problem is still to arrange the pairs of the 420x2 vector into a 28x30 matrix (or 28x(2x15) if you like so) so that elements in a row are not repeated. – RockeJoe Oct 02 '15 at 14:19
  • Don't you want to use a connectivity matrix? A(i,j)=1 if i can connect to j and 0 if not. (then you can retrieve all the pairs with find(A)) What do you want to use the matrix for? – BillBokeey Oct 02 '15 at 14:39
  • What I would like to have is a matrix where the rows are time slots and the column describes the connection in place. I see what you mean with a connectivity matrix but I don't know how to construct it. In principle I should have many of them, otherwise the matrix will be almost all 1's. I should construct 28 sparse matrix where I have only one 1 on each row and each column. – RockeJoe Oct 02 '15 at 14:44
0

Okay,

First solution i found was too use perms(1:30) but the output is way too big! It gives an array with all possible permutations of 1:30 (30! which is over 10^32.. ).

Soo we need to find another way in order to select valid rows a priori.

One possible lead would be to calculate all the rows recursively for all even values of N, from N=2 (initialization) to N=30 (result you want), i.e :

(I'll denote by Res(k) the set of rows that satisfy your conditions for N=k )

Initialization :

N=2 : Res(2)=[12];

N-->N+2 :

Res(N+2)={

  • All rows of Res(N) adding [N+1 N+2] at the end
  • For (i,j) in (1:N-1)x(2:N), i < j, find the rows that contain the pair (i j) : replace it with (i N+1)(j N) and (i N)(j N+1)

}

Example : 2-->4

Res[4]=[

  • 1234

  • 1423 and 1324 (i=1, j=2)

]

Example 2 : 4-->6

Res(6)= [

  • 123456 , 142356 , 132456

  • 152634 and 162534 (i=1 j=2) , 153624 and 163524 (i=1 j=3) , 154623 and 164523 (i=1 j=4) , 253614 and 263514 (i=2 j=3) ,132546 and 132645 (i=2 j=4) , 123546 and 123645 (i=3 j=4)

]

I'd suggest you use cell arrays to keep one pair per cell, in order to easily find pairs.

BillBokeey
  • 3,168
  • 14
  • 28