KM算法是通过给每个顶点一个标号(叫做顶标)来把求最大权匹配的问题转化为求完备匹
package test;
public class KM {
public static final int INF = 1000000;
private int row, col, size;
private int[][] edge;
private int[] flag;
private int[] hQuan;
private int[] vQuan;
private boolean[] hToken;
private boolean[] vToken;
public KM(int[][] pic) {
edge = pic;
row = edge.length;
col = edge[0].length;
hToken = new boolean[row];
vToken = new boolean[col];
hQuan = new int[row];
vQuan = new int[col];
size = row > col ? col : row;
if (row == size) {
flag = new int[col];
} else {
flag = new int[row];
}
init();
}
private void init() {
for (int i = 0; i < flag.length; i++) {
flag[i] = -1;
}
for (int i = 0; i < hToken.length; i++) {
hToken[i] = false;
}
for (int i = 0; i < vToken.length; i++) {
vToken[i] = false;
}
if (row == size) {
for (int i = 0; i < vQuan.length; i++) {
vQuan[i] = 0;
}
for (int i = 0; i < hQuan.length; i++) {
hQuan[i] = -INF;
for (int j = 0; j < vQuan.length; j++) {
hQuan[i] = max(hQuan[i], edge[i][j]);
}
}
} else {
for (int i = 0; i < hQuan.length; i++) {
hQuan[i] = 0;
}
for (int i = 0; i < vQuan.length; i++) {
vQuan[i] = -INF;
for (int j = 0; j < hQuan.length; j++) {
vQuan[i] = max(vQuan[i], edge[j][i]);
}
}
}
}
public boolean km() {
int[][] map = new int[row][col];
if (row == size) {
int dmin = INF;
do {
for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++) {
if (hQuan[i] + vQuan[j] == edge[i][j])
map[i][j] = 1;
else
map[i][j] = 0;
}
}
if (hasPerfectMatch(map))
return true;
for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++) {
if (hToken[i] && !vToken[j]) {
dmin = min(dmin, hQuan[i] + vQuan[j] - edge[i][j]);
}
}
}
if (dmin != INF && dmin > 0) {
for (int i = 0; i < row; i++) {
if (hToken[i])
hQuan[i] -= dmin;
}
for (int i = 0; i < col; i++) {
if (vToken[i])
vQuan[i] += dmin;
}
}
} while (dmin != INF && dmin > 0);
return false;
} else {
int dmin = INF;
do {
for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++) {
if (hQuan[i] + vQuan[j] == edge[i][j])
map[i][j] = 1;
else
map[i][j] = 0;
}
}
if (hasPerfectMatch(map))
return true;
for (int i = 0; i < col; i++) {
for (int j = 0; j < row; j++) {
if (vToken[i] && !hToken[j]) {
dmin = min(dmin, hQuan[i] + vQuan[j] - edge[i][j]);
}
}
}
if (dmin != INF && dmin > 0) {
for (int i = 0; i < row; i++) {
if (hToken[i])
hQuan[i] += dmin;
}
for (int i = 0; i < col; i++) {
if (vToken[i])
vQuan[i] -= dmin;
}
}
} while (dmin != INF && dmin > 0);
return false;
}
}
/**
* judge whether the map has a perfect match.
* @param map indicates whether each two points have been connected.
* @return true if there is a perfect match in the map, or false.
*/
public boolean hasPerfectMatch(int[][] map) {
int i, j;
if (row == size) {
for (i = 0; i < flag.length; i++)
flag[i] = -1;
for (i = 0; i < size; i++) {
for (j = 0; j < hToken.length; j++)
hToken[j] = false;
for (j = 0; j < vToken.length; j++)
vToken[j] = false;
if (!findAugumentPath(i, map/* ,token */))
break;
}
if (i < row)
return false;
return true;
} else {
for (i = 0; i < flag.length; i++)
flag[i] = -1;
for (i = 0; i < size; i++) {
for (j = 0; j < hToken.length; j++)
hToken[j] = false;
for (j = 0; j < vToken.length; j++)
vToken[j] = false;
if (!findAugumentPath(i, map))
break;
}
if (i < row)
return false;
return true;
}
}
/**
* judge whether there is a augument path at the <code>pos</code> point in
* <code>map</code>
* @param pos the order of the special point in the map
* @param map the map's maltrix
* @return true if there is a augumemt path at the <code>pos</code> point in
* the <code>map</code
*/
public boolean findAugumentPath(int pos, int[][] map) {
if (row == size) {
hToken[pos] = true;
for (int i = 0; i < col; i++) {
if (map[pos][i] == 1 && vToken[i] == false) {
vToken[i] = true;
if (flag[i] == -1 || findAugumentPath(flag[i], map)) {
flag[i] = pos;
return true;
}
}
}
return false;
} else {
vToken[pos] = true;
for (int i = 0; i < row; i++) {
if (map[i][pos] == 1 && hToken[i] == false) {
hToken[i] = true;
if (flag[i] == -1 || findAugumentPath(flag[i], map)) {
flag[i] = pos;
return true;
}
}
}
return false;
}
}
/**
* compute the max number of two
* @param i one digit used to compare with another one
* @param j the other digit used to compare with the first one
* @return the bigger one number of this two
*/
public int max(int i, int j) {
return i > j ? i : j;
}
public int min(int i, int j) {
return i > j ? j : i;
}
public static void main(String[] args) {
int[][] edge = { { 7, 6, 5 }, { 4, 1, 8 }, { 3, 2, 9 } };
KM temp = new KM(edge);
temp.km();
System.out.println("hello");
}
}
配的问题的。设顶点Xi的顶标为A[i],顶点Yi的顶标为B[i],顶点Xi与Yj之间的边权为w[
i,j]。在算法执行过程中的任一时刻,对于任一条边(i,j),A[i]+B[j]>=w[i,j]始终成立
。KM算法的正确性基于
以下定理:
若由二分图中所有满足A[i]+B[j]=w[i,j]的边(i,j)构成的子图(称做相等子图)有完
备匹配,那么这个完备匹配就是二分图的最大权匹配。
这个定理是显然的。因为对于二分图的任意一个匹配,如果它包含于相等子图,那么
它的边权和等于所有顶点的顶标和;如果它有的边不包含于相等子图,那么它的边权和小
于所有顶点的顶标和。所以相等子图的完备匹配一定是二分图的最大权匹配。
初始时为了使A[i]+B[j]>=w[i,j]恒成立,令A[i]为所有与顶点Xi关联的边的最大权,
B[j]=0。如果当前的相等子图没有完备匹配,就按下面的方法修改顶标以使扩大相等子图
,直到相等子图具有完备匹配为止。
我们求当前相等子图的完备匹配失败了,是因为对于某个X顶点,我们找不到一条从它
出发的交错路。这时我们获得了一棵交错树,它的叶子结点全部是X顶点。现在我们把交错
树中X顶点的顶标全都减小某个值d,Y顶点的顶标全都增加同一个值d,那么我们会发现:
两端都在交错树中的边(i,j),A[i]+B[j]的值没有变化。也就是说,它原来属于相等子图
,现在仍属于相等子图。
两端都不在交错树中的边(i,j),A[i]和B[j]都没有变化。也就是说,它原来属于(或不属
于)相等子图,现在仍属于(或不属于)相等子图。
X端不在交错树中,Y端在交错树中的边(i,j),它的A[i]+B[j]的值有所增大。它原来不属
于相等子图,现在仍不属于相等子图。
X端在交错树中,Y端不在交错树中的边(i,j),它的A[i]+B[j]的值有所减小。也就说,它
原来不属于相等子图,现在可能进入了相等子图,因而使相等子图得到了扩大。
现在的问题就是求d值了。为了使A[i]+B[j]>=w[i,j]始终成立,且至少有一条边进入
相等子图,d应该等于min{A[i]+B[j]-w[i,j]|Xi在交错树中,Yi不在交错树中}。
以上就是KM算法的基本思路。但是朴素的实现方法,时间复杂度为O(n4)——需要找O
(n)次增广路,每次增广最多需要修改O(n)次顶标,每次修改顶标时由于要枚举边来求d值
,复杂度为O(n2)。实际上KM算法的复杂度是可以做到O(n3)的。我们给每个Y顶点一个“松
弛量”函数slack,每
次开始找增广路时初始化为无穷大。在寻找增广路的过程中,检查边(i,j)时,如果它不在
相等子图中,则让slack[j]变成原值与A[i]+B[j]-w[i,j]的较小值。这样,在修改顶标时
,取所有不在交错树中的Y顶点的slack值中的最小值作为d值即可。但还要注意一点:修改
顶标后,要把所有的
slack值都减去d。
package test;
public class KM {
public static final int INF = 1000000;
private int row, col, size;
private int[][] edge;
private int[] flag;
private int[] hQuan;
private int[] vQuan;
private boolean[] hToken;
private boolean[] vToken;
public KM(int[][] pic) {
edge = pic;
row = edge.length;
col = edge[0].length;
hToken = new boolean[row];
vToken = new boolean[col];
hQuan = new int[row];
vQuan = new int[col];
size = row > col ? col : row;
if (row == size) {
flag = new int[col];
} else {
flag = new int[row];
}
init();
}
private void init() {
for (int i = 0; i < flag.length; i++) {
flag[i] = -1;
}
for (int i = 0; i < hToken.length; i++) {
hToken[i] = false;
}
for (int i = 0; i < vToken.length; i++) {
vToken[i] = false;
}
if (row == size) {
for (int i = 0; i < vQuan.length; i++) {
vQuan[i] = 0;
}
for (int i = 0; i < hQuan.length; i++) {
hQuan[i] = -INF;
for (int j = 0; j < vQuan.length; j++) {
hQuan[i] = max(hQuan[i], edge[i][j]);
}
}
} else {
for (int i = 0; i < hQuan.length; i++) {
hQuan[i] = 0;
}
for (int i = 0; i < vQuan.length; i++) {
vQuan[i] = -INF;
for (int j = 0; j < hQuan.length; j++) {
vQuan[i] = max(vQuan[i], edge[j][i]);
}
}
}
}
public boolean km() {
int[][] map = new int[row][col];
if (row == size) {
int dmin = INF;
do {
for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++) {
if (hQuan[i] + vQuan[j] == edge[i][j])
map[i][j] = 1;
else
map[i][j] = 0;
}
}
if (hasPerfectMatch(map))
return true;
for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++) {
if (hToken[i] && !vToken[j]) {
dmin = min(dmin, hQuan[i] + vQuan[j] - edge[i][j]);
}
}
}
if (dmin != INF && dmin > 0) {
for (int i = 0; i < row; i++) {
if (hToken[i])
hQuan[i] -= dmin;
}
for (int i = 0; i < col; i++) {
if (vToken[i])
vQuan[i] += dmin;
}
}
} while (dmin != INF && dmin > 0);
return false;
} else {
int dmin = INF;
do {
for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++) {
if (hQuan[i] + vQuan[j] == edge[i][j])
map[i][j] = 1;
else
map[i][j] = 0;
}
}
if (hasPerfectMatch(map))
return true;
for (int i = 0; i < col; i++) {
for (int j = 0; j < row; j++) {
if (vToken[i] && !hToken[j]) {
dmin = min(dmin, hQuan[i] + vQuan[j] - edge[i][j]);
}
}
}
if (dmin != INF && dmin > 0) {
for (int i = 0; i < row; i++) {
if (hToken[i])
hQuan[i] += dmin;
}
for (int i = 0; i < col; i++) {
if (vToken[i])
vQuan[i] -= dmin;
}
}
} while (dmin != INF && dmin > 0);
return false;
}
}
/**
* judge whether the map has a perfect match.
* @param map indicates whether each two points have been connected.
* @return true if there is a perfect match in the map, or false.
*/
public boolean hasPerfectMatch(int[][] map) {
int i, j;
if (row == size) {
for (i = 0; i < flag.length; i++)
flag[i] = -1;
for (i = 0; i < size; i++) {
for (j = 0; j < hToken.length; j++)
hToken[j] = false;
for (j = 0; j < vToken.length; j++)
vToken[j] = false;
if (!findAugumentPath(i, map/* ,token */))
break;
}
if (i < row)
return false;
return true;
} else {
for (i = 0; i < flag.length; i++)
flag[i] = -1;
for (i = 0; i < size; i++) {
for (j = 0; j < hToken.length; j++)
hToken[j] = false;
for (j = 0; j < vToken.length; j++)
vToken[j] = false;
if (!findAugumentPath(i, map))
break;
}
if (i < row)
return false;
return true;
}
}
/**
* judge whether there is a augument path at the <code>pos</code> point in
* <code>map</code>
* @param pos the order of the special point in the map
* @param map the map's maltrix
* @return true if there is a augumemt path at the <code>pos</code> point in
* the <code>map</code
*/
public boolean findAugumentPath(int pos, int[][] map) {
if (row == size) {
hToken[pos] = true;
for (int i = 0; i < col; i++) {
if (map[pos][i] == 1 && vToken[i] == false) {
vToken[i] = true;
if (flag[i] == -1 || findAugumentPath(flag[i], map)) {
flag[i] = pos;
return true;
}
}
}
return false;
} else {
vToken[pos] = true;
for (int i = 0; i < row; i++) {
if (map[i][pos] == 1 && hToken[i] == false) {
hToken[i] = true;
if (flag[i] == -1 || findAugumentPath(flag[i], map)) {
flag[i] = pos;
return true;
}
}
}
return false;
}
}
/**
* compute the max number of two
* @param i one digit used to compare with another one
* @param j the other digit used to compare with the first one
* @return the bigger one number of this two
*/
public int max(int i, int j) {
return i > j ? i : j;
}
public int min(int i, int j) {
return i > j ? j : i;
}
public static void main(String[] args) {
int[][] edge = { { 7, 6, 5 }, { 4, 1, 8 }, { 3, 2, 9 } };
KM temp = new KM(edge);
temp.km();
System.out.println("hello");
}
}