3

I like the default display of Graph much better than that of GraphPlot, but I do not see how to turn off the use of multi-edges in a directed graph. (That is, if {a,b} and {b,a} are edges, I would like one line between them that has two arrowheads.) Is it possible?

Related: is it not odd that GraphPlot[Graph[...]] produces a different display than Graph[...]? If there is no way to turn off multi-edges in Graph, one might hope to simply GraphPlot[Graph[...],MultiedgeStyle->False] and get the same style except without multi-edges.

Alan
  • 13,686
  • 19
  • 38

2 Answers2

2

if {a,b} and {b,a} are edges, I would like one line between them that has two arrowheads

ClearAll[modiFy]
modiFy[as_ : {-.05, .05}] := Graph[DeleteDuplicates[EdgeList@#, Sort @ # == Sort @ #2 &], 
 EdgeStyle -> {e : DirectedEdge[a_, b_] /; a =!= b && MemberQ[EdgeList@#, Reverse @ e] :> 
   Arrowheads[as]}, VertexCoordinates -> GraphEmbedding[#], Options @ #]&

Examples:

g1 = Graph[{1 -> 2, 2 -> 1, 3 -> 1, 3 -> 2, 4 -> 1, 4 -> 2, 4 -> 4}]

enter image description here

modiFy[] @ g1

enter image description here

modiFy[{-.08, .05}] @ g1

enter image description here

g2 = Graph[{1 -> 2, 2 -> 1, 3 -> 1, 3 -> 2, 4 -> 1, 4 -> 2, 4 -> 4}, ImageSize -> 400,
  EdgeLabels -> Placed["Name", 1/3], ImagePadding -> 20,  VertexLabels -> "Name"]

enter image description here

modiFy[] @ g2

enter image description here

kglr
  • 394,356
  • 18
  • 477
  • 896
2

"MultiEdgeDistance"

In versions 13.+ (perhaps in earlier versions too) using the option

 GraphLayout -> {"MultiEdgeDistance" -> 0}

gives the desired result:

Graph[{1 -> 2, 2 -> 1, 3 -> 1, 3 -> 2, 4 -> 1, 4 -> 2, 4 -> 4}, 
 VertexLabels -> "Name", 
 GraphLayout -> {"MultiEdgeDistance" -> 0}]

enter image description here

"SelfLoopRadius"

There is also the suboption "SelfLoopRadius" to control rendering of self-loops:

Graph[{1 -> 2, 2 -> 1, 3 -> 1, 3 -> 2, 4 -> 1, 4 -> 2, 4 -> 4}, 
 VertexLabels -> "Name", 
 GraphLayout -> {"SelfLoopRadius" -> 0}]

enter image description here

Further examples:

Grid[#, Dividers -> All] & @  Table[
 Graph[{1 -> 2, 1 -> 2, 2 -> 1, 1 -> 3, 1 -> 4, 1 -> 5, 2 -> 3, 3 -> 3, 3 -> 3},
   VertexLabels -> "Name", 
   GraphLayout -> {"MultiEdgeDistance" -> d, "SelfLoopRadius" -> r}, 
   PlotLabel -> 
     Style[Column[{"MultiEdgeDistance" -> d, "SelfLoopRadius" -> r}], 14]], 
 {r, Subdivide[3]}, {d, Subdivide[3]}]

enter image description here

kglr
  • 394,356
  • 18
  • 477
  • 896