On the bipartite graph matching

sequence

I think I can now

text

In this paper some notes as the right to learn bipartite graph matching personal style is very serious, do not go into the language

Hungary

This article does not describe the steps of the algorithm, because you can google (escape and writing are some of their summary

The main function of the algorithm that can be done in Hungary maximum matching of bipartite graph. It is looking for augmenting paths alternate algorithm, its essence is countercurrent to modify (Singercoder say)

Its core is re This array will not go back (of course, every time you want clear)

Why, in fact, I think the current arc to optimize current a little contact. Think about it, if he's back, then, is not a polynomial algorithm (laughs). But the algorithm correctness is there. re indicated whether this node is looked for augmenting paths.

Theoretically worst time complexity will be to \ (O (VE) \) okay. General card can not.

Pseudo-code algorithm

main()
{
	last...
	for 1 to n
		fill re with 0
		/* 每一次都要清空 re */
		if find(i) ans++;
	next...
}
find(u)
{
	for any edge of u
		v = e.v;
		/* 与暴力算法的区别,和当前弧异曲同工吧 */
		if re[v]
			continue
		/* 打上标记 */
		re[v] = 1
		if p[v] == NULL or find(p[v])
			/* 逻辑要清晰 */
			p[v] = u
			return true
	return false
}

Writing is not very good, but it was clear it. After all, the ability to realize the code is also very important (false).

Then this operation, it is possible to obtain maximum matching in a bipartite graph. ヾ (≧ ∇ ≦ *) ゝ

Of course, it comes naturally have a bipartite graph matching Singercoder coming out, you Hungary \ (O (nm) \) , I ran maximum flow is \ (O (\ sqrt nm) \) (no strict proof, listen dalao say), the objective that, in fact, each has its advantages.

  1. Running time: almost equal, because there is no cancer can put the card into the Hungarian \ (O (nm) \) , and constant ISAP is certainly larger than Hungary Yeah.
  2. Code difficult to achieve: Hungary after blasting all kinds of maximum flow
  3. Finished sense of accomplishment: Maximum flow completely Explosion Hungary
  4. Run Space: will this card?

Real code

bool find(int u) {
  for (int i = h[u]; i != -1; i = edge[i].lac)
  {
    int to = edge[i].to;
    if (re[to]) continue;
    re[to] = 1;
    if (!p[to] || find(p[to])) { p[to] = u; return 1; }
  }
  return 0;
}

Then we can use it to carry out his various bipartite graph associated with something.

Below are a few other extensions

  1. Minimum Vertex Cover
  2. Minimum side coverage
  3. The largest independent subset
  4. Minimum coverage disjoint paths

Minimum Vertex Cover

Evidence to the contrary, and he should be the maximum number of matches, because if there are not side cover. Then he did about the endpoint should be selected, so it is not the biggest match of contradictions

So: = Minimum Vertex Cover largest number of matched

Template: poj1325 (pit)

Minimum side coverage

So thinking, greedy think, first of all matching edges election, and then for the rest of the point pick a side

Thus: Minimum = Points edge covering - the maximum number of matches

Template: poj3020

The largest independent subset

Understood as a point to delete all sides and connected to it, then the rest point is independent subset. By deleting the minimum point can be covered,

Accordingly = maximum independent subset of points - Minimum Vertex Cover

Template: poj1466 (said to have data but do not fall into the pit ... But still very pit

Minimum coverage disjoint paths

ps: I realize this is generally because luogu network flow .. template to output path ...

Think so, we have the beginning of n disjoint paths (n points), and then to carry out even the edge, even as many of the side, we can reduce the number of paths ...

Then, after a point before point into bipartite graph matching point can be made.

= Minimum number of paths of points - the maximum number of matches

Template: poj1422

KM algorithm

KM algorithm can be solved under the maximum weight matching perfect match, personally think that Hungary had some operations)

Perfect match

Perfect match is required to match the number n

Written say: all points of the so-called perfect match is in two figures, x has a corresponding point set match and concentrate all points y point has a corresponding match is said to match the perfect match.

Analysis of algorithm correctness

The conversion algorithm for the sake of completeness the problem of matching problems by a label to each vertex (called the top mark) to the maximum weight matching requirements. Provided vertex \ (XI \) top mark is \ (LX [I] \) , the vertex \ (y_j \) top mark is \ (LY [J] \) , the edge weight is the \ (mp [i] [j ] \) . At any one time during the execution of the algorithm, for either an Edge \ (<I, J> \) have \ (lx [i] + ly [j] \ ge mp [i] [j] \) is always established.

Given a Theorem: If a bipartite graph all true \ (lx [i] + ly [j] = mp [i] [j] \) edge \ (<i, j> \ ) subgraph configuration (called do equal subgraph) there is a complete match, then this is the perfect match for the maximum weight bipartite graph matching.

This is apparently because of his other options to match the weight of each side must satisfy \ (mp [i] [j ] \ le lx [i] + ly [j] \) to ensure that there is maximum, only It is equal to the selected subgraph

Which is the best answer \ (\ sum lx [i] + ly [i] \) so

Initially in order to make \ (lx [i] + ly [j] \ ge mp [i] [j] \) constant established, so that \ (lx [i] \) for all the vertex \ (x_i \) edges associated the maximum weight, \ (LY [J] = 0 \) . If the current is not equal to the complete subgraph match, the following manner so that the enlarged modifications topmark equal subgraph, having up until complete subgraph equal match.

We currently seek a complete subgraph equal match failed, because for a \ (x \) vertex, we could not find a way staggered departure from it. Then we obtained a staggered tree, its leaf nodes are all \ (x \) vertices. We staggered tree \ (x \) top mark all the vertices of a reduced value \ (d \) , \ (the y-\) top mark the apex of all increase with a value \ (d \) , then we will find :

1) ends in a staggered edge tree \ (<I, J> \) \ (LX LY + \) (hereinafter referred to as xy) value does not change. That is, it originally belonged to equal sub-graph, which is still equal to the sub-map.

2) not in the staggered ends tree edge \ (<I, J> \) , \ (XY \) are not changed. That is, it originally belonging (or not belonging) equal sub-graph, which is still (or are not) equal to the sub-map.

. 3) \ (X \) end of the tree is not interleaved, \ (Y \) end side in a staggered tree \ (<I, J> \) , its \ (XY \) value has increased. It does not belong to equal the original sub-graph, chart still belongs to the sub-equal.

. 4) \ (X \) ends in staggered tree, \ (Y \) end edge is not interleaved tree \ (<I, J> \) , its \ (XY \) values decreased. It is not equal to the original sub-picture, you may enter the subgraph equal, thereby to equalize been expanded subgraph.

5) to the last, \ (X \) end point of each at least one line attached, \ (Y \) ends with a line attached at each point describing the final finished supplement must have equal subgraph perfect matching.

Therefore, the maximum weight the sub-sub-figure is larger step by step

In order to meet the nature and apparently has: \ (D = \ min (LX [I] + LY [J] -mp [I] [J]) \) where $ $ x_i alternately tree, \ (y_i \) is not .

Why? We want to expand equal subgraph.

For \ (D \) Solving alternately be done in solving the tree

Code

bool find(int u)
{
	visx[u] = 1;
	for(int i = 1; i <= n; ++i)
	{
		if(visy[i]) continue;
		if(lx[u] + ly[i] == mp[u][i])
		{
			visy[i] = 1;
			if(!p[i] || find(p[i]))
			{
				p[i] = u;
				return 1;
			}
		}
		else d = min(d, lx[u] + ly[i] - mp[u][i]);
        /* 在这里计算d */
	}
	return 0;
}
/* 当然了,如果可以匹配成功,那就不用d了 */

main:

	for(int k = 1; k <= n; ++k)
	{
		while(1)
		{
			memset(visx, 0, sizeof visx);
			memset(visy, 0, sizeof visy);
			d = 1 << 30;
			if(find(k)) break;
			for(int i = 1; i <= n; ++i)
			{
				if(visx[i]) lx[i] -= d;
				if(visy[i]) ly[i] += d;
			}
		}
	}

Simple mouth Hu next time complexity:

Cycle \ (n \) times each to find augmented modify \ (n \) times the top mark with \ (n \) times count \ (d \) so as \ (O (n ^ 3) \) But how will run to ...

lofty

I do not have a good talent, I'm just more than the next point effort

Proof of time complexity, it seems algorithm in a master theorem to something like it. Next to learn, a day school to the liver, may be late to write

Guess you like

Origin www.cnblogs.com/zhltao/p/12549489.html