duplication.py 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. """Functions for generating graphs based on the "duplication" method.
  2. These graph generators start with a small initial graph then duplicate
  3. nodes and (partially) duplicate their edges. These functions are
  4. generally inspired by biological networks.
  5. """
  6. import networkx as nx
  7. from networkx.exception import NetworkXError
  8. from networkx.utils import py_random_state
  9. __all__ = ["partial_duplication_graph", "duplication_divergence_graph"]
  10. @py_random_state(4)
  11. def partial_duplication_graph(N, n, p, q, seed=None):
  12. """Returns a random graph using the partial duplication model.
  13. Parameters
  14. ----------
  15. N : int
  16. The total number of nodes in the final graph.
  17. n : int
  18. The number of nodes in the initial clique.
  19. p : float
  20. The probability of joining each neighbor of a node to the
  21. duplicate node. Must be a number in the between zero and one,
  22. inclusive.
  23. q : float
  24. The probability of joining the source node to the duplicate
  25. node. Must be a number in the between zero and one, inclusive.
  26. seed : integer, random_state, or None (default)
  27. Indicator of random number generation state.
  28. See :ref:`Randomness<randomness>`.
  29. Notes
  30. -----
  31. A graph of nodes is grown by creating a fully connected graph
  32. of size `n`. The following procedure is then repeated until
  33. a total of `N` nodes have been reached.
  34. 1. A random node, *u*, is picked and a new node, *v*, is created.
  35. 2. For each neighbor of *u* an edge from the neighbor to *v* is created
  36. with probability `p`.
  37. 3. An edge from *u* to *v* is created with probability `q`.
  38. This algorithm appears in [1].
  39. This implementation allows the possibility of generating
  40. disconnected graphs.
  41. References
  42. ----------
  43. .. [1] Knudsen Michael, and Carsten Wiuf. "A Markov chain approach to
  44. randomly grown graphs." Journal of Applied Mathematics 2008.
  45. <https://doi.org/10.1155/2008/190836>
  46. """
  47. if p < 0 or p > 1 or q < 0 or q > 1:
  48. msg = "partial duplication graph must have 0 <= p, q <= 1."
  49. raise NetworkXError(msg)
  50. if n > N:
  51. raise NetworkXError("partial duplication graph must have n <= N.")
  52. G = nx.complete_graph(n)
  53. for new_node in range(n, N):
  54. # Pick a random vertex, u, already in the graph.
  55. src_node = seed.randint(0, new_node - 1)
  56. # Add a new vertex, v, to the graph.
  57. G.add_node(new_node)
  58. # For each neighbor of u...
  59. for neighbor_node in list(nx.all_neighbors(G, src_node)):
  60. # Add the neighbor to v with probability p.
  61. if seed.random() < p:
  62. G.add_edge(new_node, neighbor_node)
  63. # Join v and u with probability q.
  64. if seed.random() < q:
  65. G.add_edge(new_node, src_node)
  66. return G
  67. @py_random_state(2)
  68. def duplication_divergence_graph(n, p, seed=None):
  69. """Returns an undirected graph using the duplication-divergence model.
  70. A graph of `n` nodes is created by duplicating the initial nodes
  71. and retaining edges incident to the original nodes with a retention
  72. probability `p`.
  73. Parameters
  74. ----------
  75. n : int
  76. The desired number of nodes in the graph.
  77. p : float
  78. The probability for retaining the edge of the replicated node.
  79. seed : integer, random_state, or None (default)
  80. Indicator of random number generation state.
  81. See :ref:`Randomness<randomness>`.
  82. Returns
  83. -------
  84. G : Graph
  85. Raises
  86. ------
  87. NetworkXError
  88. If `p` is not a valid probability.
  89. If `n` is less than 2.
  90. Notes
  91. -----
  92. This algorithm appears in [1].
  93. This implementation disallows the possibility of generating
  94. disconnected graphs.
  95. References
  96. ----------
  97. .. [1] I. Ispolatov, P. L. Krapivsky, A. Yuryev,
  98. "Duplication-divergence model of protein interaction network",
  99. Phys. Rev. E, 71, 061911, 2005.
  100. """
  101. if p > 1 or p < 0:
  102. msg = f"NetworkXError p={p} is not in [0,1]."
  103. raise nx.NetworkXError(msg)
  104. if n < 2:
  105. msg = "n must be greater than or equal to 2"
  106. raise nx.NetworkXError(msg)
  107. G = nx.Graph()
  108. # Initialize the graph with two connected nodes.
  109. G.add_edge(0, 1)
  110. i = 2
  111. while i < n:
  112. # Choose a random node from current graph to duplicate.
  113. random_node = seed.choice(list(G))
  114. # Make the replica.
  115. G.add_node(i)
  116. # flag indicates whether at least one edge is connected on the replica.
  117. flag = False
  118. for nbr in G.neighbors(random_node):
  119. if seed.random() < p:
  120. # Link retention step.
  121. G.add_edge(i, nbr)
  122. flag = True
  123. if not flag:
  124. # Delete replica if no edges retained.
  125. G.remove_node(i)
  126. else:
  127. # Successful duplication.
  128. i += 1
  129. return G