Graph Theory - entry-level bipartite graph maximum matching Bipartite Matching

1. Problem Description

 Let's take a look at the concepts related to graph theory:

  • The bipartite graph : also known bipartite graph. Graph theory is a special model. Setting G = (V, E) is an undirected graph, if the set of nodes V can be partitioned into two disjoint subsets (V1, V2), and FIG each edge (i, j), the associated with two nodes i and j belong to the two different sets of nodes, called G is a bipartite graph.
  • Match : In graph theory, a matching (matching) is a collection of edges, wherein the edges are not common to any two nodes. As shown in FIG Even a match:

  • Maximum matching (matching maximum) : all matching a figure, up to match the number of edges is called the maximum matching of FIG.

Seeking bipartite graph maximum matching can be used a maximum flow (Maximal Flow) or Hungarian algorithm (Hungarian Algorithm), but let's do this two ways to solve the bipartite graph maximum matching, but realized with a simple dfs:

Now there is such an example:

A classmates and partners to go to the playground to play, ready to ride the roller coaster. There are roller coaster of a row of two seats, sure, every girl must sit together with a boy. However, everyone wants to know themselves and sit together. For example, No. 1 and No. 1 boys know the girls, so they want to sit together. In addition No. 1 and No. 2 boys and girls also know each other, so they can sit together. There are relationships like this: the boys know the girls No. 2 No. 2 and No. 3, No. 1 No. 3 boys know boys. May I ask how seating arrangements satisfactory to make the most of it? This is the biggest match typical simple bipartite graph.

2. Algorithm Design 

  1.  First, from any point of a non-matching begins u, u is from the point to any selected one side edge (assuming this is the u-> v) to start pairing. At this point, if v has not been paired, the pairing is successful, this time they found a augmenting path (just this augmenting path is relatively simple). V If this time has been paired, it would have to try a "chain reaction." If the attempt is successful, then find an augmenting path, this time to update the original pairing relationship. Here we use an array to record the match pairing relationship, such as point to point u v paired, relief do match [v] = u. After successful pairing, remember that the number of paired +1. We pairing process can be achieved by depth-first search.
  2. If the selected pair of side just failed point u from the edge of the re-election of one side, to try until u pairing results, or tried so far all sides point u.
  3. Then continue on the remaining point is not paired one by one pair, until all points are trying to complete, can not be found until the new augmenting paths.
  4. Out pairing number.

3. Source Code 

 

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
using namespace std;
const int N=111;
int e[N][N];//存储边
int matched[N];//判断是否已经匹配
int marked[N]; //判断是否已经访问
int n;
int m;

int dfs(int u)
{
    for(int i=1;i<=n;i++)
    {
        if(marked[i]==0 && e[u][i]==1) //没有被访问过且可达
        {
            marked[i]=1;
            if(matched[i]==0 || dfs(matched[i])) //如果没有配对,将i与u配对。
                                                                //用dfs遍历一编i,看有没有配对。如果没有配对,返回0
            {
                matched[i]=u;
                return 1;//配对成功返回1.不用进行dfs
            }
        }
    }
    return 0;
}

int main()
{
    int edge1;
    int edge2;
    int sum=0;
    cout << "请输入n个顶点m条边" << endl;
    cin >> n >> m;
    cout << "请输入所有可进行配对的顶点: "<< endl;
    for (int i=1;i<=m;i++)
    {
        cin >> edge1 >> edge2;
        e[edge1][edge2]=1;//说明这两个顶点之间存在边。
    }
    //初始化匹配数组。
    for (int i=1;i<=n;i++)
    {
        matched[i]=0;
    }
    for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=n;j++)
        {
            marked[j]=0; //清空上次搜索访问过的记录
        }
        if(dfs(i))
        {
            sum++;
        }
    }
    cout << "最大匹配数是:"<< endl;
    cout << sum << endl;
    return 0;
}

 

4. Test results 

 

Published 57 original articles · won praise 9 · views 3615

Guess you like

Origin blog.csdn.net/Jayphone17/article/details/102859956