# Visualizing Matrix that represents a network

I have a nxn matrix $A$ that represents a network. I have $n$ nodes in the network, and the matrix $A$ represents the number of connection (edges) between the different nodes.

For example, if $A$=0, n=5, then I have 5 nodes which are not conencted. For n=5, $A_{1,3}=2$ and $A_{i,j}=1$ otherwise, we have a network with 5 nodes where each of them has one connection, and element 1 and 3 have 2 connections (edges).

How is it possible to visualize such a matrix as a network? (which makes the interpretation much easier).

• Start with the documentation, reference.wolfram.com/language/guide/GraphsAndNetworks.html – N.J.Evans Dec 5 '15 at 16:10
• Thanks Evans. Of course I've seen this documentation, but not any of the examples are even close to what I described. I was thinking that a matrix is a very common representation for networks, thus there should be one instruction that i might be missing somehow. – Mario Krenn Dec 5 '15 at 16:25
• Your representation looks like it would work with AdjacencyGraph, or a WeightedAdjacencyGraph. – N.J.Evans Dec 5 '15 at 16:39

First, let's say you want to show a weighted graph, in particular, the graph you have. Then you can specify the EdgeStyle by making the edge 1<->3 different (thicker) than the rest:

CompleteGraph[5,
EdgeStyle -> {e_ :> Directive[Thickness[If[e == 1 <-> 3, 0.01, 0.005]]]},
VertexLabels -> "Name"
]


Here, the EdgeStyle option is not an explicit list, but a rule for the defining the Thickness. This makes it easier, since we can very clearly specify the edge 1<->3 using the If statement.

This produces a graph that is the same (as far as graph theory is concerned) as the complete graph. We have only altered the style. We can also modify the style of the edges in the same way:

CompleteGraph[5,
EdgeWeight -> {e_ :> If[SameQ[e == 1 <-> 3, True], 2, 1]},
EdgeLabels -> "EdgeWeight",
VertexLabels -> "Name"
]


In this graph, the weights of the edges are specified. They are drawn with the same thickness, but Mathematica will understand that there are weights and treat the graph accordingly.

Of course, we haven't really used your matrix yet. This might be easier, if your graph is so close to the existing complete graph -- we are just tweaking some of its properties.

If we wanted to start from scratch, we could define the adjacency matrix for your graph in any way (so long as it's a valid way to make up an adjacency matrix). Here, I'll still capitalize on the fact that it is so close to the complete graph, but you could enter the matrix manually -- and it could be anything.

matrix = AdjacencyMatrix[CompleteGraph[5]] +
SparseArray[{{3, 1} -> 1, {1, 3} -> 1}, {5, 5}];


This will allow us to re-do the above graphics, but let's specify things in terms of the matrix matrix (I am not creative with names). First of all, Mathematica will already do this as a multigraph right away, which we haven't even done yet:

AdjacencyGraph[matrix, VertexLabels -> "Name"]


Notice that the position of the vertices has changed -- making 1 and 3 closer together, not like they were in our other graphs. Mathematica is trying to rearrange the multigraph in a way it would not rearrange a simple graph.

But let's say we wanted a weighted graph and/or a stylized graph like the two above, using our matrix in the most general way possible. First, we need to make up a base graph, the underlying edge set for this graph.

basegraph =
Flatten[Table[
If[matrix[[i,j]]==0,{},i <-> j],
{i, 1, Length[matrix]}, {j, i + 1, Length[matrix]}
]];


Here, we have assumed the graph is not directed (i.e. that matrix is symmetric). If that is not true, you will need to modify this slightly.

We can reproduce the graph of thick edges:

Graph[basegraph,
EdgeStyle -> {e_ :> Directive[Thickness[0.005*matrix[[e[[1]], e[[2]]]]]]},
VertexLabels -> "Name"
]


or the graph of weights:

Graph[basegraph,
EdgeWeight -> {e_ :> matrix[[e[[1]], e[[2]]]]},
EdgeLabels -> "EdgeWeight",
VertexLabels -> "Name"
]


We can even combine them (just use all the options):

Graph[basegraph,
EdgeStyle -> {e_ :> Directive[Thickness[0.005*matrix[[e[[1]], e[[2]]]]]]},
EdgeWeight -> {e_ :> matrix[[e[[1]], e[[2]]]]},
EdgeLabels -> "EdgeWeight",
VertexLabels -> "Name"
]


• One might ask "Why not use WeightedAdjacencyMatrix to do this?" While that would certainly produce a Graph object that has the correct weighted properties, this is a question about visualization. That would produce, in this example, zero-weight loops at all vertices. In general, it would be significantly worse if there are any other non-edges in some other version of this matrix. Those non-edges would become zero-weight edges that are apparently present in the visual representation of the graph. – Kellen Myers Dec 6 '15 at 18:42
• that looks very nice, thanks for this very detailed answer! – Mario Krenn Dec 9 '15 at 8:46
• I was just trying to adjust this code, and I saw that I dont find how to apply zero-connections between some nodes. Is that possible? – Mario Krenn Dec 26 '15 at 15:28
• Off the top of my head, I think you want weight Infinity instead of zero for that to work the way I imagine you are intending. Let me know if that doesn't work (or if I'm misunderstanding). – Kellen Myers Jan 11 '16 at 1:35