Message Boards Message Boards

0
|
10977 Views
|
3 Replies
|
3 Total Likes
View groups...
Share
Share this post:

[?] Convert matrix into "Graph object"?

Dear all,

I am currently trying to get into network analysis using Mathematica 11.0. However, I have a central problem I cannot solve on my own. Hence, I would be very pleased to get support from you guys. I have set up an Input-Output Matrix and now would like to display the network relations within the 20 sectors. You can find the CSV file attached, showing the numbers. I can, for example, use MatrixPlot. However, if I try to use a network related formula (like AdjacencyMatrix, GraphAssortativity or some Centrality Measures) I always get the error "A graph object is expected at position 1". So my question is, is there a possibility to "transform" a matrix (inheriting the input-output "weights") into a graph object inheriting the matrix relations?

Thank you in advance!

Best regards Alex

Attachments:
3 Replies

There seem to be some misunderstandings here.

Graph is not a function. It is a data structure representing a graph (network). The functions you want to use, such as BetweennessCentrality, all work on this data structure. Thus you need to create a proper Graph from a matrix.

You say that you want to analyse this network using AdjacencyGraph. This is another misunderstanding. This function is not for analysis, or for computing any property. It is precisely the function that will take an adjacency matrix and will return a Graph data structure.

Thus, we can do this:

Import the matrix:

In[1]:= matrix = 
  Rest /@ Import["~/Downloads/INDio.csv", HeaderLines -> 1];

In[2]:= Dimensions[matrix]
Out[2]= {56, 56}

Ad adjacency matrix must contain only zeros and ones. Thus we must unitize it:

am = Unitize[matrix]

Then you can create a Graph from it:

g = AdjacencyGraph[am]

You can now use this graph in functions like BetweennessCentrality

If you need to add edge weights to the graph, then use WeightedAdjacencyGraph. This function will treat Infinity entries in the matrix as a missing connection, and all other entries as a connection with the given weight value. So we must replace all zero entries with Infinity:

wam = Replace[matrix, zero_ /; zero == 0 -> Infinity, {2}]

Now you can create a weighted graph:

wg = WeightedAdjacencyGraph[wam]

You can check that it does indeed have edge weights:

PropertyValue[wg, EdgeWeight]

Some functions will make use of these weights, some won't. As I said in my first response, BetweennessCentrality will not. It does make sense to compute betweenness centralities on weighted graphs, while taking the weights into account. But Mathematica cannot currently do this. The IGraph/M package can, however, I strongly suggest that you get familiar with Graphs in Mathematica before you attempt to use the package.

Be sure to look up and understand the meanings of all these different centrality measures before computing them. Betweenness centrality is based on the concept of graph distance. When computing it on a weighted graph, "weights" will represent distance. Thus for betweenness calculation, large weight = weak connection, small weight = strong connection. If I understand your description, this is not the case in your data.

To get interpretable results, you must transform the weights in some meaningful way for each different kind of centrality that you compute.

POSTED BY: Szabolcs Horvát

I really do not understand what you mean by

display the network relations within the 20 sectors.

You have an 18 by 18 matrix, without any zero elements. Is this an adjacency matrix? If yes, you can convert it to an edge-weighted graph using WeightedAdjacencyGraph. This will give you a complete graph (a Graph expression) in which each vertex is also connected to itself. I am not sure how much sense it makes treat this matrix as a graph, given the full connectivity.

Graph manipulation functions expect Graph expressions as input (since version 10.3 or 10.4 a list of rules is also accepted).


Since you have a fully connected graph, it does not matter in your case, but be aware that WeightedAdjacencyGraph represents missing edges with Infinity, not with zero. This is somewhat annoying because it is inconsistent with WeightedAdjacencyMatrix, which uses 0. While Infinity does make sense in some (not all) applications, it is much more common to see data that uses 0.

The IGraph/M package has a convenience function, IGWeightedAdjacencyGraph, which allows you to select the element representing missing edges. It uses zero (0 or 0.0) by default.

Also be aware that not all graph measures in Mathematica will make use of edge weights. BetweennessCentrality and PageRankCentrality will ignore them. EdgeBetweennessCentrality and ClosenessCentrality will make use of them. IGraph/M provides alternatives for many of these functions precisely to add weight supports (IGBetweenness, IGPageRank, etc.).


Disclosure: IGraph/M is my package. It is a work-in-progress interface to the popular igraph network analysis library.

I recommend getting comfortable with Graph and the built-in graph-manipulation functions before starting to use IGraph/M.

POSTED BY: Szabolcs Horvát

Dear Szabolcs,

thank you very much for your immediate reply. You are right, the example was not appropriate. I attached another file, illustrating my issue better. Attached you can find a 56x56 matrix illustrating sectoral trade relations in India. As you can see some sectors are not engaging in intersectoral trade, therefore the input-output value is 0. Now I would like to analyze this network in Mathematica, using for example AdjacencyGraph or BetweennessCentrality.

Unfortunately, as already explained, I always get the answer "A graph object is expected at position 1". Hence I still not understand how to create or transform my data at hand this way, that I can use these type of functions. I looked into the Graph function you linked, but I am not aware how to define the sectors as edges/vertices. Hence, my question would be how is possible to convert the matrix given in a graph object, converting the sector names given in row/column 1 as edges/vertices?

I would really appreciate if you could help me again, thank you in advance!

Best regards Alex

Attachments:
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