generators.py 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595
  1. """
  2. Generators and functions for bipartite graphs.
  3. """
  4. import math
  5. import numbers
  6. from functools import reduce
  7. import networkx as nx
  8. from networkx.utils import nodes_or_number, py_random_state
  9. __all__ = [
  10. "configuration_model",
  11. "havel_hakimi_graph",
  12. "reverse_havel_hakimi_graph",
  13. "alternating_havel_hakimi_graph",
  14. "preferential_attachment_graph",
  15. "random_graph",
  16. "gnmk_random_graph",
  17. "complete_bipartite_graph",
  18. ]
  19. @nodes_or_number([0, 1])
  20. def complete_bipartite_graph(n1, n2, create_using=None):
  21. """Returns the complete bipartite graph `K_{n_1,n_2}`.
  22. The graph is composed of two partitions with nodes 0 to (n1 - 1)
  23. in the first and nodes n1 to (n1 + n2 - 1) in the second.
  24. Each node in the first is connected to each node in the second.
  25. Parameters
  26. ----------
  27. n1, n2 : integer or iterable container of nodes
  28. If integers, nodes are from `range(n1)` and `range(n1, n1 + n2)`.
  29. If a container, the elements are the nodes.
  30. create_using : NetworkX graph instance, (default: nx.Graph)
  31. Return graph of this type.
  32. Notes
  33. -----
  34. Nodes are the integers 0 to `n1 + n2 - 1` unless either n1 or n2 are
  35. containers of nodes. If only one of n1 or n2 are integers, that
  36. integer is replaced by `range` of that integer.
  37. The nodes are assigned the attribute 'bipartite' with the value 0 or 1
  38. to indicate which bipartite set the node belongs to.
  39. This function is not imported in the main namespace.
  40. To use it use nx.bipartite.complete_bipartite_graph
  41. """
  42. G = nx.empty_graph(0, create_using)
  43. if G.is_directed():
  44. raise nx.NetworkXError("Directed Graph not supported")
  45. n1, top = n1
  46. n2, bottom = n2
  47. if isinstance(n1, numbers.Integral) and isinstance(n2, numbers.Integral):
  48. bottom = [n1 + i for i in bottom]
  49. G.add_nodes_from(top, bipartite=0)
  50. G.add_nodes_from(bottom, bipartite=1)
  51. if len(G) != len(top) + len(bottom):
  52. raise nx.NetworkXError("Inputs n1 and n2 must contain distinct nodes")
  53. G.add_edges_from((u, v) for u in top for v in bottom)
  54. G.graph["name"] = f"complete_bipartite_graph({n1}, {n2})"
  55. return G
  56. @py_random_state(3)
  57. def configuration_model(aseq, bseq, create_using=None, seed=None):
  58. """Returns a random bipartite graph from two given degree sequences.
  59. Parameters
  60. ----------
  61. aseq : list
  62. Degree sequence for node set A.
  63. bseq : list
  64. Degree sequence for node set B.
  65. create_using : NetworkX graph instance, optional
  66. Return graph of this type.
  67. seed : integer, random_state, or None (default)
  68. Indicator of random number generation state.
  69. See :ref:`Randomness<randomness>`.
  70. The graph is composed of two partitions. Set A has nodes 0 to
  71. (len(aseq) - 1) and set B has nodes len(aseq) to (len(bseq) - 1).
  72. Nodes from set A are connected to nodes in set B by choosing
  73. randomly from the possible free stubs, one in A and one in B.
  74. Notes
  75. -----
  76. The sum of the two sequences must be equal: sum(aseq)=sum(bseq)
  77. If no graph type is specified use MultiGraph with parallel edges.
  78. If you want a graph with no parallel edges use create_using=Graph()
  79. but then the resulting degree sequences might not be exact.
  80. The nodes are assigned the attribute 'bipartite' with the value 0 or 1
  81. to indicate which bipartite set the node belongs to.
  82. This function is not imported in the main namespace.
  83. To use it use nx.bipartite.configuration_model
  84. """
  85. G = nx.empty_graph(0, create_using, default=nx.MultiGraph)
  86. if G.is_directed():
  87. raise nx.NetworkXError("Directed Graph not supported")
  88. # length and sum of each sequence
  89. lena = len(aseq)
  90. lenb = len(bseq)
  91. suma = sum(aseq)
  92. sumb = sum(bseq)
  93. if not suma == sumb:
  94. raise nx.NetworkXError(
  95. f"invalid degree sequences, sum(aseq)!=sum(bseq),{suma},{sumb}"
  96. )
  97. G = _add_nodes_with_bipartite_label(G, lena, lenb)
  98. if len(aseq) == 0 or max(aseq) == 0:
  99. return G # done if no edges
  100. # build lists of degree-repeated vertex numbers
  101. stubs = [[v] * aseq[v] for v in range(0, lena)]
  102. astubs = [x for subseq in stubs for x in subseq]
  103. stubs = [[v] * bseq[v - lena] for v in range(lena, lena + lenb)]
  104. bstubs = [x for subseq in stubs for x in subseq]
  105. # shuffle lists
  106. seed.shuffle(astubs)
  107. seed.shuffle(bstubs)
  108. G.add_edges_from([astubs[i], bstubs[i]] for i in range(suma))
  109. G.name = "bipartite_configuration_model"
  110. return G
  111. def havel_hakimi_graph(aseq, bseq, create_using=None):
  112. """Returns a bipartite graph from two given degree sequences using a
  113. Havel-Hakimi style construction.
  114. The graph is composed of two partitions. Set A has nodes 0 to
  115. (len(aseq) - 1) and set B has nodes len(aseq) to (len(bseq) - 1).
  116. Nodes from the set A are connected to nodes in the set B by
  117. connecting the highest degree nodes in set A to the highest degree
  118. nodes in set B until all stubs are connected.
  119. Parameters
  120. ----------
  121. aseq : list
  122. Degree sequence for node set A.
  123. bseq : list
  124. Degree sequence for node set B.
  125. create_using : NetworkX graph instance, optional
  126. Return graph of this type.
  127. Notes
  128. -----
  129. The sum of the two sequences must be equal: sum(aseq)=sum(bseq)
  130. If no graph type is specified use MultiGraph with parallel edges.
  131. If you want a graph with no parallel edges use create_using=Graph()
  132. but then the resulting degree sequences might not be exact.
  133. The nodes are assigned the attribute 'bipartite' with the value 0 or 1
  134. to indicate which bipartite set the node belongs to.
  135. This function is not imported in the main namespace.
  136. To use it use nx.bipartite.havel_hakimi_graph
  137. """
  138. G = nx.empty_graph(0, create_using, default=nx.MultiGraph)
  139. if G.is_directed():
  140. raise nx.NetworkXError("Directed Graph not supported")
  141. # length of the each sequence
  142. naseq = len(aseq)
  143. nbseq = len(bseq)
  144. suma = sum(aseq)
  145. sumb = sum(bseq)
  146. if not suma == sumb:
  147. raise nx.NetworkXError(
  148. f"invalid degree sequences, sum(aseq)!=sum(bseq),{suma},{sumb}"
  149. )
  150. G = _add_nodes_with_bipartite_label(G, naseq, nbseq)
  151. if len(aseq) == 0 or max(aseq) == 0:
  152. return G # done if no edges
  153. # build list of degree-repeated vertex numbers
  154. astubs = [[aseq[v], v] for v in range(0, naseq)]
  155. bstubs = [[bseq[v - naseq], v] for v in range(naseq, naseq + nbseq)]
  156. astubs.sort()
  157. while astubs:
  158. (degree, u) = astubs.pop() # take of largest degree node in the a set
  159. if degree == 0:
  160. break # done, all are zero
  161. # connect the source to largest degree nodes in the b set
  162. bstubs.sort()
  163. for target in bstubs[-degree:]:
  164. v = target[1]
  165. G.add_edge(u, v)
  166. target[0] -= 1 # note this updates bstubs too.
  167. if target[0] == 0:
  168. bstubs.remove(target)
  169. G.name = "bipartite_havel_hakimi_graph"
  170. return G
  171. def reverse_havel_hakimi_graph(aseq, bseq, create_using=None):
  172. """Returns a bipartite graph from two given degree sequences using a
  173. Havel-Hakimi style construction.
  174. The graph is composed of two partitions. Set A has nodes 0 to
  175. (len(aseq) - 1) and set B has nodes len(aseq) to (len(bseq) - 1).
  176. Nodes from set A are connected to nodes in the set B by connecting
  177. the highest degree nodes in set A to the lowest degree nodes in
  178. set B until all stubs are connected.
  179. Parameters
  180. ----------
  181. aseq : list
  182. Degree sequence for node set A.
  183. bseq : list
  184. Degree sequence for node set B.
  185. create_using : NetworkX graph instance, optional
  186. Return graph of this type.
  187. Notes
  188. -----
  189. The sum of the two sequences must be equal: sum(aseq)=sum(bseq)
  190. If no graph type is specified use MultiGraph with parallel edges.
  191. If you want a graph with no parallel edges use create_using=Graph()
  192. but then the resulting degree sequences might not be exact.
  193. The nodes are assigned the attribute 'bipartite' with the value 0 or 1
  194. to indicate which bipartite set the node belongs to.
  195. This function is not imported in the main namespace.
  196. To use it use nx.bipartite.reverse_havel_hakimi_graph
  197. """
  198. G = nx.empty_graph(0, create_using, default=nx.MultiGraph)
  199. if G.is_directed():
  200. raise nx.NetworkXError("Directed Graph not supported")
  201. # length of the each sequence
  202. lena = len(aseq)
  203. lenb = len(bseq)
  204. suma = sum(aseq)
  205. sumb = sum(bseq)
  206. if not suma == sumb:
  207. raise nx.NetworkXError(
  208. f"invalid degree sequences, sum(aseq)!=sum(bseq),{suma},{sumb}"
  209. )
  210. G = _add_nodes_with_bipartite_label(G, lena, lenb)
  211. if len(aseq) == 0 or max(aseq) == 0:
  212. return G # done if no edges
  213. # build list of degree-repeated vertex numbers
  214. astubs = [[aseq[v], v] for v in range(0, lena)]
  215. bstubs = [[bseq[v - lena], v] for v in range(lena, lena + lenb)]
  216. astubs.sort()
  217. bstubs.sort()
  218. while astubs:
  219. (degree, u) = astubs.pop() # take of largest degree node in the a set
  220. if degree == 0:
  221. break # done, all are zero
  222. # connect the source to the smallest degree nodes in the b set
  223. for target in bstubs[0:degree]:
  224. v = target[1]
  225. G.add_edge(u, v)
  226. target[0] -= 1 # note this updates bstubs too.
  227. if target[0] == 0:
  228. bstubs.remove(target)
  229. G.name = "bipartite_reverse_havel_hakimi_graph"
  230. return G
  231. def alternating_havel_hakimi_graph(aseq, bseq, create_using=None):
  232. """Returns a bipartite graph from two given degree sequences using
  233. an alternating Havel-Hakimi style construction.
  234. The graph is composed of two partitions. Set A has nodes 0 to
  235. (len(aseq) - 1) and set B has nodes len(aseq) to (len(bseq) - 1).
  236. Nodes from the set A are connected to nodes in the set B by
  237. connecting the highest degree nodes in set A to alternatively the
  238. highest and the lowest degree nodes in set B until all stubs are
  239. connected.
  240. Parameters
  241. ----------
  242. aseq : list
  243. Degree sequence for node set A.
  244. bseq : list
  245. Degree sequence for node set B.
  246. create_using : NetworkX graph instance, optional
  247. Return graph of this type.
  248. Notes
  249. -----
  250. The sum of the two sequences must be equal: sum(aseq)=sum(bseq)
  251. If no graph type is specified use MultiGraph with parallel edges.
  252. If you want a graph with no parallel edges use create_using=Graph()
  253. but then the resulting degree sequences might not be exact.
  254. The nodes are assigned the attribute 'bipartite' with the value 0 or 1
  255. to indicate which bipartite set the node belongs to.
  256. This function is not imported in the main namespace.
  257. To use it use nx.bipartite.alternating_havel_hakimi_graph
  258. """
  259. G = nx.empty_graph(0, create_using, default=nx.MultiGraph)
  260. if G.is_directed():
  261. raise nx.NetworkXError("Directed Graph not supported")
  262. # length of the each sequence
  263. naseq = len(aseq)
  264. nbseq = len(bseq)
  265. suma = sum(aseq)
  266. sumb = sum(bseq)
  267. if not suma == sumb:
  268. raise nx.NetworkXError(
  269. f"invalid degree sequences, sum(aseq)!=sum(bseq),{suma},{sumb}"
  270. )
  271. G = _add_nodes_with_bipartite_label(G, naseq, nbseq)
  272. if len(aseq) == 0 or max(aseq) == 0:
  273. return G # done if no edges
  274. # build list of degree-repeated vertex numbers
  275. astubs = [[aseq[v], v] for v in range(0, naseq)]
  276. bstubs = [[bseq[v - naseq], v] for v in range(naseq, naseq + nbseq)]
  277. while astubs:
  278. astubs.sort()
  279. (degree, u) = astubs.pop() # take of largest degree node in the a set
  280. if degree == 0:
  281. break # done, all are zero
  282. bstubs.sort()
  283. small = bstubs[0 : degree // 2] # add these low degree targets
  284. large = bstubs[(-degree + degree // 2) :] # now high degree targets
  285. stubs = [x for z in zip(large, small) for x in z] # combine, sorry
  286. if len(stubs) < len(small) + len(large): # check for zip truncation
  287. stubs.append(large.pop())
  288. for target in stubs:
  289. v = target[1]
  290. G.add_edge(u, v)
  291. target[0] -= 1 # note this updates bstubs too.
  292. if target[0] == 0:
  293. bstubs.remove(target)
  294. G.name = "bipartite_alternating_havel_hakimi_graph"
  295. return G
  296. @py_random_state(3)
  297. def preferential_attachment_graph(aseq, p, create_using=None, seed=None):
  298. """Create a bipartite graph with a preferential attachment model from
  299. a given single degree sequence.
  300. The graph is composed of two partitions. Set A has nodes 0 to
  301. (len(aseq) - 1) and set B has nodes starting with node len(aseq).
  302. The number of nodes in set B is random.
  303. Parameters
  304. ----------
  305. aseq : list
  306. Degree sequence for node set A.
  307. p : float
  308. Probability that a new bottom node is added.
  309. create_using : NetworkX graph instance, optional
  310. Return graph of this type.
  311. seed : integer, random_state, or None (default)
  312. Indicator of random number generation state.
  313. See :ref:`Randomness<randomness>`.
  314. References
  315. ----------
  316. .. [1] Guillaume, J.L. and Latapy, M.,
  317. Bipartite graphs as models of complex networks.
  318. Physica A: Statistical Mechanics and its Applications,
  319. 2006, 371(2), pp.795-813.
  320. .. [2] Jean-Loup Guillaume and Matthieu Latapy,
  321. Bipartite structure of all complex networks,
  322. Inf. Process. Lett. 90, 2004, pg. 215-221
  323. https://doi.org/10.1016/j.ipl.2004.03.007
  324. Notes
  325. -----
  326. The nodes are assigned the attribute 'bipartite' with the value 0 or 1
  327. to indicate which bipartite set the node belongs to.
  328. This function is not imported in the main namespace.
  329. To use it use nx.bipartite.preferential_attachment_graph
  330. """
  331. G = nx.empty_graph(0, create_using, default=nx.MultiGraph)
  332. if G.is_directed():
  333. raise nx.NetworkXError("Directed Graph not supported")
  334. if p > 1:
  335. raise nx.NetworkXError(f"probability {p} > 1")
  336. naseq = len(aseq)
  337. G = _add_nodes_with_bipartite_label(G, naseq, 0)
  338. vv = [[v] * aseq[v] for v in range(0, naseq)]
  339. while vv:
  340. while vv[0]:
  341. source = vv[0][0]
  342. vv[0].remove(source)
  343. if seed.random() < p or len(G) == naseq:
  344. target = len(G)
  345. G.add_node(target, bipartite=1)
  346. G.add_edge(source, target)
  347. else:
  348. bb = [[b] * G.degree(b) for b in range(naseq, len(G))]
  349. # flatten the list of lists into a list.
  350. bbstubs = reduce(lambda x, y: x + y, bb)
  351. # choose preferentially a bottom node.
  352. target = seed.choice(bbstubs)
  353. G.add_node(target, bipartite=1)
  354. G.add_edge(source, target)
  355. vv.remove(vv[0])
  356. G.name = "bipartite_preferential_attachment_model"
  357. return G
  358. @py_random_state(3)
  359. def random_graph(n, m, p, seed=None, directed=False):
  360. """Returns a bipartite random graph.
  361. This is a bipartite version of the binomial (Erdős-Rényi) graph.
  362. The graph is composed of two partitions. Set A has nodes 0 to
  363. (n - 1) and set B has nodes n to (n + m - 1).
  364. Parameters
  365. ----------
  366. n : int
  367. The number of nodes in the first bipartite set.
  368. m : int
  369. The number of nodes in the second bipartite set.
  370. p : float
  371. Probability for edge creation.
  372. seed : integer, random_state, or None (default)
  373. Indicator of random number generation state.
  374. See :ref:`Randomness<randomness>`.
  375. directed : bool, optional (default=False)
  376. If True return a directed graph
  377. Notes
  378. -----
  379. The bipartite random graph algorithm chooses each of the n*m (undirected)
  380. or 2*nm (directed) possible edges with probability p.
  381. This algorithm is $O(n+m)$ where $m$ is the expected number of edges.
  382. The nodes are assigned the attribute 'bipartite' with the value 0 or 1
  383. to indicate which bipartite set the node belongs to.
  384. This function is not imported in the main namespace.
  385. To use it use nx.bipartite.random_graph
  386. See Also
  387. --------
  388. gnp_random_graph, configuration_model
  389. References
  390. ----------
  391. .. [1] Vladimir Batagelj and Ulrik Brandes,
  392. "Efficient generation of large random networks",
  393. Phys. Rev. E, 71, 036113, 2005.
  394. """
  395. G = nx.Graph()
  396. G = _add_nodes_with_bipartite_label(G, n, m)
  397. if directed:
  398. G = nx.DiGraph(G)
  399. G.name = f"fast_gnp_random_graph({n},{m},{p})"
  400. if p <= 0:
  401. return G
  402. if p >= 1:
  403. return nx.complete_bipartite_graph(n, m)
  404. lp = math.log(1.0 - p)
  405. v = 0
  406. w = -1
  407. while v < n:
  408. lr = math.log(1.0 - seed.random())
  409. w = w + 1 + int(lr / lp)
  410. while w >= m and v < n:
  411. w = w - m
  412. v = v + 1
  413. if v < n:
  414. G.add_edge(v, n + w)
  415. if directed:
  416. # use the same algorithm to
  417. # add edges from the "m" to "n" set
  418. v = 0
  419. w = -1
  420. while v < n:
  421. lr = math.log(1.0 - seed.random())
  422. w = w + 1 + int(lr / lp)
  423. while w >= m and v < n:
  424. w = w - m
  425. v = v + 1
  426. if v < n:
  427. G.add_edge(n + w, v)
  428. return G
  429. @py_random_state(3)
  430. def gnmk_random_graph(n, m, k, seed=None, directed=False):
  431. """Returns a random bipartite graph G_{n,m,k}.
  432. Produces a bipartite graph chosen randomly out of the set of all graphs
  433. with n top nodes, m bottom nodes, and k edges.
  434. The graph is composed of two sets of nodes.
  435. Set A has nodes 0 to (n - 1) and set B has nodes n to (n + m - 1).
  436. Parameters
  437. ----------
  438. n : int
  439. The number of nodes in the first bipartite set.
  440. m : int
  441. The number of nodes in the second bipartite set.
  442. k : int
  443. The number of edges
  444. seed : integer, random_state, or None (default)
  445. Indicator of random number generation state.
  446. See :ref:`Randomness<randomness>`.
  447. directed : bool, optional (default=False)
  448. If True return a directed graph
  449. Examples
  450. --------
  451. from nx.algorithms import bipartite
  452. G = bipartite.gnmk_random_graph(10,20,50)
  453. See Also
  454. --------
  455. gnm_random_graph
  456. Notes
  457. -----
  458. If k > m * n then a complete bipartite graph is returned.
  459. This graph is a bipartite version of the `G_{nm}` random graph model.
  460. The nodes are assigned the attribute 'bipartite' with the value 0 or 1
  461. to indicate which bipartite set the node belongs to.
  462. This function is not imported in the main namespace.
  463. To use it use nx.bipartite.gnmk_random_graph
  464. """
  465. G = nx.Graph()
  466. G = _add_nodes_with_bipartite_label(G, n, m)
  467. if directed:
  468. G = nx.DiGraph(G)
  469. G.name = f"bipartite_gnm_random_graph({n},{m},{k})"
  470. if n == 1 or m == 1:
  471. return G
  472. max_edges = n * m # max_edges for bipartite networks
  473. if k >= max_edges: # Maybe we should raise an exception here
  474. return nx.complete_bipartite_graph(n, m, create_using=G)
  475. top = [n for n, d in G.nodes(data=True) if d["bipartite"] == 0]
  476. bottom = list(set(G) - set(top))
  477. edge_count = 0
  478. while edge_count < k:
  479. # generate random edge,u,v
  480. u = seed.choice(top)
  481. v = seed.choice(bottom)
  482. if v in G[u]:
  483. continue
  484. else:
  485. G.add_edge(u, v)
  486. edge_count += 1
  487. return G
  488. def _add_nodes_with_bipartite_label(G, lena, lenb):
  489. G.add_nodes_from(range(0, lena + lenb))
  490. b = dict(zip(range(0, lena), [0] * lena))
  491. b.update(dict(zip(range(lena, lena + lenb), [1] * lenb)))
  492. nx.set_node_attributes(G, b, "bipartite")
  493. return G