|
| 1 | +from itertools import combinations |
| 2 | +from random import seed |
| 3 | + |
| 4 | +from igraph import Graph as LegacyGraph |
| 5 | + |
| 6 | +from igraph_ctypes.constructors import ( |
| 7 | + create_graph_from_edge_list, |
| 8 | + create_geometric_random_graph, |
| 9 | +) |
| 10 | +from igraph_ctypes.conversion import get_edge_list |
| 11 | +from igraph_ctypes.paths import components |
| 12 | +from igraph_ctypes._internal.functions import maximal_cliques |
| 13 | + |
| 14 | +seed(42) |
| 15 | + |
| 16 | +n, r, k = 1600, 0.0625, 4 |
| 17 | + |
| 18 | +new_g = create_geometric_random_graph(n, r) |
| 19 | +old_g = LegacyGraph(get_edge_list(new_g).tolist()) |
| 20 | + |
| 21 | + |
| 22 | +def _gen_pairs(sets, threshold: int): |
| 23 | + return ( |
| 24 | + (u, v) |
| 25 | + for u, v in combinations(range(len(sets)), 2) |
| 26 | + if u != v and len(sets[u] & sets[v]) >= threshold |
| 27 | + ) |
| 28 | + |
| 29 | + |
| 30 | +def _find_edgelist_pairs(sets, threshold: int): |
| 31 | + return list(_gen_pairs(sets, threshold)) |
| 32 | + |
| 33 | + |
| 34 | +def _find_edgelist(sets, threshold: int): |
| 35 | + result = [] |
| 36 | + for pair in _gen_pairs(sets, threshold): |
| 37 | + result.extend(pair) |
| 38 | + return result |
| 39 | + |
| 40 | + |
| 41 | +def clique_percolation_with_old_igraph(): |
| 42 | + cliques = [set(cl) for cl in old_g.maximal_cliques(min=k)] |
| 43 | + el = _find_edgelist_pairs(cliques, k - 1) |
| 44 | + clique_g = LegacyGraph(el, directed=False) |
| 45 | + clique_g.components() |
| 46 | + |
| 47 | + |
| 48 | +def clique_percolation_with_new_igraph(): |
| 49 | + cliques = [set(cl) for cl in maximal_cliques(new_g, min_size=4)] |
| 50 | + el = _find_edgelist(cliques, k - 1) |
| 51 | + clique_g = create_graph_from_edge_list(el, directed=False) |
| 52 | + components(clique_g) |
| 53 | + |
| 54 | + |
| 55 | +__benchmarks__ = [ |
| 56 | + ( |
| 57 | + clique_percolation_with_old_igraph, |
| 58 | + clique_percolation_with_new_igraph, |
| 59 | + "Clique percolation method", |
| 60 | + ) |
| 61 | +] |
0 commit comments