Algorithm Series note 10 (Algorithm for graph three-maximum flow and binary graph)

Source: Internet
Author: User

a simple concept of the main record stream network and the maximum flow ( The implementation algorithm of the maximum flow may be supplemented later ) , the main explanation is to use the Hungarian algorithm to find the maximum matching of the binary graph.

1 : Streaming Network

Stream network is g (V, E) is a finite graph, each of its sides (U, v) ∈ e c (U, v) ≥ 0 (U, v) e C (U, v) = 0

a source point s and a meeting point T. . and assume that each vertex is on a path from the source point to the sink point.

formalized definition: A network flow is one for all nodes u and the v all have the following characteristics Real numbers function :: Satisfies the following two properties:

Capacity Limits: 0 ≤ f (u,v) ≤ C (u,v) .

Traffic Conservation: For all nodes except source point s and meeting Point T , the net flow is equal to 0 , that is, the flow into the node is equal to the flow out of the node.

The traffic on this network is:

In addition, the flow network of multi-source and multi-meeting points can be converted to a normal network with only one source point and a sink point.


2 : Maximum Flow-- Ford-fulkerson Method

the maximum flow is to find a flow that makes the upper (1) reach maximum. Ford-fulkerson is a method, not an algorithm, because it contains several implementations that run at different times.

Concept :

(1) Surviving Networks

Surviving Networks Gf is composed of edges that still have space to adjust the flow. The calculation formula is as follows:


Note : usually when CF (u,v) = 0 , we do not add it to the surviving network.

(2) Augmented Path

Augmented Path P It's a surviving network . Gf one from the source point s A simple path to the meeting point. the maximum amount of traffic that can be added to each edge on an augmented path p is the residual capacity of the path p .

as b) The remaining networks in the s->v2->v3->t the remaining capacity is 4 . A stream is the maximum stream when and only if its surviving network does not contain any augmented paths.

Example:


(3) Cutting

The value of any cut's net flow is equal to |f| value, no more than G the capacity of any cut. The smallest cut in a network is the smallest cut in the entire network.

(4) maximum flow minimum cutting theorem


of Basic Ford-fulkerson pseudo Code of the method:


The key now is how to find the augmented path in the surviving network. These include many algorithms. For example:the Emonds-karp algorithm is the selection of the remaining network of the augmented path is a source from the point s to the meeting point T The shortest path.

This assumes that the weights for each edge are 1. Its time complexity is O (ve^2). In addition, push - the time complexity of the re-labeling algorithm is O (v^2e). the time complexity of the pre-labeling algorithm is O (v^3).

3: two-part diagram

two-part diagram : simply put, if the midpoint of the diagram can be divided into two groups, and all edges cross the boundary of the group, then this is a binary graph. To be precise: divide the vertex of a graph into two disjoint sets L and R , making each edge connected separately the vertices in L ,R . If such a division exists, the graph is a two-part graph. An equivalent definition of a binary graph is: A graph that does not contain "rings with odd numbers of edges".

Figure 1 is a two-part diagram. in order to be clear, we will later draw it into the form of Figure 2.

Match : in graph theory, a "match" ( Matching ) M is a collection of edges with no public vertices for any of the two edges.

For example, the figure 3 , Figure 4 The red side is the picture. 2 the match. up to one side of M is connected to v .

 
Span style= "COLOR: #252525" >        if matching m v is associated with m saturation vertex Span style= "COLOR: #252525" >v v Span style= "COLOR: #252525" is m- saturated, otherwise called v m- unsaturated.  

or called v is a M match.

we define matching points , matching edges , unmatched points , mismatched edges , and they are very obvious.

Example 3 in 1,4,5,7 as a matching point, the other vertices are mismatched points; 1-5 , 4-7 to match edges, the other edges are non-matching edges.

Maximum Match : A match with the largest number of matched edges in all matches of a graph, called the maximum match for this graph.

Figure 4 is a maximum match that contains 4 matches the edge of the bar.

Perfect Match : If a match in a graph, all vertices are matching points, then it is a perfect match.

Figure 4 is a perfect match. Obviously,

The perfect match must be the maximum match (any one of the perfectly matched points has been matched,

Adding a new matching edge will definitely conflict with an existing matching edge. But not every diagram has a perfect match.

For example: as shown, if there is a connecting edge between a pair of boys and girls, it means they like each other.

Is it possible to have all boys and girls 22 paired,

so that every pair of children like each other? In graph theory, this is the perfect match problem. If you change the phrase:

How many boys / girls can pair with each other? This is the maximum matching problem.


The basic concept is finished. One of the algorithms for solving the maximum matching problem is the Hungarian algorithm , and the concepts below are for this algorithm service.


Alternate Road : Starting from an unmatched point, followed by a non-matching edge, matching edge, non-matching edge ... the formed path is called alternating road.

Augmented Road : From an unmatched point, take the alternate path, if the route is another unmatched point (the point of departure is not counted),

This alternate path is called the augmented path ( Agumentingpath ).

For example, the figure 5 an augmented road in the 6 shown (the matching points in the figure are marked in red): There is an important feature of the augmented path: a non-matching edge is more than a matching edge.

Therefore, the significance of studying the augmented path is to improve the matching . Simply swap the identities of the matching and non-matching edges in the augmented path.

This does not break the matching character because there are no other matching edges in the middle of the matching node.

After the swap, the number of matching edges in the figure is 1 more than the original one. We can add matching edges and matching points in the match by constantly looking for the augmented path.

When the augmented path is not found, the maximum match is reached (this is the augmented path theorem). This is exactly what the Hungarian algorithm does.


Hungarian algorithm pseudo-code:

BOOL looking for the extensible path {while (enumerated from adjacency table K can be associated to vertex J) {if (J is not on the augmented path) {Add J to the augmented path, if (j is an open point or the corresponding item from J has an augmented path) {Modify J corresponds to K; The corresponding entry from the K has an augmented path, which returns true;}} The corresponding entry from K has no augmented path and returns false;} void Hungarian Hungary () {for i->1 to n{if (then the corresponding item from I has an augmented path) match number + +;} Output match number;}

The following is the implementation of the Hungarian algorithm, which is stored with correlation matrices and adjacency tables, respectively. The complexity of time is O (VE).

adjacency table:

/* This is only for directed graphs, and it's already two points. */#include <iostream> #include <cstring>using namespace std;//define a linked list struct link{int data ;//Store data link* next;//refers to the next node of link (int n);}; Link::link (int n) {data = N;next = NULL;}             int N1, N2, M, Ans=0;int result[101];              The number of points in the record N1 point matched by bool state[101];         Record whether each point in the n1 has been searched for link *head[101];             Records the adjacency node of points in N2 link* last[101]; The end position record of the adjacency table//Determines whether to find the augmented path from node n bool Find (const int n) {link *t = head[n];while (t! = NULL) {//n still has an adjacency node not found if (! (      State[t->data]) {//If the adjacency point T->data has not been looked up state[t->data]=true; mark t->data as already searched if ((result[t->data]==0) | | (Find (Result[t->data]))) {//If T->data does not belong to the previous match m//if T->data matches to the node can be found to the augmented path result[t->data]=n;//then you can update the match m ', where N1 points t->data match ret Urn true;//returns the matching successful flag}}t=t->next;//continues to find the next n of the adjacency node}return false;} int main () {int t1 = 0, t2 = 0;cin >> n1 >> n2 >> m;for (int i=0; i<m; i++) {cin>>t1>>t2;// Both the last and the head are global variable contents are initialized to NULL,//If the local variable, the comparison must be initialized to nUllif (last[t1]==null) last[t1]=head[t1]=new link (T2); Elselast[t1]=last[t1]->next=new link (t2);} for (int i=1, i<=n1; i++) {memset (state), if (Find (i)) state,0,sizeof;} Cout<<ans<<endl;return 0;}


Correlation matrix:

#include <iostream> #include <cstring>using namespace Std;int map[105][105]; Associative matrix int visit[105], flag[105];   Equivalent to the state in the adjacency table and ResultInt n,m;bool dfs (int a) {for (int i=1; i <= n; i++) {if (Map[a][i] &&!visit[i]) {visit[i]=1; if (flag[i] = = 0| | DFS (Flag[i])) {Flag[i] = A;return true;}}} Returnfalse;} Intmain () {while (CIN >> n >> m) {memset (map, 0, sizeof (map)), for (int i=1; i<=m; i++) {int x,y;cin >> X & Gt;> Y;map[x][y]=1;} memset (flag, 0, sizeof (flag)), int result=0;for (int i=1; I <= n;i++) {memset (visit, 0, sizeof (visit)), or if (Dfs (i)) result ++;} cout << result <<endl;} return 0;}

Note: The search for the maximum matching algorithm of the binary graph-Hungarian algorithm, is plainly the maximum flow algorithm, but it is based on the characteristics of the problem of binary graph, the maximum flow algorithm is simplified,

Increased efficiency. Of course we can still use the traditional maximum flow algorithm. At this point, you need to add a source point S and a meeting point T, the capacity is set to 1.

Edge for S to left L sets the edge of the vertex and R set to the edge of the T vertex to get a network stream g1,m is a match of Figure g when and only if F is a stream of an integer value of G1, and m=|f|.

Reference documents:

1 : http://blog.csdn.net/pi9nc/article/details/11848327 maximum matching, perfect matching and Hungarian algorithm for binary graphs

2 : http://blog.csdn.net/akof1314/article/details/4421262 Hungarian Algorithm

3 : Http://baike.baidu.com/link?url=wLE9pjsJ8qpft_ Jwakrqav-eed9p1zsivzyregavoqxhsti6ctdzmvalgkmfsgclwdri05veuikgxszsai0taa Baidu Encyclopedia

4 : http://blog.csdn.net/china8848/article/details/2287769

Algorithm Series note 10 (Algorithm for graph three-maximum flow and 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.