intersection.py 4.0 KB

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