intersection.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. """
  2. Generators for random intersection graphs.
  3. """
  4. import networkx as nx
  5. from networkx.utils import py_random_state
  6. __all__ = [
  7. "uniform_random_intersection_graph",
  8. "k_random_intersection_graph",
  9. "general_random_intersection_graph",
  10. ]
  11. @py_random_state(3)
  12. def uniform_random_intersection_graph(n, m, p, seed=None):
  13. """Returns a uniform random intersection graph.
  14. Parameters
  15. ----------
  16. n : int
  17. The number of nodes in the first bipartite set (nodes)
  18. m : int
  19. The number of nodes in the second bipartite set (attributes)
  20. p : float
  21. Probability of connecting nodes between bipartite sets
  22. seed : integer, random_state, or None (default)
  23. Indicator of random number generation state.
  24. See :ref:`Randomness<randomness>`.
  25. See Also
  26. --------
  27. gnp_random_graph
  28. References
  29. ----------
  30. .. [1] K.B. Singer-Cohen, Random Intersection Graphs, 1995,
  31. PhD thesis, Johns Hopkins University
  32. .. [2] Fill, J. A., Scheinerman, E. R., and Singer-Cohen, K. B.,
  33. Random intersection graphs when m = !(n):
  34. An equivalence theorem relating the evolution of the g(n, m, p)
  35. and g(n, p) models. Random Struct. Algorithms 16, 2 (2000), 156–176.
  36. """
  37. from networkx.algorithms import bipartite
  38. G = bipartite.random_graph(n, m, p, seed)
  39. return nx.projected_graph(G, range(n))
  40. @py_random_state(3)
  41. def k_random_intersection_graph(n, m, k, seed=None):
  42. """Returns a intersection graph with randomly chosen attribute sets for
  43. each node that are of equal size (k).
  44. Parameters
  45. ----------
  46. n : int
  47. The number of nodes in the first bipartite set (nodes)
  48. m : int
  49. The number of nodes in the second bipartite set (attributes)
  50. k : float
  51. Size of attribute set to assign to each node.
  52. seed : integer, random_state, or None (default)
  53. Indicator of random number generation state.
  54. See :ref:`Randomness<randomness>`.
  55. See Also
  56. --------
  57. gnp_random_graph, uniform_random_intersection_graph
  58. References
  59. ----------
  60. .. [1] Godehardt, E., and Jaworski, J.
  61. Two models of random intersection graphs and their applications.
  62. Electronic Notes in Discrete Mathematics 10 (2001), 129--132.
  63. """
  64. G = nx.empty_graph(n + m)
  65. mset = range(n, n + m)
  66. for v in range(n):
  67. targets = seed.sample(mset, k)
  68. G.add_edges_from(zip([v] * len(targets), targets))
  69. return nx.projected_graph(G, range(n))
  70. @py_random_state(3)
  71. def general_random_intersection_graph(n, m, p, seed=None):
  72. """Returns a random intersection graph with independent probabilities
  73. for connections between node and attribute sets.
  74. Parameters
  75. ----------
  76. n : int
  77. The number of nodes in the first bipartite set (nodes)
  78. m : int
  79. The number of nodes in the second bipartite set (attributes)
  80. p : list of floats of length m
  81. Probabilities for connecting nodes to each attribute
  82. seed : integer, random_state, or None (default)
  83. Indicator of random number generation state.
  84. See :ref:`Randomness<randomness>`.
  85. See Also
  86. --------
  87. gnp_random_graph, uniform_random_intersection_graph
  88. References
  89. ----------
  90. .. [1] Nikoletseas, S. E., Raptopoulos, C., and Spirakis, P. G.
  91. The existence and efficient construction of large independent sets
  92. in general random intersection graphs. In ICALP (2004), J. D´ıaz,
  93. J. Karhum¨aki, A. Lepist¨o, and D. Sannella, Eds., vol. 3142
  94. of Lecture Notes in Computer Science, Springer, pp. 1029–1040.
  95. """
  96. if len(p) != m:
  97. raise ValueError("Probability list p must have m elements.")
  98. G = nx.empty_graph(n + m)
  99. mset = range(n, n + m)
  100. for u in range(n):
  101. for v, q in zip(mset, p):
  102. if seed.random() < q:
  103. G.add_edge(u, v)
  104. return nx.projected_graph(G, range(n))