简体   繁体   English

给定一个说明其已知顺序的元组列表,如何对 N 个元素进行排序?

[英]How to sort N elements given a list of tuples stating their known order?

sorry for the complicated / confusing title.对不起,复杂/混乱的标题。

Basically I'm trying to implement a system that helps with the sorting of documents with no known date of writing.基本上,我正在尝试实现一个系统,该系统有助于对编写日期未知的文档进行分类。 It consists of two inputs:它由两个输入组成:

Input 1: A tuple, where the first element is the number of documents, N .输入 1:一个元组,其中第一个元素是文档数N The second element is the number of pairs of documents with a known writing order, L .第二个元素是具有已知书写顺序L的文档对的数量。 (First document was written before the second document). (第一份文件写在第二份文件之前)。

Input 2 : A list of L tuples.输入 2L元组的列表。 Each tuple contains two elements (documents).每个元组包含两个元素(文档)。 The first document was written before the second document.第一份文件写在第二份文件之前。 Eg: (1 2), (3 4) means that document1 was written before document2 and document3 was written before document4 .例如: (1 2), (3 4) 表示document1写在document2之前, document3写在document4之前。

Next, the software must determine if there is a way of sorting all documents chronologically, there can be three outputs:接下来,软件必须确定是否有一种方法可以按时间顺序对所有文档进行排序,可以有三种输出:

Inconclusive - Means that the temporal organization of the documents is inconsistent and there is no way of sorting them.不确定- 意味着文档的时间组织不一致,无法对它们进行排序。

Incomplete - Means that information is lacking and the system can't find a way of sorting.不完整- 表示信息缺失,系统无法找到排序方式。

In case the information is enough, the system should output the order in which the documents have been written.如果信息足够,系统应该 output 文件被写入的顺序。

So far, I have managed to take both inputs, but I do not know where to start in terms of sorting the documents.到目前为止,我已经设法接受了这两个输入,但我不知道从哪里开始对文档进行排序。 Any suggestions?有什么建议么?

Here's my code so far (Python3):到目前为止,这是我的代码(Python3):

LN = tuple(int(x.strip()) for x in input("Number of docs. / Number of known pairs").split(' '))
print(LN)
if (LN[1]) > (LN[0]**2):
    print("Invalid number of pairs")
else:
    A = ['none'] * LN[0]
    for i in range(LN[0]):
        t = tuple(int(x.strip()) for x in input("Pair:").split(' '))
        A[i] = t
    print(A)

I appreciate all suggestions:)我感谢所有建议:)

Build a directed graph.构建有向图。 The inputs are the edges.输入是边缘。 Check for cycles which would indicate inconsistent input.检查指示不一致输入的循环。 Find the "leftmost" node, that is the node that doesn't have any edge to it, meaning nothing to its left.找到“最左边”的节点,即没有任何边缘的节点,意味着它的左边没有任何内容。 Multiple that are leftmost?最左边的多个? Incomplete.不完整。 Then, for each node in the graph, assign the index that equals the length of the longest path from the leftmost node.然后,为图中的每个节点分配索引,该索引等于从最左边节点开始的最长路径的长度。 As there are no (directed) cycles, you could probably just do BFS starting at the leftmost node and at each step assign to the node the maximum of its current value and its value given from its parent.由于没有(定向)循环,您可能只从最左边的节点开始进行 BFS,并在每一步将其当前值的最大值及其父节点给出的值分配给该节点。 Then iterate through all nodes, and put the numbers in their corresponding indices.然后遍历所有节点,并将数字放入相应的索引中。 Two nodes have the same index assigned?两个节点分配了相同的索引? Incomplete.不完整。

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

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