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
(3) Repeat (2) operation until the augmented path is not found
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