是否有自动删除点图中 "redundant" 条边的选项?

Is there an option to automatically remove "redundant" edges in a dot graph?

我为我的 Debian 项目创建了一个依赖点图(见图)。依赖关系包括冗余边。我想要一个没有那些冗余边的更简单的图形。我可以自己计算这些,但这并不容易,因为我在 CMakeLists.txt 和 .cmake 扩展名中生成了 .dot 文件。

所以我想知道 dot 或 Graphviz 中是否有一个选项可以删除不需要的边。因此,例如,顶级 snapwebsites 项目依赖于 cssppadvgetopt。由于 cspp 包已经依赖于 advgetopt,因此不需要 snapwebsitesadvgetopt 之间的边缘。

在有向图中,这意味着:

"snapwebsites" -> "advgetopt";     <-- "auto-remove" this one
"snapwebsites" -> "csspp";

"csspp" -> "advgetopt";

那么,有这样的选择吗?

据我所知,没有这样的选项 built-in(我可能是错的……)。

最简单的方法通常是首先只在 graphviz 脚本中包含所需的边。如果这不可能,您可以在将其输出输出到布局点之前使用 gvpr(graphviz 模式扫描和处理语言)处理您的图形。

这当然意味着您必须使用 gvpr 实现对不需要的边缘的检测和抑制,然后您可以在需要时重复使用该脚本。

根据@marapet 的回答,我创建了一个脚本,我想也许其他人会从副本中受益。它也在 Snap 中! C++ 作为 clean-dependencies.gvpr.

# Run with:
#
#    /usr/bin/gvpr -o clean-dependencies.dot -f clean-dependencies.gvpr dependencies.dot
#
# Clean up the dependencies.svg from double dependencies
# In other words if A depends on B and C, and B also depends on C, we
# can remove the link between A amd C, it's not necessary in our file.

BEG_G {
    edge_t direct_edges[int];
    node_t children[int];

    node_t n = fstnode($G);
    while(n != NULL) {

        // 1. extract the current node direct children
        //
        int direct_pos = 0;
        edge_t e = fstout_sg($G, n);
        while(e != NULL) {
            direct_edges[direct_pos] = e;
            children[direct_pos] = opp(e, n);
            direct_pos = direct_pos + 1;
            e = nxtout_sg($G, e);
        }

        // 2. find all of the grand children
        //    and see whether some are duplicates, if so delete the
        //    original (direct) edge
        //
        int child_pos = direct_pos;
        int c = 0;
        for(c = 0; c < child_pos; ++c) {
            e = fstout_sg($G, children[c]);
            while(e != NULL) {
                node_t o = opp(e, children[c]);
                int idx;
                for(idx = 0; idx < direct_pos; ++idx) {
                    if(children[idx] == o) {
                        if(direct_edges[idx] != NULL) {
                            delete($G, direct_edges[idx]);
                            direct_edges[idx] = NULL;
                        }
                        break;
                    }
                }
                e = nxtout_sg($G, e);
            }
        }

        n = nxtnode_sg($G, n);
    }
}

END_G {
    $O = $G;
}

有几件事我想提一下:gvpr 函数似乎不接受数组作为输入。此外,我首先尝试使用递归方法,并且局部变量被进一步调用破坏(即在递归调用的 return 上,变量的值是来自子调用的值......所以变量是 "local" 到一个函数,但仍然只有一个实例,没有堆栈!)

希望以后的版本能解决这些问题。

$ gvpr -V
gvpr version 2.38.0 (20140413.2041)

与在 CMake 中尝试做同样的事情相比,修复我的图表已经是一种更容易的方法。