Introduction to Bipartite Graph

Source: Internet
Author: User

Determines whether an image is a bipartite graph.

The bipartite graph is a graph with two vertex sets and the two vertices of each edge in the graph are located in two vertex sets, and each vertex set has no EDGE connection!

Common method for determining a bipartite graph: Start to dye any unstained vertex, and then judge the adjacent vertex. If not, dye it in a different color than the adjacent vertex, if the color is colored and the color of the adjacent vertex is the same, it indicates that it is not a bipartite graph. If the color is different, continue to judge,
You can use BFS/DFS to traverse each time.

The maximum matching algorithm of the Bipartite Graph is as follows:

The idea of the algorithm is to constantly find the augmented rail and increase the number of matching. The augmented rail, as the name suggests, refers to a path that can increase the number of matching. In the matching problem, the augmented orbit is represented by an "staggered track". That is to say, this path composed of the edges of the graph has not been matched yet, the second side is involved in the matching, and the third side does not .. the last edge is not involved in the matching, and the start point and end point have not been selected. in this case, it is clear that he has an odd number of edges. for such a path, we can change the first edge to matched, and the second edge to unmatched... and so on. that is to say, all edges are "Reversed". It is easy to find that after this modification, the matching is still valid, but the matching number is added with a pair. in addition, an independent edge connecting two unmatched points is obviously also staggered. it can be proved that when the augmented orbit cannot be found, a maximum matching is obtained. this is the idea of the Hungarian algorithm.

// Hungarian algorithm complexity O (Nm) # include <iostream> using namespace STD; const int maxn = 1001, maxm = 1001; int N1, N2, M, ans; // N1 and N2 are the number of nodes on both sides of the bipartite graph, and 1 .. n1, 1 .. n2 No., M is the number of edges bool G [maxn] [maxm]; // graph G adjacent matrix G [x] [Y] bool y [maxm]; // The point I access Mark int link [maxm] in the Y collection; // link [y] indicates the void Init () {int X, Y OF THE x node adjacent to the Y node; memset (G, 0, sizeof (g); memset (link,-1, sizeof (Link); ans = 0; scanf ("% d ", & N1, & N2, & M); For (INT I = 1; I <= m; I ++ ){ Scanf ("% d", & X, & Y); G [x] [Y] = true ;}} bool find (int x) // whether the augmented path {for (INT I = 1; I <= n2; I ++) Starting with node X in the X set exists) if (G [x] [I] &! Y [I]) // If node I is adjacent to X and has not accessed {Y [I] = true; if (link [I] =-1 | find (link [I]) // If an uncovered vertex I is found or an extended path exists from the node adjacent to I {link [I] = x; return true ;}} return false ;} int main () {Init ();/* For (Int J = 1; j <= n2; j ++) for (INT I = 1; I <= N1; I ++) if (G [I] [J] &! Link [J]) link [J] = I; // greedy initial solution optimization */For (INT I = 1; I <= N1; I ++) {memset (Y, 0, sizeof (y); If (find (I) ans ++;} printf ("% d/N", ANS ); return 0 ;}< span> // BFS determines whether it is a binary chart </span> // 0 is white, 1 is black bool BFS (int s, int N) {queue <int> P; p. push (s); Col [s] = 1; while (! P. empty () {int from = P. front (); p. pop (); For (INT I = 1; I <= N; I ++) {If (G [from] [I] & Col [I] =-1) {P. push (I); Col [I] =! Col [from]; // color} If (G [from] [I] & Col [from] = Col [I]) // The color is the same, return false;} return true;} // hdu2444 <PRE name = "code" class = "CPP" >#include <iostream >#include <string. h> # include <stdio. h> using namespace STD; const int n = 2005; int head [N], link [N]; bool vis [N], Col [N]; int CNT, n, m; struct edge {int to; int next ;}; edge [N * n]; void Init () {CNT = 0; memset (Head,-1, sizeof (head); memset (COL, 0, sizeof (COL);} void add (int u, int v) {edge [CNT]. to = V; edge [CNT]. next = head [u]; head [u] = CNT ++;} bool color (int u) {for (INT I = head [u]; ~ I; I = edge [I]. Next) {int v = edge [I]. To; If (! Col [v]) {Col [v] =! Col [u]; If (! Color (V) return false;} else if (COL [v] = Col [u]) return false;} return true;} bool DFS (int u) {for (INT I = head [u]; ~ I; I = edge [I]. Next) {int v = edge [I]. To; If (! Vis [v]) {vis [v] = 1; if (link [v] =-1 | DFS (link [v]) {link [v] = u; return true ;}} return false;} int match () {int ans = 0; memset (link,-1, sizeof (Link); For (INT I = 1; I <= N; I ++) {memset (VIS, 0, sizeof (VIS )); if (DFS (I) ans ++;} return ans;} int main () {While (~ Scanf ("% d", & N, & M) {If (n = 1) {puts ("no"); continue;} Init (); while (M --) {int U, V; scanf ("% d", & U, & V); add (u, v); add (V, u);} Col [1] = 1; if (! Color (1) {puts ("no"); continue;} printf ("% d \ n", match ()> 1) ;}return 0 ;}


 
 
There are very few questions about maximizing the matching of a bipartite graph, and some simple changes are often made:

Variant 1: minimum vertex overwrite of a Bipartite Graph

The minimum vertex overwrite requires the least vertex (X or Y rows) to associate each edge with at least one vertex.

Knoig theorem: minimum vertex overwrites of a bipartite graph = maximum number of matching (M) of a bipartite graph ).

Variant 2: Minimum path overwrite of a DAG

Overwrite all vertices of directed acyclic graph (DAG) g with a small number of non-Intersecting simple paths.

Conclusion: The minimum number of paths covered by the Dag graph = number of nodes (N)-Maximum number of matching (m)

Variant 3: Maximum Independent Set of a Bipartite Graph

Conclusion: The maximum number of independent sets in the bipartite graph = number of nodes (N)-Maximum number of matching (m)

Introduction to Bipartite Graph

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.