#!/usr/bin/env python
from itertools import combinations
import igraph
import optparse
parser = optparse.OptionParser(usage="%prog [options] infile")
parser.add_option("-k", metavar="K", default=3, type=int,
help="use a clique size of K")
options, args = parser.parse_args()
if not args:
parser.error("Required input file as first argument")
k = options.k
g = igraph.load(args[0], format="ncol", directed=False)
cls = map(set, g.maximal_cliques(min=k))
edgelist = []
for i, j in combinations(range(len(cls)), 2):
if len(cls[i].intersection(cls[j])) >= k-1:
edgelist.append((i, j))
cg = igraph.Graph(edgelist, directed=False)
clusters = cg.clusters()
for cluster in clusters:
members = set()
for i in cluster:
members.update(cls[i])
print "\t".join(g.vs[members]["name"])
import networkx as nx
from itertools import combinations
def get_percolated_cliques(G, k):
perc_graph = nx.Graph()
cliques = list(frozenset(c) for c in nx.find_cliques(G) if len(c) >= k)
perc_graph.add_nodes_from(cliques)
# Add an edge in the clique graph for each pair of cliques that percolate
for c1, c2 in combinations(cliques, 2):
if len(c1.intersection(c2)) >= (k - 1):
perc_graph.add_edge(c1, c2)
for component in nx.connected_components(perc_graph):
yield(frozenset.union(*component))
如果你不介意使用另一种编程语言,你有CFinder(java),它是基于团渗滤(它基本上寻找紧密连接的团),OSLOM(C++),它优化统计度量,当然也有其他的。在
否则,如果您想坚持使用python,也可以通过Evans & Lambiotte '09来应用该方法:1)将图形转换为线图,2)对其应用常规的社区检测方法,例如使用igraph,以及3)获得重叠的社区。将图形转换为线图看起来不太复杂,而且应该很快,前提是原始图形不太大。无论如何,它比执行社区检测本身要快。在
注意,除了Evans&Lambiotte的方法之外,还有其他方法可以从常规(互斥的社区)方法中获取重叠的社区,例如Bennet et al. '12或{a5}。然而,实现它们似乎不那么简单。在
我不久前使用igraph的Python接口实现了Ahn等人的hierarchical link clustering算法;请参阅其源代码here。在
另外,使用igraph在Python中实现CFinder相当容易;这就是我想到的:
根据这个blog,networkx现在可以计算重叠的社区。在
下面的代码用于团过滤方法,可在Networkx 11.6中找到。Githubhere
相关问题 更多 >
编程相关推荐