__init__.py 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. r"""
  2. Compressed sparse graph routines (:mod:`scipy.sparse.csgraph`)
  3. ==============================================================
  4. .. currentmodule:: scipy.sparse.csgraph
  5. Fast graph algorithms based on sparse matrix representations.
  6. Contents
  7. --------
  8. .. autosummary::
  9. :toctree: generated/
  10. connected_components -- determine connected components of a graph
  11. laplacian -- compute the laplacian of a graph
  12. shortest_path -- compute the shortest path between points on a positive graph
  13. dijkstra -- use Dijkstra's algorithm for shortest path
  14. floyd_warshall -- use the Floyd-Warshall algorithm for shortest path
  15. bellman_ford -- use the Bellman-Ford algorithm for shortest path
  16. johnson -- use Johnson's algorithm for shortest path
  17. breadth_first_order -- compute a breadth-first order of nodes
  18. depth_first_order -- compute a depth-first order of nodes
  19. breadth_first_tree -- construct the breadth-first tree from a given node
  20. depth_first_tree -- construct a depth-first tree from a given node
  21. minimum_spanning_tree -- construct the minimum spanning tree of a graph
  22. reverse_cuthill_mckee -- compute permutation for reverse Cuthill-McKee ordering
  23. maximum_flow -- solve the maximum flow problem for a graph
  24. maximum_bipartite_matching -- compute a maximum matching of a bipartite graph
  25. min_weight_full_bipartite_matching - compute a minimum weight full matching of a bipartite graph
  26. structural_rank -- compute the structural rank of a graph
  27. NegativeCycleError
  28. .. autosummary::
  29. :toctree: generated/
  30. construct_dist_matrix
  31. csgraph_from_dense
  32. csgraph_from_masked
  33. csgraph_masked_from_dense
  34. csgraph_to_dense
  35. csgraph_to_masked
  36. reconstruct_path
  37. Graph Representations
  38. ---------------------
  39. This module uses graphs which are stored in a matrix format. A
  40. graph with N nodes can be represented by an (N x N) adjacency matrix G.
  41. If there is a connection from node i to node j, then G[i, j] = w, where
  42. w is the weight of the connection. For nodes i and j which are
  43. not connected, the value depends on the representation:
  44. - for dense array representations, non-edges are represented by
  45. G[i, j] = 0, infinity, or NaN.
  46. - for dense masked representations (of type np.ma.MaskedArray), non-edges
  47. are represented by masked values. This can be useful when graphs with
  48. zero-weight edges are desired.
  49. - for sparse array representations, non-edges are represented by
  50. non-entries in the matrix. This sort of sparse representation also
  51. allows for edges with zero weights.
  52. As a concrete example, imagine that you would like to represent the following
  53. undirected graph::
  54. G
  55. (0)
  56. / \
  57. 1 2
  58. / \
  59. (2) (1)
  60. This graph has three nodes, where node 0 and 1 are connected by an edge of
  61. weight 2, and nodes 0 and 2 are connected by an edge of weight 1.
  62. We can construct the dense, masked, and sparse representations as follows,
  63. keeping in mind that an undirected graph is represented by a symmetric matrix::
  64. >>> import numpy as np
  65. >>> G_dense = np.array([[0, 2, 1],
  66. ... [2, 0, 0],
  67. ... [1, 0, 0]])
  68. >>> G_masked = np.ma.masked_values(G_dense, 0)
  69. >>> from scipy.sparse import csr_matrix
  70. >>> G_sparse = csr_matrix(G_dense)
  71. This becomes more difficult when zero edges are significant. For example,
  72. consider the situation when we slightly modify the above graph::
  73. G2
  74. (0)
  75. / \
  76. 0 2
  77. / \
  78. (2) (1)
  79. This is identical to the previous graph, except nodes 0 and 2 are connected
  80. by an edge of zero weight. In this case, the dense representation above
  81. leads to ambiguities: how can non-edges be represented if zero is a meaningful
  82. value? In this case, either a masked or sparse representation must be used
  83. to eliminate the ambiguity::
  84. >>> import numpy as np
  85. >>> G2_data = np.array([[np.inf, 2, 0 ],
  86. ... [2, np.inf, np.inf],
  87. ... [0, np.inf, np.inf]])
  88. >>> G2_masked = np.ma.masked_invalid(G2_data)
  89. >>> from scipy.sparse.csgraph import csgraph_from_dense
  90. >>> # G2_sparse = csr_matrix(G2_data) would give the wrong result
  91. >>> G2_sparse = csgraph_from_dense(G2_data, null_value=np.inf)
  92. >>> G2_sparse.data
  93. array([ 2., 0., 2., 0.])
  94. Here we have used a utility routine from the csgraph submodule in order to
  95. convert the dense representation to a sparse representation which can be
  96. understood by the algorithms in submodule. By viewing the data array, we
  97. can see that the zero values are explicitly encoded in the graph.
  98. Directed vs. undirected
  99. ^^^^^^^^^^^^^^^^^^^^^^^
  100. Matrices may represent either directed or undirected graphs. This is
  101. specified throughout the csgraph module by a boolean keyword. Graphs are
  102. assumed to be directed by default. In a directed graph, traversal from node
  103. i to node j can be accomplished over the edge G[i, j], but not the edge
  104. G[j, i]. Consider the following dense graph::
  105. >>> import numpy as np
  106. >>> G_dense = np.array([[0, 1, 0],
  107. ... [2, 0, 3],
  108. ... [0, 4, 0]])
  109. When ``directed=True`` we get the graph::
  110. ---1--> ---3-->
  111. (0) (1) (2)
  112. <--2--- <--4---
  113. In a non-directed graph, traversal from node i to node j can be
  114. accomplished over either G[i, j] or G[j, i]. If both edges are not null,
  115. and the two have unequal weights, then the smaller of the two is used.
  116. So for the same graph, when ``directed=False`` we get the graph::
  117. (0)--1--(1)--3--(2)
  118. Note that a symmetric matrix will represent an undirected graph, regardless
  119. of whether the 'directed' keyword is set to True or False. In this case,
  120. using ``directed=True`` generally leads to more efficient computation.
  121. The routines in this module accept as input either scipy.sparse representations
  122. (csr, csc, or lil format), masked representations, or dense representations
  123. with non-edges indicated by zeros, infinities, and NaN entries.
  124. """
  125. __docformat__ = "restructuredtext en"
  126. __all__ = ['connected_components',
  127. 'laplacian',
  128. 'shortest_path',
  129. 'floyd_warshall',
  130. 'dijkstra',
  131. 'bellman_ford',
  132. 'johnson',
  133. 'breadth_first_order',
  134. 'depth_first_order',
  135. 'breadth_first_tree',
  136. 'depth_first_tree',
  137. 'minimum_spanning_tree',
  138. 'reverse_cuthill_mckee',
  139. 'maximum_flow',
  140. 'maximum_bipartite_matching',
  141. 'min_weight_full_bipartite_matching',
  142. 'structural_rank',
  143. 'construct_dist_matrix',
  144. 'reconstruct_path',
  145. 'csgraph_masked_from_dense',
  146. 'csgraph_from_dense',
  147. 'csgraph_from_masked',
  148. 'csgraph_to_dense',
  149. 'csgraph_to_masked',
  150. 'NegativeCycleError']
  151. from ._laplacian import laplacian
  152. from ._shortest_path import (
  153. shortest_path, floyd_warshall, dijkstra, bellman_ford, johnson,
  154. NegativeCycleError
  155. )
  156. from ._traversal import (
  157. breadth_first_order, depth_first_order, breadth_first_tree,
  158. depth_first_tree, connected_components
  159. )
  160. from ._min_spanning_tree import minimum_spanning_tree
  161. from ._flow import maximum_flow
  162. from ._matching import (
  163. maximum_bipartite_matching, min_weight_full_bipartite_matching
  164. )
  165. from ._reordering import reverse_cuthill_mckee, structural_rank
  166. from ._tools import (
  167. construct_dist_matrix, reconstruct_path, csgraph_from_dense,
  168. csgraph_to_dense, csgraph_masked_from_dense, csgraph_from_masked,
  169. csgraph_to_masked
  170. )
  171. from scipy._lib._testutils import PytestTester
  172. test = PytestTester(__name__)
  173. del PytestTester