I have a graph WS = RandomGraph[WattsStrogatzGraphDistribution[n, p, k]]. I need to plot several characteristics of each v: the clustering coefficient, number of triples at v, eccentricity, vertex centrality, closeness, and betweenness. I'm relatively inexperienced with Mathematica and quite lost on how to achieve this. I don't expect the exact lines of the solution, I'd just like some pointers on how to approach this. First of all, how do I get a set of all the vertices of WS? Then, how do I get the values of the different measures for each v in WS?


Update: Using GraphComputation`GraphPropertyChart for displaying vertex attributes:

ws = RandomGraph[WattsStrogatzGraphDistribution[6, 0.6, 2]];
centralities = {LocalClusteringCoefficient, EccentricityCentrality, 
   VertexDegree, ClosenessCentrality, BetweennessCentrality,  EigenvectorCentrality};
Through[centralities[ws]] // 
 Style[TableForm[Round[#, .001], TableHeadings -> {centralities, VertexList[ws]}], 
   FontFamily -> "Calibri", 24] &

enter image description here

Legended[GraphComputation`GraphPropertyChart[RemoveProperty[ws, VertexLabels], 
 Automatic -> (#[ws] + .1), 
 ImageSize -> 300, ChartStyle -> 63], 
 Placed[Style[#, 20, "Panel"], {1/2, 97/100} ]] & /@ 
    centralities /. False -> True // 
Partition[#, 3] & // Grid

enter image description here

Original answer:

Using the built-in functions mentioned in @Oska's comment:

ws = RandomGraph[WattsStrogatzGraphDistribution[6, 0.1, 2]]; 
centralities = {LocalClusteringCoefficient,  EccentricityCentrality, 
    VertexDegree, ClosenessCentrality, BetweennessCentrality}; 
Through[centralities[ws]] // Style[TableForm[Round[#, .001], 
      TableHeadings -> {centralities, VertexList[ws]}], FontFamily -> "Calibri", 24] &

enter image description here

|improve this answer|||||
  • $\begingroup$ Interesting choice of font. $\endgroup$ – m_goldberg May 10 '14 at 18:50
  • $\begingroup$ @m_goldberg, Office 2010 default font family:) $\endgroup$ – kglr May 10 '14 at 18:58
  • $\begingroup$ Your answer and @Oska's comment were very helpful. Your efforts are appreciated. $\endgroup$ – bhkj May 11 '14 at 15:50
  • $\begingroup$ @bhkj I only Googled what you typed. See here and here. $\endgroup$ – Öskå May 11 '14 at 16:02
  • $\begingroup$ @bhkj, glad it was useful for you. Thanks for the accept. $\endgroup$ – kglr May 11 '14 at 16:22

There's one measure you were asking for missing from the existing answer:

number of triples at v

This can be obtained efficiently using IGraph/M's IGAdjacentTriangleCount function.


IGAdjacentTriangleCount[graph] counts the triangles each vertex participates in. Edge directions are ignored.

IGAdjacentTriangleCount[graph, vertex] counts the triangles vertex participates in.

IGAdjacentTriangleCount[graph, {vertex1, vertex2, ...}] counts the triangles the specified vertices participate in.

It can also be implemented in pure Mathematica very concisely (though less efficiently) using the fact that if $A$ is the graph's adjacency matrix, then $(A^k)_{ij}$ contains the number of ways we can get from vertex $i$ to vertex $j$ in $k$ hops. Thus $(A^3)_{ii}$ gives twice the number of triangles that $i$ participates in (twice because the triangle, i.e. 3-loop, can be traversed in two directions).

g = RandomGraph[{10, 20}];

(* {0, 4, 3, 7, 5, 6, 0, 9, 0, 2} *)

With[{am = AdjacencyMatrix[g]},
(* {0, 4, 3, 7, 5, 6, 0, 9, 0, 2} *)

As for visualizing these, these days I much prefer doing this with IGraph/M (disclosure: I'm the author). Here's a simple example:

RandomGraph[WattsStrogatzGraphDistribution[20, 0.2]] // 
 IGVertexMap[ColorData["SolarColors"], VertexStyle -> Rescale@*BetweennessCentrality]

enter image description here

The measures you are asking for are available as the following builtin an IGraph/M functions:

  • clustering coefficient: LocalClusteringCoefficient, IGLocalClusteringCoefficient

  • number of triples: IGAdjacentTriangleCount

  • eccentricity: VertexEccentricity (for one vertex), EccentricityCentrality (inverse), IGEccentricity

  • vertex centrality: I don't know what you mean

  • closeness: ClosenessCentrality, IGCloseness, IGClosenessEstimate (for big graphs)

  • betweenness: BetweennessCentrality, IGBetweenness, IGBetweennessEstimate (for big graph)

Here's a nicer plot of all of them, also showing off IGWattsStrogatzGame, which supports some additional features not available with WattsStrogatzGraphDistribution.

g = IGWattsStrogatzGame[20, 0.2, GraphLayout -> "CircularEmbedding", VertexSize -> Large, GraphStyle -> "BasicBlack"]

Labeled[IGVertexMap[ColorData["Rainbow"], VertexStyle -> Rescale@*#, g], #] & /@
     {LocalClusteringCoefficient, IGAdjacentTriangleCount, IGEccentricity, ClosenessCentrality, BetweennessCentrality}

enter image description here

|improve this answer|||||

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.