{
  "__type": "IngestedDoc",
  "__tag": 4010,
  "_content": {
    "Notes": {
      "__type": "Section",
      "__tag": 4015,
      "children": [],
      "title": [],
      "level": 0,
      "target": null
    },
    "Warns": {
      "__type": "Section",
      "__tag": 4015,
      "children": [],
      "title": [],
      "level": 0,
      "target": null
    },
    "Raises": {
      "__type": "Section",
      "__tag": 4015,
      "children": [],
      "title": [],
      "level": 0,
      "target": null
    },
    "Yields": {
      "__type": "Section",
      "__tag": 4015,
      "children": [],
      "title": [],
      "level": 0,
      "target": null
    },
    "Methods": {
      "__type": "Section",
      "__tag": 4015,
      "children": [],
      "title": [],
      "level": 0,
      "target": null
    },
    "Returns": {
      "__type": "Section",
      "__tag": 4015,
      "children": [],
      "title": [],
      "level": 0,
      "target": null
    },
    "Summary": {
      "__type": "Section",
      "__tag": 4015,
      "children": [
        {
          "__type": "Paragraph",
          "__tag": 4045,
          "children": [
            {
              "__type": "Text",
              "__tag": 4046,
              "value": "No Docstrings"
            }
          ]
        }
      ],
      "title": [],
      "level": 0,
      "target": null
    },
    "Receives": {
      "__type": "Section",
      "__tag": 4015,
      "children": [],
      "title": [],
      "level": 0,
      "target": null
    },
    "Warnings": {
      "__type": "Section",
      "__tag": 4015,
      "children": [],
      "title": [],
      "level": 0,
      "target": null
    },
    "Attributes": {
      "__type": "Section",
      "__tag": 4015,
      "children": [],
      "title": [],
      "level": 0,
      "target": null
    },
    "Parameters": {
      "__type": "Section",
      "__tag": 4015,
      "children": [],
      "title": [],
      "level": 0,
      "target": null
    },
    "Extended Summary": {
      "__type": "Section",
      "__tag": 4015,
      "children": [],
      "title": [],
      "level": 0,
      "target": null
    },
    "Other Parameters": {
      "__type": "Section",
      "__tag": 4015,
      "children": [],
      "title": [],
      "level": 0,
      "target": null
    }
  },
  "_ordered_sections": [
    "Summary",
    "Extended Summary",
    "Parameters",
    "Attributes",
    "Methods",
    "Returns",
    "Yields",
    "Receives",
    "Other Parameters",
    "Raises",
    "Warns",
    "Warnings",
    "Notes"
  ],
  "item_file": "/scipy/sparse/csgraph/__init__.py",
  "item_line": 0,
  "item_type": "module",
  "aliases": [
    "scipy.sparse.csgraph"
  ],
  "example_section_data": {
    "__type": "Section",
    "__tag": 4015,
    "children": [],
    "title": [],
    "level": 0,
    "target": null
  },
  "see_also": [],
  "signature": null,
  "references": null,
  "qa": "scipy.sparse.csgraph",
  "arbitrary": [
    {
      "__type": "Section",
      "__tag": 4015,
      "children": [
        {
          "__type": "Paragraph",
          "__tag": 4045,
          "children": [
            {
              "__type": "Text",
              "__tag": 4046,
              "value": "Fast graph algorithms based on sparse matrix representations."
            }
          ]
        }
      ],
      "title": [
        {
          "__type": "Text",
          "__tag": 4046,
          "value": "Compressed sparse graph routines ("
        },
        {
          "__type": "InlineRole",
          "__tag": 4003,
          "value": "scipy.sparse.csgraph",
          "domain": null,
          "role": "mod",
          "inventory": null
        },
        {
          "__type": "Text",
          "__tag": 4046,
          "value": ")"
        }
      ],
      "level": 0,
      "target": null
    },
    {
      "__type": "Section",
      "__tag": 4015,
      "children": [
        {
          "__type": "Code",
          "__tag": 4050,
          "value": ".. autosummary:: \n    :toctree:generated/\n    connected_components -- determine connected components of a graph\n    laplacian -- compute the laplacian of a graph\n    shortest_path -- compute the shortest path between points on a positive graph\n    dijkstra -- use Dijkstra's algorithm for shortest path\n    floyd_warshall -- use the Floyd-Warshall algorithm for shortest path\n    bellman_ford -- use the Bellman-Ford algorithm for shortest path\n    johnson -- use Johnson's algorithm for shortest path\n    yen -- use Yen's algorithm for K-shortest paths between to nodes.\n    breadth_first_order -- compute a breadth-first order of nodes\n    depth_first_order -- compute a depth-first order of nodes\n    breadth_first_tree -- construct the breadth-first tree from a given node\n    depth_first_tree -- construct a depth-first tree from a given node\n    minimum_spanning_tree -- construct the minimum spanning tree of a graph\n    reverse_cuthill_mckee -- compute permutation for reverse Cuthill-McKee ordering\n    maximum_flow -- solve the maximum flow problem for a graph\n    maximum_bipartite_matching -- compute a maximum matching of a bipartite graph\n    min_weight_full_bipartite_matching - compute a minimum weight full matching of a bipartite graph\n    structural_rank -- compute the structural rank of a graph\n    NegativeCycleError",
          "execution_status": null
        },
        {
          "__type": "Code",
          "__tag": 4050,
          "value": ".. autosummary:: \n    :toctree:generated/\n    construct_dist_matrix\n    csgraph_from_dense\n    csgraph_from_masked\n    csgraph_masked_from_dense\n    csgraph_to_dense\n    csgraph_to_masked\n    reconstruct_path",
          "execution_status": null
        }
      ],
      "title": [
        {
          "__type": "Text",
          "__tag": 4046,
          "value": "Contents"
        }
      ],
      "level": 1,
      "target": null
    },
    {
      "__type": "Section",
      "__tag": 4015,
      "children": [
        {
          "__type": "Paragraph",
          "__tag": 4045,
          "children": [
            {
              "__type": "Text",
              "__tag": 4046,
              "value": "This module uses graphs which are stored in a matrix format. A graph with N nodes can be represented by an (N x N) adjacency matrix G. If there is a connection from node i to node j, then G[i, j] = w, where w is the weight of the connection. For nodes i and j which are not connected, the value depends on the representation:"
            }
          ]
        },
        {
          "__type": "BulletList",
          "__tag": 4053,
          "ordered": false,
          "start": 1,
          "children": [
            {
              "__type": "ListItem",
              "__tag": 4054,
              "children": [
                {
                  "__type": "Paragraph",
                  "__tag": 4045,
                  "children": [
                    {
                      "__type": "Text",
                      "__tag": 4046,
                      "value": "for dense array representations, non-edges are represented by   G[i, j] = 0, infinity, or NaN."
                    }
                  ]
                }
              ]
            },
            {
              "__type": "ListItem",
              "__tag": 4054,
              "children": [
                {
                  "__type": "Paragraph",
                  "__tag": 4045,
                  "children": [
                    {
                      "__type": "Text",
                      "__tag": 4046,
                      "value": "for dense masked representations (of type np.ma.MaskedArray), non-edges   are represented by masked values. This can be useful when graphs with   zero-weight edges are desired."
                    }
                  ]
                }
              ]
            },
            {
              "__type": "ListItem",
              "__tag": 4054,
              "children": [
                {
                  "__type": "Paragraph",
                  "__tag": 4045,
                  "children": [
                    {
                      "__type": "Text",
                      "__tag": 4046,
                      "value": "for sparse array representations, non-edges are represented by   non-entries in the matrix. This sort of sparse representation also   allows for edges with zero weights."
                    }
                  ]
                }
              ]
            }
          ]
        },
        {
          "__type": "Paragraph",
          "__tag": 4045,
          "children": [
            {
              "__type": "Text",
              "__tag": 4046,
              "value": "As a concrete example, imagine that you would like to represent the following undirected graph                "
            }
          ]
        },
        {
          "__type": "Code",
          "__tag": 4050,
          "value": "      G\n\n     (0)\n    /   \\\n   1     2\n  /       \\\n(2)       (1)",
          "execution_status": null
        },
        {
          "__type": "Paragraph",
          "__tag": 4045,
          "children": [
            {
              "__type": "Text",
              "__tag": 4046,
              "value": "This graph has three nodes, where node 0 and 1 are connected by an edge of weight 2, and nodes 0 and 2 are connected by an edge of weight 1. We can construct the dense, masked, and sparse representations as follows, keeping in mind that an undirected graph is represented by a symmetric matrix      "
            }
          ]
        },
        {
          "__type": "Code",
          "__tag": 4050,
          "value": ">>> import numpy as np\n>>> G_dense = np.array([[0, 2, 1],\n...                     [2, 0, 0],\n...                     [1, 0, 0]])\n>>> G_masked = np.ma.masked_values(G_dense, 0)\n>>> from scipy.sparse import csr_array\n>>> G_sparse = csr_array(G_dense)",
          "execution_status": null
        },
        {
          "__type": "Paragraph",
          "__tag": 4045,
          "children": [
            {
              "__type": "Text",
              "__tag": 4046,
              "value": "This becomes more difficult when zero edges are significant. For example, consider the situation when we slightly modify the above graph               "
            }
          ]
        },
        {
          "__type": "Code",
          "__tag": 4050,
          "value": "     G2\n\n     (0)\n    /   \\\n   0     2\n  /       \\\n(2)       (1)",
          "execution_status": null
        },
        {
          "__type": "Paragraph",
          "__tag": 4045,
          "children": [
            {
              "__type": "Text",
              "__tag": 4046,
              "value": "This is identical to the previous graph, except nodes 0 and 2 are connected by an edge of zero weight. In this case, the dense representation above leads to ambiguities: how can non-edges be represented if zero is a meaningful value? In this case, either a masked or sparse representation must be used to eliminate the ambiguity      "
            }
          ]
        },
        {
          "__type": "Code",
          "__tag": 4050,
          "value": ">>> import numpy as np\n>>> G2_data = np.array([[np.inf, 2,      0     ],\n...                     [2,      np.inf, np.inf],\n...                     [0,      np.inf, np.inf]])\n>>> G2_masked = np.ma.masked_invalid(G2_data)\n>>> from scipy.sparse.csgraph import csgraph_from_dense\n>>> # G2_sparse = csr_array(G2_data) would give the wrong result\n>>> G2_sparse = csgraph_from_dense(G2_data, null_value=np.inf)\n>>> G2_sparse.data\narray([ 2.,  0.,  2.,  0.])",
          "execution_status": null
        },
        {
          "__type": "Paragraph",
          "__tag": 4045,
          "children": [
            {
              "__type": "Text",
              "__tag": 4046,
              "value": "Here we have used a utility routine from the csgraph submodule in order to convert the dense representation to a sparse representation which can be understood by the algorithms in submodule. By viewing the data array, we can see that the zero values are explicitly encoded in the graph."
            }
          ]
        }
      ],
      "title": [
        {
          "__type": "Text",
          "__tag": 4046,
          "value": "Graph Representations"
        }
      ],
      "level": 1,
      "target": null
    },
    {
      "__type": "Section",
      "__tag": 4015,
      "children": [
        {
          "__type": "Paragraph",
          "__tag": 4045,
          "children": [
            {
              "__type": "Text",
              "__tag": 4046,
              "value": "Matrices may represent either directed or undirected graphs. This is specified throughout the csgraph module by a boolean keyword. Graphs are assumed to be directed by default. In a directed graph, traversal from node i to node j can be accomplished over the edge G[i, j], but not the edge G[j, i].  Consider the following dense graph      "
            }
          ]
        },
        {
          "__type": "Code",
          "__tag": 4050,
          "value": ">>> import numpy as np\n>>> G_dense = np.array([[0, 1, 0],\n...                     [2, 0, 3],\n...                     [0, 4, 0]])",
          "execution_status": null
        },
        {
          "__type": "Paragraph",
          "__tag": 4045,
          "children": [
            {
              "__type": "Text",
              "__tag": 4046,
              "value": "When "
            },
            {
              "__type": "InlineCode",
              "__tag": 4051,
              "value": "directed=True"
            },
            {
              "__type": "Text",
              "__tag": 4046,
              "value": " we get the graph        "
            }
          ]
        },
        {
          "__type": "Code",
          "__tag": 4050,
          "value": "  ---1--> ---3-->\n(0)     (1)     (2)\n  <--2--- <--4---",
          "execution_status": null
        },
        {
          "__type": "Paragraph",
          "__tag": 4045,
          "children": [
            {
              "__type": "Text",
              "__tag": 4046,
              "value": "In a non-directed graph, traversal from node i to node j can be accomplished over either G[i, j] or G[j, i].  If both edges are not null, and the two have unequal weights, then the smaller of the two is used."
            }
          ]
        },
        {
          "__type": "Paragraph",
          "__tag": 4045,
          "children": [
            {
              "__type": "Text",
              "__tag": 4046,
              "value": "So for the same graph, when "
            },
            {
              "__type": "InlineCode",
              "__tag": 4051,
              "value": "directed=False"
            },
            {
              "__type": "Text",
              "__tag": 4046,
              "value": " we get the graph      "
            }
          ]
        },
        {
          "__type": "Code",
          "__tag": 4050,
          "value": "(0)--1--(1)--3--(2)",
          "execution_status": null
        },
        {
          "__type": "Paragraph",
          "__tag": 4045,
          "children": [
            {
              "__type": "Text",
              "__tag": 4046,
              "value": "Note that a symmetric matrix will represent an undirected graph, regardless of whether the 'directed' keyword is set to True or False. In this case, using "
            },
            {
              "__type": "InlineCode",
              "__tag": 4051,
              "value": "directed=True"
            },
            {
              "__type": "Text",
              "__tag": 4046,
              "value": " generally leads to more efficient computation."
            }
          ]
        },
        {
          "__type": "Paragraph",
          "__tag": 4045,
          "children": [
            {
              "__type": "Text",
              "__tag": 4046,
              "value": "The routines in this module accept as input either scipy.sparse representations (csr, csc, or lil format), masked representations, or dense representations with non-edges indicated by zeros, infinities, and NaN entries."
            }
          ]
        }
      ],
      "title": [
        {
          "__type": "Text",
          "__tag": 4046,
          "value": "Directed vs. undirected"
        }
      ],
      "level": 2,
      "target": null
    }
  ],
  "local_refs": []
}