Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create a symmetric matrix of 1's and 0's with constant row and column sum

I'm trying to find an elegant algorithm for creating an N x N matrix of 1's and 0's, under the restrictions:

  • each row and each column must sum to Q (to be picked freely)
  • the diagonal must be 0's
  • the matrix must be symmetrical.

It is not strictly necessary for the matrix to be random (both random and non-random solutions are interesting, however), so for Q even, simply making each row a circular shift of the vector

[0 1 1 0 ... 0 0 0 ... 0 1 1] (for Q=4)

is a valid solution.

However, how to do this for Q odd? Or how to do it for Q even, but in a random fashion?

For those curious, I'm trying to test some phenomena on abstract networks.

I apologize if this has already been answered before, but none of the questions I could find had the symmetric restriction, which seems to make it much more complicated. I don't have a proof that such a matrix always exists, but I do assume so.

like image 614
Kaare Avatar asked May 20 '14 13:05

Kaare


2 Answers

The object that you're trying to construct is known more canonically as an undirected d-regular graph (where d = Q). By the handshaking theorem, N and Q cannot both be odd. If Q is even, then connect vertex v to v + k modulo N for k in {-Q/2, -Q/2 + 1, ..., -1, 1, ..., Q/2 - 1, Q/2}. If Q is odd, then N is even. Construct a (Q - 1)-regular graph as before and then add connections from v to v + N/2 modulo N.

If you want randomness, there's a Markov chain whose limiting distribution is uniform on d-regular graphs. You start with any d-regular graph. Repeatedly pick vertices v, w, x, y at random. Whenever the induced subgraph looks like

v----w

x----y ,

flip it to

v    w
|    |
x    y .
like image 200
David Eisenstat Avatar answered Oct 19 '22 16:10

David Eisenstat


You can perhaps always follow your circular shift algorithm, when possible.

The only condition you need to follow while using the circular shift algorithm is to maintain the symmetric nature in the first row.

i.e. keeping Q 1's in the first row so that Q[0,1] to Q[0,N-1] {Assuming 0 indexed rows and cols, Q[0,0] is 0.} is symmetric, a simple example being 110010011.

Hence, N = 10, Q = 5, you can get many possible arrangements such as:

0 1 0 0 1 1 1 0 0 1 
1 0 1 0 0 1 1 1 0 0 
0 1 0 1 0 0 1 1 1 0 
0 0 1 0 1 0 0 1 1 1 
1 0 0 1 0 1 0 0 1 1 
1 1 0 0 1 0 1 0 0 1 
1 1 1 0 0 1 0 1 0 0 
0 1 1 1 0 0 1 0 1 0 
0 0 1 1 1 0 0 1 0 1 
1 0 0 1 1 1 0 0 1 0 

or

0 1 1 0 0 1 0 0 1 1 
1 0 1 1 0 0 1 0 0 1 
1 1 0 1 1 0 0 1 0 0 
0 1 1 0 1 1 0 0 1 0 
0 0 1 1 0 1 1 0 0 1 
1 0 0 1 1 0 1 1 0 0 
0 1 0 0 1 1 0 1 1 0 
0 0 1 0 0 1 1 0 1 1 
1 0 0 1 0 0 1 1 0 1 
1 1 0 0 1 0 0 1 1 0 

But as you can see for odd N(that means even N-1) and odd Q there can't be any such symmetric distribution.. Hope it helped.

like image 1
Arkanath Pathak Avatar answered Oct 19 '22 17:10

Arkanath Pathak