Created
November 5, 2011 19:50
-
-
Save conradlee/1341933 to your computer and use it in GitHub Desktop.
Clique percolation in Python using NetworkX
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) |
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
How I can get the output as a community from this code??