Maximum independent set Solution

Source: Internet
Author: User

Defined:

Stand-alone set: In a diagram, find that all the points contained in a collection do not have a connecting edge to each other

Maximum independent set: The independent set with the largest number of elements in all independent sets

Before just know the maximum independent set of the binary graph = total number of points-maximum matches

But in the case of general non-direction graphs, it can't be solved.

Thinking in a different way, in fact, the largest independent set is also equivalent to create a reverse diagram (the current edges are removed, added before the edge is not selected)

It becomes the number of points for the maximum complete graph after the modification, because we want to make sure that there is no connected edge between the selected points, then the selected point 22 is necessarily

There is an edge, otherwise the diagram before the existence of the edge, that is, two points can not be fully selected, and so the resulting is a complete picture

And we ask for the most points, that is, the largest complete picture, that is, the largest group

Recently studied the solution of this kind of problem, this can be regarded as a kind of search problem, the DFS search constantly find the best solution

It is easy to see that this is a NP problem, and the complexity is also O (2^n)

So good pruning is very necessary.

Define Dp[i] is the maximum number of points that can be composed of I ~ N

So we can figure out the dp[] value backwards.

For the previous dp[] value, use the previously calculated values to prune

We are here from small to large to add nodes, to ensure that the current set of nodes to add the DP value has been obtained

For example, V is currently added and a T-Dot has been

Then we can use

T+DP[V] <=mx

T+n-t+1<=mx

To prune it (just think about it for a moment)

We use a _stack[][] array to record the nodes that can be expanded, that is, the points in this array and the points taken before each have a connecting edge, so the addition can directly constitute a complete picture

We just need to update this array every time we add a node.

Because it is a recursive solution, to prevent updates will be in the backtracking error, then the array is set to two-dimensional, the first dimension of the current collection of the largest point can be directly used

int cnt = 0;
for (int j=i+1; j<num; j + +) {
if (!mp[v][_stack[u][j]]) _stack[v][cnt++] = _stack[u][j];
}

And then POJ1419 is a naked question, just the point at which the optimal solution is recorded.

1#include <cstdio>2#include <cstring>3#include <algorithm>4 using namespacestd;5 #defineN 1056 BOOLMp[n][n];7 intT, N, K;8 intDp[n], ans[n], Tmp[n], ret, MX, CNT;9 int_stack[n][n];Ten  One voidBuildintk) A { -Memset (MP,0,sizeof(MP)); -      for(intI=1; I<=n; i++) Mp[i][i] =true; the      for(intI=0; i<k; i++){ -         intA, B; -scanf"%d%d", &a, &b); -MP[A][B] =true; +     } - } +  A voidDfsintU,intNumintStep) at { -     if(num = =0){ -         if(mx<Step) { -MX =Step; -             if(Step>ret) for(intI=1; I<=step; i++) Ans[i] =Tmp[i]; -         } in         return; -     } to  +      for(intI=0; I<num; i++){ -         intv =_stack[u][i]; the         //methods of cut the node *         if(STEP+DP[V]&LT;=MX)Continue; $         if(step+n-v+1&LT;=MX)Continue;Panax Notoginseng         //The v description can be accessed from the stack, and the V and all previous points have a connecting edge, as long as the data in the stack is re-updated. -         intCNT =0; the          for(intj=i+1; J<num; J + +){ +             if(!mp[v][_stack[u][j]]) _stack[v][cnt++] =_stack[u][j]; A         } thetmp[step+1] =v; +DFS (V, CNT, step+1); -     } $ } $  - intMain () - { the   //freopen ("a.in", "R", stdin); -scanf"%d", &T);Wuyi      while(t--){ thescanf"%d%d", &n, &k); - build (k); WuRET =0;//Init -          for(intI=n; i>=1; i--){ AboutCNT =0, mx =1; $              for(intj=i+1; J<=n; J + +){ -                 if(!mp[i][j]) _stack[i][cnt++] =J; -             } -tmp[1] =i; ADFS (I, CNT,1); +Dp[i] =MX; theRET =Max (ret, dp[i]); -         } $printf"%d\n", ret); the          for(intI=1; I<=ret; i++){ the             if(I<ret) printf ("%d", Ans[i]); the             Elseprintf"%d\n", Ans[i]); the         } -     } in     return 0; the}

Maximum independent set Solution

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.