HDU1530 Maximum Group template

Source: Internet
Author: User

Maximum clique

Time limit:20000/10000 MS (java/others) Memory limit:65536/32768 K (java/others)
Total submission (s): 4114 Accepted Submission (s): 2175


Problem Descriptiongiven a graph G (V, E), a clique is a sub-graph G (V, e), so, to all vertex pairs v1, v2 in V, there Exists an edge (V1, v2) in E. Maximum clique is the Clique, which has Maximum number of vertex.

Inputinput contains multiple tests. For each test:

The first line has one integer n, the number of vertex. (1 < n <= 50)

The following n lines have n 0 or 1 each, indicating whether a edge exists between I (line number) and J (column number).

A Test with n = 0 signals the end of input. This test should is not processed.

Outputone number for each test, the number of vertex in maximum clique.

Sample Input50 1 1 0 11 0 1 1 11 1 0 1 10 1 1 0 11 1 1 1 00

Sample Output4

Authorcheng, Long

Sourcezoj Monthly, February 2003 test instructions: Max Regiment; Max Regiment: Reference: Http://www.cnblogs.com/yefeng1627/archive/2013/03/31/2991592.html Http://www.cnblogs.com/zhj5chengfeng/p/3224092.html first, the definition

An g= graph (v,e), V is the point set, and E is the edge set. Take a subset of V U, if for any two points U and V, there is an edge (u,v) ∈e, then called U is a complete sub-graph of G. U is a regiment when and only if u is not contained in a larger complete sub-graph.

The largest group of G refers to a regiment with the highest number of fixed points.

ii. Common Practices

1. Sequential greedy heuristic search algorithm

2, local search heuristic algorithm

3. Intelligent Search Heuristic algorithm

4. Genetic algorithm

5. Simulated annealing algorithm

6. Tabu algorithm

7. Neural Network algorithm

8. Improved ant colony algorithm-ANTMCP

Looked at the list of algorithms, is not there is a feeling of scalp tingling. Anyway, I feel like this ... Because I don't have anything on it.

If you want to see the above things, Baidu Encyclopedia has some brief introduction, I was too weak, did not read.

Baidu Encyclopedia Portal: Biggest Regiment problem

Let's talk about a common search algorithm.

Of course, this algorithm is not efficient, so when the figure has more than 100 points, please use caution

Let's look at an obvious DFS:

    Initialization

Starting from a point u, add this point to a set and set it to U. Go through all the points that are connected to him, put them in another set S1, and then go through the first DFS

    First time DFS:

Select a point U1 from the S1, and this point must be connected to any point in the set U. Add the U1 in the collection S1 to the collection S2, and add the U1 to the collection U for the second time DFS

    Two times DFS:

Select a point U2 from the S2, and this point must be connected to any point in the set U. Add the U2 in the collection S2 to the collection S3, and add the U2 to the collection U for the third time DFS

    Three times DFS:

Select a point U3 from the S3, and this point must be connected to any point in the set U. Add the U3 in the collection S3 to the collection S4, and add the U3 to the collection U, fourth times DFS

......

    The bottom-most DFS:

When an S collection is an empty set, DFS ends, and then we find a complete sub-graph that updates our largest regiment with this full sub-graph. Exit the current DFS, return to the upper Dfs, and then find the next full sub-graph until all the full sub-graphs have been found

According to the Dfs method described above, it is definitely possible to get a maximum group because the DFS enumerates all the full sub-graphs again. But is this time complexity too high?

This resulted in the following DFS process, roughly the same as the DFS above, except that there are some places that are different.

First of all, we first get the next few points composed of the largest regiment in the end is how big, (at first, it must be the last point to make a single largest regiment, the point is 1) and then we then DFS:

    Initialization

Starting from a point u, add this point to the set U. Add a number larger than it and connect it to the set of points S1, in order to facilitate, the collection S1 points in order, let them from small to large arrangement, the first time DFS

    First time DFS:

Select a point U1 from the S1, traverse the S1, all the points that are numbered larger than U1 and are connected to the U1, in fact, are the points that are behind the U1 and connected to the U1, adding them to the collection S2. Similarly, the points in the S2 are also numbered from small to large. Add U1 to the collection U and perform the second time DFS

    Two times DFS:

Select a point U2 from the S2, traverse the S2, all the points that are behind the U2 and connect to the U2, and add them to the collection S3, so that the points in the S3 are numbered from small to large, and the U2 is added to the collection U for the third time DFS

    Three times DFS:

Select a point U3 from the S3, traverse the S3, all the points that are in the U3 and connect to the U3, and add them to the collection S4, so that the points in the S4 are numbered from small to large, and U3 is added to the collection U for the fourth time DFS

......

    The bottom-most DFS:

When an S collection is empty, the DFS process ends with a complete sub-graph that consists of only the next few points, and uses it to update the largest group that is composed of only the following points. Exit the current DFS, return to the upper Dfs, and then find the next full sub-graph until all the full sub-graphs have been found

The DFS process above, if not add any pruning, in fact, and the first DFS is similar, but since we are so DFS, can you think how to prune it?

Assuming that we are currently on level I DFS, we now need to select a UI from Si, add the UI-connected points in the Si collection to the set S (i+1), and add the UI to the set U

Maybe after a little thought, we thought of a pruning:

Pruning 1: If the number of points in the U set is +1 (select UI joins the U collection) the number of points behind all UI in +si ≤ the current optimal value, no more DFS

What else is there to prune?

Notice that we choose U from the back, that is, when we are in the DFS initialization, assuming that the selected point is numbered X, then we must have known with [X+1, n], [x+2, N],[x+3, n] ... [N,n] What is the maximum number of points in these intervals that can be formed

Pruning 2: If the number of points in the U set is +1 (for the same reason) +[ui, n] The maximum number of vertices that can be formed in this interval ≤ the current optimal value, no more DFS

Is it enough to have these two pruning?

No, we can also come up with a pruning:

Pruning 3: If Dfs to the bottom, we can update the answer, no more DFS, end the entire DFS process, and no longer return to the previous level continue Dfs

Why? Because if we go back to DFS, the number of points becomes larger and the available points become less (the available points are determined at the beginning of Dfs initialization, and as the number of layers of DFS continues to deepen, the available points are constantly decreasing)

With the above three pruning, 100 points within the figure, we can also very quickly out of the solution

One might ask, what if you want to know what nodes the largest group contains?

That's not easy? Each time Dfs adds a point into the U set, DFS to the lowest level, update the maximum number of groups, the points in the U set must be a complete sub-graph of the point set, with the U set to update the largest group point set on the line

Iii. Common Conclusions

1, the maximum number of points = the number of maximum independent set points in the complement graph

2. The number of maximum independent set points + the number of minimum coverage points = The number of the entire plot point in the two-part graph

3. The number of minimum coverage points = The maximum number of matches in a binary chart

4, the figure of the dyeing problem, the minimum number of colors required = The number of the largest group of points

Code:
#include <iostream>#include<cstdio>#include<cstring>using namespacestd;intn,best;intmp[102][102],num[102];BOOLDfsintS[],intSumintCnt//sum: The number of vertices connected to u, CNT indicates the number of current groups{    if(sum==0){//when the last point in this regiment is not connected to a vertex with a large number        if(cnt>best) {//problem 1:best is the number of vertices in the largest regimentbest=CNT; return 1; }        return 0; }    intt[102];  for(intI=0; i<sum;i++) {//enumerate each vertex connected to u s[i]        if(cnt+ (sum-i) <=best)return 0;//Pruning 1, if the current vertex number CNT Plus can also increase the maximum number is still less than best exit and return False        if(cnt+num[s[i]]<=best)return 0;//Pruning 2, if the current vertex number CNT plus the maximum number of vertices containing s[i] is still less than best then exit and return False        intk=0;  for(intj=i+1; j<sum;j++) {//Scans the vertices connected to s[u] in the vertices connected to u and stores them in the array t[] with a quantity of K            if(Mp[s[i]][s[j]]) t[k++]=S[j]; }        if(Dfs (t,k,cnt+1))return 1; }    return 0;}intMaxt () {if(n<=0)return 0; ints[102]; Best=0;  for(inti=n-1; i>=0; i--){        intk=0;  for(intj=i+1; j<n;j++) {//traverse the vertices between [I+1, N],            if(Mp[i][j]) s[k++]=J; } dfs (S,k,1); Num[i]=best;//draw vertex I, set out to form the maximum number of vertices in the regiment    }    returnBest ;}intMain () { while(SCANF ("%d", &n) &&N) {         for(intI=0; i<n;i++)             for(intj=0; j<n;j++) scanf ("%d",&Mp[i][j]); printf ("%d\n", Maxt ()); }    return 0;}

HDU1530 Maximum Group template

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.