How can I add and delete weighted edges in a weighted graph?

Using Graph in Mathematica I want to add a weighted edge to a weighted graph. The problem is EdgeAdd seems to always return an unweighted graph. For example:

g = Graph[{1 \[UndirectedEdge] 2, 2 \[UndirectedEdge] 3}, EdgeWeight -> {5, 11}]
g1 = EdgeDelete[g, 2 \[UndirectedEdge] 3]
g2 = EdgeAdd[g, 1 \[UndirectedEdge] 3]

Here, g1 has its edge weights messed up, so WeightedAdjacencyMatrix does not work. It seems the list of edge weights is too long since no element has been deleted. g2, on the other hand, is an unweighted graph. I even tried wrapping the added edge in

Property[1 \[UndirectedEdge] 3, EdgeWeight -> 13]

but that only got me an error when supplied to EdgeAdd.

I can bypass the EdgeDelete thing quite easily by afterwards readjusting the EdgeWeights list, but I haven't found a way to solve the EdgeAdd problem.


1 Answer 1


The problem here is that EdgeDelete[G, e] does not remove the appropriate weight $w_e$ from the properties of graph $G$ (I would say this is a bug, though it is the last one in a very long list for Graph...). First, set up explicit edge and weight lists, and a one-to-one mapping for conversions:

e = {1 \[UndirectedEdge] 2, 2 \[UndirectedEdge] 3};
w = {5, 11};
r = Thread[e -> w];

g = Graph[e, EdgeWeight -> w, VertexLabels -> "Name", ImagePadding -> 10];
{PropertyValue[g, EdgeWeight], MatrixForm@WeightedAdjacencyMatrix@g, g}

Mathematica graphics

g1 = EdgeDelete[g, 2 \[UndirectedEdge] 3];
{PropertyValue[g1, EdgeWeight], MatrixForm@WeightedAdjacencyMatrix@g1, g1}

Mathematica graphics

It is clearly reported by PropertyValue that g1 still keeps unnecessary weights.

Now one can rely on SetProperty, though it requires possibly tedious tracking and re-setting of weights:

g2 = SetProperty[EdgeDelete[g, 2 \[UndirectedEdge] 3], 
              EdgeWeight -> (DeleteCases[e, 2 \[UndirectedEdge] 3] /. r)];
{PropertyValue[g2, EdgeWeight], MatrixForm@WeightedAdjacencyMatrix@g2, g2}

Mathematica graphics

The same method works for EdgeAdd:

g3 = SetProperty[EdgeAdd[g2, 1 \[UndirectedEdge] 3], 
              EdgeWeight -> Append[PropertyValue[g2, EdgeWeight], 4]];
{PropertyValue[g3, EdgeWeight], MatrixForm@WeightedAdjacencyMatrix@g3, g3}

Mathematica graphics

  • $\begingroup$ The weighted edge that I add seems to be placed in the wrong place. E.g. I've a weighted random graph g, EdgeList[g]={1 <-> 3, 1 <-> 6, 2 <-> 5, 2 <-> 6, 3 <-> 4, 4 <-> 5, 5 <-> 6} and random weights w={0.38629, 0.967065, 0.281823, 0.689528, 0.0222693, 0.604908, 0.875763}. Then I add an edge: g3 = SetProperty[EdgeAdd[g, {6 <-> 3}], EdgeWeight -> Append[PropertyValue[g, EdgeWeight], 4.]]. The result is the edge with weight 4 is added between vertices 2 and 3 (instead of verteces 3 and 6). $\endgroup$
    – sam84
    Sep 15, 2015 at 9:21

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.