2
$\begingroup$

I'm fond of the default vertex and edge layout for a directed graph:

edges = {1 -> 2, 0 -> 2, 0 -> 4, 0 -> 1, 0 -> 3, 2 -> 4, 1 -> 2, 
   2 -> 3, 1 -> 4, 3 -> 4, 1 -> 3, 0 -> 1, 1 -> 2, 1 -> 3, 1 -> 4, 
   2 -> 3, 2 -> 4, 3 -> 4, 0 -> 1, 0 -> 1, 0 -> 2, 1 -> 2};

Graph[edges]

enter image description here

I want to use this layout for an "equivalent" undirected graph, or at least one with no arrows drawn. The default layout however is different:

Graph[UndirectedEdge @@@ edges]

enter image description here

There is no GraphLayout option (in v11.2) that yields the same layout, possibly due to the undirected edges being weighted different than directed ones in the layout algorithms. Can I force Mathematica to use the directed graphs embedding? I only need the ultimate Graphics from Show[graph].

$\endgroup$
4
  • 2
    $\begingroup$ This has nothing to do with weights. The layout you want is computed based on edge directions. It is clear that all arrows are pointing from higher layers to lower ones. It is simply not possible for undirected graphs. You can assign directions to your edges and use the directed layout, then transfer the vertex coordinates to the undirected version again. But the result will depend on how you assigned those directions. $\endgroup$ – Szabolcs Dec 21 '20 at 20:41
  • 2
    $\begingroup$ Also, if I saw such a layout in a publication, I'd assume that the arrowheads went missing by accident and this is still a directed graph. Not a good idea to try to force such a layout for undirected graph, as it implies edge directions. You'll just confuse your readers. $\endgroup$ – Szabolcs Dec 21 '20 at 20:42
  • $\begingroup$ I suspected I could extract the positions and transfer them, but didn't know quite how to do it. And hey, confusing my readers is entirely up to me ;) $\endgroup$ – Anti Earth Dec 21 '20 at 21:00
  • 2
    $\begingroup$ If you have the undirected graph g already, you can do something like DirectedGraph[g, "Acyclic", GraphLayout -> "LayeredDigraphEmbedding", EdgeShapeFunction -> "Line"] $\endgroup$ – Szabolcs Dec 21 '20 at 21:45
2
$\begingroup$

"only need the ultimate Graphics from Show[graph]"

Show[Graph @ edges] /. Arrow[b_BezierCurve, ___] :> b

enter image description here

"or at least one with no arrows drawn"

Graph[edges, EdgeShapeFunction -> "Line"]

enter image description here

$\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.