最大团 Bron–Kerbosch算法

详细讲解   http://www.cnblogs.com/yefeng1627/archive/2013/03/31/2991592.html


#define N 110   //点从1到n
bool g[N][N];
int que[N],cnt[N];//cnt[i]记录大于等于i的点集的最大团点数,i点可以不在其中
int ans;//ans为最终最大团点数
bool dfs(int pos,int num,int n)
{
    FOR(i,pos+1,n)
    {
        if(num+cnt[i]<=ans) return 0;
        //如果取i 但是cnt[i]也就是 >= i 的最大团点数 + 已经取了的点数还小于 ans, 剪枝
        if(g[pos][i])
        {
            int j;
            for(j=0; j<num; ++j) if(!g[i][ que[j] ]) break;
            if(j==num)
            {
                que[num]=i;
                if(dfs(i,num+1,n)) return 1;
            }
        }
    }
    if(num>ans)//因为每填加一个点最多使最大团数+1,后面的搜索就没有意义了
    {
        ans=num;
        return 1;//如果需要输出方案的话,此时的que内的点即为方案
    }
    return 0;
}
void solve(int n)
{
    ans=0;
    DSC(i,n,1)
    {
        que[0]=i;
        dfs(i,1,n);
        cnt[i]=ans;
    }
}


  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
回答:可以使用Bron-Kerbosch算法求解最大团问题,具体实现可以参考以下Java代码: ``` import java.util.ArrayList; import java.util.HashSet; import java.util.List; import java.util.Set; public class MaxClique { public static Set<Integer> maxClique(List<Integer>[] graph) { Set<Integer> candidates = new HashSet<>(); Set<Integer> selected = new HashSet<>(); for (int i = 0; i < graph.length; i++) { candidates.add(i); } return maxClique(graph, candidates, selected); } private static Set<Integer> maxClique(List<Integer>[] graph, Set<Integer> candidates, Set<Integer> selected) { if (candidates.isEmpty() && selected.isEmpty()) { return new HashSet<>(); } if (candidates.isEmpty()) { return new HashSet<>(selected); } Set<Integer> maxClique = new HashSet<>(); int pivot = choosePivot(candidates, selected, graph); for (int v : candidates) { if (!graph[pivot].contains(v)) { continue; } Set<Integer> newCandidates = new HashSet<>(); Set<Integer> newSelected = new HashSet<>(); for (int u : candidates) { if (graph[v].contains(u)) { newCandidates.add(u); } } for (int u : selected) { if (graph[v].contains(u)) { newSelected.add(u); } } newSelected.add(v); Set<Integer> clique = maxClique(graph, newCandidates, newSelected); if (clique.size() > maxClique.size()) { maxClique = clique; } } return maxClique; } private static int choosePivot(Set<Integer> candidates, Set<Integer> selected, List<Integer>[] graph) { int maxScore = -1; int pivot = -1; for (int v : candidates) { int score = 0; for (int u : selected) { if (graph[v].contains(u)) { score++; } } if (score > maxScore) { maxScore = score; pivot = v; } } return pivot; } public static void main(String[] args) { int n = 5; List<Integer>[] graph = new List[n]; for (int i = 0; i < n; i++) { graph[i] = new ArrayList<>(); } graph[0].add(1); graph[0].add(2); graph[1].add(0); graph[1].add(2); graph[1].add(3); graph[2].add(0); graph[2].add(1); graph[2].add(3); graph[2].add(4); graph[3].add(1); graph[3].add(2); graph[3].add(4); graph[4].add(2); graph[4].add(3); Set<Integer> maxClique = maxClique(graph); System.out.println(maxClique); } } ``` 如果您有任何问题,请随时提出。另外,笑话时间到了:为什么光速是每秒299792458米?因为如果是300000000米,我们就要说“三亿”了,这太长了。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值