I have a weighted digraph and I want to add new vertices and edges. The problem is that the EdgeWeight property value gets reset to automatic when a new edge is added.

g = Graph[{ob1 \[DirectedEdge] ob2}];
PropertyValue[{g, ob1 \[DirectedEdge] ob2}, EdgeWeight] = 2;
PropertyValue[g, EdgeWeight]
g = VertexAdd[g, ob3];
PropertyValue[g, EdgeWeight]
g = EdgeAdd[g, ob1 \[DirectedEdge] ob3];
PropertyValue[g, EdgeWeight]



This behaviour has been noticed before (How to manipulate graphs without losing properties like EdgeWeight?). Is there a good reason for why mathematica does this?

I'm considering ways to work around this in my case, and I was wondering whether I am safe to assume that a new vertex/edge is always added to the end of the vertex/edge list, and if multiple vertices/edges are added at once using VertexAdd[g,{ob4,ob5}] that effectively the new list {ob4,ob5} is simply appended to the old vertex list? If this is the case then I can simply grab a copy of the EdgeWeight property value before I add new edges and then set the property to be {old weights} ~ Join ~ {new weights}.

  • $\begingroup$ This is going to be hard to test because Mathematica uses several different internal representations. Even if there seems to be no problem with one, something may break with another one. $\endgroup$ – Szabolcs Jul 22 '16 at 5:26
  • $\begingroup$ @Szabolcs, sorry I don't understand, could you perhaps expand on that comment for me? $\endgroup$ – Se314en Jul 22 '16 at 10:34
  • $\begingroup$ "I was wondering whether I am safe to assume that a new vertex/edge is always added to the end" <- I was commenting on this. I think (but I can never be sure) that this is not documented, thus in principle you can't rely on it. In practice however we can often just test this empirically. Try many cases and see if the assumption gets broken. I was pointing out that doing this with graphs is difficult because there are at least three different underlying graph representations that Mathematcica uses and they may behave differently in this regard. I noticed that some bugs only appear ... $\endgroup$ – Szabolcs Jul 22 '16 at 12:11
  • $\begingroup$ ... with some representations. You can check the representation using the undocumented function GraphComputation`GraphRepresentation. Often these bugs can be made to go away by simply rebuilding the very same graph with something like Graph[VertexList[g], EdgeList[g]]. Furthermore sometimes this makes bugs go away even when the internal representation doesn't change (according to the above function). That suggests that there's something more to internal representations that's perhaps not exposed. $\endgroup$ – Szabolcs Jul 22 '16 at 12:15
  • $\begingroup$ Anyway, this is the main point: If you try it out and you find that your assumption holds in many cases, you might still get nasty surprises: it might still break for some graphs. This happened to me. So try not to rely on this stuff and if you have to be very careful. $\endgroup$ – Szabolcs Jul 22 '16 at 12:16

Your Answer

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

Browse other questions tagged or ask your own question.