__init__.py 7.3 KB

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