swap.py 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  1. """Swap edges in a graph."""
  2. import math
  3. import networkx as nx
  4. from networkx.utils import py_random_state
  5. __all__ = ["double_edge_swap", "connected_double_edge_swap", "directed_edge_swap"]
  6. @nx.utils.not_implemented_for("undirected")
  7. @py_random_state(3)
  8. @nx._dispatchable(mutates_input=True, returns_graph=True)
  9. def directed_edge_swap(G, *, nswap=1, max_tries=100, seed=None):
  10. """Swap three edges in a directed graph while keeping the node degrees fixed.
  11. A directed edge swap swaps three edges such that a -> b -> c -> d becomes
  12. a -> c -> b -> d. This pattern of swapping allows all possible states with the
  13. same in- and out-degree distribution in a directed graph to be reached.
  14. If the swap would create parallel edges (e.g. if a -> c already existed in the
  15. previous example), another attempt is made to find a suitable trio of edges.
  16. Parameters
  17. ----------
  18. G : DiGraph
  19. A directed graph
  20. nswap : integer (optional, default=1)
  21. Number of three-edge (directed) swaps to perform
  22. max_tries : integer (optional, default=100)
  23. Maximum number of attempts to swap edges
  24. seed : integer, random_state, or None (default)
  25. Indicator of random number generation state.
  26. See :ref:`Randomness<randomness>`.
  27. Returns
  28. -------
  29. G : DiGraph
  30. The graph after the edges are swapped.
  31. Raises
  32. ------
  33. NetworkXError
  34. If `G` is not directed, or
  35. If nswap > max_tries, or
  36. If there are fewer than 4 nodes or 3 edges in `G`.
  37. NetworkXAlgorithmError
  38. If the number of swap attempts exceeds `max_tries` before `nswap` swaps are made
  39. Notes
  40. -----
  41. Does not enforce any connectivity constraints.
  42. The graph G is modified in place.
  43. A later swap is allowed to undo a previous swap.
  44. References
  45. ----------
  46. .. [1] Erdős, Péter L., et al. “A Simple Havel-Hakimi Type Algorithm to Realize
  47. Graphical Degree Sequences of Directed Graphs.” ArXiv:0905.4913 [Math],
  48. Jan. 2010. https://doi.org/10.48550/arXiv.0905.4913.
  49. Published 2010 in Elec. J. Combinatorics (17(1)). R66.
  50. http://www.combinatorics.org/Volume_17/PDF/v17i1r66.pdf
  51. .. [2] “Combinatorics - Reaching All Possible Simple Directed Graphs with a given
  52. Degree Sequence with 2-Edge Swaps.” Mathematics Stack Exchange,
  53. https://math.stackexchange.com/questions/22272/. Accessed 30 May 2022.
  54. """
  55. if nswap > max_tries:
  56. raise nx.NetworkXError("Number of swaps > number of tries allowed.")
  57. if len(G) < 4:
  58. raise nx.NetworkXError("DiGraph has fewer than four nodes.")
  59. if len(G.edges) < 3:
  60. raise nx.NetworkXError("DiGraph has fewer than 3 edges")
  61. # Instead of choosing uniformly at random from a generated edge list,
  62. # this algorithm chooses nonuniformly from the set of nodes with
  63. # probability weighted by degree.
  64. tries = 0
  65. swapcount = 0
  66. keys, degrees = zip(*G.degree()) # keys, degree
  67. cdf = nx.utils.cumulative_distribution(degrees) # cdf of degree
  68. discrete_sequence = nx.utils.discrete_sequence
  69. while swapcount < nswap:
  70. # choose source node index from discrete distribution
  71. start_index = discrete_sequence(1, cdistribution=cdf, seed=seed)[0]
  72. start = keys[start_index]
  73. tries += 1
  74. if tries > max_tries:
  75. msg = f"Maximum number of swap attempts ({tries}) exceeded before desired swaps achieved ({nswap})."
  76. raise nx.NetworkXAlgorithmError(msg)
  77. # If the given node doesn't have any out edges, then there isn't anything to swap
  78. if G.out_degree(start) == 0:
  79. continue
  80. second = seed.choice(list(G.succ[start]))
  81. if start == second:
  82. continue
  83. if G.out_degree(second) == 0:
  84. continue
  85. third = seed.choice(list(G.succ[second]))
  86. if second == third:
  87. continue
  88. if G.out_degree(third) == 0:
  89. continue
  90. fourth = seed.choice(list(G.succ[third]))
  91. if third == fourth:
  92. continue
  93. if (
  94. third not in G.succ[start]
  95. and fourth not in G.succ[second]
  96. and second not in G.succ[third]
  97. ):
  98. # Swap nodes
  99. G.add_edge(start, third)
  100. G.add_edge(third, second)
  101. G.add_edge(second, fourth)
  102. G.remove_edge(start, second)
  103. G.remove_edge(second, third)
  104. G.remove_edge(third, fourth)
  105. swapcount += 1
  106. return G
  107. @py_random_state(3)
  108. @nx._dispatchable(mutates_input=True, returns_graph=True)
  109. def double_edge_swap(G, nswap=1, max_tries=100, seed=None):
  110. """Swap two edges in the graph while keeping the node degrees fixed.
  111. A double-edge swap removes two randomly chosen edges u-v and x-y
  112. and creates the new edges u-x and v-y::
  113. u--v u v
  114. becomes | |
  115. x--y x y
  116. If either the edge u-x or v-y already exist no swap is performed
  117. and another attempt is made to find a suitable edge pair.
  118. Parameters
  119. ----------
  120. G : graph
  121. An undirected graph
  122. nswap : integer (optional, default=1)
  123. Number of double-edge swaps to perform
  124. max_tries : integer (optional)
  125. Maximum number of attempts to swap edges
  126. seed : integer, random_state, or None (default)
  127. Indicator of random number generation state.
  128. See :ref:`Randomness<randomness>`.
  129. Returns
  130. -------
  131. G : graph
  132. The graph after double edge swaps.
  133. Raises
  134. ------
  135. NetworkXError
  136. If `G` is directed, or
  137. If `nswap` > `max_tries`, or
  138. If there are fewer than 4 nodes or 2 edges in `G`.
  139. NetworkXAlgorithmError
  140. If the number of swap attempts exceeds `max_tries` before `nswap` swaps are made
  141. Notes
  142. -----
  143. Does not enforce any connectivity constraints.
  144. The graph G is modified in place.
  145. """
  146. if G.is_directed():
  147. raise nx.NetworkXError(
  148. "double_edge_swap() not defined for directed graphs. Use directed_edge_swap instead."
  149. )
  150. if nswap > max_tries:
  151. raise nx.NetworkXError("Number of swaps > number of tries allowed.")
  152. if len(G) < 4:
  153. raise nx.NetworkXError("Graph has fewer than four nodes.")
  154. if len(G.edges) < 2:
  155. raise nx.NetworkXError("Graph has fewer than 2 edges")
  156. # Instead of choosing uniformly at random from a generated edge list,
  157. # this algorithm chooses nonuniformly from the set of nodes with
  158. # probability weighted by degree.
  159. n = 0
  160. swapcount = 0
  161. keys, degrees = zip(*G.degree()) # keys, degree
  162. cdf = nx.utils.cumulative_distribution(degrees) # cdf of degree
  163. discrete_sequence = nx.utils.discrete_sequence
  164. while swapcount < nswap:
  165. # if random.random() < 0.5: continue # trick to avoid periodicities?
  166. # pick two random edges without creating edge list
  167. # choose source node indices from discrete distribution
  168. (ui, xi) = discrete_sequence(2, cdistribution=cdf, seed=seed)
  169. if ui == xi:
  170. continue # same source, skip
  171. u = keys[ui] # convert index to label
  172. x = keys[xi]
  173. # choose target uniformly from neighbors
  174. v = seed.choice(list(G[u]))
  175. y = seed.choice(list(G[x]))
  176. if v == y:
  177. continue # same target, skip
  178. if (x not in G[u]) and (y not in G[v]): # don't create parallel edges
  179. G.add_edge(u, x)
  180. G.add_edge(v, y)
  181. G.remove_edge(u, v)
  182. G.remove_edge(x, y)
  183. swapcount += 1
  184. if n >= max_tries:
  185. e = (
  186. f"Maximum number of swap attempts ({n}) exceeded "
  187. f"before desired swaps achieved ({nswap})."
  188. )
  189. raise nx.NetworkXAlgorithmError(e)
  190. n += 1
  191. return G
  192. @py_random_state(3)
  193. @nx._dispatchable(mutates_input=True)
  194. def connected_double_edge_swap(G, nswap=1, _window_threshold=3, seed=None):
  195. """Attempts the specified number of double-edge swaps in the graph `G`.
  196. A double-edge swap removes two randomly chosen edges `(u, v)` and `(x,
  197. y)` and creates the new edges `(u, x)` and `(v, y)`::
  198. u--v u v
  199. becomes | |
  200. x--y x y
  201. If either `(u, x)` or `(v, y)` already exist, then no swap is performed
  202. so the actual number of swapped edges is always *at most* `nswap`.
  203. Parameters
  204. ----------
  205. G : graph
  206. An undirected graph
  207. nswap : integer (optional, default=1)
  208. Number of double-edge swaps to perform
  209. _window_threshold : integer
  210. The window size below which connectedness of the graph will be checked
  211. after each swap.
  212. The "window" in this function is a dynamically updated integer that
  213. represents the number of swap attempts to make before checking if the
  214. graph remains connected. It is an optimization used to decrease the
  215. running time of the algorithm in exchange for increased complexity of
  216. implementation.
  217. If the window size is below this threshold, then the algorithm checks
  218. after each swap if the graph remains connected by checking if there is a
  219. path joining the two nodes whose edge was just removed. If the window
  220. size is above this threshold, then the algorithm performs do all the
  221. swaps in the window and only then check if the graph is still connected.
  222. seed : integer, random_state, or None (default)
  223. Indicator of random number generation state.
  224. See :ref:`Randomness<randomness>`.
  225. Returns
  226. -------
  227. int
  228. The number of successful swaps
  229. Raises
  230. ------
  231. NetworkXError
  232. If the input graph is not connected, or if the graph has fewer than four
  233. nodes.
  234. Notes
  235. -----
  236. The initial graph `G` must be connected, and the resulting graph is
  237. connected. The graph `G` is modified in place.
  238. References
  239. ----------
  240. .. [1] C. Gkantsidis and M. Mihail and E. Zegura,
  241. The Markov chain simulation method for generating connected
  242. power law random graphs, 2003.
  243. http://citeseer.ist.psu.edu/gkantsidis03markov.html
  244. """
  245. if not nx.is_connected(G):
  246. raise nx.NetworkXError("Graph not connected")
  247. if len(G) < 4:
  248. raise nx.NetworkXError("Graph has fewer than four nodes.")
  249. n = 0
  250. swapcount = 0
  251. deg = G.degree()
  252. # Label key for nodes
  253. dk = [n for n, d in G.degree()]
  254. cdf = nx.utils.cumulative_distribution([d for n, d in G.degree()])
  255. discrete_sequence = nx.utils.discrete_sequence
  256. window = 1
  257. while n < nswap:
  258. wcount = 0
  259. swapped = []
  260. # If the window is small, we just check each time whether the graph is
  261. # connected by checking if the nodes that were just separated are still
  262. # connected.
  263. if window < _window_threshold:
  264. # This Boolean keeps track of whether there was a failure or not.
  265. fail = False
  266. while wcount < window and n < nswap:
  267. # Pick two random edges without creating the edge list. Choose
  268. # source nodes from the discrete degree distribution.
  269. (ui, xi) = discrete_sequence(2, cdistribution=cdf, seed=seed)
  270. # If the source nodes are the same, skip this pair.
  271. if ui == xi:
  272. continue
  273. # Convert an index to a node label.
  274. u = dk[ui]
  275. x = dk[xi]
  276. # Choose targets uniformly from neighbors.
  277. v = seed.choice(list(G.neighbors(u)))
  278. y = seed.choice(list(G.neighbors(x)))
  279. # If the target nodes are the same, skip this pair.
  280. if v == y:
  281. continue
  282. if x not in G[u] and y not in G[v]:
  283. G.remove_edge(u, v)
  284. G.remove_edge(x, y)
  285. G.add_edge(u, x)
  286. G.add_edge(v, y)
  287. swapped.append((u, v, x, y))
  288. swapcount += 1
  289. n += 1
  290. # If G remains connected...
  291. if nx.has_path(G, u, v):
  292. wcount += 1
  293. # Otherwise, undo the changes.
  294. else:
  295. G.add_edge(u, v)
  296. G.add_edge(x, y)
  297. G.remove_edge(u, x)
  298. G.remove_edge(v, y)
  299. swapcount -= 1
  300. fail = True
  301. # If one of the swaps failed, reduce the window size.
  302. if fail:
  303. window = math.ceil(window / 2)
  304. else:
  305. window += 1
  306. # If the window is large, then there is a good chance that a bunch of
  307. # swaps will work. It's quicker to do all those swaps first and then
  308. # check if the graph remains connected.
  309. else:
  310. while wcount < window and n < nswap:
  311. # Pick two random edges without creating the edge list. Choose
  312. # source nodes from the discrete degree distribution.
  313. (ui, xi) = discrete_sequence(2, cdistribution=cdf, seed=seed)
  314. # If the source nodes are the same, skip this pair.
  315. if ui == xi:
  316. continue
  317. # Convert an index to a node label.
  318. u = dk[ui]
  319. x = dk[xi]
  320. # Choose targets uniformly from neighbors.
  321. v = seed.choice(list(G.neighbors(u)))
  322. y = seed.choice(list(G.neighbors(x)))
  323. # If the target nodes are the same, skip this pair.
  324. if v == y:
  325. continue
  326. if x not in G[u] and y not in G[v]:
  327. G.remove_edge(u, v)
  328. G.remove_edge(x, y)
  329. G.add_edge(u, x)
  330. G.add_edge(v, y)
  331. swapped.append((u, v, x, y))
  332. swapcount += 1
  333. n += 1
  334. wcount += 1
  335. # If the graph remains connected, increase the window size.
  336. if nx.is_connected(G):
  337. window += 1
  338. # Otherwise, undo the changes from the previous window and decrease
  339. # the window size.
  340. else:
  341. while swapped:
  342. (u, v, x, y) = swapped.pop()
  343. G.add_edge(u, v)
  344. G.add_edge(x, y)
  345. G.remove_edge(u, x)
  346. G.remove_edge(v, y)
  347. swapcount -= 1
  348. window = math.ceil(window / 2)
  349. return swapcount