Graph Theory 3 bipartite graph matching, graph theory Bipartite Graph Matching

Source: Internet
Author: User

Graph Theory 3 bipartite graph matching, graph theory Bipartite Graph Matching

You can learn http://www.renfei.org/blog/bipartite-matching.html here first

Template

According to the above blog, the bipartite graph matching can be divided into four types

Maximum number of matches: Maximum number of matched Edges

Minimum vertex coverage: Select the least vertex so that at least one endpoint of any edge is selected.

Maximum number of Independent Tasks: Select the most vertices so that any two points selected are not connected.

Minimum path overwrite count: For a DAG (directed acyclic graph), select at least one path so that each vertex belongs to and belongs to only one path. The path length can be 0 (that is, a single point ).

Theorem 1: Maximum number of matching = least vertex overwrite (this is the Konig theorem)

Theorem 2: maximum matching number = Maximum Independent Number

Theorem 3: Minimum path overwrite = vertex count-maximum matching count

1. Maximum number of matches

Maximum number of matched Edges

Luogop3386 [TEMPLATE] Bipartite Graph Matching

P3386 [TEMPLATE] improved the difficulty of Bipartite Graph Matching +/province selection-Question Background bipartite graph topic description given a bipartite graph, where the number of nodes is n, m, and the number of edges is e, returns the input/output format of the maximum number of matches in a bipartite graph. The input format is the first line, n, m, e, and the second to e + 1. Each line has two positive integers u, v, indicating u, v has a concatenation output format: one row in total, the maximum matching input and output sample of the bipartite graph input sample #1 11 1 output sample # description n, m <=, 1 <= u <= n, 1 <= v <= m due to Data Pitfalls, you may encounter v> m. Please filter out v> m data. Algorithm: Bipartite Graph MatchingDescription # include <iostream> # include <cstdio> # include <cstring> # define maxm 100010 # define maxn 1010 using namespace std; int n, m, E, num, head [maxm], link [maxn], vis [maxn], sum; struct node {int to, pre;} e [maxm]; void Insert (int from, int) {e [++ num]. to = to; e [num]. pre = head [from]; head [from] = num;} int dfs (int x) {for (int I = head [x]; I; I = e [I]. pre) {int v = e [I]. to; vis [v] = 1; if (link [v] = 0 | dfs (link [v]) {link [v] = x; return 1 ;}} return 0 ;}int main () {scanf ("% d", & n, & m, & E); int x, y; for (int I = 1; I <= E; I ++) {scanf ("% d", & x, & y); Insert (x, y + n) ;}for (int I = 1; I <= n; I ++) {memset (vis, 0, sizeof (vis )); if (dfs (I) sum ++;} printf ("% d", sum );}Edge table RE + MLE # include <iostream> # include <cstdio> # include <cstring> # define maxn 1010 using namespace std; int n, m, e, link [maxn], re [maxn] [maxn], vis [maxn], ans; int dfs (int x) {for (int I = 1; I <= m; I ++) if (vis [I] = 0 & re [x] [I]) {vis [I] = 1; if (link [I] = 0 | dfs (link [I]) {link [I] = x; return 1 ;}} return 0 ;} int main () {scanf ("% d", & n, & m, & e); int x, y; for (int I = 1; I <= e; I ++) {scanf ("% d", & x, & y); re [x] [y] = 1 ;}for (int I = 1; I <= n; I ++) {memset (vis, 0, sizeof (vis); if (dfs (I) ans ++ ;} printf ("% d", ans );}Adjacent matrix AC2. least vertex coverage

Select the least vertex so that at least one endpoint of any edge is selected.

There is a theorem that when we determine that a question can be replaced by a minimum point, We will directly use the code for finding the maximum number of matches.

Poj3041Asteroids

Follow the same rule to keep up with one question

Question: Give A n * n matrix and m points on the matrix. Ask how many rows or columns have been deleted at least, and then all the points will disappear. (Lenovo: give n intersection vertices (xi, yi) of m edge in a graph. If you want to use a few vertices at least, you can associate them with all edges.) idea: minimum coverage of the Hungarian algorithm: The minimum coverage requires that the minimum number of points (x or y sets) be used in a bipartite graph ), associate the edges of each connected two point sets with at least one of them. According to the konig theorem, the minimum vertex overwrite number of a bipartite graph is equal to the maximum number of matches. To understand this, convert (x, y) to an edge of x_y, and convert this side of x = a to (, the rest is the basic Hungarian algorithm implementation.Description # include <iostream> # include <cstring> # include <cstdio> using namespace std; # define maxn 501 # define maxm 10010int n, k, num, head [maxm], link [maxn], vis [maxn]; struct node {int to, pre;} e [maxm]; void Insert (int from, int) {e [++ num]. to = to; e [num]. pre = head [from]; head [from] = num;} int dfs (int x) {for (int I = head [x]; I; I = e [I]. pre) {int v = e [I]. to; if (vis [v] = 0) {vis [v] = 1; if (link [v] = 0 | dfs (link [v]) {link [v] = x; return 1 ;}} return 0 ;}int main () {scanf ("% d", & n, & k ); int x, y; for (int I = 1; I <= k; I ++) {scanf ("% d", & x, & y ); insert (x, y);} int ans = 0; for (int I = 1; I <= n; I ++) {memset (vis, 0, sizeof (vis); if (dfs (I) ans ++;} printf ("% d", ans );}Edge table AC # include <iostream> # include <cstdio> # include <cstring> # define maxn 1010 using namespace std; int n, m, e, link [maxn], re [maxn] [maxn], vis [maxn], ans; int dfs (int x) {for (int I = 1; I <= m; I ++) if (vis [I] = 0 & re [x] [I]) {vis [I] = 1; if (link [I] = 0 | dfs (link [I]) {link [I] = x; return 1 ;}} return 0 ;} int main () {scanf ("% d", & n, & e); m = n; int x, y; for (int I = 1; I <= e; I ++) {scanf ("% d", & x, & y); re [x] [y] = 1 ;}for (int I = 1; I <= n; I ++) {memset (vis, 0, sizeof (vis); if (dfs (I) ans ++ ;} printf ("% d", ans );}Maximum number of independent numbers in the adjacent matrix AC3.

Select the most vertices so that any two points are not connected

Poj 1466 Girls and Boys

Because the largest independent set in the bipartite graph does not provide specific boys and girls, you can double the data, that is, n boys and n girls,
According to the theorem, the maximum independent set = total-Number of matches (this question should be divided by 2)
Provides a series of willingness to match men and women. Calculates the maximum number of people in a set. Matching is not supported. Sample Input70: (3) 4 5 61: (2) 4 62: (0) 3: (0) 4: (2) 0 15: (1) 06: (2) 0 130: (2) 1 21: (1) 02: (1) 0 Sample Output52Description # include <iostream> # include <cstdio> # include <cstring> # define maxn 510 using namespace std; int link [maxn], vis [maxn], map [maxn] [maxn], n; int dfs (int x) {for (int I = 1; I <= n; I ++) {if (vis [I] = 0 & map [x] [I]) {vis [I] = 1; if (link [I] = 0 | dfs (link [I]) {link [I] = x; return 1 ;}} return 0 ;} int main () {freopen ("1.txt"," r ", stdin); while (scanf (" % d ", & n )! = EOF) {memset (map, 0, sizeof (map); memset (link, 0, sizeof (link); for (int I = 1; I <= n; I ++) {int u, w, v; scanf ("% d: (% d)", & u, & w); u ++; for (int j = 1; j <= w; j ++) {scanf ("% d", & v); v ++; map [u] [v] = map [v] [u] = 1 ;}} int ans = 0; for (int I = 1; I <= n; I ++) {memset (vis, 0, sizeof (vis); if (dfs (I) ans ++;} printf ("% d \ n ", n-ans/2 );}}AC4. minimum path overwrite count

For a DAG (directed acyclic graph), select at least one path so that each vertex belongs to and belongs to only one path. The path length can be 0 (that is, a single point ).

 

Contact Us

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.