2
$\begingroup$

Consider the following graph:

Clear[a, b, c]
graph = Graph[{b -> a, a -> c}, 
  VertexLabels -> {b -> 2, a -> 1, c -> 3}]

However

PropertyValue[graph, VertexLabels]

produces

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

Which is not consistent with the order of

VertexList[graph]

which produces

{b, a, c}

I use the following to extract the vertex labels from a graph:

vertexLabels[g_] := #[[2]] & /@ PropertyValue[g, VertexLabels]

This does not produce the label list in an order that is consistent with VertexList.

The result is:

{1, 3, 2}

Rather than

{2, 1, 3} 

Which would follow the order of vertices in VertexList[g]

Is there a way to address this?

And is there a reason why PropertyValue does not follow the VertexList order consistently?

I tried

Clear[a, b, c]
graph = Graph[{b -> a, a -> c}, 
  VertexLabels -> {b -> 2, a -> 1, c -> 3}]

I assumed that by adjusting the VertexLabels, so their assignment follows the vertex order of VertexList (first b, then a, then c), the result of PropertyValue would be match the order of VertexList.

This is not the case.

The code produces:

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

as before.

I could adapt the code to reorganise the output to match the order of VertexList.

Is there another way to ensure this?

$\endgroup$
4
  • 2
    $\begingroup$ PropertyValue[{graph, #}, VertexLabels] & /@ VertexList[graph1]? $\endgroup$ – kglr Mar 20 at 16:56
  • $\begingroup$ Great. Odd that PropertyValue picks a different order. $\endgroup$ – Mike Mar 20 at 17:17
  • 2
    $\begingroup$ Weird indeed. You get {a -> 1, c -> 3, b -> 2} regardless of the order you specify the weights PropertyValue[Graph[{b -> a, a -> c}, VertexLabels -> #], VertexLabels] & /@ Permutations[{a -> 1, b -> 2, c -> 3}]. This is so even if you specify the vertex list in the first argument of Graph: PropertyValue[Graph[{a, b, c}, {b -> a, a -> c}, VertexLabels -> #], VertexLabels] & /@ Permutations[{a -> 1, b -> 2, c -> 3}] $\endgroup$ – kglr Mar 20 at 17:39
  • 2
    $\begingroup$ ... also regardless of the order the edges are given: PropertyValue[Graph[Reverse@{b -> a, a -> c}, VertexLabels -> #], VertexLabels] & /@ Permutations[{a -> 1, b -> 2, c -> 3}] $\endgroup$ – kglr Mar 20 at 17:41
2
$\begingroup$

A work-around:

First a simple fix to get what you need:

graph = Graph[{b -> a, a -> c}, VertexLabels -> {b -> 2, a -> 1, c -> 3}];

PropertyValue[{graph, #}, VertexLabels] & /@ VertexList[graph]
{2, 1, 3}
VertexList[graph]
  {b, a, c}

If you need list of rules:

Thread[VertexList[graph] -> (PropertyValue[{graph, #}, VertexLabels] & /@ 
    VertexList[graph])]
 {b -> 2, a -> 1, c -> 3}

We get the same result (1) regardless of the order vertex labels are given, (2) regardless of the order edges are input, (3) regardless of whether a vertex list is specified in the first argument of Graph and (4) regardless of whether we use PropertyValue or AnnotationValue:

PropertyValue[Graph[{b -> a, a -> c}, VertexLabels -> #], 
   VertexLabels] & /@ Permutations[{a -> 1, b -> 2, c -> 3}]

PropertyValue[Graph[Reverse@{b -> a, a -> c}, VertexLabels -> #], 
   VertexLabels] & /@ Permutations[{a -> 1, b -> 2, c -> 3}]

PropertyValue[Graph[{a, b, c}, {b -> a, a -> c}, VertexLabels -> #], 
   VertexLabels] & /@ Permutations[{a -> 1, b -> 2, c -> 3}]

(and same lines with PropertyValue replaced with AnnotationValue) all give

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

Note: This also happens with VertexShapeFunction:

PropertyValue[Graph[{b -> a, a -> c}, 
  VertexShapeFunction -> {b -> "Square", a -> "Star", c -> "Triangle"}], 
 VertexShapeFunction]
{b -> "Square", c -> "Triangle", a -> "Star"}

We get the same result for all the variations above.

However, it does not happen with other properties like VertexShape or VertexStyle.

What is going on?

The source of the mysterious ordering {a, c, b} seems to be the ordering of vertices in sorted edge list of the input graph:

VertexList[Sort[EdgeList[Graph[{b -> a, a -> c}, 
  VertexLabels -> {a -> 1, b -> 2, c -> 3}]] ]]
 {a, c, b}   

We get {a, c, b} for all the combinations explored below:

VertexList[Sort[EdgeList[Graph[{b -> a, a -> c}, 
    VertexLabels -> #]] ]] & /@ Permutations[{a -> 1, b -> 2, c -> 3}]

VertexList[Sort[EdgeList[Graph[Reverse@{b -> a, a -> c}, 
    VertexLabels -> #]] ]] & /@ Permutations[{a -> 1, b -> 2, c -> 3}]

VertexList[Sort[EdgeList[Graph[{a, b, c}, {b -> a, a -> c}, 
    VertexLabels -> #]] ]] & /@ Permutations[{a -> 1, b -> 2, c -> 3}]

VertexList[Sort[EdgeList[Graph[#, {b -> a, a -> c}, 
    VertexLabels -> {a -> 1, b -> 2, c -> 3}]] ]] & /@ Permutations[{a, b, c}] 
$\endgroup$
1
  • 1
    $\begingroup$ + thanks for the list of rules comment. Would love to know what goes on internally. It is as if PropertyValue/AnnotationValue performs a sort internally, fixing matters in a specific order. None that looks familiar though. $\endgroup$ – Mike Mar 20 at 18:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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