8

对于一个大学项目,我正在尝试实现Bron–Kerbosch 算法,即在给定图中列出所有最大派系。

我正在尝试实现第一个算法(没有旋转),但我的代码在维基百科的示例中测试后并没有产生所有答案,到目前为止我的代码是:

# dealing with a graph as list of lists 
graph = [[0,1,0,0,1,0],[1,0,1,0,1,0],[0,1,0,1,0,0],[0,0,1,0,1,1],[1,1,0,1,0,0],[0,0,0,1,0,0]]


#function determines the neighbors of a given vertex
def N(vertex):
    c = 0
    l = []
    for i in graph[vertex]:
        if i is 1 :
         l.append(c)
        c+=1   
    return l 

#the Bron-Kerbosch recursive algorithm
def bronk(r,p,x):
    if len(p) == 0 and len(x) == 0:
        print r
        return
    for vertex in p:
        r_new = r[::]
        r_new.append(vertex)
        p_new = [val for val in p if val in N(vertex)] # p intersects N(vertex)
        x_new = [val for val in x if val in N(vertex)] # x intersects N(vertex)
        bronk(r_new,p_new,x_new)
        p.remove(vertex)
        x.append(vertex)


    bronk([], [0,1,2,3,4,5], [])

为什么我只得到部分答案有什么帮助?

4

3 回答 3

8

Python 变得困惑,因为您正在修改它正在迭代的列表。

改变

for vertex in p:

for vertex in p[:]:

这将导致它迭代 p 的副本。

您可以在http://effbot.org/zone/python-list.htm阅读更多相关信息。

于 2012-12-16T19:39:37.667 回答
7

正如@VaughnCato 正确指出的那样,错误正在迭代P[:]。我认为值得注意的是,您可以“产生”此结果,而不是打印,如下所示(在此重构代码中):

def bronk2(R, P, X, g):
    if not any((P, X)):
        yield R
    for v in P[:]:
        R_v = R + [v]
        P_v = [v1 for v1 in P if v1 in N(v, g)]
        X_v = [v1 for v1 in X if v1 in N(v, g)]
        for r in bronk2(R_v, P_v, X_v, g):
            yield r
        P.remove(v)
        X.append(v)
def N(v, g):
    return [i for i, n_v in enumerate(g[v]) if n_v]

In [99]: list(bronk2([], range(6), [], graph))
Out[99]: [[0, 1, 4], [1, 2], [2, 3], [3, 4], [3, 5]]

如果有人在未来寻找 Bron-Kerbosch 算法实现......

于 2012-12-16T21:35:23.480 回答
3

来自维基百科的 Bron-Kerbosch 算法的实现:

无需旋转

算法BronKerbosch1(R, P, X)
    如果 P  X都是空的,则将R
        报告为P中每个顶点v  最大集团
    do 
        BronKerbosch1( R ⋃ { v }, PN ( v ), XN ( v ))
         P  := P \ { v }
         X  := X ⋃ { v }
 
adj_matrix = [
    [0, 1, 0, 0, 1, 0],
    [1, 0, 1, 0, 1, 0],
    [0, 1, 0, 1, 0, 0],
    [0, 0, 1, 0, 1, 1],
    [1, 1, 0, 1, 0, 0],
    [0, 0, 0, 1, 0, 0]]

图形

N = {
    i: set(num for num, j in enumerate(row) if j)
    for i, row in enumerate(adj_matrix)
}

print(N)
# {0: {1, 4}, 1: {0, 2, 4}, 2: {1, 3}, 3: {2, 4, 5}, 4: {0, 1, 3}, 5: {3}}

def BronKerbosch1(P, R=None, X=None):
    P = set(P)
    R = set() if R is None else R
    X = set() if X is None else X
    if not P and not X:
        yield R
    while P:
        v = P.pop()
        yield from BronKerbosch1(
            P=P.intersection(N[v]), R=R.union([v]), X=X.intersection(N[v]))
        X.add(v)

P = N.keys()
print(list(BronKerbosch1(P)))
# [{0, 1, 4}, {1, 2}, {2, 3}, {3, 4}, {3, 5}]

带旋转

算法BronKerbosch2(R, P, X)
    如果 PX都是空的,则将R
        报告为最大集团为P \ N( u )的每个顶点v选择PX中
    的枢轴顶点u
      
        BronKerbosch2( R ⋃ { v }, P ⋂ N( v ), X ⋂ N( v ))
         P  := P \ { v }
         X  := X ⋃ { v }

import random  

def BronKerbosch2(P, R=None, X=None):
    P = set(P)
    R = set() if R is None else R
    X = set() if X is None else X
    if not P and not X:
        yield R
    try:
        u = random.choice(list(P.union(X)))
        S = P.difference(N[u])
    # if union of P and X is empty
    except IndexError:
        S = P
    for v in S:
        yield from BronKerbosch2(
            P=P.intersection(N[v]), R=R.union([v]), X=X.intersection(N[v]))
        P.remove(v)
        X.add(v)
  
print(list(BronKerbosch2(P)))
# [{0, 1, 4}, {1, 2}, {2, 3}, {3, 4}, {3, 5}]
于 2019-12-14T22:04:03.120 回答