【问题标题】:Is there an option to automatically remove "redundant" edges in a dot graph?是否可以选择自动删除点图中的“冗余”边缘?
【发布时间】:2019-11-18 18:34:29
【问题描述】:

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

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

在有向图中,这意味着:

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

"csspp" -> "advgetopt";

那么,有这样的选择吗?

【问题讨论】:

  • 投票,因为我没有看到评论为什么这个问题被否决了。至少我带着同样的问题来到这里。

标签: dependencies graphviz dot


【解决方案1】:

根据@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 函数似乎不接受数组作为输入。此外,我首先尝试使用递归方法,并且局部变量被进一步调用破坏(即在递归调用返回时,变量的值是来自子调用的值......所以变量是“本地”的函数,但仍然只有一个实例,没有堆栈!)

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

$ gvpr -V
gvpr version 2.38.0 (20140413.2041)

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

【讨论】:

    【解决方案2】:

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

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

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

    【讨论】:

    • 对了,你也可以使用其中一种编程语言的 Graphviz 库(例如 Python 的 pygraphviz)来处理和修改图形。
    • @Dany,这可能比 gvpr 更好。我遇到了各种问题(请参阅我的答案),这些问题在 pygraphviz 中可能不存在。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多