Function Repository Resource:

AnnotatedVertexSelect

Source Notebook

Select vertices based on annotations

Contributed by: Michael Sollami

ResourceFunction["AnnotatedVertexSelect"][g,crit]

picks out all vertices vi of graph g for which crit[vi] is True.

Details and Options

Any vertices that do not have the queried keys are dropped.
Internally, ResourceFunction["AnnotatedVertexSelect"] constructs a cached Dataset using AnnotationValue, which is recomputed only when the graph changes.
ResourceFunction["AnnotatedVertexSelect"] accepts functions crit of named variables based on the AnnotationKeys of the vertices. This includes the built-in symbolic ones (e.g. "VertexLabels", "VertexWeight", "VertexCoordinates", etc.) as well as custom strings (e.g. "CustomKey") that are defined.

Examples

Basic Examples (4) 

Find labeled vertices in a graph:

In[1]:=
ResourceFunction["AnnotatedVertexSelect"][
 Graph[{1, 2, 3}, {1 \[DirectedEdge] 2}, AnnotationRules -> {2 -> {"labeled" -> True}}], #labeled &]
Out[1]=

Select vertices based on annotations:

In[2]:=
g = Graph[{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}, {Null, {{1, 2}, {3, 4}, {3, 5}, {3, 6}, {2, 6}, {2, 7}, {2, 8}, {2, 9}, {2, 10}, {10, 11}, {4, 5}, {4, 12}, {5, 7}, {5, 12}, {
    7, 8}, {8, 13}, {9, 14}, {9, 13}, {12, 13}, {13, 15}}}, {VertexLabels -> {6 -> "Barbadori", 3 -> "Castellani", 11 -> "Pazzi", 5 -> "Strozzi", 8 -> "Tornabuoni", 7 -> "Ridolfi", 10 -> "Salviati", 14 -> "Ginori", 9 -> "Albizzi", 4 -> "Peruzzi", 12 -> "Bischeri", 13 -> "Guadagni", 2 -> "Medici", 15 -> "Lamberteschi", 1 -> "Acciaiuoli"}}];
In[3]:=
v = ResourceFunction["AnnotatedVertexSelect"][g, StringEndsQ[#VertexLabels, "zzi"] &]
Out[3]=
In[4]:=
HighlightGraph[g, v]
Out[4]=

Select vertices based on coordinates:

In[5]:=
g = CompleteGraph[4!, VertexSize -> Large];
v = ResourceFunction["AnnotatedVertexSelect"][g, And @@ Thread[#VertexCoordinates < 0] &];
HighlightGraph[g, v, ImageSize -> Medium]
Out[6]=

Use multiple string and symbolic arguments in selection criteria:

In[7]:=
g = GraphData["PappusGraph"];
Scan[(g = Annotate[{g, #}, "SomeProperty" -> RandomInteger[100]];
    g = Annotate[{g, #}, VertexSize -> RandomReal[{.1, .8}]];
    g = Annotate[{g, #}, VertexLabels -> RandomInteger[100]]) &, VertexList@g];
In[8]:=
v = ResourceFunction["AnnotatedVertexSelect"][
   g, (EvenQ@#"SomeProperty" && #VertexSize > .6 || PrimeQ@#VertexLabels) &];
In[9]:=
HighlightGraph[g, v, ImageSize -> Medium]
Out[9]=

Publisher

Michael Sollami

Version History

  • 1.0.0 – 19 June 2020

Related Resources

License Information