# EdgeAdd function not adding edges to a RandomGraph[ ] with no edges

Bug introduced in 9.0 or earlier, fixed in 11.3

I have a graph:

g = RandomGraph[{1, 0}]


So basically, it is just one vertex. I want to add some vertices and edges in the following steps so i tried this first to add a new vertex:

g = VertexAdd[g, VertexCount[g] + 1];


Since the vertex names are just numbers, I added the next unused integer. I want my g graph to be updated (have a new vertex added to it) to the new state so I used the = operator.

Then I tried to add an edge between those by using the EdgeAdd function:

g = EdgeAdd[g, {1\[UndirectedEdge]2}]


And the output is just:

EdgeAdd[, 2<->3]


Why is it not working? Is it some kind of bug or am i using something wrong here?

• One thing that's interesting is that RandomGraph[{1,0}] =!= Graph[{1}, {}] despite the fact that they have identical FullForms, and Hash to the same value. – Pillsy Nov 15 '15 at 20:32
• @Pillsy, michelson: I noticed the same thing about those FullForms. That seems to indicate that hidden information is stored in those objects that is not accessible to the user. Michelson, I would suggest that you report this to Wolfram Support as a potential bug! – MarcoB Nov 15 '15 at 21:35
• Please do report this problem to Wolfram support. – Szabolcs Nov 16 '15 at 12:32
• @Szabolcs I've reported it – Pillsy Nov 16 '15 at 12:56

Perhaps it's a nuisance (bug?) of RandomGraph[ ]. Here is a way to force it:

g = Graph[VertexList@#, EdgeList@#] &@RandomGraph[{1, 0}];

• What about the nuisance tag? :) – Kuba Nov 15 '15 at 19:33
• @Kuba I'll propose it after the [DeeplyNestedDoLoops] one :) – Dr. belisarius Nov 15 '15 at 19:40

There are several Graph-related bugs that show up only for certain graphs, most likely due to how these graphs are stored internally. There are several possible internal graph representations used by Mathematica.

When you find that something like this happens, try recreating the graph in one of several possible ways:

g = Uncompress@Compress[g]


will preserve all properties.

g = Graph[VertexList[g], EdgeList[g]]


g = GraphComputationToGraphRepresentation[g, "Incidence"]


may work too if the graph is not empty (i.e. has more than zero vertices).

If you don't mind messing with System symbols, you can unprotect EdgeAdd, and incorporate @Szabolcs's workaround. Here is an attempt to do so:

$BugFix=True; Protect[$BugFix];

EdgeAdd[g_, e___] /; $BugFix := Block[{$BugFix = False},
Module[{res},
res = Quiet @ Check[EdgeAdd[g,e], $Failed]; ( Replace[res, _EdgeAdd :> EdgeAdd[Uncompress@Compress@g, e]] ) /; res=!=$Failed
]
]

EdgeAdd[ GraphComplement[CompleteGraph[5]], 1 <-> 2 ]

• We could even have a pattern like g_ /; GraphComputationGraphRepresentation[g] === "Simple" and then GraphComputationToGraphRepresentation[g, "Incidence"]. That also avoids trying to convert the null graph (which has a "NullGraph" representation). – Szabolcs Oct 11 '17 at 17:47
• @Szabolcs Another idea is to modify the Graph construction functions to make sure the result is able to be used in EdgeAdd and friends. – Carl Woll Oct 11 '17 at 17:50