-1

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

4

1 回答 1

0

最佳解决方案是这样的,因为在一个完整的图中有 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;
}
于 2016-07-01T22:49:15.570 回答