0

I have a symmetric adjacency matrix with zero value on its diagonal. now i am looking for reordering method to show community which divides the matrix in two cliques with +1 and -1 values respectively. it would be appreciated if someone could help me in this regards.

for instance: matrix(10,10)

  0     1    -1     1     1    -1     1     1    -1    -1
  1     0    -1     1     1    -1     1    -1    -1    -1
 -1    -1     0    -1    -1     1     1     1     1    -1
  1     1    -1     0     1    -1     1    -1    -1    -1
  1     1    -1     1     0    -1     1     1    -1    -1
 -1    -1     1    -1    -1     0    -1     1     1     1
  1     1     1     1     1    -1     0     1     1     1
  1    -1     1    -1     1     1     1     0    -1    -1
 -1    -1     1    -1    -1     1     1    -1     0     1
 -1    -1    -1    -1    -1     1     1    -1     1     0

output must be :

  1     1     1     1     1    -1    -1    -1    -1    -1
  1     1     1     1     1    -1    -1    -1    -1    -1
  1     1     1     1     1     1    -1    -1    -1    -1
  1     1     1     1     1    -1    -1    -1    -1    -1
  1     1     1     1     1    -1    -1    -1    -1    -1
 -1    -1    -1    -1    -1     1     1     1     1     1
 -1    -1    -1    -1    -1     1     1     1     1     1
 -1    -1    -1    -1    -1     1     1     1     1     1
 -1    -1    -1    -1    -1     1     1     1     1     1
 -1    -1    -1    -1    -1     1     1     1     1     1

zero entries can be considered as 1

A. Donda
  • 8,381
  • 2
  • 20
  • 49
sahar
  • 3
  • 3
  • What do you want as an output? – user1543042 Jul 24 '15 at 12:05
  • i need to check the community in this matrix,, i want to know is it feasible to draw two cliques by sorting the rows and columns. indeed one clique including elements with value 1 and the other clique with value -1. – sahar Jul 24 '15 at 12:42
  • Put out a matrix which says `out = ...`. And then explain what the features are. – user1543042 Jul 24 '15 at 13:11

1 Answers1

0

Because of the example, I'm not certain how to deal with most cases, so I guessed.

a = round(3*rand(10) - 1.5);
a(logical(eye(size(a)))) = 0;
b = cliques(a);

This maintains the symmetry requirement and only needs to break from the "wings" of -1's when there is one remaining after all the other filters (i.e. if the number of -1's is odd then to maintain symmetry an element must go on the diagonal).

function b = cliques(a)
    a(a == 0) = 1;
    n = sum(a(:) == -1);

    s = floor(sqrt(n/2));

    b = ones(size(a));
    b(end - s + 1:end, 1:s) = -1;
    b(1:s, end - s + 1:end) = -1;

    n = n - 2*s^2;

    bands = floor(n/4);
    b(s+1, end - bands + 1:end) = -1;
    b(end - bands + 1:end, s+1) = -1;

    b(end - s, 1:bands) = -1;
    b(1:bands, end - s) = -1;

    n = n - 4*bands;

    dots = floor(n/2);
    if dots
        b(end - s, s + 1) = -1;
        b(s + 1, end - s) = -1;
    end

    n = n - 2*dots;

    if n
        b(1,1) = -1;
    end

    contourf(b, 'LevelList', [-1,1]);
    set(gca,'Ydir','reverse')

    sum(a(:)) == sum(b(:))
end
user1543042
  • 3,422
  • 1
  • 17
  • 31
  • Dona thank you so much for your assistance, i checked it with another interaction matrix and it did work. but i cannot understand how you knew it must be written in this way, will you explain it briefly? – sahar Jul 29 '15 at 15:59
  • It starts by finding the number of unplace `-1`'s in your matrix (this is `n`, and I'll call it's original value `n0`). It calculates the largest square that fit in `n/2` elements (the side length of the square is `s`). Now `n = n0 - 2s^2 < 2s+1`. It then find how far it can advance 4 bands of equal length (this is `b`) next to the square. So `n = n0 - 2s^2 - 4b < 4`. Next it finds if it can place a point at the top corner of each square (this is `dots`). Meaning `n = n0 - 2s^2 - 4b - 2*dots < 2`. Finally if `n = 1` it places it in the upper.left corner and `n = 0`. – user1543042 Jul 29 '15 at 18:57
  • This algorithm works only in special cases ( cases that i absolutely know can be divided in two factions), i was wondering how it is feasible to get the out put by sorting the matrix(input) respect to a specific row or column ? – sahar Jul 30 '15 at 17:01
  • This is all ready more broad than the example you gave so I'm not sure how I could have extrapolated any further. Also, I'm not sure what you mean, do you want to specify a set of starting points and build from there? – user1543042 Jul 30 '15 at 17:25
  • the written code does not work based on sorting row and columns to show the community, it finds the different elements and then place them next together. i want to know how it is possible to get the same result by sorting rows and columns with respect to a certain column(row). i tried this method but i couldn't get the desired outcome... someone who mentioned this result in his paper told, if i want the code be more practical in different cases matrix must can be sorted by the entries of one particular column (or row). – sahar Jul 31 '15 at 07:58