在python中找到最大的clique

时间:2017-09-27 07:25:17

标签: python graph networkx

我最近试图将最大clique算法移植到python,但我似乎无法正确实现它。目标是找到图中最大的团。我的目标是实施,在this文章,图1(简单变体)中说明。我目前的代码如下:

import networkx as nx
import operator

if __name__ == "__main__":


    ## read the graph
    G = nx.Graph()
    G.add_edge(1,2)
    G.add_edge(1,3)
    G.add_edge(3,2)
    G.add_edge(3,4)


    C = nx.coloring.greedy_color(G, strategy=nx.coloring.strategy_largest_first)
    R = G.nodes()

    qu = []
    qmax = []    

    def maxClique(R,C,qu,qmax):

        while len(R) > 0:
            cdict= sorted(C.items(), key=operator.itemgetter(1),reverse=True)
            pivot = cdict[0][0]
            color = cdict[0][1]
            R.remove(pivot)

            if len(set(qu)) + int(color) > len(set(qmax)):
                qu.append(pivot)
                neighbours = G.neighbors(pivot)                
                intersection = [x for x in R if x in neighbours]

                if len(intersection) > 0:
                    subgraph = G.subgraph(intersection)
                    Cx = nx.coloring.greedy_color(subgraph, strategy=nx.coloring.strategy_largest_first)
                    maxClique(intersection,Cx,list(set(qu)),qmax)

                elif len(qu) > len(qmax):
                    qmax = qu
                    qu = qu.remove(pivot)

                else:
                    return qmax

    print(maxClique(R,C,qu,qmax))

因此,算法必须将{1,2,3}标识为max-clique。

0 个答案:

没有答案