I think you've stumbled upon a side-effect of Graph
objects being "stateful". They're legit objects, which we can get a glimpse of in their atomiticity:
g1 = Graph[{1}, {}];
g2 = Graph[{1, 1}, {}];
g3 = FirstCase[ToExpression@ToBoxes@g1, _Graph, None, \[Infinity]];
g4 = FirstCase[ToExpression@ToBoxes@g2, _Graph, None, \[Infinity]];
g1 === g2 === g3 === g4
True
Quiet[Check[g2[[1]], $Failed, Part::partd]]
$Failed
And of course there's the fact that the reported expression structure doesn't actually correspond to the internal structure.
Here's a cool rundown of all these Qs on these different Graph
objects:
gfuncs =
{GraphQ, PathGraphQ, TreeGraphQ, MixedGraphQ, PlanarGraphQ,
SimpleGraphQ, AcyclicGraphQ, CompleteGraphQ, DirectedGraphQ,
EulerianGraphQ, LoopFreeGraphQ, WeightedGraphQ, ConnectedGraphQ,
IsomorphicGraphQ, UndirectedGraphQ, HamiltonianGraphQ,
WeaklyConnectedGraphQ};
Prepend[Through[gfuncs[#]] & /@ {g1, g2, g3, g4}, gfuncs] //
Transpose // NewlineateInput
{
{GraphQ, True, True, True, True},
{PathGraphQ, True, True, True, True},
{TreeGraphQ, True, True, True, True},
{MixedGraphQ, False, False, False, False},
{PlanarGraphQ, True, True, True, True},
{SimpleGraphQ, True, True, True, True},
{AcyclicGraphQ, True, True, True, True},
{CompleteGraphQ, True, True, True, True},
{DirectedGraphQ, True, True, True, True},
{EulerianGraphQ, True, True, True, True},
{LoopFreeGraphQ, True, True, True, True},
{WeightedGraphQ, False, False, False, False},
{ConnectedGraphQ, True, False, True, False},
{IsomorphicGraphQ, True, True, True, True},
{UndirectedGraphQ, True, True, True, True},
{HamiltonianGraphQ, True, True, True, True},
{WeaklyConnectedGraphQ, True, True, True, True}
}
We can see that, for whatever reason, only ConnectedGraphQ
shows this behavior.
The fact that g4
has the same behavior as g1
is very odd. It might have to do with the memory sharing of that core Expression
object, but that's just errant speculation
Graph
objects. $\endgroup$