2
$\begingroup$

In the last example under the Applications section of the GraphAssortativity documentation, Mathematica uses a network in which nodes are students from a high school. The analysis is made for degree and race, but I do not know how to get the race of each node, How can I get them?

$\endgroup$
1
  • $\begingroup$ AnnotationRules /. AbsoluteOptions[g, AnnotationRules] $\endgroup$
    – Bob Hanlon
    Dec 7, 2022 at 1:12

2 Answers 2

2
$\begingroup$

You will find the documentation for graph "annotations" (called properties in older versions) here: http://reference.wolfram.com/language/guide/GraphAnnotations.html Bob shows you how to use the functions, so I won't discuss it here.

The IGraph/M package offers a graph property management system that is more restrictive than Mathematica's built-in, but also simpler to use, more consistent, and often more convenient for practical applications. IGraph/M generally uses lists to represent properties where the $k$th element of a list is the property value associated with the $k$th vertex or edge. Unlike built-in functions, IGraph/M maintains a clear separation between vertex and edge properties.

We can retrieve the list of vertex and edge properties like so:

In[10]:= IGEdgePropertyList[g]
Out[10]= {EdgeShapeFunction, EdgeStyle}

In[11]:= IGVertexPropertyList[g]
Out[11]= {"Race", VertexCoordinates, VertexShape, VertexShapeFunction, VertexSize, VertexStyle}

You can see that there is a vertex property called "Race". There are no other custom properties. All the others are build-in ones.

To retrieve the values for the "Race" property, you can use

IGVertexProp["Race"][g]

enter image description here

If we wanted to get an association between vertex names and property values, we could use:

IGVertexAssociate[IGVertexProp["Race"]][g]

enter image description here

Why does IGVertexProp not return an association by default? The reason is that there is also an IGEdgeProp function. Since multigraphs may contain more than one edge between the same two vertices, represented using the same expression in Mathematica, edges cannot in general be used as association keys. Association keys must be unique. Thus constructing the association is left to the user.

There are also functions to compute one vertex property based on another one. Notice that the values of the "Race" property are colours. If vertices weren't already coloured based on this, we could colour them simply using

IGVertexMap[# &, VertexStyle -> IGEdgeProp["Race"], g]

But since they are, let's do something else. Let us transform the "Race" property from colours to strings.

g2 = IGVertexMap[
   Which[
     First[#] < 1/3, "Black",
     First[#] < 2/3, "Mixed",
     True, "White"] &,
   "Race",
   g
];
In[32]:= IGVertexProp["Race"][g2]

Out[32]= {"White", "White", "White", "White", "White", "White", \
"White", "White", "White", "White", "White", "White", "White", \
"White", "White", "White", "White", "White", "White", "White", \
"White", "White", "White", "White", "White", "White", "White", \
"Black", "Black", "Mixed", "White", "White", "Black", "Black", \
"Black", "Black", "Black", "Black", "Black", "Black", "Black", \
"Black", "Black", "Black", "Black", "Black", "Black", "Black", \
"Black", "Black", "Black", "Black", "Black", "Black", "Black", \
"Black", "Black", "Black", "Black", "Mixed", "White", "White", \
"White", "White", "White", "White", "White", "White", "White", \
"White", "White", "White", "White", "White", "White", "White", \
"White", "White", "White", "White", "White", "White", "White", \
"Black", "Black", "Black", "Black", "Black", "Black", "Mixed", \
"White", "White", "Black", "Black", "Black", "Black", "Black", \
"Black", "Black", "Black", "Black", "Black", "Black", "Black", \
"Black", "Mixed", "Mixed", "Mixed", "Mixed", "Mixed", "Mixed", \
"Mixed", "Mixed", "Mixed", "Mixed", "Mixed", "Mixed", "Mixed", \
"Mixed", "Mixed"}

If you work with networks in Mathematica, I highly recommend IGraph/M, not just because it's my package, but because I find it literally indispensable in my day-to-day use.

$\endgroup$
2
$\begingroup$
$Version

(* "13.1.0 for Mac OS X x86 (64-bit) (June 16, 2022)" *)

Clear["Global`*"]

Copy and pasting a graph from the Applications section of the documentation for GraphAssortativity enter image description here

Then executing

annotations = AnnotationRules /. AbsoluteOptions[g, AnnotationRules]

enter image description here

This can be manipulated as desired, e.g.,

blackVertices = Select[annotations, ! FreeQ[#, GrayLevel[0]] &][[All, 1]]

(* {47, 104, 59, 89, 42, 40, 38, 46, 36, 57, 29, 84, 95, 105, 41, 48, \
55, 35, 94, 45, 39, 99, 85, 97, 86, 87, 93, 37, 34, 100, 33, 44, 103, \
102, 28, 53, 101, 56, 52, 50, 96, 58, 43, 49, 88, 51, 98, 54} *)
$\endgroup$

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.