Graphical artifact Graphviz -- a good helper for learning data structure


   Official website Download and install the version of the corresponding system, and enter it in terminal dot -version can check whether the installation is successful. If the corresponding version information does not appear, you can consider configuring environment variables or reinstalling.

1. Basic grammar

   like C language, Graphviz also has some keywords that cannot be used as user-defined variables. The following are some basic keywords:

Graph: defines an undirected graph, which is described as a--b

digraph: defines a directed graph, which is described as a - > B

subgraph: define subgraphs, which can be framed separately

Strict: it prohibits the creation of multiple lines. In a directed graph, only one line can point from one node to another; In an undirected graph, only one line can be connected between two nodes. For example, the difference between a directed graph with and without strict:

[strict]digraph { 
  a -> b
  a -> b
  b -> a [color=red]
  b -> a [color=red]
}

   an overall code structure. Only some common attributes are listed below. You can view them if you need to use more attributes Official website related documents:

[strict](graph | digraph)[ID]
{
	/* Declare the Attributes of some graphs. The end of each statement can be used; If separated, it can also be omitted*/
	layout=neato # Define the mapping engine and change the combination form of node s. Options include dot, neato, fdp and patchwork   
	rankdir= "RL" # Define the direction of the arrow. Options include "LR", "BT" (bottom - > top) and "TB"
    labeljust=l   #Adjust the label position, left (l) (c) right (r)
    graph [class="ClassA"] # Define classification
    bgcolor=lightgray  # Figure background color
    color="yellow" # Box color    
    
    node [shape=record,color = red,fillcolor=yellow, style="rounded,filled"] #Define the global properties of node
    # Shape defines the shape of node; style defines the attributes of node: filled and rounded
    
    edge [style = dotted,weight=8,color=red] # Define global properties of edge
    # style defines the type of line. Options include: dotted, bold, and invisible
    # weight defines the line width; Color defines the color of the line
    
    subgraph cluster  # Subgraph definition
    {
        /*[Attributes], For the attributes of subgraphs, refer to the attributes of graphs above*/
        
        # After the global attributes defined by node and edge are listed, you can also add [] after the statement to specialize a node or edge
        NodeA [label="{ a | b | c }"] # Multiple label s can be separated by |
        NodeB [label="<f0> one|<f1> two"] # f0 f2 f3... Represents different positions in a node.
        
        # A node can also point to multiple nodes:
        A -- {B;C} # Equivalent to a -- B, a -- C
        # Undirected graph
        # NodeA -- NodeB [style = dotted,label="It is an edge"] 
        
        #Directed graph
        # NodeA -> NodeB [dir=forward] # Optional back, both and none define the direction of the arrow.
        # NodeA -> NodeB [arrowhead = diamond] # Defines the style of the arrow.
    }
}

The following attributes can be applied to graph, subgraph, edge, node:

label="It is a graph" # Custom label
fontname="Comic Sans MS" # Set the font. The default is "times Roman"
fontsize="40" # Set font size
fontcolor = red # Set font color

supplement

2. Compile and run

An online view of the results website

  • (not recommended) you can directly enter the code in the command line, and the software will generate pictures in the current folder. The basic idea is to use pipeline compilation and redirect or output the results. The disadvantage is that if you make a complex diagram, the command to be input is very long and it is not easy to debug.
echo 'graph { a -- b }' | dot -Tpng -o out.png
  • You can create a new folder under the current folder dot ending text file test dot, and then edit it in the file
graph { a -- b }

Then enter on the command line:

dot -Tpng test.dot -o test.png #Lowercase o
dot -Tpng test.dot > test.png  #Redirection form
dot -Tpng -O test.dot #If the generated file is not specified, capital O can be used, and the corresponding suffix file will be automatically generated according to - T

# other:
dot -? #View additional options

Graphviz can generate a variety of file formats, which can be viewed for details file.

  • If you find it too cumbersome to enter a command on the command line, you can write a makefile. For example:
test.png:test.dot
	dot -Tpng -O $^
	
.PHONY:clean
clean:
	rm -f *.png

  of course, you can use some ides, vscode or Sublime ; Take underline as an example. Simply configure it and press Ctrl + B to facilitate output.

3. Some examples

3.1 a simple binary tree

digraph BinaryTree  {
    a -> b
    a -> c
    b -> d
    d [label="null"];
    node1[shape=point, style=invis]
    b -> node1[weight=10, style=invis]
    b -> e
    c -> f
    node2[shape=point, style=invis]
    c -> node2[weight=10, style=invis]
    c -> g
    g [label="null"];
    e -> h
    h [label="null"];
    node3[shape=point, style=invis]
    e -> node3[weight=10, style=invis]

    e -> i
    i [label="null"];
    f -> k
    k [label="null"];
    node4[shape=point, style=invis]
    f -> node4[weight=10, style=invis]
    f -> j
    j [label="null"];
}

Posted by krish_s on Thu, 26 May 2022 01:13:13 +0300