How can I change VertexWeights in a Graph?

I evaluate

gt = Graph[{"A" <-> "B", "B" <-> "C", "A" <-> "C", "C" <-> "D"}, VertexLabels -> "Name"]
gt = SetProperty[{gt, "B"}, VertexWeight -> 3];


I check that the property is set.

PropertyValue[{gt, "B"}, VertexWeight]

3


Next I evaluate

gt = SetProperty[{gt, "A"}, VertexWeight -> 4];


and check

PropertyValue[{gt, "A"}, VertexWeight]

4


Then I check to confirm the B vertex still has the weight I set

PropertyValue[{gt, "B"}, VertexWeight]

1 (* should be 3 *)


Any ideas what I'm doing wrong?

-
This clearly shouldn't happen. It is a bug that I can reproduce in version 9.0.1. –  Szabolcs Mar 12 '14 at 18:26

1 Answer

I can reproduce this in version 9.0.1 and I think this is a bug. As a workaround, you can set all the vertices' weight at once instead of one-by-one using a different syntax:

gt = SetProperty[gt, VertexWeight -> {"A" -> 4, "B" -> 3}];
VertexList[gt] -> PropertyValue[gt, VertexWeight] // Thread
(* {"A" -> 4, "B" -> 3, "C" -> 1, "D" -> 1} *)


Trying to set the VertexWeight on any single vertex afterwards will re-set the VertexWeight of all other values to 1 (which is incorrect behaviour).

In version 8.0.4 SetProperty doesn't work at all with VertexWeight: it either returns \$Failed or simply doesn't have an effect.

-