Skip to content

Instantly share code, notes, and snippets.

@conradlee
Created November 5, 2011 19:50
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save conradlee/1341933 to your computer and use it in GitHub Desktop.
Save conradlee/1341933 to your computer and use it in GitHub Desktop.
Clique percolation in Python using NetworkX
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))
@neha30
Copy link

neha30 commented Jun 15, 2017

How I can get the output as a community from this code??

@b1sakher
Copy link

The function returns a Generator.
Generators are iterators, a kind of iterable you can only iterate over once. Generators do not store all the values in memory, they generate the values on the fly:

mygenerator = get_percolated_cliques(g,k)
for i in mygenerator:
... print(i)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment