kcomponents.py 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. """
  2. Moody and White algorithm for k-components
  3. """
  4. from collections import defaultdict
  5. from itertools import combinations
  6. from operator import itemgetter
  7. import networkx as nx
  8. # Define the default maximum flow function.
  9. from networkx.algorithms.flow import edmonds_karp
  10. from networkx.utils import not_implemented_for
  11. default_flow_func = edmonds_karp
  12. __all__ = ["k_components"]
  13. @not_implemented_for("directed")
  14. def k_components(G, flow_func=None):
  15. r"""Returns the k-component structure of a graph G.
  16. A `k`-component is a maximal subgraph of a graph G that has, at least,
  17. node connectivity `k`: we need to remove at least `k` nodes to break it
  18. into more components. `k`-components have an inherent hierarchical
  19. structure because they are nested in terms of connectivity: a connected
  20. graph can contain several 2-components, each of which can contain
  21. one or more 3-components, and so forth.
  22. Parameters
  23. ----------
  24. G : NetworkX graph
  25. flow_func : function
  26. Function to perform the underlying flow computations. Default value
  27. :meth:`edmonds_karp`. This function performs better in sparse graphs with
  28. right tailed degree distributions. :meth:`shortest_augmenting_path` will
  29. perform better in denser graphs.
  30. Returns
  31. -------
  32. k_components : dict
  33. Dictionary with all connectivity levels `k` in the input Graph as keys
  34. and a list of sets of nodes that form a k-component of level `k` as
  35. values.
  36. Raises
  37. ------
  38. NetworkXNotImplemented
  39. If the input graph is directed.
  40. Examples
  41. --------
  42. >>> # Petersen graph has 10 nodes and it is triconnected, thus all
  43. >>> # nodes are in a single component on all three connectivity levels
  44. >>> G = nx.petersen_graph()
  45. >>> k_components = nx.k_components(G)
  46. Notes
  47. -----
  48. Moody and White [1]_ (appendix A) provide an algorithm for identifying
  49. k-components in a graph, which is based on Kanevsky's algorithm [2]_
  50. for finding all minimum-size node cut-sets of a graph (implemented in
  51. :meth:`all_node_cuts` function):
  52. 1. Compute node connectivity, k, of the input graph G.
  53. 2. Identify all k-cutsets at the current level of connectivity using
  54. Kanevsky's algorithm.
  55. 3. Generate new graph components based on the removal of
  56. these cutsets. Nodes in a cutset belong to both sides
  57. of the induced cut.
  58. 4. If the graph is neither complete nor trivial, return to 1;
  59. else end.
  60. This implementation also uses some heuristics (see [3]_ for details)
  61. to speed up the computation.
  62. See also
  63. --------
  64. node_connectivity
  65. all_node_cuts
  66. biconnected_components : special case of this function when k=2
  67. k_edge_components : similar to this function, but uses edge-connectivity
  68. instead of node-connectivity
  69. References
  70. ----------
  71. .. [1] Moody, J. and D. White (2003). Social cohesion and embeddedness:
  72. A hierarchical conception of social groups.
  73. American Sociological Review 68(1), 103--28.
  74. http://www2.asanet.org/journals/ASRFeb03MoodyWhite.pdf
  75. .. [2] Kanevsky, A. (1993). Finding all minimum-size separating vertex
  76. sets in a graph. Networks 23(6), 533--541.
  77. http://onlinelibrary.wiley.com/doi/10.1002/net.3230230604/abstract
  78. .. [3] Torrents, J. and F. Ferraro (2015). Structural Cohesion:
  79. Visualization and Heuristics for Fast Computation.
  80. https://arxiv.org/pdf/1503.04476v1
  81. """
  82. # Dictionary with connectivity level (k) as keys and a list of
  83. # sets of nodes that form a k-component as values. Note that
  84. # k-components can overlap (but only k - 1 nodes).
  85. k_components = defaultdict(list)
  86. # Define default flow function
  87. if flow_func is None:
  88. flow_func = default_flow_func
  89. # Bicomponents as a base to check for higher order k-components
  90. for component in nx.connected_components(G):
  91. # isolated nodes have connectivity 0
  92. comp = set(component)
  93. if len(comp) > 1:
  94. k_components[1].append(comp)
  95. bicomponents = [G.subgraph(c) for c in nx.biconnected_components(G)]
  96. for bicomponent in bicomponents:
  97. bicomp = set(bicomponent)
  98. # avoid considering dyads as bicomponents
  99. if len(bicomp) > 2:
  100. k_components[2].append(bicomp)
  101. for B in bicomponents:
  102. if len(B) <= 2:
  103. continue
  104. k = nx.node_connectivity(B, flow_func=flow_func)
  105. if k > 2:
  106. k_components[k].append(set(B))
  107. # Perform cuts in a DFS like order.
  108. cuts = list(nx.all_node_cuts(B, k=k, flow_func=flow_func))
  109. stack = [(k, _generate_partition(B, cuts, k))]
  110. while stack:
  111. (parent_k, partition) = stack[-1]
  112. try:
  113. nodes = next(partition)
  114. C = B.subgraph(nodes)
  115. this_k = nx.node_connectivity(C, flow_func=flow_func)
  116. if this_k > parent_k and this_k > 2:
  117. k_components[this_k].append(set(C))
  118. cuts = list(nx.all_node_cuts(C, k=this_k, flow_func=flow_func))
  119. if cuts:
  120. stack.append((this_k, _generate_partition(C, cuts, this_k)))
  121. except StopIteration:
  122. stack.pop()
  123. # This is necessary because k-components may only be reported at their
  124. # maximum k level. But we want to return a dictionary in which keys are
  125. # connectivity levels and values list of sets of components, without
  126. # skipping any connectivity level. Also, it's possible that subsets of
  127. # an already detected k-component appear at a level k. Checking for this
  128. # in the while loop above penalizes the common case. Thus we also have to
  129. # _consolidate all connectivity levels in _reconstruct_k_components.
  130. return _reconstruct_k_components(k_components)
  131. def _consolidate(sets, k):
  132. """Merge sets that share k or more elements.
  133. See: http://rosettacode.org/wiki/Set_consolidation
  134. The iterative python implementation posted there is
  135. faster than this because of the overhead of building a
  136. Graph and calling nx.connected_components, but it's not
  137. clear for us if we can use it in NetworkX because there
  138. is no licence for the code.
  139. """
  140. G = nx.Graph()
  141. nodes = dict(enumerate(sets))
  142. G.add_nodes_from(nodes)
  143. G.add_edges_from(
  144. (u, v) for u, v in combinations(nodes, 2) if len(nodes[u] & nodes[v]) >= k
  145. )
  146. for component in nx.connected_components(G):
  147. yield set.union(*[nodes[n] for n in component])
  148. def _generate_partition(G, cuts, k):
  149. def has_nbrs_in_partition(G, node, partition):
  150. return any(n in partition for n in G[node])
  151. components = []
  152. nodes = {n for n, d in G.degree() if d > k} - {n for cut in cuts for n in cut}
  153. H = G.subgraph(nodes)
  154. for cc in nx.connected_components(H):
  155. component = set(cc)
  156. for cut in cuts:
  157. for node in cut:
  158. if has_nbrs_in_partition(G, node, cc):
  159. component.add(node)
  160. if len(component) < G.order():
  161. components.append(component)
  162. yield from _consolidate(components, k + 1)
  163. def _reconstruct_k_components(k_comps):
  164. result = {}
  165. max_k = max(k_comps)
  166. for k in reversed(range(1, max_k + 1)):
  167. if k == max_k:
  168. result[k] = list(_consolidate(k_comps[k], k))
  169. elif k not in k_comps:
  170. result[k] = list(_consolidate(result[k + 1], k))
  171. else:
  172. nodes_at_k = set.union(*k_comps[k])
  173. to_add = [c for c in result[k + 1] if any(n not in nodes_at_k for n in c)]
  174. if to_add:
  175. result[k] = list(_consolidate(k_comps[k] + to_add, k))
  176. else:
  177. result[k] = list(_consolidate(k_comps[k], k))
  178. return result
  179. def build_k_number_dict(kcomps):
  180. result = {}
  181. for k, comps in sorted(kcomps.items(), key=itemgetter(0)):
  182. for comp in comps:
  183. for node in comp:
  184. result[node] = k
  185. return result