5
$\begingroup$

Is there any way of scaling the sizes of the vertices of a network proportional to the vertex-degrees of the vertices in Mathematica? Thanks.

$\endgroup$
1

2 Answers 2

8
$\begingroup$
SeedRandom[5]
g = RandomGraph[{6, 10}]

Mathematica graphics

vd = Thread[VertexList@g -> Normalize[VertexDegree@g, Total]];
g2 = SetProperty[g, VertexSize -> vd]

Mathematica graphics

$\endgroup$
6
  • $\begingroup$ Great! Why are you dividing by 20 here? $\endgroup$
    – dbm
    Commented May 6, 2016 at 18:21
  • $\begingroup$ @dbm, dividing by 20 was to scale the vertex sizes. It is removed in the updated post. $\endgroup$
    – kglr
    Commented May 6, 2016 at 18:26
  • $\begingroup$ Got it. However, this code doesn't work for say g = RandomGraph[{90, 120}] $\endgroup$
    – dbm
    Commented May 6, 2016 at 18:29
  • $\begingroup$ @dbm, maybe you can use Normalize[VertexDegree@g, Max] for large graphs. $\endgroup$
    – kglr
    Commented May 6, 2016 at 18:33
  • 1
    $\begingroup$ @dbm, it is because when we normalize by Total each vertex degree divided by Total[VertexDegree@g] (240) gives a very small vertex size. If we normalize by Max each vertex degree is divided by 7 (maximum of the vertex degrees). Alternatively, instead of Normalize[...], you can use Rescale[VertexDegree@g, Through[{Min, Max}[VertexDegree@g]], {a, b}] with your choice of a and b. $\endgroup$
    – kglr
    Commented May 6, 2016 at 18:44
2
$\begingroup$

This code scales the vertex size and attaches a heatmap color to the vertices. The result is as follows:

ClearAll["Global`*"];
(* Define some utility functions *)
addVertices[G_, numVertices_] := (
  GN = G;
  vc = VertexCount[GN];
  (* Preferential Attachment Part A *)
  For[i = 1, i <= numVertices, i++,
   (* Preferential Attachment Part A Barabasi / Albert Method *)
   (* The rich get richer... *)
   GN = EdgeAdd[
     GN, (vc + i) \[UndirectedEdge] RandomChoice[GraphCenter[GN]]];
   (* Preferential Attachment Experiment 1 *)
   GN = EdgeAdd[
     GN, (vc + i) \[UndirectedEdge] RandomChoice[GraphPriphery[GN]]];
   ];
  Return[Graph[GN, VertexLabels -> Automatic, 
    PlotLabel -> "New G with additional vertices"]]
  )
If[$KernelCount < 1, LaunchKernels[]];
Framed[Style[
  StringJoin["Running [", ToString[$KernelCount], "] kernels"]], 
 Background -> LightBlue]
numNodes = 900;
m = 3; (* Parameter of attachment *)
G = Graph[RandomGraph[BarabasiAlbertGraphDistribution[numNodes, m]], 
  VertexStyle -> White];
vcount = VertexCount[G];
heatIndex = Table[VertexDegree[G, i], {i, 1, vcount}] ;
Length[Range[Max[heatIndex]]];
tempRange = Table[ColorData["TemperatureMap"][r], {r, 0., 1., 1/%}];
tempDegree = Table[tempRange[[heatIndex[[i]]]], {i, 1, vcount}];
vs = Table[i -> tempDegree[[i]], {i, 1, vcount}];
vh = Table[
   i -> (Log[numNodes]*m*heatIndex[[i]])/(Max[heatIndex]), {i, 1, 
    vcount}];
pl = Style[
   StringJoin[
    "Heat/Size Map of Random Barabasi/Albert Graph\nO(V) = ", 
    ToString[numNodes], ", Diameter G = ", ToString[GraphDiameter[G]],
     ", m = ", ToString[m]], Bold, FontFamily -> "Comic Sans MS"];
G = Graph[G, VertexStyle -> vs, VertexSize -> vh, PlotLabel -> pl]

enter image description here

$\endgroup$
2
  • $\begingroup$ There are still some scaling issues in the above graph. We are trying to scale based on two different variables, O(V) and the parameter m, i.e. the parameter of attachment. I notice in the first answer a normalization was used but I used a Log of the number of nodes. There is something strange, though with Mathematica, in that if you change O(V) to 1000 the vertices all resort to the same size??? $\endgroup$ Commented Jun 22, 2022 at 11:23
  • $\begingroup$ Neither of the above proposed solutions scale properly when the number of nodes goes over 1000. Its as if something changes with Wolfram's scaling. $\endgroup$ Commented Jun 22, 2022 at 14:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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