匈牙利算法是求二分图的最大匹配的一种算法,它的根据就是Hall定理中充分性证明中的思想。( 卢开澄,卢化明. 图论及其应用.清华大学出版社[M],1995年.)
使用举例:要给定条件(二分图)和初始匹配.
int[,] TestMaxArray = new int[5, 5]{
{ 1, 1,-1, -1, -1 },
{ -1, 1, 1, -1, -1 },
{ -1, 1,-1, -1, 1 },
{ -1,-1, 1, -1, -1 },
{ -1,-1, 1, 1, 1 }};
int[,] GivenArray = new int[5, 5]{
{ 1, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 1 },
{ 0, 0, 0, 0, 0 },
{ 0, 0, 1, 0, 2 }};
MaxMatch testtest = new MaxMatch(TestMaxArray, GivenArray);//实例化
int [,] resultArray = testtest.resultArray();//结果
下面是实现代码: MaxMatch.csusing System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
namespace MaxMatch
{
/// <summary>
/// The values of Array must be -1 or 1 or 0
/// </summary>
public class MaxMatch
{
private int[,] OriginData;
private int[,] GivenMatch;
private int cn, rn;
private ArrayList OldIndexOfV1, OldIndexOfV2;
private int[] SequencedIndexOfV2, TV1;
public MaxMatch(int[,] OriginData, int[,] GivenMatch)//, int cn, int rn)
{
this.OriginData = OriginData;
this.GivenMatch = GivenMatch;
GivenMatch = this.GivenMatch;
this.cn = OriginData.GetLength(1);
this.rn = OriginData.GetLength(0);
this.GivenMatch = new int[rn, cn];
for (int i = 0; i < rn; i++)
{
for (int j = 0; j < cn; j++)
{
int bTemp = 0;
bTemp = OriginData[i, j];
if (bTemp == -1)