10
$\begingroup$

I suspect this is just a bug in Mathematica. Here is the same graph embedded with different options for GraphLayout:

edges =
{
UndirectedEdge[1, 2],
UndirectedEdge[2, 3],
UndirectedEdge[3, 5],
UndirectedEdge[4, 7],
UndirectedEdge[4, 8],
UndirectedEdge[5, 6],
UndirectedEdge[5, 7]
};
{Graph[edges, GraphLayout -> "GridEmbedding", ImageSize -> Medium], 
 Graph[edges, GraphLayout -> "LayeredEmbedding", ImageSize -> Medium]}

The output is shown below.

Output

The two graphs appear to have different connectivity. What is up with this?

$\endgroup$
  • $\begingroup$ add the options EdgeShapeFunction -> "CurvedArc" and VertexLabels -> "Name" to both Graphs to avoid overlapping edges. $\endgroup$ – kglr Jan 17 at 1:03
  • 1
    $\begingroup$ Ahh, the EdgeShapeFunction is what I need I suppose. In my actual case rather than MWE, I did have labelled vertices, but it still didn't occur to me that Mathematica was drawing the edges to exactly overlap. Thank you! $\endgroup$ – Andrew Patrick Turner Jan 17 at 1:10
  • 3
    $\begingroup$ Don't use GridEmbedding for anything else but a GridGraph. It's a dumb method that puts vertices on a grid in the order they appear in the vertex list. It will almost always result in overlapping edges unless your graph has a very specific structure and vertex ordering (i.e. it's what GridGraph generates, possibly with some missing edges) $\endgroup$ – Szabolcs Jan 17 at 8:05
  • $\begingroup$ The "CurvedArc" option is pretty neat; however, where is it documented? I went through the v12 documentation and it doesn't appear at all as near as I can tell. $\endgroup$ – Mark Kotanchek Jan 30 at 17:38
11
$\begingroup$

Edges 4 <-> 7 and 4 <-> 8 overlap in the first graph. You can make the edges curved using the option EdgeShapeFunction -> "CurvedArc" to avoid overlapping edges:

Row[{Graph[edges, 
    GraphLayout -> "GridEmbedding", ImageSize -> Medium, VertexLabels -> "Name"], 
  Graph[edges, 
    GraphLayout -> "GridEmbedding", ImageSize -> Medium, VertexLabels -> "Name", 
    EdgeShapeFunction -> "CurvedArc"], 
  Graph[edges, 
    GraphLayout -> "LayeredEmbedding", ImageSize -> Medium, VertexLabels -> "Name"], 
  Graph[edges, 
    GraphLayout -> "LayeredEmbedding", ImageSize -> Medium, VertexLabels -> "Name", 
    EdgeShapeFunction -> "CurvedArc"]}, Spacer[10]]

enter image description here

Alternatively, used "CurvedArc" only for the edges 4 <-> 7 and 4 <-> 8:

Graph[edges,
 GraphLayout -> "GridEmbedding", ImageSize -> Medium, VertexLabels -> "Name", 
 EdgeShapeFunction -> {UndirectedEdge[4, _] -> "CurvedArc"}]

enter image description here

|improve this answer|||||
$\endgroup$

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.