如何检查依赖图中的附加冲突信息?

发布于 2024-12-08 12:06:50 字数 579 浏览 1 评论 0原文

当您有一组项目的依赖图时,您可以执行标准主题排序来检查该图是否包含循环。如果存在循环,那么就存在一种依赖关系,如果不违反另一个依赖关系,就无法满足该依赖关系。

但是冲突信息呢?我的意思是您拥有的结构:

V - a set of items
E - a set of dependency edges: E\subset V\times V
C - a set of conflict edges: C\subset V\times V

检查依赖图是否包含无法满足的冲突信息的标准算法是什么?

例如:

V = { a, b, c }
E = { ( a -> b), (b->c) }
C = { (a -> c) }

此示例显示了不健全的依赖关系图,因为 c 依赖于 a 并且同时存在 c 是没有意义的> 给定的a 被指定为冲突。

这种模型的一个现实示例是包管理器,其中包描述可能包括依赖和冲突规范。另一个示例是基于依赖关系的运行服务,其中只有在没有正在运行的冲突作业时才能启动作业。

When you have a dependency graph of a set of items you can do a standard topical sort to check if the graph contains cycles. If there is a cycle then there is a dependency that can not be satisfied without violating another.

But what about conflict informations? I mean a structure where you have:

V - a set of items
E - a set of dependency edges: E\subset V\times V
C - a set of conflict edges: C\subset V\times V

What is the standard algorithm to check if the dependency graph contains conflict information that can not be satisfied?

For example:

V = { a, b, c }
E = { ( a -> b), (b->c) }
C = { (a -> c) }

This example shows an unsound dependency graph because it does not make sense that c depends on a and at the same time the presence of c given a is specified as a conflict.

One real world example of such a model is package managers, where package descriptions may include depend and conflict specifications. Another example is a dependency based run-service, where a job can only be started if no conflicting job is already running.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

拧巴小姐 2024-12-15 12:06:50

我不知道标准算法,但这有效:

  1. 像往常一样找到(V,E)的拓扑排序(如果返回不稳定
    发现循环依赖)。
  2. 以DFS/BFS的方式,对每个
    唯一的依赖路径/组件(解释如下)。
  3. 遍历 C
    检查是否不存在满足 (u,v) 的对 label(u) =
    标签(v)
    。如果有这样一对==>返回不稳定,否则==>
    回归稳定。

运行时间O(|E|+|V|+|C|)
由于拓扑排序和DFS在(V,E)中是线性的,而第三部分在C中是线性的。

第 2 阶段的说明:

我们从依赖图的顶部开始(或者如果您愿意,可以从拓扑排序的开始)开始,选择第一个节点和第一个标签,例如 1。我们以DFS(或BFS)的方式遍历图中的每条边;只要我们仍然保持连接,我们就继续用相同的标签标记节点。一旦连接“耗尽”,我们就会增加标签并继续 DFS/BFS。

即从第一个节点可到达的所有内容都被标记为1。一旦我们耗尽了该节点(DFS 或 BFS 算法中的外循环)的可达性,我们就会增加标签并选择下一个未搜索的节点。

正确性证明:

我们做了一个关键的观察——如果C中存在一对(u,v),则该图是不稳定的标签(u)=标签(v)

首先,我并不是按照指示引用 C 中的元素,因为存在对称性。如果 C 中的 (u -> v) 这意味着,用你的话来说,给定 u, v不可能存在。因此它们不能同时存在,这意味着 u 都不能依赖于 v (因为要使 u 存在,v code> 必须存在,这是不可能的),反之亦然。

有了这样的理解,我们就可以证明上述观察结果:
我们注意到,如果 u 依赖于 v,则 label(u) = label(v) ,反之亦然。这是构造的一个简单结果,其中可达性(以及依赖性)定义了标签。因此,如果我们假设有这样的一对,则该图是不稳定的(如上段所述)。

观察的另一个方向(不稳定==>对)也很容易看到。如果我们假设图不稳定,那么有一些 u 不可能存在。这个u要么依赖于某些冲突的东西,要么某个其他节点依赖于它并且该对是冲突的。不管怎样,我们在 C 中找到了一对具有相同标签的 (u,v)

I don't know about a standard algorithm, but this works:

  1. Find the topological sort of (V,E) as usual (returning unstable if
    a cyclic dependency is found).
  2. In a DFS/BFS manner, label each
    dependency trail/component uniquely (explanation below).
  3. Traverse C and
    check that there exists no pair (u,v) such that label(u) =
    label(v)
    . If there is such a pair ==> return unstable, otherwise ==>
    return stable.

Running time O(|E|+|V|+|C|):
Since topological sort and DFS are linear in (V,E), and the third part is linear in C.

Explanation of stage 2:

We begin at the top of the dependency graph (or if you like, the beginning of the topological sort), pick the first node, and the first label, say 1. We traverse each edge in the graph in a DFS (or BFS) manner; so long as we are still connected we continue labelling the nodes with the same label. As soon as the connectivity "runs out", we increment the label and continue in the DFS/BFS.

I.e. everything reachable from the first node is labelled 1. Once we've exhausted reachability of that node (the outer loop in the DFS or BFS algorithm), we increment the label and pick the next unsearched node.

Proof of correctness:

We make one key observation - that the graph is unstable iff there exists some pair (u,v) in C such that label(u) = label(v).

Firstly, I'm not referring to elements in C as directed, because there exists a symmetry. If (u -> v) in C that means that, in your words, given u, v cannot exist. So they cannot both exist together, meaning neither u can be dependant on v (because then for u to exist, v must exist, which is impossible), nor the opposite.

With that understanding we can prove the above observation:
We note that label(u) = label(v) if either u is dependant on v, or the other way around. That is a simple result of the construction, where reachability (and thus dependency) defined the labels. So if we have assume we such a pair, the graph is unstable (as explained the the above paragraph).

The other direction of the observation (unstable ==> pair) is easy to see as well. If we assume the graph is unstable then there is some u that cannot exist. This u is either dependant on something conflicting, or some other node is dependent on it and that pair is conflicting. Either way, we found a pair (u,v) in C that have the same label.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文