I have a weighted, directed graph with 100 vertices and the maximal number of edges, 9900. Are there Mathematica tools or packages available to visualize which edges have large weights? (If you know of non-Mathematica software, that's fine, too.)

Background: My institution has 100 majors, and I'm interested in studying how students change majors, where they start compared to where they end up.

  • 1
    $\begingroup$ What do you mean by "the maximal number of edges, 20,000"? A 100-vertex (simple) directed graph won't have more than 9900 edges. I am not sure how to draw this many edges and still show some useful information about individual ones ... $\endgroup$ – Szabolcs Feb 5 '18 at 20:29
  • $\begingroup$ @Szabolcs OK 9900 in each direction, omitting loops, gives 19800. The large number is precisely the question - I'm looking for a visualization to emphasize the largest weights. $\endgroup$ – stopple Feb 5 '18 at 22:20
  • $\begingroup$ If there's an edge in each direction, that would give 9900 total, not 19800. Why don't you just drop the low-weight edges (threshold the weighted adjacency matrix) and adjust the thickness/opacity of the remaining ones according to their weights? $\endgroup$ – Szabolcs Feb 5 '18 at 22:28
  • $\begingroup$ @Szabolcs Of course you're right; I'll edit the question. And thanks for the suggestion. I'll try that, but am interested if there's a better approach. $\endgroup$ – stopple Feb 5 '18 at 22:37
  • $\begingroup$ Perhaps you should consider statistical validation of the edges, which is another, a tad more involved method of pruning away the less important edges, see for example here. Unfortunately, I am unfamiliar with the details so I won't be able to help you further. $\endgroup$ – Kiro Feb 6 '18 at 6:23

I will use IGraph/M for the following answer.

Here's a complete directed graph on 100 with weighted edges. There are lots of edges with small weights and a few with large weights.

graph = CompleteGraph[100, DirectedEdges -> True, 
   EdgeWeight -> RandomVariate[ExponentialDistribution[5], 100 99]];

We will:

  • remove edges below a certain weight threshold
  • style the remaining edges based on weight by adjusting their opacity and thickness

First, I need to utility functions.

Threshold an array (replace values below the threshold by zero). Threshold does not seem to work on sparse arrays.

threshold[arr_, th_] := arr UnitStep[arr - th]

Scale the elements of an array so that the largest is 1.

scale[arr_] := arr/Max@Abs[arr]

Now we convert the graph to a weighted adjacency matrix, threshold, convert back, then style it.

   threshold[WeightedAdjacencyMatrix[graph], 0.9], 
   GraphLayout -> "CircularEmbedding", ImageSize -> Large
] //
  Directive[AbsoluteThickness[5 #], Opacity[#], Arrowheads[0.05 #]] &,
  EdgeStyle -> scale@*IGEdgeProp[EdgeWeight]

enter image description here

We could use a smaller threshold to include more edges, but compute the opacity based on a power of the weight, to emphasize strong edges and fade out weak ones.

  threshold[WeightedAdjacencyMatrix[graph], 0.7], 
  GraphLayout -> "CircularEmbedding", ImageSize -> Large] //     
  Directive[AbsoluteThickness[5 #], Opacity[#^3], 
    Arrowheads[0.05 #]] &, EdgeStyle -> scale@*IGEdgeProp[EdgeWeight]]

enter image description here

These choices about visualization are yours to make.


To draw the subgraph with large weights, you might want to apply the following function:

select[matrix_, lB_, uB_] := 
  matrix*Map[Boole[lB <= # <= uB] &, matrix, {-1}];
sa = SparseArray[select[mat, .1, .6]];
weightedG = Graph[sa["NonzeroPositions"], EdgeWeight -> sa["NonzeroValues"], DirectedEdges -> True];

You can apply the select[...] function for various domains {lB, uB} on matrix mat and draw the directed graph only for the desired domain.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.