I'm looking for an algorithm to perform some kind of extended array sorting when relationships between elements can contradict each other.
我正在寻找一种算法来执行某种扩展数组排序,当元素之间的关系可以相互矛盾时。
so we have a set I (items) consisting of n items i1...in
所以我们有一组I(项目)由n项i1 ... in组成
There is a set R (relationships) consisting of m relationships defined between items in I
存在一组R(关系),其由在I中的项之间定义的m个关系组成
the relationships can contradict each other so that, e.g., one relationship says that A>B
and the other that A<B
.
这些关系可以相互矛盾,例如,一个关系表示A> B,另一个关系表示A 。
e.g.
r1:i1<i35
r2:i100<i4
...
rm:i45>i3
generally, r and m (sizes of sets) can be any positive integers.
通常,r和m(集合的大小)可以是任何正整数。
the task is to sort I so the items go in such a way that preferably the lower ones (based on relationships) go before higher ones...
任务是对I进行排序,以便项目以这样的方式进行:优选地,较低的(基于关系)在较高的之前...
I'm looking for an algorithm which will sort the set so that it is as close to the "optimal" order as possible. I guess there must be a well known algorithm to solve problems like this.
我正在寻找一种算法,它会对集合进行排序,使其尽可能接近“最佳”顺序。我想必须有一个众所周知的算法来解决这样的问题。
Thanks!
1 个解决方案
#1
5
I think the most sensible way to measure the quality of a particular ordering is the number of given relationships that it violates. If you decide to use this measure, then the problem is equivalent to (Minimum) Feedback Arc Set. Unfortunately, this problem is NP-hard, so no efficient (polynomial-time) algorithm is likely to exist.
我认为衡量特定排序质量的最明智的方法是它违反的给定关系的数量。如果您决定使用此度量,则问题等同于(最小)反馈弧集。不幸的是,这个问题是NP难的,因此不可能存在有效的(多项式时间)算法。
In the Feedback Arc Set problem, you are given a directed graph, and asked to find a minimum-size set of edges which, if deleted, would destroy all cycles in the graph.
在反馈弧集问题中,您将获得一个有向图,并要求查找最小尺寸的边集,如果删除这些边将破坏图中的所有周期。
To see how this corresponds to your problem, observe that we can represent each item as a vertex in a graph, and each relationship as a directed edge between two vertices (pointing to, say, the smaller one). There is a conflict if and only if there is a cycle in this graph -- that is, if there is an ordered list of 2 or more distinct vertices v_1, v_2, ..., v_k such that v_i < v_(i+1) for all i < k, and also v_k < v_1. It is impossible to order these k vertices without violating at least one constraint. Conversely, if no cycle exists -- that is, if the graph is a directed acyclic graph -- then topological sort can quickly (in linear time) find a valid order that violates no constraints. Thus the size of a feedback arc set is the smallest number of edges that you would need to remove in order to obtain a graph that can be ordered without violating any constraint -- or equivalently, the smallest number of edges that must be violated in any ordering.
要了解这与您的问题如何对应,请注意我们可以将每个项目表示为图形中的顶点,并将每个关系表示为两个顶点之间的有向边缘(指向较小的顶点)。当且仅当此图中存在循环时才会发生冲突 - 即,如果存在2个或更多不同顶点v_1,v_2,...,v_k的有序列表,则v_i
#1
5
I think the most sensible way to measure the quality of a particular ordering is the number of given relationships that it violates. If you decide to use this measure, then the problem is equivalent to (Minimum) Feedback Arc Set. Unfortunately, this problem is NP-hard, so no efficient (polynomial-time) algorithm is likely to exist.
我认为衡量特定排序质量的最明智的方法是它违反的给定关系的数量。如果您决定使用此度量,则问题等同于(最小)反馈弧集。不幸的是,这个问题是NP难的,因此不可能存在有效的(多项式时间)算法。
In the Feedback Arc Set problem, you are given a directed graph, and asked to find a minimum-size set of edges which, if deleted, would destroy all cycles in the graph.
在反馈弧集问题中,您将获得一个有向图,并要求查找最小尺寸的边集,如果删除这些边将破坏图中的所有周期。
To see how this corresponds to your problem, observe that we can represent each item as a vertex in a graph, and each relationship as a directed edge between two vertices (pointing to, say, the smaller one). There is a conflict if and only if there is a cycle in this graph -- that is, if there is an ordered list of 2 or more distinct vertices v_1, v_2, ..., v_k such that v_i < v_(i+1) for all i < k, and also v_k < v_1. It is impossible to order these k vertices without violating at least one constraint. Conversely, if no cycle exists -- that is, if the graph is a directed acyclic graph -- then topological sort can quickly (in linear time) find a valid order that violates no constraints. Thus the size of a feedback arc set is the smallest number of edges that you would need to remove in order to obtain a graph that can be ordered without violating any constraint -- or equivalently, the smallest number of edges that must be violated in any ordering.
要了解这与您的问题如何对应,请注意我们可以将每个项目表示为图形中的顶点,并将每个关系表示为两个顶点之间的有向边缘(指向较小的顶点)。当且仅当此图中存在循环时才会发生冲突 - 即,如果存在2个或更多不同顶点v_1,v_2,...,v_k的有序列表,则v_i