To get community kernels with most degrees. Parameters ---------- G : graph An undirected graph. C : int #communities Returns ------- kernels
(G, C: List[frozenset], weight="weight")
| 8 | |
| 9 | @not_implemented_for("multigraph") |
| 10 | def get_community_kernel(G, C: List[frozenset], weight="weight"): |
| 11 | """ |
| 12 | To get community kernels with most degrees. |
| 13 | Parameters |
| 14 | ---------- |
| 15 | G : graph |
| 16 | An undirected graph. |
| 17 | C : int |
| 18 | #communities |
| 19 | |
| 20 | Returns |
| 21 | ------- |
| 22 | kernels |
| 23 | """ |
| 24 | area = [] |
| 25 | for i in range(len(G)): |
| 26 | area.append(0) |
| 27 | for i, cc in enumerate(C): |
| 28 | for each_node in cc: |
| 29 | area[each_node - 1] += 1 << i # node_id from 1 to n. |
| 30 | kernels = [] |
| 31 | cnt = 0 |
| 32 | for i in range(len(C)): |
| 33 | mask = 1 << i |
| 34 | cnt += 1 |
| 35 | q = [] |
| 36 | p = [] |
| 37 | for i in range(len(G)): |
| 38 | if (area[i] & mask) == mask: |
| 39 | q.append((G.degree(weight=weight)[i + 1], i + 1)) |
| 40 | q.sort() |
| 41 | q.reverse() |
| 42 | for i in range( |
| 43 | max(int(len(q) / 100), min(2, len(q))) |
| 44 | ): # latter of min for test. |
| 45 | p.append(q[i][1]) |
| 46 | kernels.append(p) |
| 47 | if len(kernels) < 2: |
| 48 | print("ERROR: WE should have at least 2 communities.") |
| 49 | for i in range(len(kernels)): |
| 50 | if len(kernels[i]) == 0: |
| 51 | print("Community %d is too small." % i) |
| 52 | return None |
| 53 | return kernels |
| 54 | |
| 55 | |
| 56 | def get_structural_holes_MaxD(G, k, C: List[frozenset]): |
no test coverage detected