文章目录

  • 分配问题
  • 匈牙利算法
    • 算法步骤
    • 算法实现
      • python版本
      • C++版本

分配问题

分配问题/指派问题(Assignment Problem)作为线性规划问题的一个特例,在运筹学研究中占有重要的地位,一直受到广泛的重视。

假如有n个工人和n项工作,每个工人只能执行一个工作,并且每个工作只能分配给一个工人,每个工人做不同类型工作的成本是不同的。如何将这n项工作分配给这n个工人,从而使得总体成本最低。

用矩阵C(i,j)C(i,j)C(i,j)来表示n个工人中每个个工人执行不同工作的成本。分配问题就是将工作分配给工人,以使总成本最小化。1


如果想找到成本最低的分配,最直观的方法是运用暴力穷举。对于nxn的矩阵来说,第一行有n个选择,第二行有n-1个选择,以此类推,总共有n!n!n!种可能的选择,时间复杂度在指数级,计算成本太高。

所以,James Munkre在1950年代提出匈牙利算法,用于解决分配问题/指派问题,时间复杂度在多项式级,最坏情况下的O(n3)2

匈牙利算法

算法步骤

步骤0: 创建一个称为成本矩阵的nxm矩阵,其中每个元素代表将n个工人之一分配给m个工作之一的成本。旋转矩阵,以便至少有与行一样多的列,并令K= min(n,m)。

步骤1: 对于矩阵的每一行,找到最小的元素,并将其从其行中的每个元素中减去。转到步骤2。

步骤2: 在生成的矩阵中找到0(Z)。如果其行或列中没有加星号的零,请加星号Z。对矩阵中的每个元素重复此操作。转到步骤3。

步骤3: 覆盖有0*的每一列。如果覆盖了K列,则加星号的零表示完整的唯一赋值集。在这种情况下,请转到“完成”,否则,请转到步骤4。步骤3是贪婪方法的示例。如果最小值全部在不同的行中,则它们的位置表示最小的成对分配。

步骤4: 找到一个未覆盖的零并将其准备好。如果包含0 *的行中没有准备好的0’,请转到步骤5。否则,覆盖该行并找出包含0 *的列。继续以这种方式进行操作,直到没有剩余的零为止。保存最小的发现值,然后转到步骤6。

步骤5:构造如下一系列交替的填色和加星号的零:
令Z0代表在步骤4中发现的未覆盖的准备好的零 0’。
令Z1表示Z0列中的星号零 0*(如果有的话)。
令Z2表示Z1行中的准备好的零 0’(始终为1个)。
继续直到0’所在列没有星标零 0*,终止该序列。取消对每个已加星标的零的星标,对系列中的每个0’加星标,去除所有的’和覆盖线。 步骤5是增强路径算法(稳定婚姻问题)的示例。



返回步骤3。


步骤6: 将在第4步中找到的最小值添加到每个覆盖行的每个元素中,并将其从每个未覆盖列的每个元素中减去。返回第4步,而不更改任何星号,或遮盖线。





完成: 分配对由成本矩阵中加星号的零的位置指示。如果C(i,j)为星号零,则将与行i关联的元素分配给与列j关联的元素。

算法实现

python版本

scipy中有对应的接口scipy.optimize.linear_sum_assignment3,输入代价矩阵,即可得到分配问题的结果:

    >>> cost = np.array([[4, 1, 3], [2, 0, 5], [3, 2, 2]])>>> from scipy.optimize import linear_sum_assignment>>> row_ind, col_ind = linear_sum_assignment(cost)>>> col_indarray([1, 0, 2])>>> cost[row_ind, col_ind].sum()5

scipy.optimize.linear_sum_assignment的源码如下4

# Hungarian algorithm (Kuhn-Munkres) for solving the linear sum assignment
# problem. Taken from scikit-learn. Based on original code by Brian Clapper,
# adapted to NumPy by Gael Varoquaux.
# Further improvements by Ben Root, Vlad Niculae and Lars Buitinck.
#
# Copyright (c) 2008 Brian M. Clapper <bmc@clapper.org>, Gael Varoquaux
# Author: Brian M. Clapper, Gael Varoquaux
# License: 3-clause BSDimport numpy as npdef linear_sum_assignment(cost_matrix):"""Solve the linear sum assignment problem.The linear sum assignment problem is also known as minimum weight matchingin bipartite graphs. A problem instance is described by a matrix C, whereeach C[i,j] is the cost of matching vertex i of the first partite set(a "worker") and vertex j of the second set (a "job"). The goal is to finda complete assignment of workers to jobs of minimal cost.Formally, let X be a boolean matrix where :math:`X[i,j] = 1` iff row i isassigned to column j. Then the optimal assignment has cost.. math::\min \sum_i \sum_j C_{i,j} X_{i,j}s.t. each row is assignment to at most one column, and each column to atmost one row.This function can also solve a generalization of the classic assignmentproblem where the cost matrix is rectangular. If it has more rows thancolumns, then not every row needs to be assigned to a column, and viceversa.The method used is the Hungarian algorithm, also known as the Munkres orKuhn-Munkres algorithm.Parameters----------cost_matrix : arrayThe cost matrix of the bipartite graph.Returns-------row_ind, col_ind : arrayAn array of row indices and one of corresponding column indices givingthe optimal assignment. The cost of the assignment can be computedas ``cost_matrix[row_ind, col_ind].sum()``. The row indices will besorted; in the case of a square cost matrix they will be equal to``numpy.arange(cost_matrix.shape[0])``.Notes-----.. versionadded:: 0.17.0Examples-------->>> cost = np.array([[4, 1, 3], [2, 0, 5], [3, 2, 2]])>>> from scipy.optimize import linear_sum_assignment>>> row_ind, col_ind = linear_sum_assignment(cost)>>> col_indarray([1, 0, 2])>>> cost[row_ind, col_ind].sum()5References----------1. http://csclab.murraystate.edu/bob.pilgrim/445/munkres.html2. Harold W. Kuhn. The Hungarian Method for the assignment problem.*Naval Research Logistics Quarterly*, 2:83-97, 1955.3. Harold W. Kuhn. Variants of the Hungarian method for assignmentproblems. *Naval Research Logistics Quarterly*, 3: 253-258, 1956.4. Munkres, J. Algorithms for the Assignment and Transportation Problems.*J. SIAM*, 5(1):32-38, March, 1957.5. https://en.wikipedia.org/wiki/Hungarian_algorithm"""cost_matrix = np.asarray(cost_matrix)if len(cost_matrix.shape) != 2:raise ValueError("expected a matrix (2-d array), got a %r array"% (cost_matrix.shape,))# The algorithm expects more columns than rows in the cost matrix.'''代价矩阵需要列数 ≥ 行数'''if cost_matrix.shape[1] < cost_matrix.shape[0]:cost_matrix = cost_matrix.Ttransposed = Trueelse:transposed = Falsestate = _Hungary(cost_matrix)# No need to bother with assignments if one of the dimensions# of the cost matrix is zero-length.step = None if 0 in cost_matrix.shape else _step1while step is not None:step = step(state)if transposed:marked = state.marked.Telse:marked = state.markedreturn np.where(marked == 1)class _Hungary(object):"""State of the Hungarian algorithm.Parameters----------cost_matrix : 2D matrixThe cost matrix. Must have shape[1] >= shape[0]."""def __init__(self, cost_matrix):self.C = cost_matrix.copy()n, m = self.C.shapeself.row_uncovered = np.ones(n, dtype=bool)self.col_uncovered = np.ones(m, dtype=bool)self.Z0_r = 0self.Z0_c = 0self.path = np.zeros((n + m, 2), dtype=int)self.marked = np.zeros((n, m), dtype=int)def _clear_covers(self):"""Clear all covered matrix cells"""self.row_uncovered[:] = Trueself.col_uncovered[:] = True# Individual steps of the algorithm follow, as a state machine: they return
# the next step to be taken (function to be called), if any.def _step1(state):"""Steps 1 and 2 in the Wikipedia page.""""""Step 1: For each row of the matrix, find the smallest element andsubtract it from every element in its row.    减去每一行的最小值"""state.C -= state.C.min(axis=1)[:, np.newaxis]"""Step 2: Find a zero (Z) in the resulting matrix. If there is nostarred zero in its row or column, star Z. Repeat for each elementin the matrix.    如果一行或列中没有星标的0,则标记0*"""for i, j in zip(*np.where(state.C == 0)):if state.col_uncovered[j] and state.row_uncovered[i]:state.marked[i, j] = 1state.col_uncovered[j] = Falsestate.row_uncovered[i] = Falsestate._clear_covers()return _step3def _step3(state):"""Step3:Cover each column containing a starred zero. If n columns are covered,the starred zeros describe a complete set of unique assignments.In this case, Go to DONE, otherwise, Go to Step 4.覆盖每列包含加星号的零。如果覆盖了n列,加星号的零表示完整的唯一结果集。"""marked = (state.marked == 1)state.col_uncovered[np.any(marked, axis=0)] = Falseif marked.sum() < state.C.shape[0]:return _step4def _step4(state):"""Step4:Find a noncovered zero and prime it. If there is no starred zeroin the row containing this primed zero, Go to Step 5. Otherwise,cover this row and uncover the column containing the starredzero. Continue in this manner until there are no uncovered zerosleft. Save the smallest uncovered value and Go to Step 6.找到一个未覆盖的零并将其准备好。 如果准备好的零所在行中没有加星号的零,请转到步骤5。否则,覆盖该行并找出包含加注星号的零的列。 继续以这种方式进行操作,直到没有剩余的零为止。保存最小的发现值,然后转到步骤6。"""# We convert to int as numpy operations are faster on intC = (state.C == 0).astype(int)covered_C = C * state.row_uncovered[:, np.newaxis]covered_C *= np.asarray(state.col_uncovered, dtype=int)n = state.C.shape[0]m = state.C.shape[1]while True:# Find an uncovered zerorow, col = np.unravel_index(np.argmax(covered_C), (n, m))if covered_C[row, col] == 0:return _step6else:state.marked[row, col] = 2# Find the first starred element in the rowstar_col = np.argmax(state.marked[row] == 1)if state.marked[row, star_col] != 1:# Could not find onestate.Z0_r = rowstate.Z0_c = colreturn _step5else:col = star_colstate.row_uncovered[row] = Falsestate.col_uncovered[col] = Truecovered_C[:, col] = C[:, col] * (np.asarray(state.row_uncovered, dtype=int))covered_C[row] = 0def _step5(state):"""Step5:Construct a series of alternating primed and starred zeros as follows.Let Z0 represent the uncovered primed zero found in Step 4.Let Z1 denote the starred zero in the column of Z0 (if any).Let Z2 denote the primed zero in the row of Z1 (there will always be one).Continue until the series terminates at a primed zero that has no starredzero in its column. Unstar each starred zero of the series, star eachprimed zero of the series, erase all primes and uncover every line in thematrix. Return to Step 3构造如下一系列交替的填色和加星号的零:令Z0代表在步骤4中发现的未覆盖的准备好的零 0'。令Z1表示Z0列中的星号零 0*(如果有的话)。令Z2表示Z1行中的准备好的零 0'(始终为1个)。继续直到0'所在列没有星标0*,终止该序列。取消对每个已加星标的零的星标,对系列中的每个0'加星标,去除所有的'和覆盖线。 返回步骤3。"""count = 0path = state.pathpath[count, 0] = state.Z0_rpath[count, 1] = state.Z0_cwhile True:# Find the first starred element in the col defined by# the path.row = np.argmax(state.marked[:, path[count, 1]] == 1)if state.marked[row, path[count, 1]] != 1:# Could not find onebreakelse:count += 1path[count, 0] = rowpath[count, 1] = path[count - 1, 1]# Find the first prime element in the row defined by the# first path stepcol = np.argmax(state.marked[path[count, 0]] == 2)if state.marked[row, col] != 2:col = -1count += 1path[count, 0] = path[count - 1, 0]path[count, 1] = col# Convert pathsfor i in range(count + 1):if state.marked[path[i, 0], path[i, 1]] == 1:state.marked[path[i, 0], path[i, 1]] = 0else:state.marked[path[i, 0], path[i, 1]] = 1state._clear_covers()# Erase all prime markingsstate.marked[state.marked == 2] = 0return _step3def _step6(state):"""Step 6: Add the value found in Step 4 to every element of each covered row,and subtract it from every element of each uncovered column.Return to Step 4 without altering any stars, primes, or covered lines.将在第4步中找到的值添加到每个覆盖行的每个元素中,并将其从每个未覆盖列的每个元素中减去。返回第4步,而不更改任何星号,或遮盖线。"""# the smallest uncovered value in the matrixif np.any(state.row_uncovered) and np.any(state.col_uncovered):minval = np.min(state.C[state.row_uncovered], axis=0)minval = np.min(minval[state.col_uncovered])state.C[~state.row_uncovered] += minvalstate.C[:, state.col_uncovered] -= minvalreturn _step4

C++版本

参考多目标跟踪项目中的实现:github


  1. https://brc2.com/the-algorithm-workshop/ ↩︎

  2. Jin K W . A new implementation of an algorithm for the optimal assignment problem: An improved version of Munkres’ algorithm[J]. Bit Numerical Mathematics, 1979, 19(3):418-424. ↩︎

  3. https://docs.scipy.org/doc/scipy/reference/generated/scipy.optimize.linear_sum_assignment.html ↩︎

  4. https://github.com/scipy/scipy/blob/v0.18.1/scipy/optimize/_hungarian.py#L13-L107 ↩︎

图解匈牙利算法(含python代码)相关推荐

  1. 12种降维方法终极指南(含Python代码)

    12种降维方法终极指南(含Python代码) 你遇到过特征超过1000个的数据集吗?超过5万个的呢?我遇到过.降维是一个非常具有挑战性的任务,尤其是当你不知道该从哪里开始的时候.拥有这么多变量既是一个 ...

  2. 一文看懂Stacking!(含Python代码)

    一文看懂Stacking!(含Python代码) https://mp.weixin.qq.com/s/faQNTGgBZdZyyZscdhjwUQ 转载于:https://www.cnblogs.c ...

  3. 感知机算法之Python代码实现

    感知机算法之Python代码实现 1.算法简介 感知机学习算法原始形式: 输入:训练集T 输出:w,b 感知机模型:f(x)=sign(w·x+b) 算法步骤: 1.初始化参数w0,b0 2.在训练集 ...

  4. 提升机器算法LightGBM(图解+理论+增量训练python代码+lightGBM调参方法)

    LightGBM是个快速的,分布式的,高性能的基于决策树算法的梯度提升框架.可用于排序,分类,回归以及很多其他的机器学习任务中. 在竞赛题中,我们知道XGBoost算法非常热门,它是一种优秀的拉动框架 ...

  5. BPR贝叶斯个性化推荐算法—推荐系统基础算法(含python代码实现以及详细例子讲解)

    BPR贝叶斯个性化排序算法 一.问题导入 二.显示反馈与隐式反馈 2.1 显式反馈与隐式反馈基本概念 2.2 显式反馈与隐式反馈的比较 2.3 显式反馈与隐式反馈的评价方法 2.3.1 显式反馈数据模 ...

  6. 密码学——保序加密算法(OPE算法-2009年提出)通俗易懂解析(小学生都能懂!)含python代码

    一. 预备知识 保序加密算法:最初是由2009年,Boldyreva等四个人提出来的,可简称BCLO-09算法,论文题目为<Order-Preserving Symmetric Encrypti ...

  7. 如何用机器学习算法来进行电影分类?(含Python代码)

    电影分析--K近邻算法 周末,小迪与女朋友小西走出电影院,回味着刚刚看过的电影. 小迪:刚刚的电影很精彩,打斗场景非常真实,又是一部优秀的动作片! 小西:是吗?我怎么感觉这是一部爱情片呢?真心被男主女 ...

  8. python最长匹配_二分图最大匹配:匈牙利算法的python实现

    二分图匹配是很常见的算法问题,一般用匈牙利算法解决二分图最大匹配问题,但是目前网上绝大多数都是C/C++实现版本,没有python版本,于是就用python实现了一下深度优先的匈牙利算法,本文使用的是 ...

  9. 匈牙利算法与python实现

    匈牙利算法 0 引出 最近看DETR论文,发现其通过匈牙利算法来进行预测和ground truth匹配,从而实现set prediction.这个思路很有意思,并且该匹配算法能适用多种问题,因此,对其 ...

最新文章

  1. 常用的数据结构-散列表
  2. mysql 2014_mysql错误之2014
  3. yolov3 paddle
  4. 不透明度opacity进阶
  5. sdn和nfv的区别—Vecloud微云
  6. pthread_create如何传递两个参数以上的参数
  7. 移动端html游戏开发,GitHub - PromeYang/GameBuilder: GameBuilder 是移动端轻量HTML5游戏快速开发框架,主要应用于活动推广。...
  8. Windows 下 Nginx + PHP5 的安装与配置
  9. RocketMQ特性、专业术语(Producer,Producer Group,Consumer Group,Topic,Message,Tag,Broker,Name Server)等
  10. JavaScript 访问对象属性和方法及区别
  11. STM32关闭CAN外设的自动重传功能
  12. 数据的gzip压缩解压缩_使用GZIP和压缩数据
  13. mssql 批量导入mysql_mssql 数据库 批量导入指令
  14. MySQL常见的库操作,表操作,数据操作集锦及一些注意事项
  15. js中短路运算符 ||
  16. linux编译时间,CentOS下快速编译安装NTP时间同步服务器
  17. javaweb(ssh)体育赛事网上售票系统案例
  18. 波特率和比特率之间的关系
  19. 明翰英语教学系列之雅思篇V1.9(持续更新)
  20. 互联网产品经理是做什么的

热门文章

  1. asp.net 发送邮件函数两则
  2. linux下安装php两种模式区别
  3. JavaScript 内存机制(前端同学进阶必备)
  4. 常见的SQL笔试题和面试题:SQL经典50题
  5. 不相交集类及其应用生成迷宫
  6. SweetAlert2网页弹窗---JAVASCRIPT弹窗
  7. 【C语言】逗号运算符的使用举例
  8. linux运行级别与服务
  9. 如何使用粒子氩气进行位置跟踪
  10. 计算机网络应用简介_计算机网络简介