【问题标题】:Finding All Cliques of an Undirected Graph查找无向图的所有团
【发布时间】:2016-06-23 18:33:39
【问题描述】:

如何列出无向图的所有派系? (并非所有最大派系,如 Bron-Kerbosch 算法)

【问题讨论】:

  • 我仍在寻找算法。但是我找到了 Bron-Kerbosch 算法的一些代码(但问题是,该算法返回所有 maximal 派系,而不是所有派系)
  • 这不是 StackOverflow 的工作方式。我们帮助您解决特定的代码问题,而不是家庭作业类型的算法问题。如果您有尝试使用的特定代码,但由于某种原因无法正常工作,请分享。否则,请尝试在programmers.stackexchange.com 询问。

标签: graph-theory graph-algorithm clique-problem


【解决方案1】:

最优解是这样的,因为在一个完整的图中有 2^n 个团。考虑使用递归函数的所有节点子集。对于每个子集,如果子集的节点之间存在所有边,则将计数器加 1:(这几乎是 C++ 中的伪代码)

int clique_counter = 0;
int n; //number of nodes in graph
//i imagine nodes are numbered from 1 to n

void f(int x, vector <int> &v){ //x is the current node
    if(x == n){
        bool is_clique = true;
        for(int i = 0; i < v.size(); i++){
            for(int j = i + 1; j < v.size(); j++){
                if(there is not an edge between node v[i] and v[j]) //it can't be clique
                    is_clique = false;
            }
        }
        if(is_clique == true){
            clique_counter++;
        }
        return;
    }

    //if x < n

    f(x + 1, v);

    v.push_back(x);
    f(x + 1, v);
    v.pop_back();
}


int main(){
    vector <int> v;
    f(1, v);
    cout << clique_counter << endl;

    return 0;
}

【讨论】:

    猜你喜欢
    • 2014-01-02
    • 2011-04-30
    • 2022-01-21
    • 2013-11-25
    • 2020-10-09
    • 2020-04-02
    • 1970-01-01
    • 2010-10-07
    • 1970-01-01
    相关资源
    最近更新 更多