查找无向图的所有派系

时间:2016-06-23 18:33:40

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

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

1 个答案:

答案 0 :(得分: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;
}