redundancy.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. """Node redundancy for bipartite graphs."""
  2. from itertools import combinations
  3. from networkx import NetworkXError
  4. __all__ = ["node_redundancy"]
  5. def node_redundancy(G, nodes=None):
  6. r"""Computes the node redundancy coefficients for the nodes in the bipartite
  7. graph `G`.
  8. The redundancy coefficient of a node `v` is the fraction of pairs of
  9. neighbors of `v` that are both linked to other nodes. In a one-mode
  10. projection these nodes would be linked together even if `v` were
  11. not there.
  12. More formally, for any vertex `v`, the *redundancy coefficient of `v`* is
  13. defined by
  14. .. math::
  15. rc(v) = \frac{|\{\{u, w\} \subseteq N(v),
  16. \: \exists v' \neq v,\: (v',u) \in E\:
  17. \mathrm{and}\: (v',w) \in E\}|}{ \frac{|N(v)|(|N(v)|-1)}{2}},
  18. where `N(v)` is the set of neighbors of `v` in `G`.
  19. Parameters
  20. ----------
  21. G : graph
  22. A bipartite graph
  23. nodes : list or iterable (optional)
  24. Compute redundancy for these nodes. The default is all nodes in G.
  25. Returns
  26. -------
  27. redundancy : dictionary
  28. A dictionary keyed by node with the node redundancy value.
  29. Examples
  30. --------
  31. Compute the redundancy coefficient of each node in a graph::
  32. >>> from networkx.algorithms import bipartite
  33. >>> G = nx.cycle_graph(4)
  34. >>> rc = bipartite.node_redundancy(G)
  35. >>> rc[0]
  36. 1.0
  37. Compute the average redundancy for the graph::
  38. >>> from networkx.algorithms import bipartite
  39. >>> G = nx.cycle_graph(4)
  40. >>> rc = bipartite.node_redundancy(G)
  41. >>> sum(rc.values()) / len(G)
  42. 1.0
  43. Compute the average redundancy for a set of nodes::
  44. >>> from networkx.algorithms import bipartite
  45. >>> G = nx.cycle_graph(4)
  46. >>> rc = bipartite.node_redundancy(G)
  47. >>> nodes = [0, 2]
  48. >>> sum(rc[n] for n in nodes) / len(nodes)
  49. 1.0
  50. Raises
  51. ------
  52. NetworkXError
  53. If any of the nodes in the graph (or in `nodes`, if specified) has
  54. (out-)degree less than two (which would result in division by zero,
  55. according to the definition of the redundancy coefficient).
  56. References
  57. ----------
  58. .. [1] Latapy, Matthieu, Clémence Magnien, and Nathalie Del Vecchio (2008).
  59. Basic notions for the analysis of large two-mode networks.
  60. Social Networks 30(1), 31--48.
  61. """
  62. if nodes is None:
  63. nodes = G
  64. if any(len(G[v]) < 2 for v in nodes):
  65. raise NetworkXError(
  66. "Cannot compute redundancy coefficient for a node"
  67. " that has fewer than two neighbors."
  68. )
  69. # TODO This can be trivially parallelized.
  70. return {v: _node_redundancy(G, v) for v in nodes}
  71. def _node_redundancy(G, v):
  72. """Returns the redundancy of the node `v` in the bipartite graph `G`.
  73. If `G` is a graph with `n` nodes, the redundancy of a node is the ratio
  74. of the "overlap" of `v` to the maximum possible overlap of `v`
  75. according to its degree. The overlap of `v` is the number of pairs of
  76. neighbors that have mutual neighbors themselves, other than `v`.
  77. `v` must have at least two neighbors in `G`.
  78. """
  79. n = len(G[v])
  80. overlap = sum(
  81. 1 for (u, w) in combinations(G[v], 2) if (set(G[u]) & set(G[w])) - {v}
  82. )
  83. return (2 * overlap) / (n * (n - 1))