Algorithm Practice series The Hungarian algorithm of maximal matching of-hiho1122 binary graph

Source: Internet
Author: User

Title Address: http://hihocoder.com/problemset/problem/1122

The key to this topic is 2 a question: 1 point to use BFS construction of a binary map 2 : For the two-part diagram S The nodes in the, traverse to find the augmented path . ( Hungarian algorithm to find the maximum match of the binary graph ) each finding an augmented road I found a match more.

The code is as follows:

/* This topic has two points to note: 1 points with the BFS structure of the two-point Figure 2: for the binary map of the node in S, traverse to find the augmented path (the Hungarian algorithm to find the largest match of the binary map) each found an augmented road to find a match * * #include <iostream># Include <string.h> #include <queue>using namespace std; #define MAXSIZE 10010struct gnode{int vertex; Gnode *adj;}; Class Graph{public:graph (int v, int e) {//Graph constructor Initialization vertexnum = V;arcnum = E;memset (result, 0, sizeof (result)); for (int i = 1; I <= vertexnum;  i++) {Color[i] = 2;edges[i] = NULL;   Local variables must be initialized otherwise they cannot be compared with null}}void initedges (int u, int v);       void Showgraph (); bool Findpath (int u);    Traverse the augmented path int visited[maxsize];   Identifies if an int color[maxsize] has been accessed; 0 1 means that the relative color 2 is represented as access used to construct the binary diagram ~graph () {for (int i = 1; I <= vertexnum; i++) Destroy (Edges[i]);}       BOOL Bfs_travel (); Breadth traversal structure dichotomy diagram Private:int vertexnum;int arcnum;   Gnode *edges[maxsize];int Result[maxsize];          Save V2 vertex void Destroy (Gnode *pnode) that matches in V1; Destroy diagram void initundirectededges (int u, int v), bool BFs (int v);}; void Graph::initedges (int u, int v) {initundirectededges (U, v); Initundirectededges (v, u);} void Graph::initundirectededges (int u, int v) {Gnode *adjnode = new Gnode (); Adjnode->vertex = V;adjnode->adj = NULL;i F (Edges[u] = null) edges[u] = Adjnode;else{gnode *pnode = Edges[u];while (Pnode->adj! = null) Pnode = Pnode->adj;pnod E->adj = Adjnode;}} BOOL Graph::bfs (int v) {queue<int> Q;q.push (v); Color[v] = 0;while (!q.empty ()) {int u = q.front (); Q.pop (); Gnode *pnode = Edges[u];while (pnode! = NULL) {if (Color[pnode->vertex] = = Color[u]) return false;if (color[pnode-> Vertex] = = 2) {Q.push (Pnode->vertex); Color[pnode->vertex] =!color[u];} pnode = Pnode->adj;}} return true;} BOOL Graph::bfs_travel () {for (int i = 1; I <= vertexnum; i++) {if (color[i] = = 2) if (!BFS (i)) return false;} return true;} void Graph::d Estroy (Gnode *pnode) {if (Pnode! = NULL) {Destroy (Pnode->adj);d elete Pnode;}} Looking for an augmented road bool Graph::findpath (int u) {gnode *pnode = edges[u];while (pnode! = NULL) {int v = pnode->vertex;if (!visited[v ]) {Visited[v] = 1;//==0 indicates that the vertex in the s that corresponds to continue looking for result[v] is not matched.F (result[v] = = 0 | | findpath (RESULT[V])) {Result[v] = U;return true;}} Pnode = Pnode->adj;} return false;}  void Graph::showgraph () {for (int i = 1; I <= vertexnum; i++) {cout << i << "--";          Gnode *p = edges[i];              while (P! = NULL) {cout << ("<< p->vertex <<") ";          p = p->adj;      } cout << Endl;    }}int Main () {int N, m;cin >> N >> M;   Input vertex and Edge graph G (n,m); for (int i = 0; i < M; i++) {int u, v;cin >> u >> v;g.initedges (U, v); The structure diagram uses adjacency table to store}//g.showgraph (); int ans = 0;if (G.bfs_travel ()) {//Whether it can constitute a binary graph and an identity binary chart color[i]=0 or 1for (int i = 1; I <= N; i++) {if (!g.color[i]) {memset (g.visited, 0, sizeof (g.visited)), if (G.findpath (i)) ans++;}}} cout << ans << endl;return 0;} /*5 43 21 35 41 5*/

Post-Problem feeling:

Implement an algorithm The first thing is (1) Understanding the process of algorithm implementation ( by Example ) (2) preliminary identification of the data structures required by the algorithm

(3) Encoding Implementation

in response to a big problem or project, the mind immediately has to think of the difficult problems to be solved. ( For example, there are 2 points ) , and then a search algorithm to solve

Algorithm Practice series The Hungarian algorithm of maximal matching of-hiho1122 binary 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.