mst.py 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122
  1. """
  2. Algorithms for calculating min/max spanning trees/forests.
  3. """
  4. from dataclasses import dataclass, field
  5. from enum import Enum
  6. from heapq import heappop, heappush
  7. from itertools import count
  8. from math import isnan
  9. from operator import itemgetter
  10. from queue import PriorityQueue
  11. import networkx as nx
  12. from networkx.utils import UnionFind, not_implemented_for, py_random_state
  13. __all__ = [
  14. "minimum_spanning_edges",
  15. "maximum_spanning_edges",
  16. "minimum_spanning_tree",
  17. "maximum_spanning_tree",
  18. "random_spanning_tree",
  19. "partition_spanning_tree",
  20. "EdgePartition",
  21. "SpanningTreeIterator",
  22. ]
  23. class EdgePartition(Enum):
  24. """
  25. An enum to store the state of an edge partition. The enum is written to the
  26. edges of a graph before being pasted to `kruskal_mst_edges`. Options are:
  27. - EdgePartition.OPEN
  28. - EdgePartition.INCLUDED
  29. - EdgePartition.EXCLUDED
  30. """
  31. OPEN = 0
  32. INCLUDED = 1
  33. EXCLUDED = 2
  34. @not_implemented_for("multigraph")
  35. def boruvka_mst_edges(
  36. G, minimum=True, weight="weight", keys=False, data=True, ignore_nan=False
  37. ):
  38. """Iterate over edges of a Borůvka's algorithm min/max spanning tree.
  39. Parameters
  40. ----------
  41. G : NetworkX Graph
  42. The edges of `G` must have distinct weights,
  43. otherwise the edges may not form a tree.
  44. minimum : bool (default: True)
  45. Find the minimum (True) or maximum (False) spanning tree.
  46. weight : string (default: 'weight')
  47. The name of the edge attribute holding the edge weights.
  48. keys : bool (default: True)
  49. This argument is ignored since this function is not
  50. implemented for multigraphs; it exists only for consistency
  51. with the other minimum spanning tree functions.
  52. data : bool (default: True)
  53. Flag for whether to yield edge attribute dicts.
  54. If True, yield edges `(u, v, d)`, where `d` is the attribute dict.
  55. If False, yield edges `(u, v)`.
  56. ignore_nan : bool (default: False)
  57. If a NaN is found as an edge weight normally an exception is raised.
  58. If `ignore_nan is True` then that edge is ignored instead.
  59. """
  60. # Initialize a forest, assuming initially that it is the discrete
  61. # partition of the nodes of the graph.
  62. forest = UnionFind(G)
  63. def best_edge(component):
  64. """Returns the optimum (minimum or maximum) edge on the edge
  65. boundary of the given set of nodes.
  66. A return value of ``None`` indicates an empty boundary.
  67. """
  68. sign = 1 if minimum else -1
  69. minwt = float("inf")
  70. boundary = None
  71. for e in nx.edge_boundary(G, component, data=True):
  72. wt = e[-1].get(weight, 1) * sign
  73. if isnan(wt):
  74. if ignore_nan:
  75. continue
  76. msg = f"NaN found as an edge weight. Edge {e}"
  77. raise ValueError(msg)
  78. if wt < minwt:
  79. minwt = wt
  80. boundary = e
  81. return boundary
  82. # Determine the optimum edge in the edge boundary of each component
  83. # in the forest.
  84. best_edges = (best_edge(component) for component in forest.to_sets())
  85. best_edges = [edge for edge in best_edges if edge is not None]
  86. # If each entry was ``None``, that means the graph was disconnected,
  87. # so we are done generating the forest.
  88. while best_edges:
  89. # Determine the optimum edge in the edge boundary of each
  90. # component in the forest.
  91. #
  92. # This must be a sequence, not an iterator. In this list, the
  93. # same edge may appear twice, in different orientations (but
  94. # that's okay, since a union operation will be called on the
  95. # endpoints the first time it is seen, but not the second time).
  96. #
  97. # Any ``None`` indicates that the edge boundary for that
  98. # component was empty, so that part of the forest has been
  99. # completed.
  100. #
  101. # TODO This can be parallelized, both in the outer loop over
  102. # each component in the forest and in the computation of the
  103. # minimum. (Same goes for the identical lines outside the loop.)
  104. best_edges = (best_edge(component) for component in forest.to_sets())
  105. best_edges = [edge for edge in best_edges if edge is not None]
  106. # Join trees in the forest using the best edges, and yield that
  107. # edge, since it is part of the spanning tree.
  108. #
  109. # TODO This loop can be parallelized, to an extent (the union
  110. # operation must be atomic).
  111. for u, v, d in best_edges:
  112. if forest[u] != forest[v]:
  113. if data:
  114. yield u, v, d
  115. else:
  116. yield u, v
  117. forest.union(u, v)
  118. def kruskal_mst_edges(
  119. G, minimum, weight="weight", keys=True, data=True, ignore_nan=False, partition=None
  120. ):
  121. """
  122. Iterate over edge of a Kruskal's algorithm min/max spanning tree.
  123. Parameters
  124. ----------
  125. G : NetworkX Graph
  126. The graph holding the tree of interest.
  127. minimum : bool (default: True)
  128. Find the minimum (True) or maximum (False) spanning tree.
  129. weight : string (default: 'weight')
  130. The name of the edge attribute holding the edge weights.
  131. keys : bool (default: True)
  132. If `G` is a multigraph, `keys` controls whether edge keys ar yielded.
  133. Otherwise `keys` is ignored.
  134. data : bool (default: True)
  135. Flag for whether to yield edge attribute dicts.
  136. If True, yield edges `(u, v, d)`, where `d` is the attribute dict.
  137. If False, yield edges `(u, v)`.
  138. ignore_nan : bool (default: False)
  139. If a NaN is found as an edge weight normally an exception is raised.
  140. If `ignore_nan is True` then that edge is ignored instead.
  141. partition : string (default: None)
  142. The name of the edge attribute holding the partition data, if it exists.
  143. Partition data is written to the edges using the `EdgePartition` enum.
  144. If a partition exists, all included edges and none of the excluded edges
  145. will appear in the final tree. Open edges may or may not be used.
  146. Yields
  147. ------
  148. edge tuple
  149. The edges as discovered by Kruskal's method. Each edge can
  150. take the following forms: `(u, v)`, `(u, v, d)` or `(u, v, k, d)`
  151. depending on the `key` and `data` parameters
  152. """
  153. subtrees = UnionFind()
  154. if G.is_multigraph():
  155. edges = G.edges(keys=True, data=True)
  156. else:
  157. edges = G.edges(data=True)
  158. """
  159. Sort the edges of the graph with respect to the partition data.
  160. Edges are returned in the following order:
  161. * Included edges
  162. * Open edges from smallest to largest weight
  163. * Excluded edges
  164. """
  165. included_edges = []
  166. open_edges = []
  167. for e in edges:
  168. d = e[-1]
  169. wt = d.get(weight, 1)
  170. if isnan(wt):
  171. if ignore_nan:
  172. continue
  173. raise ValueError(f"NaN found as an edge weight. Edge {e}")
  174. edge = (wt,) + e
  175. if d.get(partition) == EdgePartition.INCLUDED:
  176. included_edges.append(edge)
  177. elif d.get(partition) == EdgePartition.EXCLUDED:
  178. continue
  179. else:
  180. open_edges.append(edge)
  181. if minimum:
  182. sorted_open_edges = sorted(open_edges, key=itemgetter(0))
  183. else:
  184. sorted_open_edges = sorted(open_edges, key=itemgetter(0), reverse=True)
  185. # Condense the lists into one
  186. included_edges.extend(sorted_open_edges)
  187. sorted_edges = included_edges
  188. del open_edges, sorted_open_edges, included_edges
  189. # Multigraphs need to handle edge keys in addition to edge data.
  190. if G.is_multigraph():
  191. for wt, u, v, k, d in sorted_edges:
  192. if subtrees[u] != subtrees[v]:
  193. if keys:
  194. if data:
  195. yield u, v, k, d
  196. else:
  197. yield u, v, k
  198. else:
  199. if data:
  200. yield u, v, d
  201. else:
  202. yield u, v
  203. subtrees.union(u, v)
  204. else:
  205. for wt, u, v, d in sorted_edges:
  206. if subtrees[u] != subtrees[v]:
  207. if data:
  208. yield u, v, d
  209. else:
  210. yield u, v
  211. subtrees.union(u, v)
  212. def prim_mst_edges(G, minimum, weight="weight", keys=True, data=True, ignore_nan=False):
  213. """Iterate over edges of Prim's algorithm min/max spanning tree.
  214. Parameters
  215. ----------
  216. G : NetworkX Graph
  217. The graph holding the tree of interest.
  218. minimum : bool (default: True)
  219. Find the minimum (True) or maximum (False) spanning tree.
  220. weight : string (default: 'weight')
  221. The name of the edge attribute holding the edge weights.
  222. keys : bool (default: True)
  223. If `G` is a multigraph, `keys` controls whether edge keys ar yielded.
  224. Otherwise `keys` is ignored.
  225. data : bool (default: True)
  226. Flag for whether to yield edge attribute dicts.
  227. If True, yield edges `(u, v, d)`, where `d` is the attribute dict.
  228. If False, yield edges `(u, v)`.
  229. ignore_nan : bool (default: False)
  230. If a NaN is found as an edge weight normally an exception is raised.
  231. If `ignore_nan is True` then that edge is ignored instead.
  232. """
  233. is_multigraph = G.is_multigraph()
  234. push = heappush
  235. pop = heappop
  236. nodes = set(G)
  237. c = count()
  238. sign = 1 if minimum else -1
  239. while nodes:
  240. u = nodes.pop()
  241. frontier = []
  242. visited = {u}
  243. if is_multigraph:
  244. for v, keydict in G.adj[u].items():
  245. for k, d in keydict.items():
  246. wt = d.get(weight, 1) * sign
  247. if isnan(wt):
  248. if ignore_nan:
  249. continue
  250. msg = f"NaN found as an edge weight. Edge {(u, v, k, d)}"
  251. raise ValueError(msg)
  252. push(frontier, (wt, next(c), u, v, k, d))
  253. else:
  254. for v, d in G.adj[u].items():
  255. wt = d.get(weight, 1) * sign
  256. if isnan(wt):
  257. if ignore_nan:
  258. continue
  259. msg = f"NaN found as an edge weight. Edge {(u, v, d)}"
  260. raise ValueError(msg)
  261. push(frontier, (wt, next(c), u, v, d))
  262. while nodes and frontier:
  263. if is_multigraph:
  264. W, _, u, v, k, d = pop(frontier)
  265. else:
  266. W, _, u, v, d = pop(frontier)
  267. if v in visited or v not in nodes:
  268. continue
  269. # Multigraphs need to handle edge keys in addition to edge data.
  270. if is_multigraph and keys:
  271. if data:
  272. yield u, v, k, d
  273. else:
  274. yield u, v, k
  275. else:
  276. if data:
  277. yield u, v, d
  278. else:
  279. yield u, v
  280. # update frontier
  281. visited.add(v)
  282. nodes.discard(v)
  283. if is_multigraph:
  284. for w, keydict in G.adj[v].items():
  285. if w in visited:
  286. continue
  287. for k2, d2 in keydict.items():
  288. new_weight = d2.get(weight, 1) * sign
  289. if isnan(new_weight):
  290. if ignore_nan:
  291. continue
  292. msg = f"NaN found as an edge weight. Edge {(v, w, k2, d2)}"
  293. raise ValueError(msg)
  294. push(frontier, (new_weight, next(c), v, w, k2, d2))
  295. else:
  296. for w, d2 in G.adj[v].items():
  297. if w in visited:
  298. continue
  299. new_weight = d2.get(weight, 1) * sign
  300. if isnan(new_weight):
  301. if ignore_nan:
  302. continue
  303. msg = f"NaN found as an edge weight. Edge {(v, w, d2)}"
  304. raise ValueError(msg)
  305. push(frontier, (new_weight, next(c), v, w, d2))
  306. ALGORITHMS = {
  307. "boruvka": boruvka_mst_edges,
  308. "borůvka": boruvka_mst_edges,
  309. "kruskal": kruskal_mst_edges,
  310. "prim": prim_mst_edges,
  311. }
  312. @not_implemented_for("directed")
  313. def minimum_spanning_edges(
  314. G, algorithm="kruskal", weight="weight", keys=True, data=True, ignore_nan=False
  315. ):
  316. """Generate edges in a minimum spanning forest of an undirected
  317. weighted graph.
  318. A minimum spanning tree is a subgraph of the graph (a tree)
  319. with the minimum sum of edge weights. A spanning forest is a
  320. union of the spanning trees for each connected component of the graph.
  321. Parameters
  322. ----------
  323. G : undirected Graph
  324. An undirected graph. If `G` is connected, then the algorithm finds a
  325. spanning tree. Otherwise, a spanning forest is found.
  326. algorithm : string
  327. The algorithm to use when finding a minimum spanning tree. Valid
  328. choices are 'kruskal', 'prim', or 'boruvka'. The default is 'kruskal'.
  329. weight : string
  330. Edge data key to use for weight (default 'weight').
  331. keys : bool
  332. Whether to yield edge key in multigraphs in addition to the edge.
  333. If `G` is not a multigraph, this is ignored.
  334. data : bool, optional
  335. If True yield the edge data along with the edge.
  336. ignore_nan : bool (default: False)
  337. If a NaN is found as an edge weight normally an exception is raised.
  338. If `ignore_nan is True` then that edge is ignored instead.
  339. Returns
  340. -------
  341. edges : iterator
  342. An iterator over edges in a maximum spanning tree of `G`.
  343. Edges connecting nodes `u` and `v` are represented as tuples:
  344. `(u, v, k, d)` or `(u, v, k)` or `(u, v, d)` or `(u, v)`
  345. If `G` is a multigraph, `keys` indicates whether the edge key `k` will
  346. be reported in the third position in the edge tuple. `data` indicates
  347. whether the edge datadict `d` will appear at the end of the edge tuple.
  348. If `G` is not a multigraph, the tuples are `(u, v, d)` if `data` is True
  349. or `(u, v)` if `data` is False.
  350. Examples
  351. --------
  352. >>> from networkx.algorithms import tree
  353. Find minimum spanning edges by Kruskal's algorithm
  354. >>> G = nx.cycle_graph(4)
  355. >>> G.add_edge(0, 3, weight=2)
  356. >>> mst = tree.minimum_spanning_edges(G, algorithm="kruskal", data=False)
  357. >>> edgelist = list(mst)
  358. >>> sorted(sorted(e) for e in edgelist)
  359. [[0, 1], [1, 2], [2, 3]]
  360. Find minimum spanning edges by Prim's algorithm
  361. >>> G = nx.cycle_graph(4)
  362. >>> G.add_edge(0, 3, weight=2)
  363. >>> mst = tree.minimum_spanning_edges(G, algorithm="prim", data=False)
  364. >>> edgelist = list(mst)
  365. >>> sorted(sorted(e) for e in edgelist)
  366. [[0, 1], [1, 2], [2, 3]]
  367. Notes
  368. -----
  369. For Borůvka's algorithm, each edge must have a weight attribute, and
  370. each edge weight must be distinct.
  371. For the other algorithms, if the graph edges do not have a weight
  372. attribute a default weight of 1 will be used.
  373. Modified code from David Eppstein, April 2006
  374. http://www.ics.uci.edu/~eppstein/PADS/
  375. """
  376. try:
  377. algo = ALGORITHMS[algorithm]
  378. except KeyError as err:
  379. msg = f"{algorithm} is not a valid choice for an algorithm."
  380. raise ValueError(msg) from err
  381. return algo(
  382. G, minimum=True, weight=weight, keys=keys, data=data, ignore_nan=ignore_nan
  383. )
  384. @not_implemented_for("directed")
  385. def maximum_spanning_edges(
  386. G, algorithm="kruskal", weight="weight", keys=True, data=True, ignore_nan=False
  387. ):
  388. """Generate edges in a maximum spanning forest of an undirected
  389. weighted graph.
  390. A maximum spanning tree is a subgraph of the graph (a tree)
  391. with the maximum possible sum of edge weights. A spanning forest is a
  392. union of the spanning trees for each connected component of the graph.
  393. Parameters
  394. ----------
  395. G : undirected Graph
  396. An undirected graph. If `G` is connected, then the algorithm finds a
  397. spanning tree. Otherwise, a spanning forest is found.
  398. algorithm : string
  399. The algorithm to use when finding a maximum spanning tree. Valid
  400. choices are 'kruskal', 'prim', or 'boruvka'. The default is 'kruskal'.
  401. weight : string
  402. Edge data key to use for weight (default 'weight').
  403. keys : bool
  404. Whether to yield edge key in multigraphs in addition to the edge.
  405. If `G` is not a multigraph, this is ignored.
  406. data : bool, optional
  407. If True yield the edge data along with the edge.
  408. ignore_nan : bool (default: False)
  409. If a NaN is found as an edge weight normally an exception is raised.
  410. If `ignore_nan is True` then that edge is ignored instead.
  411. Returns
  412. -------
  413. edges : iterator
  414. An iterator over edges in a maximum spanning tree of `G`.
  415. Edges connecting nodes `u` and `v` are represented as tuples:
  416. `(u, v, k, d)` or `(u, v, k)` or `(u, v, d)` or `(u, v)`
  417. If `G` is a multigraph, `keys` indicates whether the edge key `k` will
  418. be reported in the third position in the edge tuple. `data` indicates
  419. whether the edge datadict `d` will appear at the end of the edge tuple.
  420. If `G` is not a multigraph, the tuples are `(u, v, d)` if `data` is True
  421. or `(u, v)` if `data` is False.
  422. Examples
  423. --------
  424. >>> from networkx.algorithms import tree
  425. Find maximum spanning edges by Kruskal's algorithm
  426. >>> G = nx.cycle_graph(4)
  427. >>> G.add_edge(0, 3, weight=2)
  428. >>> mst = tree.maximum_spanning_edges(G, algorithm="kruskal", data=False)
  429. >>> edgelist = list(mst)
  430. >>> sorted(sorted(e) for e in edgelist)
  431. [[0, 1], [0, 3], [1, 2]]
  432. Find maximum spanning edges by Prim's algorithm
  433. >>> G = nx.cycle_graph(4)
  434. >>> G.add_edge(0, 3, weight=2) # assign weight 2 to edge 0-3
  435. >>> mst = tree.maximum_spanning_edges(G, algorithm="prim", data=False)
  436. >>> edgelist = list(mst)
  437. >>> sorted(sorted(e) for e in edgelist)
  438. [[0, 1], [0, 3], [2, 3]]
  439. Notes
  440. -----
  441. For Borůvka's algorithm, each edge must have a weight attribute, and
  442. each edge weight must be distinct.
  443. For the other algorithms, if the graph edges do not have a weight
  444. attribute a default weight of 1 will be used.
  445. Modified code from David Eppstein, April 2006
  446. http://www.ics.uci.edu/~eppstein/PADS/
  447. """
  448. try:
  449. algo = ALGORITHMS[algorithm]
  450. except KeyError as err:
  451. msg = f"{algorithm} is not a valid choice for an algorithm."
  452. raise ValueError(msg) from err
  453. return algo(
  454. G, minimum=False, weight=weight, keys=keys, data=data, ignore_nan=ignore_nan
  455. )
  456. def minimum_spanning_tree(G, weight="weight", algorithm="kruskal", ignore_nan=False):
  457. """Returns a minimum spanning tree or forest on an undirected graph `G`.
  458. Parameters
  459. ----------
  460. G : undirected graph
  461. An undirected graph. If `G` is connected, then the algorithm finds a
  462. spanning tree. Otherwise, a spanning forest is found.
  463. weight : str
  464. Data key to use for edge weights.
  465. algorithm : string
  466. The algorithm to use when finding a minimum spanning tree. Valid
  467. choices are 'kruskal', 'prim', or 'boruvka'. The default is
  468. 'kruskal'.
  469. ignore_nan : bool (default: False)
  470. If a NaN is found as an edge weight normally an exception is raised.
  471. If `ignore_nan is True` then that edge is ignored instead.
  472. Returns
  473. -------
  474. G : NetworkX Graph
  475. A minimum spanning tree or forest.
  476. Examples
  477. --------
  478. >>> G = nx.cycle_graph(4)
  479. >>> G.add_edge(0, 3, weight=2)
  480. >>> T = nx.minimum_spanning_tree(G)
  481. >>> sorted(T.edges(data=True))
  482. [(0, 1, {}), (1, 2, {}), (2, 3, {})]
  483. Notes
  484. -----
  485. For Borůvka's algorithm, each edge must have a weight attribute, and
  486. each edge weight must be distinct.
  487. For the other algorithms, if the graph edges do not have a weight
  488. attribute a default weight of 1 will be used.
  489. There may be more than one tree with the same minimum or maximum weight.
  490. See :mod:`networkx.tree.recognition` for more detailed definitions.
  491. Isolated nodes with self-loops are in the tree as edgeless isolated nodes.
  492. """
  493. edges = minimum_spanning_edges(
  494. G, algorithm, weight, keys=True, data=True, ignore_nan=ignore_nan
  495. )
  496. T = G.__class__() # Same graph class as G
  497. T.graph.update(G.graph)
  498. T.add_nodes_from(G.nodes.items())
  499. T.add_edges_from(edges)
  500. return T
  501. def partition_spanning_tree(
  502. G, minimum=True, weight="weight", partition="partition", ignore_nan=False
  503. ):
  504. """
  505. Find a spanning tree while respecting a partition of edges.
  506. Edges can be flagged as either `INCLUDED` which are required to be in the
  507. returned tree, `EXCLUDED`, which cannot be in the returned tree and `OPEN`.
  508. This is used in the SpanningTreeIterator to create new partitions following
  509. the algorithm of Sörensen and Janssens [1]_.
  510. Parameters
  511. ----------
  512. G : undirected graph
  513. An undirected graph.
  514. minimum : bool (default: True)
  515. Determines whether the returned tree is the minimum spanning tree of
  516. the partition of the maximum one.
  517. weight : str
  518. Data key to use for edge weights.
  519. partition : str
  520. The key for the edge attribute containing the partition
  521. data on the graph. Edges can be included, excluded or open using the
  522. `EdgePartition` enum.
  523. ignore_nan : bool (default: False)
  524. If a NaN is found as an edge weight normally an exception is raised.
  525. If `ignore_nan is True` then that edge is ignored instead.
  526. Returns
  527. -------
  528. G : NetworkX Graph
  529. A minimum spanning tree using all of the included edges in the graph and
  530. none of the excluded edges.
  531. References
  532. ----------
  533. .. [1] G.K. Janssens, K. Sörensen, An algorithm to generate all spanning
  534. trees in order of increasing cost, Pesquisa Operacional, 2005-08,
  535. Vol. 25 (2), p. 219-229,
  536. https://www.scielo.br/j/pope/a/XHswBwRwJyrfL88dmMwYNWp/?lang=en
  537. """
  538. edges = kruskal_mst_edges(
  539. G,
  540. minimum,
  541. weight,
  542. keys=True,
  543. data=True,
  544. ignore_nan=ignore_nan,
  545. partition=partition,
  546. )
  547. T = G.__class__() # Same graph class as G
  548. T.graph.update(G.graph)
  549. T.add_nodes_from(G.nodes.items())
  550. T.add_edges_from(edges)
  551. return T
  552. def maximum_spanning_tree(G, weight="weight", algorithm="kruskal", ignore_nan=False):
  553. """Returns a maximum spanning tree or forest on an undirected graph `G`.
  554. Parameters
  555. ----------
  556. G : undirected graph
  557. An undirected graph. If `G` is connected, then the algorithm finds a
  558. spanning tree. Otherwise, a spanning forest is found.
  559. weight : str
  560. Data key to use for edge weights.
  561. algorithm : string
  562. The algorithm to use when finding a maximum spanning tree. Valid
  563. choices are 'kruskal', 'prim', or 'boruvka'. The default is
  564. 'kruskal'.
  565. ignore_nan : bool (default: False)
  566. If a NaN is found as an edge weight normally an exception is raised.
  567. If `ignore_nan is True` then that edge is ignored instead.
  568. Returns
  569. -------
  570. G : NetworkX Graph
  571. A maximum spanning tree or forest.
  572. Examples
  573. --------
  574. >>> G = nx.cycle_graph(4)
  575. >>> G.add_edge(0, 3, weight=2)
  576. >>> T = nx.maximum_spanning_tree(G)
  577. >>> sorted(T.edges(data=True))
  578. [(0, 1, {}), (0, 3, {'weight': 2}), (1, 2, {})]
  579. Notes
  580. -----
  581. For Borůvka's algorithm, each edge must have a weight attribute, and
  582. each edge weight must be distinct.
  583. For the other algorithms, if the graph edges do not have a weight
  584. attribute a default weight of 1 will be used.
  585. There may be more than one tree with the same minimum or maximum weight.
  586. See :mod:`networkx.tree.recognition` for more detailed definitions.
  587. Isolated nodes with self-loops are in the tree as edgeless isolated nodes.
  588. """
  589. edges = maximum_spanning_edges(
  590. G, algorithm, weight, keys=True, data=True, ignore_nan=ignore_nan
  591. )
  592. edges = list(edges)
  593. T = G.__class__() # Same graph class as G
  594. T.graph.update(G.graph)
  595. T.add_nodes_from(G.nodes.items())
  596. T.add_edges_from(edges)
  597. return T
  598. @py_random_state(3)
  599. def random_spanning_tree(G, weight=None, *, multiplicative=True, seed=None):
  600. """
  601. Sample a random spanning tree using the edges weights of `G`.
  602. This function supports two different methods for determining the
  603. probability of the graph. If ``multiplicative=True``, the probability
  604. is based on the product of edge weights, and if ``multiplicative=False``
  605. it is based on the sum of the edge weight. However, since it is
  606. easier to determine the total weight of all spanning trees for the
  607. multiplicative version, that is significantly faster and should be used if
  608. possible. Additionally, setting `weight` to `None` will cause a spanning tree
  609. to be selected with uniform probability.
  610. The function uses algorithm A8 in [1]_ .
  611. Parameters
  612. ----------
  613. G : nx.Graph
  614. An undirected version of the original graph.
  615. weight : string
  616. The edge key for the edge attribute holding edge weight.
  617. multiplicative : bool, default=True
  618. If `True`, the probability of each tree is the product of its edge weight
  619. over the sum of the product of all the spanning trees in the graph. If
  620. `False`, the probability is the sum of its edge weight over the sum of
  621. the sum of weights for all spanning trees in the graph.
  622. seed : integer, random_state, or None (default)
  623. Indicator of random number generation state.
  624. See :ref:`Randomness<randomness>`.
  625. Returns
  626. -------
  627. nx.Graph
  628. A spanning tree using the distribution defined by the weight of the tree.
  629. References
  630. ----------
  631. .. [1] V. Kulkarni, Generating random combinatorial objects, Journal of
  632. Algorithms, 11 (1990), pp. 185–207
  633. """
  634. def find_node(merged_nodes, node):
  635. """
  636. We can think of clusters of contracted nodes as having one
  637. representative in the graph. Each node which is not in merged_nodes
  638. is still its own representative. Since a representative can be later
  639. contracted, we need to recursively search though the dict to find
  640. the final representative, but once we know it we can use path
  641. compression to speed up the access of the representative for next time.
  642. This cannot be replaced by the standard NetworkX union_find since that
  643. data structure will merge nodes with less representing nodes into the
  644. one with more representing nodes but this function requires we merge
  645. them using the order that contract_edges contracts using.
  646. Parameters
  647. ----------
  648. merged_nodes : dict
  649. The dict storing the mapping from node to representative
  650. node
  651. The node whose representative we seek
  652. Returns
  653. -------
  654. The representative of the `node`
  655. """
  656. if node not in merged_nodes:
  657. return node
  658. else:
  659. rep = find_node(merged_nodes, merged_nodes[node])
  660. merged_nodes[node] = rep
  661. return rep
  662. def prepare_graph():
  663. """
  664. For the graph `G`, remove all edges not in the set `V` and then
  665. contract all edges in the set `U`.
  666. Returns
  667. -------
  668. A copy of `G` which has had all edges not in `V` removed and all edges
  669. in `U` contracted.
  670. """
  671. # The result is a MultiGraph version of G so that parallel edges are
  672. # allowed during edge contraction
  673. result = nx.MultiGraph(incoming_graph_data=G)
  674. # Remove all edges not in V
  675. edges_to_remove = set(result.edges()).difference(V)
  676. result.remove_edges_from(edges_to_remove)
  677. # Contract all edges in U
  678. #
  679. # Imagine that you have two edges to contract and they share an
  680. # endpoint like this:
  681. # [0] ----- [1] ----- [2]
  682. # If we contract (0, 1) first, the contraction function will always
  683. # delete the second node it is passed so the resulting graph would be
  684. # [0] ----- [2]
  685. # and edge (1, 2) no longer exists but (0, 2) would need to be contracted
  686. # in its place now. That is why I use the below dict as a merge-find
  687. # data structure with path compression to track how the nodes are merged.
  688. merged_nodes = {}
  689. for u, v in U:
  690. u_rep = find_node(merged_nodes, u)
  691. v_rep = find_node(merged_nodes, v)
  692. # We cannot contract a node with itself
  693. if u_rep == v_rep:
  694. continue
  695. nx.contracted_nodes(result, u_rep, v_rep, self_loops=False, copy=False)
  696. merged_nodes[v_rep] = u_rep
  697. return merged_nodes, result
  698. def spanning_tree_total_weight(G, weight):
  699. """
  700. Find the sum of weights of the spanning trees of `G` using the
  701. approioate `method`.
  702. This is easy if the chosen method is 'multiplicative', since we can
  703. use Kirchhoff's Tree Matrix Theorem directly. However, with the
  704. 'additive' method, this process is slightly more complex and less
  705. computatiionally efficient as we have to find the number of spanning
  706. trees which contain each possible edge in the graph.
  707. Parameters
  708. ----------
  709. G : NetworkX Graph
  710. The graph to find the total weight of all spanning trees on.
  711. weight : string
  712. The key for the weight edge attribute of the graph.
  713. Returns
  714. -------
  715. float
  716. The sum of either the multiplicative or additive weight for all
  717. spanning trees in the graph.
  718. """
  719. if multiplicative:
  720. return nx.total_spanning_tree_weight(G, weight)
  721. else:
  722. # There are two cases for the total spanning tree additive weight.
  723. # 1. There is one edge in the graph. Then the only spanning tree is
  724. # that edge itself, which will have a total weight of that edge
  725. # itself.
  726. if G.number_of_edges() == 1:
  727. return G.edges(data=weight).__iter__().__next__()[2]
  728. # 2. There are more than two edges in the graph. Then, we can find the
  729. # total weight of the spanning trees using the formula in the
  730. # reference paper: take the weight of that edge and multiple it by
  731. # the number of spanning trees which have to include that edge. This
  732. # can be accomplished by contracting the edge and finding the
  733. # multiplicative total spanning tree weight if the weight of each edge
  734. # is assumed to be 1, which is conveniently built into networkx already,
  735. # by calling total_spanning_tree_weight with weight=None
  736. else:
  737. total = 0
  738. for u, v, w in G.edges(data=weight):
  739. total += w * nx.total_spanning_tree_weight(
  740. nx.contracted_edge(G, edge=(u, v), self_loops=False), None
  741. )
  742. return total
  743. U = set()
  744. st_cached_value = 0
  745. V = set(G.edges())
  746. shuffled_edges = list(G.edges())
  747. seed.shuffle(shuffled_edges)
  748. for u, v in shuffled_edges:
  749. e_weight = G[u][v][weight] if weight is not None else 1
  750. node_map, prepared_G = prepare_graph()
  751. G_total_tree_weight = spanning_tree_total_weight(prepared_G, weight)
  752. # Add the edge to U so that we can compute the total tree weight
  753. # assuming we include that edge
  754. # Now, if (u, v) cannot exist in G because it is fully contracted out
  755. # of existence, then it by definition cannot influence G_e's Kirchhoff
  756. # value. But, we also cannot pick it.
  757. rep_edge = (find_node(node_map, u), find_node(node_map, v))
  758. # Check to see if the 'representative edge' for the current edge is
  759. # in prepared_G. If so, then we can pick it.
  760. if rep_edge in prepared_G.edges:
  761. prepared_G_e = nx.contracted_edge(
  762. prepared_G, edge=rep_edge, self_loops=False
  763. )
  764. G_e_total_tree_weight = spanning_tree_total_weight(prepared_G_e, weight)
  765. if multiplicative:
  766. threshold = e_weight * G_e_total_tree_weight / G_total_tree_weight
  767. else:
  768. numerator = (
  769. st_cached_value + e_weight
  770. ) * nx.total_spanning_tree_weight(prepared_G_e) + G_e_total_tree_weight
  771. denominator = (
  772. st_cached_value * nx.total_spanning_tree_weight(prepared_G)
  773. + G_total_tree_weight
  774. )
  775. threshold = numerator / denominator
  776. else:
  777. threshold = 0.0
  778. z = seed.uniform(0.0, 1.0)
  779. if z > threshold:
  780. # Remove the edge from V since we did not pick it.
  781. V.remove((u, v))
  782. else:
  783. # Add the edge to U since we picked it.
  784. st_cached_value += e_weight
  785. U.add((u, v))
  786. # If we decide to keep an edge, it may complete the spanning tree.
  787. if len(U) == G.number_of_nodes() - 1:
  788. spanning_tree = nx.Graph()
  789. spanning_tree.add_edges_from(U)
  790. return spanning_tree
  791. raise Exception(f"Something went wrong! Only {len(U)} edges in the spanning tree!")
  792. class SpanningTreeIterator:
  793. """
  794. Iterate over all spanning trees of a graph in either increasing or
  795. decreasing cost.
  796. Notes
  797. -----
  798. This iterator uses the partition scheme from [1]_ (included edges,
  799. excluded edges and open edges) as well as a modified Kruskal's Algorithm
  800. to generate minimum spanning trees which respect the partition of edges.
  801. For spanning trees with the same weight, ties are broken arbitrarily.
  802. References
  803. ----------
  804. .. [1] G.K. Janssens, K. Sörensen, An algorithm to generate all spanning
  805. trees in order of increasing cost, Pesquisa Operacional, 2005-08,
  806. Vol. 25 (2), p. 219-229,
  807. https://www.scielo.br/j/pope/a/XHswBwRwJyrfL88dmMwYNWp/?lang=en
  808. """
  809. @dataclass(order=True)
  810. class Partition:
  811. """
  812. This dataclass represents a partition and stores a dict with the edge
  813. data and the weight of the minimum spanning tree of the partition dict.
  814. """
  815. mst_weight: float
  816. partition_dict: dict = field(compare=False)
  817. def __copy__(self):
  818. return SpanningTreeIterator.Partition(
  819. self.mst_weight, self.partition_dict.copy()
  820. )
  821. def __init__(self, G, weight="weight", minimum=True, ignore_nan=False):
  822. """
  823. Initialize the iterator
  824. Parameters
  825. ----------
  826. G : nx.Graph
  827. The directed graph which we need to iterate trees over
  828. weight : String, default = "weight"
  829. The edge attribute used to store the weight of the edge
  830. minimum : bool, default = True
  831. Return the trees in increasing order while true and decreasing order
  832. while false.
  833. ignore_nan : bool, default = False
  834. If a NaN is found as an edge weight normally an exception is raised.
  835. If `ignore_nan is True` then that edge is ignored instead.
  836. """
  837. self.G = G.copy()
  838. self.weight = weight
  839. self.minimum = minimum
  840. self.ignore_nan = ignore_nan
  841. # Randomly create a key for an edge attribute to hold the partition data
  842. self.partition_key = (
  843. "SpanningTreeIterators super secret partition attribute name"
  844. )
  845. def __iter__(self):
  846. """
  847. Returns
  848. -------
  849. SpanningTreeIterator
  850. The iterator object for this graph
  851. """
  852. self.partition_queue = PriorityQueue()
  853. self._clear_partition(self.G)
  854. mst_weight = partition_spanning_tree(
  855. self.G, self.minimum, self.weight, self.partition_key, self.ignore_nan
  856. ).size(weight=self.weight)
  857. self.partition_queue.put(
  858. self.Partition(mst_weight if self.minimum else -mst_weight, {})
  859. )
  860. return self
  861. def __next__(self):
  862. """
  863. Returns
  864. -------
  865. (multi)Graph
  866. The spanning tree of next greatest weight, which ties broken
  867. arbitrarily.
  868. """
  869. if self.partition_queue.empty():
  870. del self.G, self.partition_queue
  871. raise StopIteration
  872. partition = self.partition_queue.get()
  873. self._write_partition(partition)
  874. next_tree = partition_spanning_tree(
  875. self.G, self.minimum, self.weight, self.partition_key, self.ignore_nan
  876. )
  877. self._partition(partition, next_tree)
  878. self._clear_partition(next_tree)
  879. return next_tree
  880. def _partition(self, partition, partition_tree):
  881. """
  882. Create new partitions based of the minimum spanning tree of the
  883. current minimum partition.
  884. Parameters
  885. ----------
  886. partition : Partition
  887. The Partition instance used to generate the current minimum spanning
  888. tree.
  889. partition_tree : nx.Graph
  890. The minimum spanning tree of the input partition.
  891. """
  892. # create two new partitions with the data from the input partition dict
  893. p1 = self.Partition(0, partition.partition_dict.copy())
  894. p2 = self.Partition(0, partition.partition_dict.copy())
  895. for e in partition_tree.edges:
  896. # determine if the edge was open or included
  897. if e not in partition.partition_dict:
  898. # This is an open edge
  899. p1.partition_dict[e] = EdgePartition.EXCLUDED
  900. p2.partition_dict[e] = EdgePartition.INCLUDED
  901. self._write_partition(p1)
  902. p1_mst = partition_spanning_tree(
  903. self.G,
  904. self.minimum,
  905. self.weight,
  906. self.partition_key,
  907. self.ignore_nan,
  908. )
  909. p1_mst_weight = p1_mst.size(weight=self.weight)
  910. if nx.is_connected(p1_mst):
  911. p1.mst_weight = p1_mst_weight if self.minimum else -p1_mst_weight
  912. self.partition_queue.put(p1.__copy__())
  913. p1.partition_dict = p2.partition_dict.copy()
  914. def _write_partition(self, partition):
  915. """
  916. Writes the desired partition into the graph to calculate the minimum
  917. spanning tree.
  918. Parameters
  919. ----------
  920. partition : Partition
  921. A Partition dataclass describing a partition on the edges of the
  922. graph.
  923. """
  924. for u, v, d in self.G.edges(data=True):
  925. if (u, v) in partition.partition_dict:
  926. d[self.partition_key] = partition.partition_dict[(u, v)]
  927. else:
  928. d[self.partition_key] = EdgePartition.OPEN
  929. def _clear_partition(self, G):
  930. """
  931. Removes partition data from the graph
  932. """
  933. for u, v, d in G.edges(data=True):
  934. if self.partition_key in d:
  935. del d[self.partition_key]