Search code examples
graphviz

graphviz, straighten out skiplist?


I have a digraph made up to draw a skiplist, which it does fairly well, but the alignment needs to be improved.

digraph {
  rankdir=LR
  node [shape=record,weight=4]
  edge [weight=10000]

  X [label="<f0>•|<f1>•|<f2>•|<f3>•|<f4>Head"]
  A [label="<f3>•|<f4>4"]
  B [label="<f1>•|<f2>•|<f3>•|<f4>8"]
  C [label="<f3>•|<f4>15"]
  D [label="<f0>•|<f1>•|<f2>•|<f3>•|<f4>16"]
  E [label="<f2>•|<f3>•|<f4>23"]
  F [label="<f2>•|<f3>•|<f4>42"]
  Y [label="<f0>•|<f1>•|<f2>•|<f3>•|<f4>Tail"]

  X:f0 -> D:f0
  X:f1 -> B:f1
  X:f2 -> B:f2
  X:f3 -> A:f3
  X:f4 -> A:f4

  A:f3 -> B:f3
  A:f4 -> B:f4

  B:f1 -> D:f1
  B:f2 -> D:f2
  B:f3 -> C:f3
  B:f4 -> C:f4

  C:f3 -> D:f3
  C:f4 -> D:f4

  D:f0 -> Y:f0
  D:f1 -> Y:f1
  D:f2 -> E:f2
  D:f3 -> E:f3
  D:f4 -> E:f4

  E:f3 -> F:f3
  E:f4 -> F:f4

  F:f2 -> Y:f2
  F:f3 -> Y:f3
  F:f4 -> Y:f4
}

This produces: enter image description here

I would like to produce something more like this: enter image description here

How do I make the edges be straight? And how do align the nodes to be all on the same baseline?


Solution

  • For this graph, all that needs to be done is to add this to the graph:

    nodesep=0
    

    (You then may also remove the weight attributes)

    nodesep: specifies the minimum space between two adjacent nodes in the same rank

    The way I imagine it works, is that nodesep adds some padding to the sides of the nodes. If there is a nearby edge, the node will be moved to respect the padding.