Skip to content

Graphviz

Basic Structure

// Filename: graph.dot
digraph G {
    // Nodes
    A [label="Node A"];
    B [label="Node B"];

    // Edges
    A -> B [label="Edge from A to B"];
}

Common Attributes

node [shape=shape_name, color=color, style=style, label=label];
edge [color=color, style=style, label=label];

Node Shapes

box, ellipse, circle, diamond, polygon, record, plaintext, ...

Edge Styles

solid, dashed, dotted, bold, invis, filled, bold, tapered, ...

Graph Types

digraph, graph, subgraph

Subgraphs

subgraph cluster_name {
    // Nodes and edges
}

Attributes and Styles

node_name [attribute=value, ...];
edge_name [attribute=value, ...];
graph_name [attribute=value, ...];

Labels

node_name [label="Node Label"];
edge_name [label="Edge Label"];

Rankdir (Layout Direction)

rankdir=TB // Top to bottom (default)
rankdir=LR // Left to right
rankdir=BT // Bottom to top
rankdir=RL // Right to left

Group and Rank

{rank=same; node1; node2; ...;} // Horizontal alignment
{rank=min; node1; node2; ...;} // Align nodes to the top
{rank=max; node1; node2; ...;} // Align nodes to the bottom

Clusters

subgraph cluster_name {
    label="Cluster Label";
    // Nodes and edges
}

Node and Edge Attributes

node_name [shape=shape_name, color=color, style=style, label=label];
edge_name [color=color, style=style, label=label];

Colors

color="color_name" // e.g., "red", "blue", "#FF9900"

Running Graphviz

dot -Tformat input.dot -o output.format

Example Output Formats

  • png
  • svg
  • pdf
  • ps
  • jpeg

Command Line Options

  • -G: Set default graph attributes
  • -N: Set default node attributes
  • -E: Set default edge attributes
  • -K: Specify layout engine (dot, neato, twopi, circo, fdp)
  • -o: Output file

Basic DOT Syntax:

// Create a directed graph
digraph G {
    // Nodes
    A [label="Node A"];
    B [shape=box, label="Node B"];

    // Edges
    A -> B [label="Edge from A to B"];
}

Node Attributes:

node_name [attribute=value, ...]
  • shape: Node shape (e.g., box, circle, ellipse, etc.)
  • color: Node color
  • style: Node style (e.g., filled, dashed, bold, etc.)
  • label: Node label
  • fontsize: Font size for the label
  • fontcolor: Font color for the label
  • tooltip: Tooltip text

Edge Attributes:

source -> target [attribute=value, ...]
  • label: Edge label
  • color: Edge color
  • style: Edge style (e.g., dashed, dotted, solid, etc.)
  • dir: Edge direction (e.g., forward, back, both, none)
  • weight: Edge weight for layout algorithms

Graph Attributes:

graph [attribute=value, ...]
  • rankdir: Layout direction (e.g., TB, LR)
  • bgcolor: Background color of the graph
  • label: Graph label
  • fontsize: Font size for the label
  • fontcolor: Font color for the label

Examples:

Directed Graph with Subgraphs:

digraph G {
    // Node and edge definitions

    subgraph cluster_0 {
        label="Subgraph 1";
        // Nodes and edges within subgraph
    }

    subgraph cluster_1 {
        label="Subgraph 2";
        // Nodes and edges within subgraph
    }

    A -> B;
    B -> C;

    { rank=same; A, B, C }
}

Undirected Graph with Attributes:

graph G {
    // Node attributes
    A [shape=box, color=blue];
    B [shape=circle, color=red];

    // Edge attributes
    A -- B [label="Edge between A and B", color=green];

    // Graph attributes
    rankdir=LR;
    label="My Undirected Graph";
}

Rendering DOT Files:

  • Save your DOT code in a .dot file.
  • Use the dot command to render the graph in various formats:
    dot -Tpng input.dot -o output.png
    

Layout Algorithms (used with -K flag):

  • dot: Hierarchical layout (default for directed graphs)
  • neato: Spring model layout (default for undirected graphs)
  • fdp: Force-directed placement
  • sfdp: Multiscale version of fdp
  • circo: Circular layout
  • twopi: Radial layout

Graphviz Resources:

Remember that this cheat sheet provides a basic overview of Graphviz and the DOT language. For more complex diagrams and advanced features, refer to the official documentation and examples provided by the Graphviz community.