Graphviz - 对齐节点和集群

时间:2018-03-18 13:53:21

标签: graphviz dot

我正在尝试使用Graphviz绘制有限状态图,但我不能像我想要的那样得到它。这是我到目前为止所写的内容:

digraph G {
    rankdir = LR;

    subgraph cluster_op1 {

        subgraph cluster_0 {
            1 -> 2 [label="a"]  
        }

        subgraph cluster_1 {
            3 -> {4 6} [label="ε"]
            4 -> 5 [label="b"]
            5 -> 4 [label="ε"]
            5 -> 6 [label="ε"]
        }

        subgraph cluster_2 {
            7 -> {8 10} [label="ε"]
            8 -> 9 [label="c"]
            9 -> 8 [label="ε"]
            9 -> 10 [label="ε"]
        }   

        2 -> 3 [label="ε"]
        6 -> 7 [label="ε"]
    }

    subgraph cluster_op2 {

        subgraph cluster_3 {
            11 -> {12 14} [label="ε"]
            12 -> 13 [label="ab"]
            13 -> 12 [label="ε"]
            13 -> 14 [label="ε"]
        }   

        subgraph cluster_4 {
            15 -> 16 [label="c"]    
        }

        14->15 [label="ε"]
    }

    0 -> {1 11} [label="ε"]
    {10 16} -> 17 [label="ε"]
}

这是它的样子: enter image description here

我想要的是:

  1. cluster_op1中的所有节点都要水平对齐(cluster_op2相同)
  2. cluster_op2位于cluster_op1
  3. 之下的中心位置
  4. 从节点0cluster_op1cluster_op2的边缘长度相同(从这两个群集到节点17的边缘相同)

1 个答案:

答案 0 :(得分:2)

这不仅是一个真正的解决方案,而且它可以满足您的需求,至少对于此处提供的案例而言。

它使用三个元素:

以促进节点之间的直线,

权重以帮助群组被指向另外两个节点的一个节点混淆,所有节点都在同一个群组中;并使这些集群在它们之间排成一行,

invis 边缘将cluster_op2对齐cluster_op1 - 因此它不会居中,而是手动调整。

此外,我已将b -> a替换为适用的a -> b[ dir = back ];我认为这是一种很好的做法,可以避免难以发现的错误。

这是您编辑的代码:

digraph G {
    rankdir = LR;
    0, 1, 2, 15, 16, 17
    3, 4, 5, 6     [ group = 1 ];
    7, 8, 9, 10    [ group = 2 ];
    11, 12, 13, 14 [ group = 3 ];
    2 -> 11[ style = invis ];

    subgraph cluster_op1 {

        subgraph cluster_0 {
            1 -> 2 [ label="a" ]; 
        }

        subgraph cluster_1 {
            3 -> 4 [ label="ε", weight = 4 ];
            3 -> 6 [ label="ε" ];
            4 -> 5 [ label="b" ];
            4 -> 5 [ label="ε", dir = back];
            5 -> 6 [ label="ε", weight = 4 ];
        }

        subgraph cluster_2 {
            7 -> {8 10} [label="ε"]
            8 -> 9 [label="c"]
            8 -> 9 [ label="ε", dir = back ];
            9 -> 10 [label="ε"]
        }   

        2 -> 3 [ label="ε", weight = 10 ;]
        6 -> 7 [ label="ε" ];
    }

    subgraph cluster_op2 {

        subgraph cluster_3 {
            11 -> 12 [ label="ε", weight = 4 ];
            11 -> 14 [ label="ε" ];
            12 -> 13 [ label="ab" ];
            12 -> 13 [ label="ε", dir = back ];
            13 -> 14 [ label="ε", weight = 4 ];
        }   

        subgraph cluster_4 {
            15 -> 16 [label="c"]    
        }

        14->15 [ label="ε", weight = 10 ];
    }

    0 -> {1 11} [label="ε"]
    {10 16} -> 17 [label="ε"]
}

产生

enter image description here