简体   繁体   English

矩阵变换的匈牙利算法

[英]Hungarian algorithm for change of matrix

I need to implement the realization of Hungarian algorithm for such task: I have any example of matrix(actually I need this for cluster analysis):我需要为这样的任务实现匈牙利算法的实现:我有任何矩阵示例(实际上我需要它进行聚类分析):

X<-matrix(c(-1,1,2,-1,2,3,1,2,3), nrow=3, ncol=3, byrow = TRUE)
X

I need to do some permutations of rows or columns in order to receive such result: all diagonal elements should be maximum.我需要对行或列进行一些排列才能收到这样的结果:所有对角线元素都应该是最大的。 Here I will show some photos:在这里,我将展示一些照片:矩阵示例 , where I have 3 rows and 3 columns and then I must have a result: ,我有 3 行 3 列,然后我必须有一个结果:矩阵的变化 . .

As is shown on picture, there is a permutation: first column become third column and after this, new first and second columns change positions.如图所示,有一个排列:第一列变成第三列,之后,新的第一列和第二列改变位置。 How can I do such thing using Hungarian algorithm?我怎样才能使用匈牙利算法做这样的事情?

Try with RcppHungarian function.尝试使用 RcppHungarian 函数。 However, the values have to be non-negative, so I have changed the data a bit!但是,这些值必须是非负的,所以我稍微改变了数据!

library(RcppHungarian)
x<-matrix(c(1,2,3,1,2,3,2,2,3), nrow=3, ncol=3, byrow = TRUE)
HungarianSolver(x)

I'll use this matrix from your question as an example:我将使用您问题中的这个矩阵作为示例:

 2  7  1
 0  0 10
 8  2  0

First of all the Hungarian Algorithm finds a minimum, not a maximum, so we need to multiply all of the vales be -1 so that when you run the algorithm and it finds a minimum, it will actually be the maximum from the original matrix.首先,匈牙利算法找到最小值,而不是最大值,因此我们需要将所有值乘以 -1,以便当您运行算法并找到最小值时,它实际上是原始矩阵中的最大值。

-2 -7  -1
-0 -0 -10
-8 -2  -0

The Hungarian Algorithm only works with non-negative numbers, and while the code you're using for it should be able to handle them, you can get rid of them yourself by subtracting the smallest number in the matrix (-10 in this case) from ever value in the matrix.匈牙利算法仅适用于非负数,虽然您使用的代码应该能够处理它们,但您可以通过减去矩阵中的最小数(在这种情况下为 -10)来摆脱它们从矩阵中的值开始。

 8  3  9
10 10  0
 2  8 10

Now we run the Hungarian Algorithm on this matrix and get a list of indices of the minimum matching found by the algorithm.现在我们在这个矩阵上运行匈牙利算法并获得算法找到的最小匹配的索引列表。 We want to sort this list by the x indices.我们想按 x 索引对这个列表进行排序。

[(0,2), (1,0), (2,1)]

This minimum matching is also the maximum matching for our original matrix.这个最小匹配也是我们原始矩阵的最大匹配。 Since this matching is sorted by the x indices, we can use the y indices as indexes into our original matrix.由于此匹配按 x 索引排序,因此我们可以使用 y 索引作为原始矩阵的索引。 That is, the first index pair is (0,2) , so the first row of our final matrix will be the third row from the original matrix.也就是说,第一个索引对是(0,2) ,因此我们最终矩阵的第一行将是原始矩阵的第三行。

(0,2) -> first row in final matrix is third row in original matrix (0,2) -> 最终矩阵中的第一行是原始矩阵中的第三行
(1,0) -> second row in final matrix is first row in original matrix (1,0) -> 最终矩阵中的第二行是原始矩阵中的第一行
(2,1) -> third row in final matrix is second row in original matrix (2,1) -> 最终矩阵中的第三行是原始矩阵中的第二行

 8  2  0
 2  7  1
 0  0 10

This doesn't exactly match the permutation you gave in your question, but it is still a valid permutation.这与您在问题中给出的排列不完全匹配,但它仍然是一个有效的排列。

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

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