- Is there a way to retrieve a list of labels, either vertex or edge, from a graph?
- Similarly, can a shape function access the label associated with the vertex or edge it is handed?
1 Answer
$\begingroup$
$\endgroup$
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]
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}