Gab*_*iel 8 algorithm merge directed-graph sortedlist poset
我有一些列表中包含可变数量的元素.每个列表都已排序,但排序算法未知.我想将列表合并到一个大列表中,该列表包含相同顺序的所有列表,没有重复.
示例输入:
预期结果:
通过匹配输入序列来获得预期结果,以便以正确的顺序获得包含每个输入序列的元素的合并结果,如下所示:
XS M L XL
S M XXL
XXS XS S L
-------------------
XXS XS S M L XL XXL
Run Code Online (Sandbox Code Playgroud)
如果存在具有不明确位置的元素,则该函数应通知.在这里,它将是XXL(它可以保持在M,L或XL之后)并且我需要在XL之后手动指定其位置(因为在这里我知道排序算法并且可以帮助).我想要定义每两个元素的对,每个元素按照原始列表的顺序排列.从这个可以建立完整的列表.
jcs*_*nyi 14
这可以使用拓扑排序算法来解决.
如果您将每个输入序列视为通过有向图的路径,拓扑排序将从左到右排序您的节点集,使每个有向边指向右侧.

拓扑排序的维基百科页面包括这个算法,首先由Arthur Kahn在1962年描述:
L ? Empty list that will contain the sorted elements
S ? Set of all nodes with no incoming edges
while S is non-empty do
remove a node n from S
insert n into L
for each node m with an edge e from n to m do
remove edge e from the graph
if m has no other incoming edges then
insert m into S
if graph has edges then
return error (graph has at least one cycle)
else
return L (a topologically sorted order)
Run Code Online (Sandbox Code Playgroud)
如果编写了这个算法,如果找到不明确的序列,它实际上并不会失败,但是通过在循环开头插入一个检查很容易添加,如下所示:
...
while S is non-empty do
if S contains more than 1 item
return error (inputs are ambiguous)
remove a node n from S
...
Run Code Online (Sandbox Code Playgroud)
我不知道你在使用什么语言,但我把这个PHP实现作为一个概念证明:
function mergeSequences($sequences, $detectAmbiguity = false) {
// build a list of nodes, with each node recording a list of all incoming edges
$nodes = array();
foreach ($sequences as $seq) {
foreach ($seq as $i => $item) {
if (!isset($nodes[$item])) $nodes[$item] = array();
if ($i !== 0) {
$nodes[$item][] = $seq[$i-1];
}
}
}
// build a list of all nodes with no incoming edges
$avail = array();
foreach ($nodes as $item => $edges) {
if (count($edges) == 0) {
$avail[] = $item;
unset($nodes[$item]);
}
}
$sorted = array();
$curr = '(start)';
while (count($avail) > 0) {
// optional: check for ambiguous sequence
if ($detectAmbiguity && count($avail) > 1) {
throw new Exception("Ambiguous sequence: {$curr} can be followed by " . join(' or ', $avail));
}
// get the next item and add it to the sorted list
$curr = array_pop($avail);
$sorted[] = $curr;
// remove all edges from the currently selected items to all others
foreach ($nodes as $item => $edges) {
$nodes[$item] = array_diff($edges, array($curr));
if (count($nodes[$item]) == 0) {
$avail[] = $item;
unset($nodes[$item]);
}
}
}
if (count($nodes) > 0) {
throw new Exception('Sequences contain conflicting information. Cannot continue after: ' . join(', ', $sorted));
}
return $sorted;
}
Run Code Online (Sandbox Code Playgroud)
您可以像这样调用函数:
$input = array(
array('XS', 'M', 'L', 'XL'),
array('S', 'M', 'XXL'),
array('XXS', 'XS', 'S', 'L'),
);
echo(join(', ', mergeSequences($input)));
echo(join(', ', mergeSequences($input, true)));
Run Code Online (Sandbox Code Playgroud)
要获得以下输出:
XXS, XS, S, M, XXL, L, XL
Uncaught exception 'Exception' with message 'Ambiguous sequence: M can be followed by L or XXL'
Run Code Online (Sandbox Code Playgroud)
您正在尝试合并部分有序的集合或posets.合并的模糊部分称为antichains.所以,你想要一个合并posets的算法,并告诉你什么是反链.
这是一篇描述合并posets和检测反链的算法的文章,以及第一作者主页的链接,以防你想联系他以查看是否有任何可用的源代码.
这就是我要做的: