首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >最大团优化

最大团优化
EN

Stack Overflow用户
提问于 2020-10-04 10:41:45
回答 1查看 111关注 0票数 1

我正在尝试寻找一个图(邻接矩阵)中的最大团,这个图可以有50个节点。目前,当图的大小达到n=40左右时,我所拥有的开始永远耗费时间。有没有人能找到我能做的优化?

代码语言:javascript
复制
  public static void maxClique(Graph graph, List<Integer> clique) {
    //Get the newest node added to the clique
    int currentValue = clique.get(clique.size() - 1);

    if (clique.size() > max.size()) {
      max = clique;
    }

    // Check every node
    for (int i = 0; i < graph.size; i++) {

      // If the node being checked is connected to the current node, and isn't the current node
      if (graph.matrix[currentValue][i] == 1 && !clique.contains(i)) {
        //Check if the new clique is bigger than the current max.


        //Make a new clique and add all the nodes from the current clique to it
        ArrayList<Integer> newClique = new ArrayList<>();
        newClique.addAll(clique);
        //Add the new node
        newClique.add(i);

        //Repeat
        if (makesNewClique(graph, clique, i)) {
          maxClique(graph, newClique);
        }
      }
    }
  }

全班内容:https://pastebin.com/fNPjvgUm邻接矩阵:https://pastebin.com/yypN9K4L

EN

回答 1

Stack Overflow用户

发布于 2020-10-04 13:00:21

David Eisenstat的评论给出了答案,这是我为稍后来到这里的任何人提供的代码:

代码语言:javascript
复制
  public void bronKerbosh(Set<Integer> p, Set<Integer> r, Set<Integer> x) {
    if (x.isEmpty() && p.isEmpty() && r.size() > max.size()) {
      max = new ArrayList<>();
      max.addAll(r);
    }

    Object[] pArr = p.toArray();
    for (Object vTemp : pArr) {
      int v = (int)vTemp;

      Set<Integer> newR = new HashSet<>();
      newR.addAll(r);
      newR.add(v);

      bronKerbosh(intersect(p, neighbors(v)), newR, intersect(x, neighbors(v)));
      p.remove(v);
      x.add(v);
    }
  }
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/64190700

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档