最大团与极大团的学习

最大团与极大团的学习

POJ 1419
数据非常弱,暴力搜索可以在0ms拿下……那个n完全就是吓人的……
正解是求图的最大独立集,和最大团是对偶关系,用原图的补图就最大团就可以了
最最暴力的搜索代码如下
复制代码
      
      
      
      
#include < iostream >
#include
< cstring >
#include
< cstdio >
using namespace std;
const int SIZE = 100 ;
int ans[SIZE], tmp[SIZE], maps[SIZE][SIZE], dgr[SIZE];
int maxClique, n, m;
bool judge( int x, int now)
{
for ( int i = 0 ;i < now;i ++ )
if ( ! maps[x][tmp[i]])
return false ;
return true ;
}
void dfs( int x, int now)
{
if (x >= n)
return ;
if (now > maxClique)
{
maxClique
= now;
for ( int i = 0 ;i < now;i ++ )
ans[i]
= tmp[i];
}
dfs(x
+ 1 , now);
if (dgr[x] >= now)
{
if (judge(x, now))
{
tmp[now]
= x;
dfs(x
+ 1 , now + 1 );
}
}
else
{
for ( int i = 0 ;i < n;i ++ )
dgr[i]
-- ;
dfs(x
+ 1 , now);
for ( int i = 0 ;i < n;i ++ )
dgr[i]
++ ;
}
}

int main()
{
int cases = 0 ;
scanf(
" %d " , & cases);
while (cases -- )
{
scanf(
" %d %d " , & n, & m);
int a, b;
memset(maps,
true , sizeof (maps));
fill(dgr, dgr
+ n, n - 1 );
for ( int i = 0 ;i < m;i ++ )
{
scanf(
" %d %d " , & a, & b);
maps[a
- 1 ][b - 1 ] = 0 ;
dgr[a
- 1 ] ++ ;
maps[b
- 1 ][a - 1 ] = 0 ;
dgr[b
- 1 ] ++ ;
}
maxClique
= 0 ;
dfs(
0 , 0 );
printf(
" %d\n " , maxClique);
for ( int i = 0 ;i < maxClique;i ++ )
printf(
" %d " , ans[i] + 1 );
puts(
"" );
}
return 0 ;
}
复制代码

ZOJ 1492 赤裸裸的最大团模板题
用来校验模板不错
我的模板
复制代码
      
      
      
      
1 class MaxClique
2 {
3 public :
4 void addedge( int i, int j)
5 {
6 maps[i][j] = true ;
7 maps[j][i] = true ;
8 }
9 void initialize( int n)
10 {
11 memset(maps, false , sizeof (maps));
12 this -> n = n;
13 mc = 0 ;
14 }
15 int getAnswer( int * ans = 0 )
16 {
17 findMaxClique(n);
18 if (ans != 0 )
19 copy(answer, answer + mc, ans);
20 return mc;
21 }
22 private :
23 const static int N = 50 ;
24 bool maps[N][N], found;
25 int c[N], answer[N], record[N];
26 int mc, n;
27 void findMaxClique( int n)
28 {
29 for ( int i = n - 1 ;i >= 0 ;i -- )
30 {
31 found = false ;
32 int tail = 0 , s[N];
33 for ( int j = i + 1 ;j < n;j ++ )
34 if (maps[i][j])
35 s[tail ++ ] = j;
36 record[ 0 ] = i;
37 dfs(tail, s, 1 );
38 c[i] = mc;
39 }
40 }
41 void dfs( int GraphSize, int * s, int CliqueSize)
42 {
43 if (GraphSize == 0 )
44 {
45 if (CliqueSize > mc)
46 {
47 mc = CliqueSize;
48 found = true ;
49 copy(record, record + mc, answer);
50 }
51 return ;
52 }
53 for ( int i = 0 ;i < GraphSize;i ++ )
54 {
55 if (CliqueSize + GraphSize <= mc || c[s[i]] + CliqueSize <= mc)
56 return ;
57 int tmps[N],tmpSize = 0 ;
58 record[CliqueSize] = s[i];
59 for ( int j = i + 1 ;j < GraphSize;j ++ )
60 if (maps[s[i]][s[j]])
61 tmps[tmpSize ++ ] = s[j];
62 dfs(tmpSize, tmps, CliqueSize + 1 );
63 if (found)
64 return ;
65 }
66
67 }
68 };
复制代码


POJ 1129 && HOJ 1115
以前认为图的染色数等于最大团中的节点的个数

但是是错误的!!!例如一个图只有一个圈,圈长为大于3的奇数,此时3着色,但是最大团为2!

数据弱可以水过!正解是搜索


POJ 2989 && HOJ 2279 All friends
有待研究
学习最大团的好资料

http://bchine.com/mjmjmtl/wp-content/uploads/2010/03/MaxClique.pdf
wiki的极大团介绍
http://en.wikipedia.org/wiki/Bron%E2%80%93Kerbosch_algorithm


最大团和极大团的关系

maximal cliques, whose vertices are not a subset of the vertices of a larger clique,
and maximum cliques, which are the largest among all cliques in a graph (maximum
cliques are clearly maximal
论文中的原话

你可能感兴趣的:(最大团与极大团的学习)