最大团问题

  

一、定义

 

一个无向图 G=(V,E),V 是点集,E 是边集。取 V 的一个子集 U,若对于 U 中任意两个点 u 和 v,有边 (u,v)∈E,那么称 U 是 G 的一个完全子图。 U 是一个团当且仅当 U 不被包含在一个更大的完全子图中。

G的最大团指的是定点数最多的一个团。

 

二、常用做法

 

1、顺序贪婪启发式搜索算法

2、局部搜索启发式算法

3、智能搜索启发式算法

4、遗传算法

5、模拟退火算法

6、禁忌算法

7、神经网络算法

8、改进蚁群算法-AntMCP

 

看了所列出的算法,是不是有一种头皮发麻的感觉。反正我是这样的感觉...因为上面的东西我都不会...

如果你想看上面的东西,百度百科中有一些简略的介绍,我太弱,没看懂。

百度百科传送门:最大团问题

 

下面说说常用的一种搜索算法

当然,这种算法很不高效,所以当图中有 100 个点以上时,请慎用

 

先看看一个显而易见的 DFS :

    初始化:

      从一个点 u 开始,把这个点加入到一个集合中,设为 U。遍历一遍所有和他相连的点,把他们放入另一个集合 S1 中,接下来进行第一遍 DFS

    第一遍 DFS :

      从 S1 中选择一个点 u1,这个点肯定和集合 U 中的任何一个点相连。把集合 S1 中 u1 能访问到的点加入到集合 S2 中,并把 u1 加入到集合 U 中,进行第二遍 DFS

    第二遍 DFS :

      从 S2 中选择一个点 u2,这个点肯定和集合 U 中的任何一个点相连。把集合 S2 中 u2 能访问到的点加入到集合 S3 中,并把 u2 加入到集合 U 中,进行第三遍 DFS

    第三遍 DFS :

      从 S3 中选择一个点 u3,这个点肯定和集合 U 中的任何一个点相连。把集合 S3 中 u3 能访问到的点加入到集合 S4 中,并把 u3 加入到集合 U 中,进行第四遍 DFS

    ......

    最底层的 DFS :

      当某个 S 集合为空集的时候,DFS 结束,这时候我们就找到了一个完全子图,用这个完全子图更新我们的最大团。退出当前的 DFS,返回上层 DFS,接着找下一个完全子图,直到找完所有的完全子图

 

按照上面介绍的 DFS 方法,肯定能够得到一个最大团,因为该 DFS 把所有的完全子图都枚举了一遍。但是这样做的时间复杂度是不是太高了?

于是产生了下面的 DFS 过程,大致上和上面的 DFS 一样,只不过有一些地方不太一样了

 

首先,我们先得到后几个点组成的最大团到底是多大,(最开始的时候肯定是最后一个点单独构成一个最大团,点数为1)然后我们再 DFS:

    初始化:

      从一个点 u 开始,把这个点加入集合 U 中。将编号比它大的且和它相连的点加入集合 S1 中,为了方便,将集合 S1 中的点有序,让他们从小到大排列,进行第一遍 DFS

    第一遍 DFS :

      从 S1 中选择一个点 u1,遍历 S1 中,所有编号比 u1 大且和 u1 相连的点,其实也就是排在 u1 后面,并且和 u1 相连的点,将它们加入集合 S2 中。同理,让 S2 中的点也按照编号也从小到大排列。将 u1 加入集合 U 中,进行第二遍 DFS

    第二遍 DFS :

      从 S2 中选择一个点 u2,遍历 S2 中,所有排在 u2 后面且和 u2 相连的点,并把它们加入集合 S3 中,让 S3 中的点按照编号从小到大排列,将 u2 加入集合 U 中进行第三遍 DFS

    第三遍 DFS :

      从 S3 中选择一个点 u3,遍历 S3 中,所有排在 u3 后面且和 u3 相连的点,并把它们加入集合 S4 中,让 S4 中的点按照编号从小到大排列,将 u3 加入集合 U 中进行第四遍 DFS

    ......

    最底层的 DFS :

      当某个 S 集合为空时,DFS 过程结束,得到一个只用后面几个点构成的完全子图,并用它去更新只用后面几个点构成的最大团。退出当前 DFS,返回上层 DFS,接着找下一个完全子图,直到找完所有的完全子图

 

上面的 DFS 过程,如果不加任何剪枝的话,其实和第一个 DFS 是差不多的,但是既然我们都这样 DFS 了,能不能想一想怎么剪枝呢?

假设我们当前处于第 i 层 DFS,现在需要从 Si 中选择一个 ui,把在 Si 集合中排在 ui 后面的和 ui 相连的点加入集合 S(i+1) 中,把 ui 加到集合 U 中

可能大家稍作思考之后就想到了一个剪枝:

 

剪枝1:如果 U 集合中的点的数量+1(选择 ui 加入 U 集合中)+Si 中所有 ui 后面的点的数量  当前最优值,不用再 DFS 了

 

还有什么剪枝呢?

注意到我们是从后往前选择 u 的,也就是说,我们在 DFS 初始化的时候,假设选择的是编号为 x 的点,那么我们肯定已经知道了用 [x+1, n] ,[x+2, n],[x+3, n] ...[n,n] 这些区间中的点能构成的最大团的数量是多大

 

剪枝2:如果 U 集合中的点的数量+1(理由同上)+[ui, n]这个区间中能构成的最大团的顶点数量  当前最优值,不用再 DFS了

 

有这两个剪枝就够了吗?

不,我们还能想出一个剪枝来:

 

剪枝3:如果 DFS 到最底层,我们能够更新答案,不用再 DFS 了,结束整个 DFS 过程,也不再返回上一层继续 DFS 了

 

为什么?因为我们如果再继续往后 DFS 的话,点的编号变大了,可用的点变少了(可用的点在一开始 DFS 初始化的时候就确定了,随着不断的加深 DFS 的层数,可用的点在不断的减少)

 

有了上面三个剪枝,100 个点以内的图,我们也能非常快的出解了

 

可能有人会问,如果想知道最大团包含哪些节点该怎么办?

这还不简单?每次 DFS 都会加一个点进入 U 集合中,DFS 到最底层,更新最大团数量的时候,U 集合中的点一定是一个完全子图中的点集,用 U 集合更新最大团的点集就行了

 

三、常用结论

 

1、最大团点的数量=补图中最大独立集点的数量

2、二分图中,最大独立集点的数量+最小覆盖点的数量=整个图点的数量

3、二分图中,最小覆盖点的数量=最大匹配的数量

4、图的染色问题中,最少需要的颜色的数量=最大团点的数量

 

四、刷题练手

 

1、先来一道裸题:ZOJ 1492 Maximum Clique

给了一个最多包含 50 个点的无向图,让求这个图中最大团所包含的的点的数量

直接按照上面所讲的 DFS 过程做就行

 1 #include <iostream>
 2 #include <cstring>
 3 #include <cstdio>
 4 
 5 using namespace std;
 6 
 7 struct MAX_CLIQUE {
 8     static const int N=60;
 9 
10     bool G[N][N];
11     int n, Max[N], Alt[N][N], ans;
12 
13     bool DFS(int cur, int tot) {
14         if(cur==0) {
15             if(tot>ans) {
16                 ans=tot;
17                 return 1;
18             }
19             return 0;
20         }
21         for(int i=0; i<cur; i++) {
22             if(cur-i+tot<=ans) return 0;
23             int u=Alt[tot][i];
24             if(Max[u]+tot<=ans) return 0;
25             int nxt=0;
26             for(int j=i+1; j<cur; j++)
27                 if(G[u][Alt[tot][j]]) Alt[tot+1][nxt++]=Alt[tot][j];
28             if(DFS(nxt, tot+1)) return 1;
29         }
30         return 0;
31     }
32 
33     int MaxClique() {
34         ans=0, memset(Max, 0, sizeof Max);
35         for(int i=n-1; i>=0; i--) {
36             int cur=0;
37             for(int j=i+1; j<n; j++) if(G[i][j]) Alt[1][cur++]=j;
38             DFS(cur, 1);
39             Max[i]=ans;
40         }
41         return ans;
42     }
43 };
44 
45 MAX_CLIQUE fuck;
46 
47 int main() {
48     while(scanf("%d", &fuck.n), fuck.n) {
49         for(int i=0; i<fuck.n; i++)
50             for(int j=0; j<fuck.n; j++)
51                 scanf("%d", &fuck.G[i][j]);
52         printf("%d\n", fuck.MaxClique());
53     }
54     return 0;
55 }
ZOJ 1492

 

2、来一个稍微麻烦点的题:HDU 3585 maximum shortest distance

给了平面上 n 个点,要求选出 k 个点来,使得这 k 个点中,距离最近的两个点的距离最大。n 最大为50

二分答案后,如果两个点之间的距离大于当前的判断值,加边,在用最大团跑一下,根据得到最大团点的数量和 k 的大小关系,调整二分的上下界

 1 #include <iostream>
 2 #include <cstring>
 3 #include <cstdio>
 4 #include <cmath>
 5 
 6 using namespace std;
 7 
 8 const int N=60;
 9 
10 struct MAX_CLIQUE {
11     bool G[N][N];
12     int n, Max[N], Alt[N][N], ans;
13 
14     bool DFS(int cur, int tot) {
15         if(cur==0) {
16             if(tot>ans) {
17                 ans=tot;
18                 return 1;
19             }
20             return 0;
21         }
22         for(int i=0; i<cur; i++) {
23             if(cur-i+tot<=ans) return 0;
24             int u=Alt[tot][i];
25             if(Max[u]+tot<=ans) return 0;
26             int nxt=0;
27             for(int j=i+1; j<cur; j++)
28                 if(G[u][Alt[tot][j]]) Alt[tot+1][nxt++]=Alt[tot][j];
29             if(DFS(nxt, tot+1)) return 1;
30         }
31         return 0;
32     }
33 
34     int MaxClique() {
35         ans=0, memset(Max, 0, sizeof Max);
36         for(int i=n-1; i>=0; i--) {
37             int cur=0;
38             for(int j=i+1; j<n; j++) if(G[i][j]) Alt[1][cur++]=j;
39             DFS(cur, 1);
40             Max[i]=ans;
41         }
42         return ans;
43     }
44 };
45 
46 struct Point {
47     double x, y;
48 
49     double dis(Point A) {
50         return sqrt((x-A.x)*(x-A.x)+(y-A.y)*(y-A.y));
51     }
52 } A[N];
53 int n, m;
54 
55 MAX_CLIQUE fuck;
56 
57 void build(double R) {
58     fuck.n=n;
59     for(int i=0; i<n; i++)
60         for(int j=0; j<n; j++) {
61             if(A[i].dis(A[j])>=R) fuck.G[i][j]=1;
62             else fuck.G[i][j]=0;
63         }
64 }
65 
66 int main() {
67     while(scanf("%d%d", &n, &m)!=EOF) {
68         for(int i=0; i<n; i++) {
69             scanf("%lf%lf", &A[i].x, &A[i].y);
70         }
71         double L=0, R=20000.0;
72         for(int T=0; T<40; T++) {
73             double dis=(L+R)/2.0;
74             build(dis);
75             if(fuck.MaxClique()>=m) L=dis;
76             else R=dis;
77         }
78         printf("%.2lf\n", L);
79     }
80     return 0;
81 }
HDU 3585

 

3、来一个一般无向图最大独立集的题目:POJ 1419 Graph Coloring

给了一个有 n 个点 m 条边的无向图,要求用黑、白两种色给图中顶点涂色,相邻的两个顶点不能涂成黑色,求最多能有多少顶点涂成黑色。图中最多有 100 个点

利用上面提到的结论:最大团点的数量=补图中最大独立集点的数量。建立补图,求最大团即可

 1 #include <iostream>
 2 #include <cstring>
 3 #include <cstdio>
 4 
 5 using namespace std;
 6 
 7 struct MAX_CLIQUE {
 8     static const int N=106;
 9 
10     bool G[N][N];
11     int Max[N], Alt[N][N], x[N], y[N];
12     int n, ans, *path, *res;
13 
14     bool DFS(int cur, int tot) {
15         if(cur==0) {
16             if(tot>ans) {
17                 swap(path, res), ans=tot;
18                 return 1;
19             }
20             return 0;
21         }
22         for(int i=0; i<cur; i++) {
23             if(cur-i+tot<=ans) return 0;
24             int u=Alt[tot][i];
25             if(Max[u]+tot<=ans) return 0;
26             int nxt=0;
27             for(int j=i+1; j<cur; j++)
28                 if(G[u][Alt[tot][j]]) Alt[tot+1][nxt++]=Alt[tot][j];
29             path[tot+1]=u;
30             if(DFS(nxt, tot+1)) return 1;
31         }
32         return 0;
33     }
34 
35     int MaxClique() {
36         ans=0, memset(Max, 0, sizeof Max);
37         path=x, res=y;
38         for(int i=n-1, cur; i>=0; i--) {
39             path[1]=i, cur=0;
40             for(int j=i+1; j<n; j++) if(G[i][j]) Alt[1][cur++]=j;
41             DFS(cur, 1);
42             Max[i]=ans;
43         }
44         return ans;
45     }
46 };
47 
48 MAX_CLIQUE fuck;
49 
50 int main() {
51     int T, m;
52     scanf("%d", &T);
53     for(int ca=1; ca<=T; ca++) {
54         scanf("%d%d", &fuck.n, &m);
55         memset(fuck.G, true, sizeof fuck.G);
56         for(int i=0, a, b; i<m; i++) {
57             scanf("%d%d", &a, &b);
58             fuck.G[a-1][b-1]=fuck.G[b-1][a-1]=0;
59         }
60         int ans=fuck.MaxClique();
61         printf("%d\n", ans);
62         for(int i=1; i<=ans; i++) {
63             printf("%d", fuck.res[i]+1);
64             if(i==ans) printf("\n"); else printf(" ");
65         }
66     }
67     return 0;
68 }
POJ 1419

 

4、来一个染色问题:POJ 1129 Channel Allocation

最多 26 广播电台...我还是讲抽象之后的题意吧:最多26个点的无向图,要求相邻的节点不能染成同一个颜色,问最少需要多少颜色染完所有的顶点

利用上面提到的结论:图的染色问题中,最少需要的颜色的数量=最大团点的数量,建图,跑最大团即可,另外,这题还需要构造解

 1 #include <iostream>
 2 #include <cstring>
 3 #include <cstdio>
 4 
 5 using namespace std;
 6 
 7 struct MAX_CLIQUE {
 8     static const int N=27;
 9 
10     bool G[N][N];
11     int n, Max[N], Alt[N][N], ans;
12 
13     bool DFS(int cur, int tot) {
14         if(cur==0) {
15             if(tot>ans) {
16                 ans=tot;
17                 return 1;
18             }
19             return 0;
20         }
21         for(int i=0; i<cur; i++) {
22             if(cur-i+tot<=ans) return 0;
23             int u=Alt[tot][i];
24             if(Max[u]+tot<=ans) return 0;
25             int nxt=0;
26             for(int j=i+1; j<cur; j++)
27                 if(G[u][Alt[tot][j]]) Alt[tot+1][nxt++]=Alt[tot][j];
28             if(DFS(nxt, tot+1)) return 1;
29         }
30         return 0;
31     }
32 
33     int MaxClique() {
34         ans=0, memset(Max, 0, sizeof Max);
35         for(int i=n-1; i>=0; i--) {
36             int cur=0;
37             for(int j=i+1; j<n; j++) if(G[i][j]) Alt[1][cur++]=j;
38             DFS(cur, 1);
39             Max[i]=ans;
40         }
41         return ans;
42     }
43 };
44 
45 MAX_CLIQUE fuck;
46 char buff[30];
47 
48 int main() {
49     while(scanf("%d", &fuck.n), fuck.n) {
50         memset(fuck.G, 0, sizeof fuck.G);
51         for(int i=0; i<fuck.n; i++) {
52             scanf("%s", buff);
53             for(int j=2; buff[j]; j++) {
54                 int u=buff[j]-'A';
55                 fuck.G[i][u]=fuck.G[u][i]=1;
56             }
57         }
58         int ans=fuck.MaxClique();
59         if(ans==1) printf("1 channel needed.\n");
60         else printf("%d channels needed. \n", ans);
61     }
62     return 0;
63 }
POJ 1129

 

先来这么4道题吧,暂时就遇见了这么多,和二分图有关的就懒得贴上来了

 

 

 

posted @ 2013-07-29 22:49  jianzhang.zj  阅读(17321)  评论(6编辑  收藏  举报