17
$\begingroup$

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?

$\endgroup$
  • 3
    $\begingroup$ 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. $\endgroup$ – Pillsy Nov 15 '15 at 20:32
  • $\begingroup$ @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! $\endgroup$ – MarcoB Nov 15 '15 at 21:35
  • $\begingroup$ Please do report this problem to Wolfram support. $\endgroup$ – Szabolcs Nov 16 '15 at 12:32
  • 1
    $\begingroup$ @Szabolcs I've reported it $\endgroup$ – Pillsy Nov 16 '15 at 12:56
8
$\begingroup$

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

g = Graph[VertexList@#, EdgeList@#] &@RandomGraph[{1, 0}];
g1 = VertexAdd[g, 2];
g2 = EdgeAdd[g1, UndirectedEdge[1, 2]]
$\endgroup$
  • 2
    $\begingroup$ What about the nuisance tag? :) $\endgroup$ – Kuba Nov 15 '15 at 19:33
  • $\begingroup$ @Kuba I'll propose it after the [DeeplyNestedDoLoops] one :) $\endgroup$ – Dr. belisarius Nov 15 '15 at 19:40
8
$\begingroup$

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]]

works but discards properties.

g = GraphComputation`ToGraphRepresentation[g, "Incidence"]

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

$\endgroup$
2
$\begingroup$

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];

Unprotect[EdgeAdd];
Clear[EdgeAdd];
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
    ]
]
Protect[EdgeAdd];

A couple examples:

EdgeAdd[ GraphComplement[CompleteGraph[5]], 1 <-> 2 ]
EdgeAdd[ RandomGraph[{1, 0}], 1 <-> 2 ]

enter image description here

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

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.