Graphviz - Notes2021-01-04

Graphviz - the “Graph Visualizaion Software” is a great tool to draw any kind of graph-based diagrams. I collect some notes here from my most used features, so that I don’t have to look them up again.

Draw Linked List Diagram

digraph G {
    # Title for actual graph:
    labelloc=t; # top
    labeljust=l; # left
    labelfontsize=20;
    labelfontname="Courier";
    label="Example: Linked List";
    
    rankdir=LR;
    
    n1 [
        shape=plaintext;
        label=<<TABLE BORDER="0">
       <TR><TD BORDER="1" PORT="one">var node1</TD></TR>
       <TR><TD BORDER="0" > </TD></TR>
       <TR><TD BORDER="1" PORT="two">var node2</TD></TR>
    </TABLE>>]

    subgraph g2{
        node [shape=record, width=1];

        e [label="{ <data> 12 | <ref>  }"]
        f [label="{ <data> 99 | <ref>  }"];
        g [label="{ <data> 37 | <ref>  }"];
        h [label="{ <data> 59 | <ref>  }"];
        ins [label="{ <data> 42 | <ref>  }"]
        null2 [color=white, labelloc=l, label="null", width=0.1];
        e:ref:c -> f:data [arrowhead=vee, arrowtail=dot, dir=both, tailclip=false;color=gray];
        e:ref:c -> ins:data [arrowhead=vee, arrowtail=dot, dir=both, tailclip=false];
        f:ref:c -> g:data [arrowhead=vee, arrowtail=dot, dir=both, tailclip=false];
        g:ref:c -> h:data [arrowhead=vee, arrowtail=dot, dir=both, tailclip=false];    
        h:ref:c -> null2 [arrowhead=vee, arrowtail=dot, dir=both, tailclip=false];    
        ins:ref:c -> f:data  [arrowhead=vee, arrowtail=dot, dir=both, tailclip=false];
    }
    
    subgraph g1{
        node [shape=record, width=1];

        a [label="{ <data> 12 | <ref>  }"]
        b [label="{ <data> 99 | <ref>  }"];
        c [label="{ <data> 37 | <ref>  }"];
        d [label="{ <data> 59 | <ref>  }"];
        null [color=white, labelloc=l, label="null", width=0.1];
        a:ref:c -> b:data [arrowhead=vee, arrowtail=dot, dir=both, tailclip=false];
        b:ref:c -> c:data [arrowhead=vee, arrowtail=dot, dir=both, tailclip=false];
        c:ref:c -> d:data [arrowhead=vee, arrowtail=dot, dir=both, tailclip=false];    
        d:ref:c -> null [arrowhead=vee, arrowtail=dot, dir=both, tailclip=false];    
    }
    
    n1:one -> a:data
    n1:two ->e:data

}

Linked List Example

Drawing simple arrays

digraph G {
    node [shape=plaintext, fontcolor=red, fontsize=18];
    "Indizes:" -> "Werte:" -> "Array mit Tabelle:" [color=white];

    node[shape=record fixedsize=true width=4 fontcolor=black];

    arr[label="1|5|8|3|2|10"]
    indices [label="0 | 1 | 2 | 3| 4 | 5", color=white];
    arr2[shape=plaintext,width=5,label=<
        <table border="0" fixedsize="true" cellspacing="0">
        <tr>
            <td border="1" width="30">1</td>
            <td border="1" width="30">2</td>
            <td border="1" width="30" bgcolor="gray">3</td>
            <td border="1" width="30" bgcolor="gray">4</td>
        </tr>
        </table>
    >]

    { ranksep=0; nodesep=0; rank=same; "Werte:"; arr }
    { ranksep=0; rank=same; "Indizes:"; indices }
    { ranksep=0; rank=same; "Array mit Tabelle:"; arr2 }
}

Array example