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.