{
  "__type": "IngestedDoc",
  "__tag": 4010,
  "_content": {
    "Notes": {
      "__type": "Section",
      "__tag": 4015,
      "children": [
        {
          "__type": "Paragraph",
          "__tag": 4045,
          "children": [
            {
              "__type": "Text",
              "__tag": 4046,
              "value": "Yen's algorithm is a graph search algorithm that finds single-source "
            },
            {
              "__type": "ParamRef",
              "__tag": 4071,
              "name": "K"
            },
            {
              "__type": "Text",
              "__tag": 4046,
              "value": "-shortest loopless paths for a graph with nonnegative edge cost. The algorithm was published by Jin Y. Yen in 1971 and employs any shortest path algorithm to find the best path, then proceeds to find "
            },
            {
              "__type": "InlineCode",
              "__tag": 4051,
              "value": "K - 1"
            },
            {
              "__type": "Text",
              "__tag": 4046,
              "value": " deviations of the best path."
            }
          ]
        },
        {
          "__type": "Paragraph",
          "__tag": 4045,
          "children": [
            {
              "__type": "Text",
              "__tag": 4046,
              "value": "The algorithm is based on Dijsktra's algorithm for finding each shortest path. In case there are negative edges in the graph, Johnson's algorithm is applied."
            }
          ]
        },
        {
          "__type": "Paragraph",
          "__tag": 4045,
          "children": [
            {
              "__type": "Text",
              "__tag": 4046,
              "value": "If multiple valid solutions are possible, output may vary with SciPy and Python version."
            }
          ]
        }
      ],
      "title": [],
      "level": 0,
      "target": null
    },
    "Warns": {
      "__type": "Section",
      "__tag": 4015,
      "children": [],
      "title": [],
      "level": 0,
      "target": null
    },
    "Raises": {
      "__type": "Section",
      "__tag": 4015,
      "children": [
        {
          "__type": "Parameters",
          "__tag": 4026,
          "children": [
            {
              "__type": "DocParam",
              "__tag": 4016,
              "name": "",
              "annotation": "NegativeCycleError:",
              "desc": [
                {
                  "__type": "Paragraph",
                  "__tag": 4045,
                  "children": [
                    {
                      "__type": "Text",
                      "__tag": 4046,
                      "value": "If there are negative cycles in the graph"
                    }
                  ]
                }
              ]
            }
          ]
        }
      ],
      "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": [
        {
          "__type": "Parameters",
          "__tag": 4026,
          "children": [
            {
              "__type": "DocParam",
              "__tag": 4016,
              "name": "dist_array",
              "annotation": "ndarray",
              "desc": [
                {
                  "__type": "Paragraph",
                  "__tag": 4045,
                  "children": [
                    {
                      "__type": "Text",
                      "__tag": 4046,
                      "value": "Array of size "
                    },
                    {
                      "__type": "InlineCode",
                      "__tag": 4051,
                      "value": "M"
                    },
                    {
                      "__type": "Text",
                      "__tag": 4046,
                      "value": " of shortest distances between the source and sink nodes. "
                    },
                    {
                      "__type": "InlineCode",
                      "__tag": 4051,
                      "value": "dist_array[i]"
                    },
                    {
                      "__type": "Text",
                      "__tag": 4046,
                      "value": " gives the i-th shortest distance from the source to the sink along the graph. "
                    },
                    {
                      "__type": "InlineCode",
                      "__tag": 4051,
                      "value": "M"
                    },
                    {
                      "__type": "Text",
                      "__tag": 4046,
                      "value": " is the number of shortest paths found, which is less than or equal to "
                    },
                    {
                      "__type": "ParamRef",
                      "__tag": 4071,
                      "name": "K"
                    },
                    {
                      "__type": "Text",
                      "__tag": 4046,
                      "value": "."
                    }
                  ]
                }
              ]
            },
            {
              "__type": "DocParam",
              "__tag": 4016,
              "name": "predecessors",
              "annotation": "ndarray",
              "desc": [
                {
                  "__type": "Paragraph",
                  "__tag": 4045,
                  "children": [
                    {
                      "__type": "Text",
                      "__tag": 4046,
                      "value": "Returned only if "
                    },
                    {
                      "__type": "InlineCode",
                      "__tag": 4051,
                      "value": "return_predecessors == True"
                    },
                    {
                      "__type": "Text",
                      "__tag": 4046,
                      "value": ". The M x N matrix of predecessors, which can be used to reconstruct the shortest paths. "
                    },
                    {
                      "__type": "InlineCode",
                      "__tag": 4051,
                      "value": "M"
                    },
                    {
                      "__type": "Text",
                      "__tag": 4046,
                      "value": " is the number of shortest paths found, which is less than or equal to "
                    },
                    {
                      "__type": "ParamRef",
                      "__tag": 4071,
                      "name": "K"
                    },
                    {
                      "__type": "Text",
                      "__tag": 4046,
                      "value": ". Row "
                    },
                    {
                      "__type": "InlineCode",
                      "__tag": 4051,
                      "value": "i"
                    },
                    {
                      "__type": "Text",
                      "__tag": 4046,
                      "value": " of the predecessor matrix contains information on the "
                    },
                    {
                      "__type": "InlineCode",
                      "__tag": 4051,
                      "value": "i"
                    },
                    {
                      "__type": "Text",
                      "__tag": 4046,
                      "value": "-th shortest path from the source to the sink: each entry "
                    },
                    {
                      "__type": "InlineCode",
                      "__tag": 4051,
                      "value": "predecessors[i, j]"
                    },
                    {
                      "__type": "Text",
                      "__tag": 4046,
                      "value": " gives the index of the previous node in the path from the source to node "
                    },
                    {
                      "__type": "InlineCode",
                      "__tag": 4051,
                      "value": "j"
                    },
                    {
                      "__type": "Text",
                      "__tag": 4046,
                      "value": ".  If the path does not pass via node "
                    },
                    {
                      "__type": "InlineCode",
                      "__tag": 4051,
                      "value": "j"
                    },
                    {
                      "__type": "Text",
                      "__tag": 4046,
                      "value": ", then "
                    },
                    {
                      "__type": "InlineCode",
                      "__tag": 4051,
                      "value": "predecessors[i, j] = -9999"
                    },
                    {
                      "__type": "Text",
                      "__tag": 4046,
                      "value": "."
                    }
                  ]
                }
              ]
            }
          ]
        }
      ],
      "title": [],
      "level": 0,
      "target": null
    },
    "Summary": {
      "__type": "Section",
      "__tag": 4015,
      "children": [
        {
          "__type": "Paragraph",
          "__tag": 4045,
          "children": [
            {
              "__type": "Text",
              "__tag": 4046,
              "value": "Yen's K-Shortest Paths algorithm on a directed or undirected graph."
            }
          ]
        }
      ],
      "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": [
        {
          "__type": "Parameters",
          "__tag": 4026,
          "children": [
            {
              "__type": "DocParam",
              "__tag": 4016,
              "name": "csgraph",
              "annotation": "array_like, or sparse array or matrix, 2 dimensions",
              "desc": [
                {
                  "__type": "Paragraph",
                  "__tag": 4045,
                  "children": [
                    {
                      "__type": "Text",
                      "__tag": 4046,
                      "value": "The N x N array of distances representing the input graph."
                    }
                  ]
                }
              ]
            },
            {
              "__type": "DocParam",
              "__tag": 4016,
              "name": "source",
              "annotation": "int",
              "desc": [
                {
                  "__type": "Paragraph",
                  "__tag": 4045,
                  "children": [
                    {
                      "__type": "Text",
                      "__tag": 4046,
                      "value": "The index of the starting node for the paths."
                    }
                  ]
                }
              ]
            },
            {
              "__type": "DocParam",
              "__tag": 4016,
              "name": "sink",
              "annotation": "int",
              "desc": [
                {
                  "__type": "Paragraph",
                  "__tag": 4045,
                  "children": [
                    {
                      "__type": "Text",
                      "__tag": 4046,
                      "value": "The index of the ending node for the paths."
                    }
                  ]
                }
              ]
            },
            {
              "__type": "DocParam",
              "__tag": 4016,
              "name": "K",
              "annotation": "int",
              "desc": [
                {
                  "__type": "Paragraph",
                  "__tag": 4045,
                  "children": [
                    {
                      "__type": "Text",
                      "__tag": 4046,
                      "value": "The number of shortest paths to find."
                    }
                  ]
                }
              ]
            },
            {
              "__type": "DocParam",
              "__tag": 4016,
              "name": "directed",
              "annotation": "bool, optional",
              "desc": [
                {
                  "__type": "Paragraph",
                  "__tag": 4045,
                  "children": [
                    {
                      "__type": "Text",
                      "__tag": 4046,
                      "value": "If "
                    },
                    {
                      "__type": "InlineCode",
                      "__tag": 4051,
                      "value": "True"
                    },
                    {
                      "__type": "Text",
                      "__tag": 4046,
                      "value": " (default), then find the shortest path on a directed graph: only move from point "
                    },
                    {
                      "__type": "InlineCode",
                      "__tag": 4051,
                      "value": "i"
                    },
                    {
                      "__type": "Text",
                      "__tag": 4046,
                      "value": " to point "
                    },
                    {
                      "__type": "InlineCode",
                      "__tag": 4051,
                      "value": "j"
                    },
                    {
                      "__type": "Text",
                      "__tag": 4046,
                      "value": " along paths "
                    },
                    {
                      "__type": "InlineCode",
                      "__tag": 4051,
                      "value": "csgraph[i, j]"
                    },
                    {
                      "__type": "Text",
                      "__tag": 4046,
                      "value": ". If False, then find the shortest path on an undirected graph: the algorithm can progress from point i to j along "
                    },
                    {
                      "__type": "InlineCode",
                      "__tag": 4051,
                      "value": "csgraph[i, j]"
                    },
                    {
                      "__type": "Text",
                      "__tag": 4046,
                      "value": " or "
                    },
                    {
                      "__type": "InlineCode",
                      "__tag": 4051,
                      "value": "csgraph[j, i]"
                    },
                    {
                      "__type": "Text",
                      "__tag": 4046,
                      "value": "."
                    }
                  ]
                }
              ]
            },
            {
              "__type": "DocParam",
              "__tag": 4016,
              "name": "return_predecessors",
              "annotation": "bool, optional",
              "desc": [
                {
                  "__type": "Paragraph",
                  "__tag": 4045,
                  "children": [
                    {
                      "__type": "Text",
                      "__tag": 4046,
                      "value": "If "
                    },
                    {
                      "__type": "InlineCode",
                      "__tag": 4051,
                      "value": "True"
                    },
                    {
                      "__type": "Text",
                      "__tag": 4046,
                      "value": ", return the size "
                    },
                    {
                      "__type": "InlineCode",
                      "__tag": 4051,
                      "value": "(M, N)"
                    },
                    {
                      "__type": "Text",
                      "__tag": 4046,
                      "value": " predecessor matrix. Default: "
                    },
                    {
                      "__type": "InlineCode",
                      "__tag": 4051,
                      "value": "False"
                    },
                    {
                      "__type": "Text",
                      "__tag": 4046,
                      "value": "."
                    }
                  ]
                }
              ]
            },
            {
              "__type": "DocParam",
              "__tag": 4016,
              "name": "unweighted",
              "annotation": "bool, optional",
              "desc": [
                {
                  "__type": "Paragraph",
                  "__tag": 4045,
                  "children": [
                    {
                      "__type": "Text",
                      "__tag": 4046,
                      "value": "If "
                    },
                    {
                      "__type": "InlineCode",
                      "__tag": 4051,
                      "value": "True"
                    },
                    {
                      "__type": "Text",
                      "__tag": 4046,
                      "value": ", then find unweighted distances. That is, rather than finding the path between each point such that the sum of weights is minimized, find the path such that the number of edges is minimized. Default: "
                    },
                    {
                      "__type": "InlineCode",
                      "__tag": 4051,
                      "value": "False"
                    },
                    {
                      "__type": "Text",
                      "__tag": 4046,
                      "value": "."
                    }
                  ]
                }
              ]
            }
          ]
        }
      ],
      "title": [],
      "level": 0,
      "target": null
    },
    "Extended Summary": {
      "__type": "Section",
      "__tag": 4015,
      "children": [
        {
          "__type": "Admonition",
          "__tag": 4056,
          "kind": "versionadded",
          "base_type": "neutral",
          "children": [
            {
              "__type": "AdmonitionTitle",
              "__tag": 4055,
              "children": [
                {
                  "__type": "Text",
                  "__tag": 4046,
                  "value": "versionadded 1.14.0"
                }
              ]
            }
          ]
        }
      ],
      "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": null,
  "item_line": null,
  "item_type": "cython_function_or_method",
  "aliases": [
    "scipy.sparse.csgraph.yen"
  ],
  "example_section_data": {
    "__type": "Section",
    "__tag": 4015,
    "children": [
      {
        "__type": "Code",
        "__tag": 4050,
        "value": "from scipy.sparse import csr_array\nfrom scipy.sparse.csgraph import yen\n",
        "execution_status": "success"
      },
      {
        "__type": "Text",
        "__tag": 4046,
        "value": "\n"
      },
      {
        "__type": "Code",
        "__tag": 4050,
        "value": "graph = [\n[0, 1, 2, 0],\n[0, 0, 0, 1],\n[2, 0, 0, 3],\n[0, 0, 0, 0]\n]\ngraph = csr_array(graph)\n",
        "execution_status": "success"
      },
      {
        "__type": "Code",
        "__tag": 4050,
        "value": "print(graph)\n",
        "execution_status": "failure"
      },
      {
        "__type": "Text",
        "__tag": 4046,
        "value": "\n"
      },
      {
        "__type": "Code",
        "__tag": 4050,
        "value": "dist_array, predecessors = yen(csgraph=graph, source=0, sink=3, K=2,\n                               directed=False, return_predecessors=True)\ndist_array\n",
        "execution_status": "success"
      },
      {
        "__type": "Code",
        "__tag": 4050,
        "value": "predecessors\n",
        "execution_status": "failure"
      }
    ],
    "title": [],
    "level": 0,
    "target": null
  },
  "see_also": [],
  "signature": {
    "__type": "SignatureNode",
    "__tag": 4029,
    "kind": "function",
    "parameters": [
      {
        "__type": "SigParam",
        "__tag": 4030,
        "name": "csgraph",
        "annotation": {
          "__type": "Empty",
          "__tag": 4031
        },
        "kind": "POSITIONAL_OR_KEYWORD",
        "default": {
          "__type": "Empty",
          "__tag": 4031
        }
      },
      {
        "__type": "SigParam",
        "__tag": 4030,
        "name": "source",
        "annotation": {
          "__type": "Empty",
          "__tag": 4031
        },
        "kind": "POSITIONAL_OR_KEYWORD",
        "default": {
          "__type": "Empty",
          "__tag": 4031
        }
      },
      {
        "__type": "SigParam",
        "__tag": 4030,
        "name": "sink",
        "annotation": {
          "__type": "Empty",
          "__tag": 4031
        },
        "kind": "POSITIONAL_OR_KEYWORD",
        "default": {
          "__type": "Empty",
          "__tag": 4031
        }
      },
      {
        "__type": "SigParam",
        "__tag": 4030,
        "name": "K",
        "annotation": {
          "__type": "Empty",
          "__tag": 4031
        },
        "kind": "POSITIONAL_OR_KEYWORD",
        "default": {
          "__type": "Empty",
          "__tag": 4031
        }
      },
      {
        "__type": "SigParam",
        "__tag": 4030,
        "name": "directed",
        "annotation": {
          "__type": "Empty",
          "__tag": 4031
        },
        "kind": "KEYWORD_ONLY",
        "default": "True"
      },
      {
        "__type": "SigParam",
        "__tag": 4030,
        "name": "return_predecessors",
        "annotation": {
          "__type": "Empty",
          "__tag": 4031
        },
        "kind": "KEYWORD_ONLY",
        "default": "False"
      },
      {
        "__type": "SigParam",
        "__tag": 4030,
        "name": "unweighted",
        "annotation": {
          "__type": "Empty",
          "__tag": 4031
        },
        "kind": "KEYWORD_ONLY",
        "default": "False"
      }
    ],
    "return_annotation": {
      "__type": "Empty",
      "__tag": 4031
    },
    "target_name": "yen"
  },
  "references": [
    ".. [1] https://en.wikipedia.org/wiki/Yen%27s_algorithm",
    ".. [2] https://www.ams.org/journals/qam/1970-27-04/S0033-569X-1970-0253822-7/"
  ],
  "qa": "scipy.sparse.csgraph._shortest_path:yen",
  "arbitrary": [],
  "local_refs": [
    "K",
    "csgraph",
    "directed",
    "dist_array",
    "predecessors",
    "return_predecessors",
    "sink",
    "source",
    "unweighted"
  ]
}