Message Boards Message Boards

GROUPS:

[✓]Create a network of 300 nodes that can be clustered into 100-node chunks

Posted 7 months ago
721 Views
|
3 Replies
|
5 Total Likes
|

I have a rather basic problem where I am asked to create a network of 300 nodes that can be clustered into 100-node chunks. Essentially, the nodes have many connections within a chunk and comparatively few between different100-node chunks.

For example, if I made the connections with a chunk occur with 100% probability and between chunks occur 0% I get the following:

edges = {};
For[l = 1, l <= 3  , l++, h = 100*l; 
 For[n = 100 (l - 1) + 1, n < 100*l + 1, n++, 
  For[m = n + 1, m <= 300, m++, 
   If[m <= h,  
    If[RandomReal[{0, 1}] > 0.0, AppendTo[edges, n <-> m]], 
    If[RandomReal[{0, 1}] > 0.99, AppendTo[edges, n <-> m]]]]]]

enter image description here

which is expected and gives a corresponding MatrixPlot of the Adjacency Matrix as:

enter image description here

Which is also all well and good. Now, something very strange seems to be going on when I try to make the connections between the 100-node chunks be non zero. For example, if I made the probability be 1%, the folowing is obtained:

enter image description here

This looks ok, but it becomes very evident that there is a problem when one looks at the corresponding Matrix Plot of the Adjacency Matrix enter image description here

Evidently, there is a very big problem somewhere. I did try to check this in a bit more detail and it seems that everything is working properly with the Matrix Plot. It seems that there is somewhere a problem with my code - but where? The code, in my opinion, seems rather simple and straightforward. I am really confused with what the issue is and could use assistance, it doesn't seem possible that Mathematica would have a problem with something so basic.

3 Replies
Posted 7 months ago

I'd say the indexing of vertices in the AdjacencyMatrix is simply not the same as when you created your Edges list. Looking at your outside-the-square-in-the-same-column/row parts of your MatrixPlot, I would suspect that the vertices to which the vertices of your first group connected were put in the next positions of AdjacencyMatrix indices in the sequence they first showed up on your Edges list.

I suspect you will simply have to rearrange your AdjacencyMatrix rows and columns 'by hand' to get a 'clean' MatrixPlot.

Hope this helps.

The model you are trying to implement is called the stochastic block model. IGraph/M has it built in.

g = IGStochasticBlockModelGame[{{1, 0.01, 0.01}, {0.01, 1, 0.01}, {0.01, 0.01, 1}}, {100, 100, 100}]

Mathematica graphics

IGAdjacencyMatrixPlot[g]

Mathematica graphics


As for why you don't see this adjacency matrix: look at VertexList[g] for your graph and my graph. Both have vertices named as 1, 2, ..., 300, but yours has them in some random order, which the adjacency matrix follows. Remember that the vertex name is not the same thing as the vertex index. The name can be anything. The index goes from one to the vertex count, and corresponds to the position of the vertex in the VertexList. The rows/columns of the AdjacencyMatrix follow the same order. You could use Graph[Range[300], edges] to control the vertex ordering. Or you could reorder vertices in an existing graph with IGReoderVertices from IGraph/M


Finally, if you're new to Mathematica, I strongly suggest you forget that For even exists in the language. If you use it, you'll certainly end up doing things in a convoluted, un-Mathematica-like way, which will slow down your learning process. See https://mathematica.stackexchange.com/questions/134609/why-should-i-avoid-the-for-loop-in-mathematica/12

Posted 7 months ago

Yep yep, it seems that the problem really was that I should have said Graph[Range[300], edges] instead of Graph[edges].

Thanks for the replies!

Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract