匈牙利算法寻找最大匹配
Posted 202xxx
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了匈牙利算法寻找最大匹配相关的知识,希望对你有一定的参考价值。
问题背景
给定一个x与y对应的连接图,要求每个xi与yi最多只能匹配一次,求最大的匹配次数
求解思路
(1)将x与y的连接转换成矩阵,可相互连接标记为1,其余为0
y1 | y2 | y3 | y4 | y5 | y6 | y7 | |
x1 | 1 | 1 | 0 | 1 | 0 | 0 | 0 |
x2 | 0 | 1 | 0 | 0 | 1 | 0 | 0 |
x3 | 1 | 0 | 0 | 1 | 0 | 0 | 1 |
x4 | 0 | 0 | 1 | 1 | 0 | 1 | 0 |
x5 | 0 | 0 | 0 | 1 | 0 | 0 | 0 |
x6 | 0 | 0 | 0 | 1 | 0 | 0 | 0 |
(2)最外层循环x1到x6,即第一行到第六行,每一行分别从y1到y7遍历
(3)其中used_b = [0, 0, 0, 0, 0, 0, 0]为某一行中被使用的y1
(4)conection_b = [-1,-1,-1,-1,-1,-1,-1]其中每个元素的取值范围为0-5,代表了y对应的第几行x
(5)从第一行开始,选中了y1,conection_b发生变化[0, -1, -1, -1, -1, -1, -1]
i: 0
find: 0
_index: 0
_used_b: [1, 0, 0, 0, 0, 0, 0]
_conection_b: [-1, -1, -1, -1, -1, -1, -1]
index: 0
conection_b: [0, -1, -1, -1, -1, -1, -1]
count
y1 | y2 | y3 | y4 | y5 | y6 | y7 | |
x1 | 1 | 1 | 0 | 1 | 0 | 0 | 0 |
(6)第二行,选中了y2,conection_b发生变化[0, 1, -1, -1, -1, -1, -1]
i: 1
find: 1
_index: 1
_used_b: [0, 1, 0, 0, 0, 0, 0]
_conection_b: [0, -1, -1, -1, -1, -1, -1]
index: 1
conection_b: [0, 1, -1, -1, -1, -1, -1]
count
y1 | y2 | y3 | y4 | y5 | y6 | y7 | |
x1 | 1 | 1 | 0 | 1 | 0 | 0 | 0 |
x2 | 0 | 1 | 0 | 0 | 1 | 0 | 0 |
(7)第三行,选中了y1,由于第一行也选中了y1,因此进入递归
i: 2
find: 2
_index: 0
_used_b: [1, 0, 0, 0, 0, 0, 0]
_conection_b: [0, 1, -1, -1, -1, -1, -1]
find: 0
_index: 1
_used_b: [1, 1, 0, 0, 0, 0, 0]
_conection_b: [0, 1, -1, -1, -1, -1, -1]
find: 1
_index: 4
_used_b: [1, 1, 0, 0, 1, 0, 0]
_conection_b: [0, 1, -1, -1, -1, -1, -1]
index: 4
conection_b: [0, 1, -1, -1, 1, -1, -1]
index: 1
conection_b: [0, 0, -1, -1, 1, -1, -1]
index: 0
conection_b: [2, 0, -1, -1, 1, -1, -1]
count
y1 | y2 | y3 | y4 | y5 | y6 | y7 | |
x1 | 1 | 1 | 0 | 1 | 0 | 0 | 0 |
x2 | 0 | 1 | 0 | 0 | 1 | 0 | 0 |
x3 | 1 | 0 | 0 | 1 | 0 | 0 | 1 |
(8)得到冲突行的index为0,因此开始find(0),发现y2也被使用,因此继续递归find(1)
y1 | y2 | y3 | y4 | y5 | y6 | y7 | |
x1 | 1 | 1 | 0 | 1 | 0 | 0 | 0 |
x2 | 0 | 1 | 0 | 0 | 1 | 0 | 0 |
x3 | 1 | 0 | 0 | 1 | 0 | 0 | 1 |
(9)最后x2找到了y5,因此递归结束,conection_b: [2, 0, -1, -1, 1, -1, -1]
y1 | y2 | y3 | y4 | y5 | y6 | y7 | |
x1 | 1 | 1 | 0 | 1 | 0 | 0 | 0 |
x2 | 0 | 1 | 0 | 0 | 1 | 0 | 0 |
x3 | 1 | 0 | 0 | 1 | 0 | 0 | 1 |
(10)第四行,选中了y3,由于没有冲突,所以conection_b: [2, 0, 3, -1, 1, -1, -1]
i: 3
find: 3
_index: 2
_used_b: [0, 0, 1, 0, 0, 0, 0]
_conection_b: [2, 0, -1, -1, 1, -1, -1]
index: 2
conection_b: [2, 0, 3, -1, 1, -1, -1]
count
y1 | y2 | y3 | y4 | y5 | y6 | y7 | |
x1 | 1 | 1 | 0 | 1 | 0 | 0 | 0 |
x2 | 0 | 1 | 0 | 0 | 1 | 0 | 0 |
x3 | 1 | 0 | 0 | 1 | 0 | 0 | 1 |
x4 | 0 | 0 | 1 | 1 | 0 | 1 | 0 |
(11)第五行,选中了y4,由于没有冲突,所以conection_b: [2, 0, 3, 4, 1, -1, -1]
i: 4
find: 4
_index: 3
_used_b: [0, 0, 0, 1, 0, 0, 0]
_conection_b: [2, 0, 3, -1, 1, -1, -1]
index: 3
conection_b: [2, 0, 3, 4, 1, -1, -1]
count
y1 | y2 | y3 | y4 | y5 | y6 | y7 | |
x1 | 1 | 1 | 0 | 1 | 0 | 0 | 0 |
x2 | 0 | 1 | 0 | 0 | 1 | 0 | 0 |
x3 | 1 | 0 | 0 | 1 | 0 | 0 | 1 |
x4 | 0 | 0 | 1 | 1 | 0 | 1 | 0 |
x5 | 0 | 0 | 0 | 1 | 0 | 0 | 0 |
(12)第六行,选中了y4,由于与第五行发生了冲突,所以进入递归find(4)
y1 | y2 | y3 | y4 | y5 | y6 | y7 | |
x1 | 1 | 1 | 0 | 1 | 0 | 0 | 0 |
x2 | 0 | 1 | 0 | 0 | 1 | 0 | 0 |
x3 | 1 | 0 | 0 | 1 | 0 | 0 | 1 |
x4 | 0 | 0 | 1 | 1 | 0 | 1 | 0 |
x5 | 0 | 0 | 0 | 1 | 0 | 0 | 0 |
x6 | 0 | 0 | 0 | 1 | 0 | 0 | 0 |
i: 5
find: 5
_index: 3
_used_b: [0, 0, 0, 1, 0, 0, 0]
_conection_b: [2, 0, 3, 4, 1, -1, -1]
find: 4
used_b: [0, 0, 0, 1, 0, 0, 0]
conection_b: [2, 0, 3, 4, 1, -1, -1]
5
(13)由于第五行中除了y4,没有其他可选线项,因此 find(4) = 0,conection_b没有被改变,因此:conection_b: [2, 0, 3, 4, 1, -1, -1]
y1 | y2 | y3 | y4 | y5 | y6 | y7 | |
x1 | 1 | 1 | 0 | 1 | 0 | 0 | 0 |
x2 | 0 | 1 | 0 | 0 | 1 | 0 | 0 |
x3 | 1 | 0 | 0 | 1 | 0 | 0 | 1 |
x4 | 0 | 0 | 1 | 1 | 0 | 1 | 0 |
x5 | 0 | 0 | 0 | 1 | 0 | 0 | 0 |
x6 | 0 | 0 | 0 | 1 | 0 | 0 | 0 |
代码
>>>def find(x):
... print("find:", x)
... for index in range(7):
... if matrix[x][index] == 1 and used_b[index] == 0:
... used_b[index] = 1
... print("_index:", index)
... print("_used_b:", str(used_b))
... print("_conection_b:", str(conection_b))
... if conection_b[index] == -1 or find(conection_b[index]) != 0:
... print("index:", index)
... conection_b[index] = x
... print("conection_b:", str(conection_b))
... return 1
... return 0
>>>matrix = [
... [1,1,0,1,0,0,0],
... [0,1,0,0,1,0,0],
... [1,0,0,1,0,0,1],
... [0,0,1,1,0,1,0],
... [0,0,0,1,0,0,0],
... [0,0,0,1,0,0,0]
... ]
>>>conection_b = [-1 for _ in range(7)]
>>>count = 0
>>>for i in range(6):
... used_b = [0 for _ in range(7)]
... print("i:",i)
... if find(i):
... print("count")
... count += 1
>>>print("used_b:", str(used_b))
>>>print("conection_b:", str(conection_b))
>>>print(count)
i: 0
find: 0
_index: 0
_used_b: [1, 0, 0, 0, 0, 0, 0]
_conection_b: [-1, -1, -1, -1, -1, -1, -1]
index: 0
conection_b: [0, -1, -1, -1, -1, -1, -1]
count
i: 1
find: 1
_index: 1
_used_b: [0, 1, 0, 0, 0, 0, 0]
_conection_b: [0, -1, -1, -1, -1, -1, -1]
index: 1
conection_b: [0, 1, -1, -1, -1, -1, -1]
count
i: 2
find: 2
_index: 0
_used_b: [1, 0, 0, 0, 0, 0, 0]
_conection_b: [0, 1, -1, -1, -1, -1, -1]
find: 0
_index: 1
_used_b: [1, 1, 0, 0, 0, 0, 0]
_conection_b: [0, 1, -1, -1, -1, -1, -1]
find: 1
_index: 4
_used_b: [1, 1, 0, 0, 1, 0, 0]
_conection_b: [0, 1, -1, -1, -1, -1, -1]
index: 4
conection_b: [0, 1, -1, -1, 1, -1, -1]
index: 1
conection_b: [0, 0, -1, -1, 1, -1, -1]
index: 0
conection_b: [2, 0, -1, -1, 1, -1, -1]
count
i: 3
find: 3
_index: 2
_used_b: [0, 0, 1, 0, 0, 0, 0]
_conection_b: [2, 0, -1, -1, 1, -1, -1]
index: 2
conection_b: [2, 0, 3, -1, 1, -1, -1]
count
i: 4
find: 4
_index: 3
_used_b: [0, 0, 0, 1, 0, 0, 0]
_conection_b: [2, 0, 3, -1, 1, -1, -1]
index: 3
conection_b: [2, 0, 3, 4, 1, -1, -1]
count
i: 5
find: 5
_index: 3
_used_b: [0, 0, 0, 1, 0, 0, 0]
_conection_b: [2, 0, 3, 4, 1, -1, -1]
find: 4
used_b: [0, 0, 0, 1, 0, 0, 0]
conection_b: [2, 0, 3, 4, 1, -1, -1]
5
总结
匈牙利算法的核心思想为:优先考虑最后一行,如果发生冲突,则寻找冲突行可替代项,如果没有可替代项,丢弃最后一行,如果有可替代项,则使用其替代,如果替代之后发生冲突则进入递归,发现冲突不可解除,则丢弃最后一行,冲突可以解除,则使用该方案。
find递归函数的意义在于回溯上一阶段方案能否找到可替代项,使得整个匹配方案之间两两不发生冲突。
以上是关于匈牙利算法寻找最大匹配的主要内容,如果未能解决你的问题,请参考以下文章