0
$\begingroup$
  1. Is there a way to retrieve a list of labels, either vertex or edge, from a graph?
  2. Similarly, can a shape function access the label associated with the vertex or edge it is handed?
$\endgroup$

1 Answer 1

2
$\begingroup$

You can access labels of a graph using "AbsoluteOptions". Here is an example:

We define same arbitrary graph:

vl = {1 -> a, 2 -> b, 3 -> c}; el = {1 \[UndirectedEdge] 2 -> e1, 
  2 \[UndirectedEdge] 3 -> e2, 3 \[UndirectedEdge] 1 -> e3};
gr = Graph[{1 \[UndirectedEdge] 2, 2 \[UndirectedEdge] 3, 
   3 \[UndirectedEdge] 1}, EdgeLabels -> el, VertexLabels -> vl]

enter image description here

To get the vertex labels:

VertexLabels /.  AbsoluteOptions[gr, VertexLabels]

{3 -> c, 2 -> b, 1 -> a}

Or if you only want the values.

{1, 2, 3} /. (VertexLabels /.  AbsoluteOptions[gr, VertexLabels])

{a, b, c}

and to get the edge labels:

EdgeLabels /.  AbsoluteOptions[gr, EdgeLabels]

{3 \[UndirectedEdge] 1 -> e3, 1 \[UndirectedEdge] 2 -> e1, 
 2 \[UndirectedEdge] 3 -> e2}

And the values:

{3 \[UndirectedEdge] 1, 1 \[UndirectedEdge] 2, 
  2 \[UndirectedEdge] 3} /.  (EdgeLabels /.  
   AbsoluteOptions[gr, EdgeLabels])

{e3, e1, e2}
$\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.