ivan
ivan

Reputation: 6322

How to make this graph with graphviz?

I'd like to use graphviz to produce a graph like this one:

insertion-sort

How can I write such a graph?

So far, I've tried:

digraph
{
  rankdir = "LR";
  subgraph cluster0
  {
    rank = same { indices array }
    color = white;
    indices [ shape = record, color = white, label = "{ 1 | 2 | 3 | 4 | 5 | 6 }" ];
    array [ shape = record, label = "{ 1 | 2 | 4 | 5 | 6 | 3 }" ];
  }
  nodesep = .0;
}

producing:

enter image description here

Upvotes: 1

Views: 132

Answers (1)

match
match

Reputation: 11070

There is no builtin way to change the style for individual records, or to easily link edges to them. For this you need to switch to using HTML-like syntax:

digraph records {
  edge [color="gray" arrowhead="vee"]

  node1 [
    label = <<table border="0" cellspacing="0">
          <tr>
          <td>1</td>
          <td>2</td>
          <td>3</td>
          <td>4</td>
          <td>5</td>
          <td>6</td>
          <td>6</td>
          </tr>
          <tr>
            <td port="p1" width="20" border="1" bgcolor="white">1</td>
            <td port="p2" width="20" border="1" bgcolor="gray">2</td>
            <td port="p3" width="20" border="1" bgcolor="gray">4</td>
            <td port="p4" width="20" border="1" bgcolor="gray">5</td>
            <td port="p5" width="20" border="1" bgcolor="gray">6</td>
            <td port="p6" width="20" border="1" bgcolor="black"><font color="white">3</font></td>
          </tr>
          </table>>
  ]

  # :s = attach arrow at the south compass point
  node1:p3:s -> node1:p4:s
  node1:p4:s -> node1:p5:s
  node1:p5:s -> node1:p6:s
  node1:p6:s -> node1:p3:s [color="black"]

}

enter image description here

You'll need to play around a bit to get the edges the right length to not overlap etc.

Upvotes: 1

Related Questions