# Using Hungarian algorithm to find the maximum matching of two-fractal graphs

Source: Internet
Author: User

1, using the Hungarian algorithm to find the maximum matching of the two-fractal graph

Algorithm outline:
(1) m is empty
(2) Find an augmented path p, by taking the inverse operation to obtain a larger match m ' instead of M
Program folders: 22222

#include < iostream >
#include < FStream >
using namespace Std;

const int MAXN = 100;
int UN, VN; Number of U,v
BOOL G[MAXN][MAXN]; G[I][J] means Xi is connected to YJ
int XM[MAXN], YM[MAXN]; Output amount
BOOL CHK[MAXN]; The auxiliary quantity checks whether a wheel y[v] is checked

BOOL SearchPath (int u)
{
int V;
for (v = 0; v < VN; v + +)
{
if (G[u][v] &&! chk[v])
{
CHK[V] = true;
if (ym[v] = =-1 | | SearchPath (Ym[v]))
{
YM[V] = u;
Xm[u] = v;
return true;
}
}
}
return false;
}

int Maxmatch ()
{
int u;
int ret = 0;
Memset (XM,-1, sizeof (XM));
memset (YM,-1, sizeof (YM));
for (U = 0; u < UN; U + +)
{
if (xm[u] = =-1)
{
Memset (CHK, False, sizeof (CHK)),//sets buffers to a specified character.

if (SearchPath (u)) ret + +;
}
}
return ret;
}

int main ()
{
int I, k;
int TU, TV;
Ifstream cin ("test.txt");
CIN >> UN >> VN >> k;
Memset (g, false, sizeof (g));
for (i = 0; i < K; i + +)
{
CIN >> TU >> TV;
G[TU][TV] = true;
}
int M = Maxmatch ();
cout << "Total Match:" << M << Endl;
for (i = 0; i < MAXN; i + +)
if (xm[i]! =-1)
cout << i << ' << xm[i] << Endl;
System ("pause");

return 0;
}

/**/ /* **********
Test data:
3 3 3
1 1
1 0
2 2
********** */

Using Hungarian algorithm to find the maximum matching of two-fractal graphs

Related Keywords:

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

## A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

• #### Sales Support

1 on 1 presale consultation

• #### After-Sales Support

24/7 Technical Support 6 Free Tickets per Quarter Faster Response

• Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.