二分图最大匹配总结【转自kb神】

转载来源:http://www.cnblogs.com/kuangbin/archive/2012/08/26/2657446.html

无限膜拜kb神啊!!!

二分图匹配(匈牙利算法)

1。一个二分图中的最大匹配数等于这个图中的最小点覆盖数

König定理是一个二分图中很重要的定理,它的意思是,一个二分图中的最大匹配数等于这个图中的最小点覆盖数。如果你还不知道什么是最小点覆盖,我也在这里说一下:假如选了一个点就相当于覆盖了以它为端点的所有边,你需要选择最少的点来覆盖所有的边。

 

2。最小路径覆盖=最小路径覆盖=|G|-最大匹配数

 在一个N*N的有向图中,路径覆盖就是在图中找一些路经,使之覆盖了图中的所有顶点,
 且任何一个顶点有且只有一条路径与之关联;(如果把这些路径中的每条路径从它的起始点走到它的终点,
 那么恰好可以经过图中的每个顶点一次且仅一次);如果不考虑图中存在回路,那么每每条路径就是一个弱连通子集.

由上面可以得出:

 1.一个单独的顶点是一条路径;
 2.如果存在一路径p1,p2,......pk,其中p1 为起点,pk为终点,那么在覆盖图中,顶点p1,p2,......pk不再与其它的
   顶点之间存在有向边.

最小路径覆盖就是找出最小的路径条数,使之成为G的一个路径覆盖.

 路径覆盖与二分图匹配的关系:最小路径覆盖=|G|-最大匹配数;

3。二分图最大独立集=顶点数-二分图最大匹配

独立集:图中任意两个顶点都不相连的顶点集合。


二分图模板:

 

模板一:匈牙利算法

 

/* **************************************************************************
//二分图匹配(匈牙利算法的DFS实现)
//初始化:g[][]两边顶点的划分情况
//建立g[i][j]表示i->j的有向边就可以了,是左边向右边的匹配
//g没有边相连则初始化为0
//uN是匹配左边的顶点数,vN是匹配右边的顶点数
//调用:res=hungary();输出最大匹配数
//优点:适用于稠密图,DFS找增广路,实现简洁易于理解
//时间复杂度:O(VE)
//***************************************************************************/
//顶点编号从0开始的
const int MAXN=510;
int uN,vN;//u,v数目
int g[MAXN][MAXN];
int linker[MAXN];
bool used[MAXN];
bool dfs(int u)//从左边开始找增广路径
{
    int v;
    for(v=0;v<vN;v++)//这个顶点编号从0开始,若要从1开始需要修改
      if(g[u][v]&&!used[v])
      {
          used[v]=true;
          if(linker[v]==-1||dfs(linker[v]))
          {//找增广路,反向
              linker[v]=u;
              return true;
          }
      }
    return false;//这个不要忘了,经常忘记这句
}
int hungary()
{
    int res=0;
    int u;
    memset(linker,-1,sizeof(linker));
    for(u=0;u<uN;u++)
    {
        memset(used,0,sizeof(used));
        if(dfs(u)) res++;
    }
    return res;
}
//******************************************************************************/


模板二: Hopcroft-Carp算法

这个算法比匈牙利算法的时间复杂度要小,大数据可以采用这个算法

/* *********************************************
二分图匹配(Hopcroft-Carp的算法)。
初始化:g[][]邻接矩阵
调用:res=MaxMatch();  Nx,Ny要初始化!!!
时间复杂大为 O(V^0.5 E)
 
适用于数据较大的二分匹配
需要queue头文件
********************************************** */
const int MAXN=3000;
const int INF=1<<28;
int g[MAXN][MAXN],Mx[MAXN],My[MAXN],Nx,Ny;
int dx[MAXN],dy[MAXN],dis;
bool vst[MAXN];
bool searchP()
{
    queue<int>Q;
    dis=INF;
    memset(dx,-1,sizeof(dx));
    memset(dy,-1,sizeof(dy));
    for(int i=0;i<Nx;i++)
        if(Mx[i]==-1)
        {
            Q.push(i);
            dx[i]=0;
        }
    while(!Q.empty())
    {
        int u=Q.front();
        Q.pop();
        if(dx[u]>dis)  break;
        for(int v=0;v<Ny;v++)
            if(g[u][v]&&dy[v]==-1)
            {
                dy[v]=dx[u]+1;
                if(My[v]==-1)  dis=dy[v];
                else
                {
                    dx[My[v]]=dy[v]+1;
                    Q.push(My[v]);
                }
            }
    }
    return dis!=INF;
}
bool DFS(int u)
{
    for(int v=0;v<Ny;v++)
       if(!vst[v]&&g[u][v]&&dy[v]==dx[u]+1)
       {
           vst[v]=1;
           if(My[v]!=-1&&dy[v]==dis) continue;
           if(My[v]==-1||DFS(My[v]))
           {
               My[v]=u;
               Mx[u]=v;
               return 1;
           }
       }
    return 0;
}
int MaxMatch()
{
    int res=0;
    memset(Mx,-1,sizeof(Mx));
    memset(My,-1,sizeof(My));
    while(searchP())
    {
        memset(vst,0,sizeof(vst));
        for(int i=0;i<Nx;i++)
          if(Mx[i]==-1&&DFS(i))  res++;
    }
    return res;
}
//**************************************************************************/

下面的程序效率很高。是用vector实现邻接表的匈牙利算法。

处理点比较多的效率很高。1500的点都没有问题


/*

HDU 1054

用STL中的vector建立邻接表实现匈牙利算法

效率比较高

 

 G++  578ms  580K

*/

#include<stdio.h>

#include<iostream>

#include<algorithm>

#include<string.h>

#include<vector>

using namespace std;

 

//************************************************

const int MAXN=1505;//这个值要超过两边个数的较大者,因为有linker

int linker[MAXN];

bool used[MAXN];

vector<int>map[MAXN];

int uN;

bool dfs(int u)

{

    for(int i=0;i<map[u].size();i++)

    {

        if(!used[map[u][i]])

        {

            used[map[u][i]]=true;

            if(linker[map[u][i]]==-1||dfs(linker[map[u][i]]))

            {

                linker[map[u][i]]=u;

                return true;

            }

        }

    }

    return false;

}

inthungary()

{

    int u;

    int res=0;

    memset(linker,-1,sizeof(linker));

    for(u=0;u<uN;u++)

    {

        memset(used,false,sizeof(used));

        if(dfs(u)) res++;

    }

    return res;

}

//*****************************************************

int main()

{

    int u,k,v;

    int n;

    while(scanf("%d",&n)!=EOF)

    {

        for(int i=0;i<MAXN;i++)

           map[i].clear();

        for(int i=0;i<n;i++)

        {

            scanf("%d:(%d)",&u,&k);

            while(k--)

            {

                scanf("%d",&v);

                map[u].push_back(v);

                map[v].push_back(u);

            }

        }

        uN=n;

        printf("%d\n",hungary()/2);

    }

    return 0;

}

例题:

POJ 3020  Antenna Placement

Time Limit: 1000MS

 

Memory Limit: 65536K

Total Submissions: 4739

 

Accepted: 2359

Description

The Global Aerial Research Centre has been allotted the task of building the fifth generation of mobile phone nets in Sweden. The most striking reason why they got the job, is their discovery of a new, highly noise resistant, antenna. It is called 4DAir, and comes in four types. Each type can only transmit and receive signals in a direction aligned with a (slightly skewed) latitudinal and longitudinal grid, because of the interacting electromagnetic field of the earth. The four types correspond to antennas operating in the directions north, west, south, and east, respectively. Below is an example picture of places of interest, depicted by twelve small rings, and nine 4DAir antennas depicted by ellipses covering them. 

Obviously, it is desirable to use as few antennas as possible, but still provide coverage for each place of interest. We model the problem as follows: Let A be a rectangular matrix describing the surface of Sweden, where an entry of A either is a point of interest, which must be covered by at least one antenna, or empty space. Antennas can only be positioned at an entry in A. When an antenna is placed at row r and column c, this entry is considered covered, but also one of the neighbouring entries (c+1,r),(c,r+1),(c-1,r), or (c,r-1), is covered depending on the type chosen for this particular antenna. What is the least number of antennas for which there exists a placement in A such that all points of interest are covered?

Input

On the first row of input is a single positive integer n, specifying the number of scenarios that follow. Each scenario begins with a row containing two positive integers h and w, with 1 <= h <= 40 and 0 < w <= 10. Thereafter is a matrix presented, describing the points of interest in Sweden in the form of h lines, each containing w characters from the set ['*','o']. A '*'-character symbolises a point of interest, whereas a 'o'-character represents open space.

Output

For each scenario, output the minimum number of antennas necessary to cover all '*'-entries in the scenario's matrix, on a row of its own.

Sample Input

2

7 9

ooo**oooo

**oo*ooo*

o*oo**o**

ooooooooo

*******oo

o*o*oo*oo

*******oo

10 1

*

*

*

o

*

*

*

*

*

*

Sample Output

17

5


#include<stdio.h>

#include<algorithm>

#include<string.h>

#include<iostream>

using namespace std;

 

 

/* **************************************************************************

//二分图匹配(匈牙利算法的DFS实现)

//初始化:g[][]两边顶点的划分情况

//建立g[i][j]表示i->j的有向边就可以了,是左边向右边的匹配

//g没有边相连则初始化为0

//uN是匹配左边的顶点数,vN是匹配右边的顶点数

//调用:res=hungary();输出最大匹配数

//优点:适用于稠密图,DFS找增广路,实现简洁易于理解

//时间复杂度:O(VE)

//***************************************************************************/

//顶点编号从0开始的

const int MAXN=510;

int uN,vN;//u,v数目

int g[MAXN][MAXN];

int linker[MAXN];

bool used[MAXN];

bool dfs(int u)//从左边开始找增广路径

{

    int v;

    for(v=0;v<vN;v++)//这个顶点编号从0开始,若要从1开始需要修改

      if(g[u][v]&&!used[v])

      {

          used[v]=true;

          if(linker[v]==-1||dfs(linker[v]))

          {//找增广路,反向

              linker[v]=u;

              return true;

          }

      }

    return false;//这个不要忘了,经常忘记这句

}

int hungary()

{

    int res=0;

    int u;

    memset(linker,-1,sizeof(linker));

    for(u=0;u<uN;u++)

    {

        memset(used,0,sizeof(used));

        if(dfs(u)) res++;

    }

    return res;

}

//******************************************************************************/

 

char map[50][50];

int hash[50][50];

int main()

{

    int T;

    int h,w;

    scanf("%d",&T);

    int tol;

    while(T--)

    {

        scanf("%d%d",&h,&w);

        tol=0;

        for(int i=0;i<h;i++)

        {

            scanf("%s",&map[i]);

            for(int j=0;j<w;j++)

             if(map[i][j]=='*')

               hash[i][j]=tol++;

        }

        memset(g,0,sizeof(g));

        for(int i=0;i<h;i++)

          for(int j=0;j<w;j++)

            if(map[i][j]=='*')

            {

                if(i>0&&map[i-1][j]=='*')g[hash[i][j]][hash[i-1][j]]=1;

                if(i<h-1&&map[i+1][j]=='*') g[hash[i][j]][hash[i+1][j]]=1;

                if(j>0&&map[i][j-1]=='*')  g[hash[i][j]][hash[i][j-1]]=1;

                if(j<w-1&&map[i][j+1]=='*')  g[hash[i][j]][hash[i][j+1]]=1;

            }

        uN=vN=tol;

        printf("%d\n",tol-hungary()/2);

    }

    return 0;

}

Treasure Exploration

Time Limit: 6000MS

 

Memory Limit: 65536K

Total Submissions: 5480

 

Accepted: 2154

Description

Have you ever read any book about treasure exploration? Have you ever see any film about treasure exploration? Have you ever explored treasure? If you never have such experiences, you would never know what fun treasure exploring brings to you. 
Recently, a company named EUC (Exploring the Unknown Company) plan to explore an unknown place on Mars, which is considered full of treasure. For fast development of technology and bad environment for human beings, EUC sends some robots to explore the treasure. 
To make it easy, we use a graph, which is formed by N points (these N points are numbered from 1 to N), to represent the places to be explored. And some points are connected by one-way road, which means that, through the road, a robot can only move from one end to the other end, but cannot move back. For some unknown reasons, there is no circle in this graph. The robots can be sent to any point from Earth by rockets. After landing, the robot can visit some points through the roads, and it can choose some points, which are on its roads, to explore. You should notice that the roads of two different robots may contain some same point. 
For financial reason, EUC wants to use minimal number of robots to explore all the points on Mars. 
As an ICPCer, who has excellent programming skill, can your help EUC?

Input

The input will consist of several test cases. For each test case, two integers N (1 <= N <= 500) and M (0 <= M <= 5000) are given in the first line, indicating the number of points and the number of one-way roads in the graph respectively. Each of the following M lines contains two different integers A and B, indicating there is a one-way from A to B (0 < A, B <= N). The input is terminated by a single line with two zeros.

Output

For each test of the input, print a line containing the least robots needed.

Sample Input

1 0

2 1

1 2

2 0

0 0

Sample Output

1

1

2

此题跟普通的路径覆盖有点不同。

You should notice that the roads of two different robots may contain some same point.

也就是不同的路径可以有重复点。

先用floyed求闭包,就是把间接相连的点也连起来。

 

/*

POJ 2594

求最大独立集=顶点数-最大匹配数

*/

#include<stdio.h>

#include<string.h>

#include<algorithm>

#include<iostream>

using namespace std;

/* **************************************************************************

//二分图匹配(匈牙利算法的DFS实现)

//初始化:g[][]两边顶点的划分情况

//建立g[i][j]表示i->j的有向边就可以了,是左边向右边的匹配

//g没有边相连则初始化为0

//uN是匹配左边的顶点数,vN是匹配右边的顶点数

//调用:res=hungary();输出最大匹配数

//优点:适用于稠密图,DFS找增广路,实现简洁易于理解

//时间复杂度:O(VE)

//***************************************************************************/

//顶点编号从0开始的

const int MAXN=510;

int uN,vN;//u,v数目

int g[MAXN][MAXN];

int linker[MAXN];

bool used[MAXN];

bool dfs(int u)//从左边开始找增广路径

{

    int v;

    for(v=0;v<vN;v++)//这个顶点编号从0开始,若要从1开始需要修改

      if(g[u][v]&&!used[v])

      {

          used[v]=true;

          if(linker[v]==-1||dfs(linker[v]))

          {//找增广路,反向

              linker[v]=u;

              return true;

          }

      }

    return false;//这个不要忘了,经常忘记这句

}

int hungary()

{

    int res=0;

    int u;

    memset(linker,-1,sizeof(linker));

    for(u=0;u<uN;u++)

    {

        memset(used,0,sizeof(used));

        if(dfs(u)) res++;

    }

    return res;

}

//******************************************************************************/

 

void floyed(int n)//求传递闭包

{

    for(int i=0;i<n;i++)

      for(int j=0;j<n;j++)

      {

          if(g[i][j]==0)

          {

              for(int k=0;k<n;k++)

              {

                  if(g[i][k]==1&&g[k][j]==1)

                  {

                      g[i][j]=1;

                      break;

                  }

              }

          }

      }

}

 

int main()

{

    int n,m;

    int u,v;

    while(scanf("%d%d",&n,&m))

    {

        if(n==0&&m==0)break;

        uN=vN=n;

        memset(g,0,sizeof(g));

        while(m--)

        {

            scanf("%d%d",&u,&v);

            u--;v--;

            g[u][v]=1;

        }

        floyed(n);

        printf("%d\n",n-hungary());

    }

    return 0;

}



你可能感兴趣的:(二分图最大匹配总结【转自kb神】)