Algorithm 如何对不同的相关单元进行排序?

Algorithm 如何对不同的相关单元进行排序?,algorithm,math,Algorithm,Math,我已经给出了一个任务,在这个任务中,用户输入一些单元关系,我们必须从高到低对它们进行排序。 最好的算法是什么 我放置了一些输入/输出对以澄清问题: 输入: km = 1000 m m = 100 cm cm = 10 mm km = 100000 cm km = 1000000 mm m = 1000 mm B = 8 b MiB = 1024 KiB KiB = 1024 B Mib = 1048576 b Mib = 1024 Kib B = 8 b MiB = 1048576 B M

我已经给出了一个任务,在这个任务中,用户输入一些单元关系,我们必须从高到低对它们进行排序。 最好的算法是什么

我放置了一些输入/输出对以澄清问题:

输入:

km = 1000 m
m = 100 cm
cm = 10 mm
km = 100000 cm
km = 1000000 mm
m = 1000 mm
B = 8 b
MiB = 1024 KiB
KiB = 1024 B
Mib = 1048576 b
Mib = 1024 Kib
B = 8 b
MiB = 1048576 B
MiB = 1024 KiB
MiB = 8192 Kib
MiB = 8 Mib
输出:

1km = 1000m = 100000cm = 1000000mm
1km = 1000m = 100000cm = 1000000mm
1MiB = 8Mib = 1024KiB = 8192Kib = 1048576B = 8388608b
1MiB = 8Mib = 1024KiB = 8192Kib = 1048576B = 8388608b
输入:

km = 1000 m
m = 100 cm
cm = 10 mm
km = 100000 cm
km = 1000000 mm
m = 1000 mm
B = 8 b
MiB = 1024 KiB
KiB = 1024 B
Mib = 1048576 b
Mib = 1024 Kib
B = 8 b
MiB = 1048576 B
MiB = 1024 KiB
MiB = 8192 Kib
MiB = 8 Mib
输出:

1km = 1000m = 100000cm = 1000000mm
1km = 1000m = 100000cm = 1000000mm
1MiB = 8Mib = 1024KiB = 8192Kib = 1048576B = 8388608b
1MiB = 8Mib = 1024KiB = 8192Kib = 1048576B = 8388608b
输入:

km = 1000 m
m = 100 cm
cm = 10 mm
km = 100000 cm
km = 1000000 mm
m = 1000 mm
B = 8 b
MiB = 1024 KiB
KiB = 1024 B
Mib = 1048576 b
Mib = 1024 Kib
B = 8 b
MiB = 1048576 B
MiB = 1024 KiB
MiB = 8192 Kib
MiB = 8 Mib
输出:

1km = 1000m = 100000cm = 1000000mm
1km = 1000m = 100000cm = 1000000mm
1MiB = 8Mib = 1024KiB = 8192Kib = 1048576B = 8388608b
1MiB = 8Mib = 1024KiB = 8192Kib = 1048576B = 8388608b
输入:

km = 1000 m
m = 100 cm
cm = 10 mm
km = 100000 cm
km = 1000000 mm
m = 1000 mm
B = 8 b
MiB = 1024 KiB
KiB = 1024 B
Mib = 1048576 b
Mib = 1024 Kib
B = 8 b
MiB = 1048576 B
MiB = 1024 KiB
MiB = 8192 Kib
MiB = 8 Mib
输出:

1km = 1000m = 100000cm = 1000000mm
1km = 1000m = 100000cm = 1000000mm
1MiB = 8Mib = 1024KiB = 8192Kib = 1048576B = 8388608b
1MiB = 8Mib = 1024KiB = 8192Kib = 1048576B = 8388608b

如何根据给定的输出生成输出?

您可以找到以下算法:

 1. detect all existing units: `n` units
 2. create a `n x n` matrix `M` such that the same rows and columns show 
    the corresponding unit. put all elements of the main diagonal of the 
    matrix to `1`.
 3. put the specified value in the input into the corresponding row and column. 
 4. put zero for the transpose of the row and the column in step 3.
 5. put `-1` for all other elements

Now, based on `M` you can easily find the biggest unit:

 5.1 candidate_maxs <-- Find columns with only one non-zero positive element 

 not_max <-- []
  
 6. while len(candidate_max)> 1:

    a. take a pair <i, l> and find a column h such that both (i, h) 
       and (l, h) are known, i.e., they are positive. 
       If M[i, h] > M[l, h]:
            remove_item <-- l
        Else:
            remove_item <-- i
        candidate_max.remove(remove_item)
        not_max.append(remove_item)
     b. if cannot find such a pair, find a pair <i, l>: i from 
        candidate_max and h from not_max with the same property.
        If M[i, h] < M[l, h]:
            candidate_max.remove(i)
            not_max.append(i)
 biggest_unit <-- The only element of candidate_max
但是,算法的时间复杂度为θ^2,即如果您明智地在步骤6上实现循环,则n是单位数

实例 输入1

km = 1000 m
m = 100 cm
cm = 10 mm
解决方案:

      km   m    cm   mm
    km 1  1000  -1   -1
     m 0    1   100  -1
    cm -1   0    1   10
    mm -1  -1    0    1

M = [1  1000  -1  -1
     0    1   100 -1
    -1    0    1  10
    -1   -1    0   1]

===> 6. `biggest_unit` <--- km (column 1)

7.1 Find first `-1` in the first row and column 3: (1,3)
    Find strictly positive value in row 2 such that (1,2) is strictly 
    positive and non-identity. So, the missing value of `(1,3)` must be 
    `1000 * 100 = 100000`. 

7.2 Find the second `-1` in the first row and column 4: (1,4)
    Find strictly positive value in row 3 such that (1,3) is strictly 
 
    positive and non-identity. So, the missing value of `(1,4)` must be 
    `100000 * 10 = 1000000`.

The loop is finished here and we have:

M = [1  1000  100000  1000000
     0    1     100      -1
    -1    0      1       10
    -1   -1      0        1]

Now you can sort the elements of the first row in ascending order.
 
       km    m    cm     mm
    km  1   -1  100000 1000000
     m -1    1   -1     1000
    cm  0   -1    1      -1
    mm  0    0   -1       1

M = [1   -1  100000 1000000
    -1    1   -1     1000
     0   -1    1      -1
     0    0   -1       1]

===> 

6.1 candidate_max = [1, 2]

6.2 Compare them on column 4 and remove 2

biggest_unit <-- column 1

And by going forward on step 7, 
Find first `-1` in the first row and column 2: (1,2)
Find a strictly positive and non-identity value in row 2:(1,4)
So, the missing value of `(1,2)` must be `1000000 / 1000 = 1000`.
In sum, we have: 

M = [1  1000  100000 1000000
    -1    1   -1     1000
     0   -1    1      -1
     0    0   -1       1]

Now you can sort the elements of the first row in ascending order (step 8).
 
解决方案:

      km   m    cm   mm
    km 1  1000  -1   -1
     m 0    1   100  -1
    cm -1   0    1   10
    mm -1  -1    0    1

M = [1  1000  -1  -1
     0    1   100 -1
    -1    0    1  10
    -1   -1    0   1]

===> 6. `biggest_unit` <--- km (column 1)

7.1 Find first `-1` in the first row and column 3: (1,3)
    Find strictly positive value in row 2 such that (1,2) is strictly 
    positive and non-identity. So, the missing value of `(1,3)` must be 
    `1000 * 100 = 100000`. 

7.2 Find the second `-1` in the first row and column 4: (1,4)
    Find strictly positive value in row 3 such that (1,3) is strictly 
 
    positive and non-identity. So, the missing value of `(1,4)` must be 
    `100000 * 10 = 1000000`.

The loop is finished here and we have:

M = [1  1000  100000  1000000
     0    1     100      -1
    -1    0      1       10
    -1   -1      0        1]

Now you can sort the elements of the first row in ascending order.
 
       km    m    cm     mm
    km  1   -1  100000 1000000
     m -1    1   -1     1000
    cm  0   -1    1      -1
    mm  0    0   -1       1

M = [1   -1  100000 1000000
    -1    1   -1     1000
     0   -1    1      -1
     0    0   -1       1]

===> 

6.1 candidate_max = [1, 2]

6.2 Compare them on column 4 and remove 2

biggest_unit <-- column 1

And by going forward on step 7, 
Find first `-1` in the first row and column 2: (1,2)
Find a strictly positive and non-identity value in row 2:(1,4)
So, the missing value of `(1,2)` must be `1000000 / 1000 = 1000`.
In sum, we have: 

M = [1  1000  100000 1000000
    -1    1   -1     1000
     0   -1    1      -1
     0    0   -1       1]

Now you can sort the elements of the first row in ascending order (step 8).
 

我对基于图形的解决方案的尝试。示例3是最有趣的,所以我将采用一个、多个步骤和多个接收器

将B=NA转换为边A->B并将其标记为n,n>1。如果它不是一个连接的DAG,那么它是不一致的。 通过使多个连接I->J->K跳到I->K,将I->J的n乘以J->K,将其简化为二部图。任何不一致都表明问题不一致。 这一步的想法是只产生一个最大的价值。左顶点的阶数大于1,P和{Q,R}在右集合中,其中P->Q标记为n1,P->R标记为n2,1R不变,而Q->R标记为n2/n1,在本例中为Mib,从右向左。 图是具有单个右节点的二部图吗?不,转到2。 对边缘进行排序

带n1的X->Z。。。Y->Z随n2变为1 Z=n1 X=…=n2 Y


这可以看作是线性代数中的一个问题。也许是矩阵消去法?你确定吗?你能再澄清一下吗?因为我尝试了矩阵消去法,但没有成功,可能是我做错了Math.SE可能知道确切的术语,但它是保守的,超过这个数量的一个会出现在相反的对角线条目中。只要它在每列中有n-1个非对角项,并且没有不一致的条目,它就应该是可满足的。例如,KiB/B=KiB/MiB*MiB/B=MiB/KiB^-1*MiB/B。将所有单元转换为单个公共单元,排序,然后再转换回。使用适当的单元库,如或、。它们还将所有单元转换为一个公共单元,以便在内部存储。在转换中已经存在某种单位。另见Brilliant,这个算法叫什么?@MostafaSolati,谢谢。对不起,我不知道这个的具体名字。但是,你可以称之为OmG的算法;我是说你从哪儿弄来的D这是你自己发明的,还是解决这些问题的一种常见做法?@MostafaSolati当然。它是我的,除非我应该引用它的起源!不幸的是,您的解决方案并非在所有情况下都有效,例如,它无法在示例2中找到最大的单元。它有解决办法吗?