I need to replace the EdgeList of a graph with a new list of edges generated by other code.
I use an hour glass display for vertices:
vf[{xc_, yc_}, name_, {w_, h_}] :=
Block[{xmin = xc - w, xmax = xc + w, ymin = yc - h, ymax = yc + h},
Polygon[{{xmin, ymin}, {xmax, ymax}, {xmin, ymax}, {xmax, ymin}}]];
The graph is of the following form:
graph = Graph[List[1, 2, 3], List[DirectedEdge[1, 3], DirectedEdge[2, 3]],
List[Rule[ImageSize, List[240, 240]],
Rule[VertexCoordinates,
List[List[0.`, -1.`], List[1.`, -1.`], List[1.`, 0.`]]],
Rule[VertexLabels,
List[Rule[3,
Placed[List[Style[3, RGBColor[1, 0, 0]],
Style[3, RGBColor[0, 0, 1]]], List[Before, After]]],
Rule[1, Placed[
List[Style[1, RGBColor[1, 0, 0]], Style[2, RGBColor[0, 0, 1]]],
List[Before, After]]],
Rule[2, Placed[
List[Style[2, RGBColor[1, 0, 0]], Style[1, RGBColor[0, 0, 1]]],
List[Before, After]]]]], Rule[VertexShapeFunction, List[vf]],
Rule[VertexWeight, List[1, 2, 3]]]]
Its edges are: {1 -> 3, 2 -> 3}
which I want to replace with: {2 -> 3, 1 -> 2}
(this is only an example, the solution needs to work on any replacing set of edges)
Without affecting any other aspects of the graph. I want to preserve all aspects of the graph as given in its definition, bar the edges. These can be changed. I.e. I want to preserve colours, vertices, weights and labels as specified in the graph.
The following approach:
EdgeList[graph] = {2 -> 3, 1 -> 2}
Gives the error: Tag Edgelist in Edgelist[...] is Protected.
How do I change the EdgeList (without altering the other properties of a graph)?


Graph[VertexList[g], newEdges]doesn't work for you? – Szabolcs Sep 27 '22 at 20:39vfis missing. Please be specific about what you mean by "the end result is incorrect". You did not respond about what aspects of the original graph you want to preserve. – Szabolcs Sep 27 '22 at 21:28Graph[VertexList[graph], newEdges], Mathematica returns it unchanged, without any error messages? Do you mean that you do receive a result, but when you take that result and try to evaluate it a second time, it fails? If so, how does it fail, what errors are there? Can you show a complete minimal example that illustrates the problem? (1) This is what I did (in full detail) (2) This is what I expected (3) This is what I got instead. – Szabolcs Sep 27 '22 at 21:36Graph[VertexList[graph], newEdges, displayOpts](where you've defined displayOpts to be the whole sequence of options that you want). – lericr Sep 27 '22 at 21:39