简体   繁体   English

通过成对关系检查队列一致性的高效算法

[英]Efficient algorithm to check queue consistency by pairwise relationship

We have n items entering and leaving a queue, they could enter and leave at any time.我们有 n 个项目进入和离开队列,它们可以随时进入和离开。 The information we have is that for each pair (a,b), we know either 1) a leaves the queue before b enters or vice versa;我们得到的信息是,对于每一对 (a,b),我们知道 1) a 在 b 进入之前离开队列,反之亦然; 2) at some point, a and b are in the queue at the same time. 2) 在某个时刻,a 和 b 同时在队列中。 or 3) no information at all.或 3) 根本没有任何信息。 Suppose we are given a list of pairwise information for all (n choose 2) pairs, find a O(n^2) algorithm to determine if there is any inconsistency in the information.假设我们得到了所有(n 选择 2)对的成对信息列表,找到一个 O(n^2) 算法来确定信息中是否存在任何不一致。

For example, a leaves before b enters(for simplicity, write it as a>b), b>c, and c=a is inconsistent, since there is no possible timeline of a,b,c to enter and leave the queue that makes all three statements true.例如,a在b进入之前离开(为简单起见,写为a>b),b>c,c=a是不一致的,因为没有a,b,c的时间线可能进入和离开的队列使所有三个陈述都成立。 I thought about making this a graph transversal problem, each item as a vertex, pairwise relation as an edge, if a>b or b<a then it's a directed edge, if a=b then it's an undirected edge, then this problem may be reduced to some sort of cycle detection in a mixed graph (with directed or undirected edges), where algorithms such as DFS or BFS can be applied.我想过把它变成一个图横向问题,每个项目作为一个顶点,成对关系作为一条边,如果 a>b 或 b<a 则它是有向边,如果 a=b 则它是无向边,那么这个问题可能简化为混合图中(具有有向或无向边)中的某种循环检测,其中可以应用 DFS 或 BFS 等算法。 However, it's not just any kind of cycle leads to inconsistency, obviously if all edges in the cycle are directed, then it's inconsistent for sure, as > or < is transitive.然而,不仅仅是任何一种循环都会导致不一致,显然如果循环中的所有边都是有向的,那么它肯定是不一致的,因为 > 或 < 是可传递的。 However, if all edges in a cycle are undirected, there is no inconsistency.但是,如果循环中的所有边都是无向的,则不存在不一致。 Or if some are directed or some are undirected, could be either inconsistent or consistent.或者,如果有些是有向的或有些是无向的,则可能不一致或一致。 For example, a>b, b>c, and c=a is inconsistent, while a=b,b=c and c>a is consistent.例如a>b、b>c、c=a不一致,而a=b、b=c、c>a一致。

Any input and idea will be greatly appreciated!任何输入和想法将不胜感激!

Think of this as a digraph, where there's an arc from a to b if a leaves before b enters, and vice versa.将其视为一个有向图,如果ab进入之前离开,则从ab有一条弧线,反之亦然。

For nodes (x,y) that are in the queue at the same time, note that this means that for every vertex w, a consistent graph doesn't have x < w < y, and also doesn't have x > w > y.对于同时在队列中的节点 (x,y),注意这意味着对于每个顶点 w,一致图没有 x < w < y,也没有 x > w >是的。 Reflect this by merging such nodes into a single node, preserving all arcs.通过将这些节点合并为一个节点来反映这一点,保留所有弧。

Then, run a topological sort to find a topological ordering of the nodes.然后,运行拓扑排序以找到节点的拓扑排序。

There's an inconsistency if and only if there's no topological ordering.当且仅当没有拓扑排序时才存在不一致。

Running time: topo sort is O(nodes + edges) which is O(n^2).运行时间:拓扑排序为 O(nodes + edges),即 O(n^2)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM