【发布时间】:2021-08-01 10:35:04
【问题描述】:
我使用networkx库实现了算法 Bron-Kerbosch 库来查找图 G 中的所有最大团。为了加快我的分析,在非常大的图中,我想修改此算法以仅从图 G 中提取一个最大团。修改此算法是最佳解决方案,还是有更高效的算法?
from typing import Iterator, List
import networkx as nx
def bron_kerbosch_1(r: list, p: list, x: list, graph: nx.classes.graph.Graph) -> Iterator[List]:
"""Basic form (i.e., without pivoting) of the Bron–Kerbosch algorithm.
"""
if not any((p, x)):
yield r
for node in p[:]:
r_new = r + [node] # R ⋃ {v}
p_new = list(set(p).intersection(set(graph.neighbors(node)))) # P ⋂ N(v)
x_new = list(set(x).intersection(set(graph.neighbors(node)))) # X ⋂ N(v)
yield from BronKerbosch.bron_kerbosch_1(r_new, p_new, x_new, graph)
p.remove(node)
x.append(node)
G = nx.Graph()
edges = [(0, 1), (0, 4), (1, 4), (1, 2), (2, 3), (3, 4), (3, 5)]
G.add_edges_from(edges)
nodes = list(G.nodes)
maximal_cliques = list(bron_kerbosch_1(r=[], p=nodes[:], x=[], graph=G))
print(maximal_cliques)
>> [[0, 4, 1], [2, 1], [2, 3], [4, 3], [5, 3]]
【问题讨论】: