首页 文章

Graphviz .dot节点排序

提问于
浏览
27

我正在构建一个epsilon NFA,以使用规范构造识别正则表达式 . 我正在使用子图来对正则表达式的各个部分进行分组 . 由于dot决定移动节点的顺序,*运算符给了我特别的麻烦 . 我已经尝试添加边缘权重以强制特定边缘变短以保持边缘的顺序,但这似乎不起作用 .

我想要做的是强制子图中的节点按特定顺序放置,以便输出图可以识别为特定类型的(众所周知的)构造 . 在下面的示例中,我希望边缘3,4,5和6按此顺序放置,但是点将它们按6,3,4,5的顺序放置 . 任何指针都会被欣赏 .

请注意,当前权重参数与完全没有权重参数没有任何区别 .

我有以下内容

digraph G {
    rankdir = LR;
    node [shape = none];
            0 [label = "start"];
    node [shape = circle];
            1 [label = "q1"];
            2 [label = "q2"];
            3 [label = "q3"];
            4 [label = "q4"];
            5 [label = "q5"];
    node [shape = doublecircle];
            6 [label = "q6"];
    subgraph re1 {
            rank = same;
            edge[label = "0"];
            1 -> 2;
    };
    subgraph re2 {
            rank = same;
            edge[label = "ε"];
                    3 -> 4 [weight = 10];
            edge[label = "1"];
                    4 -> 5 [weight = 10];
            edge[label = "ε"];
                    5 -> 6 [weight = 10];
                    5 -> 4 [weight = 1];
                    6 -> 3 [weight = 1];
    };
    edge[color=black];
            0 -> 1
    edge[label = "ε"];
            2 -> 3;
}

graphiz output

1 回答

  • 32

    这是我写这个图的方式:

    • 首先,对我来说这是一个从上到下而不是从左到右的图形,因此我删除了 rankdir=LR 并仅为节点0/1和节点2/3添加了 rank=same .

    • 我删除了所有的重量

    • 最重要的是,我将 constraint=false 添加到与图形方向相反的边缘 - 从节点4到节点5的边缘,以及从节点6到节点3的边缘 .

    这里的来源:

    digraph G {
        0 [label = "start", shape = none]; 
        node [shape = circle];
        1 [label = "q1"];
        2 [label = "q2"];
        3 [label = "q3"];
        4 [label = "q4"];
        5 [label = "q5"];
        6 [label = "q6", shape = doublecircle];
    
        {rank = same; 0 -> 1; }
        1 -> 2 [label = "0"];
        {rank = same; 2 -> 3 [label = "ε"]; }
        4 -> 5 [label = "1"];
        edge [label = "ε"];
        3 -> 4;
        5 -> 6;
        5 -> 4 [constraint = false];
        6 -> 3 [constraint = false];
    }
    

    这是结果:

    graphviz output

    现在,如果你想,你可以保留 rankdir=LR ,只需取你发布的标记,移除权重并将 constraint=false 添加到与我相同的边缘,它也可以 .

相关问题