code: def shortest_path(
      G, source=None, target=None, weight=None, method="dijkstra"
  ):
      """Compute shortest paths in the graph.
  
      Parameters
      ----------
      G : NetworkX graph
  
      source : node, optional
          Starting node for path. If not specified, compute shortest
          paths for each possible starting node.
  
      target : node, optional
          Ending node for path. If not specified, compute shortest
          paths to all possible nodes.
  
      weight : None, string or function, optional (default = None)
          If None, every edge has weight/distance/cost 1.
          If a string, use this edge attribute as the edge weight.
          Any edge attribute not present defaults to 1.
          If this is a function, the weight of an edge is the value
          returned by the function. The function must accept exactly
          three positional arguments: the two endpoints of an edge and
          the dictionary of edge attributes for that edge.
          The function must return a number.
  
      method : string, optional (default = 'dijkstra')
          The algorithm to use to compute the path.
          Supported options: 'dijkstra', 'bellman-ford'.
          Other inputs produce a ValueError.
          If `weight` is None, unweighted graph methods are used, and this
          suggestion is ignored.
  
      Returns
      -------
      path: list or dictionary
          All returned paths include both the source and target in the path.
  
          If the source and target are both specified, return a single list
          of nodes in a shortest path from the source to the target.
  
          If only the source is specified, return a dictionary keyed by
          targets with a list of nodes in a shortest path from the source
          to one of the targets.
  
          If only the target is specified, return a dictionary keyed by
          sources with a list of nodes in a shortest path from one of the
          sources to the target.
  
          If neither the source nor target are specified return a dictionary
          of dictionaries with path[source][target]=[list of nodes in path].
  
      Raises
      ------
      NodeNotFound
          If `source` is not in `G`.
  
      ValueError
          If `method` is not among the supported options.
  
      Examples
      --------
      >>> G = nx.path_graph(5)
      >>> print(nx.shortest_path(G, source=0, target=4))
      [0, 1, 2, 3, 4]
      >>> p = nx.shortest_path(G, source=0)  # target not specified
      >>> p[3] # shortest path from source=0 to target=3
      [0, 1, 2, 3]
      >>> p = nx.shortest_path(G, target=4)  # source not specified
      >>> p[1] # shortest path from source=1 to target=4
      [1, 2, 3, 4]
      >>> p = dict(nx.shortest_path(G))  # source, target not specified
      >>> p[2][4] # shortest path from source=2 to target=4
      [2, 3, 4]
  
      Notes
      -----
      There may be more than one shortest path between a source and target.
      This returns only one of them.
  
      See Also
      --------
      all_pairs_shortest_path
      all_pairs_dijkstra_path
      all_pairs_bellman_ford_path
      single_source_shortest_path
      single_source_dijkstra_path
      single_source_bellman_ford_path
      """
      if method not in ("dijkstra", "bellman-ford"):
          # so we don't need to check in each branch later
          raise ValueError(f"method not supported: {method}")
      method = "unweighted" if weight is None else method
      if source is None:
          if target is None:
              warnings.warn(
                  (
                      "\n\nshortest_path will return an iterator that yields\n"
                      "(node, path) pairs instead of a dictionary when source\n"
                      "and target are unspecified beginning in version 3.5\n\n"
                      "To keep the current behavior, use:\n\n"
                      "\tdict(nx.shortest_path(G))"
                  ),
                  FutureWarning,
                  stacklevel=3,
              )
  
              # Find paths between all pairs.
              if method == "unweighted":
                  paths = nx.all_pairs_shortest_path(G)
              elif method == "dijkstra":
                  paths = nx.all_pairs_dijkstra_path(G, weight=weight)
              else:  # method == 'bellman-ford':
                  paths = nx.all_pairs_bellman_ford_path(
                      G, weight=weight
                  )
          else:
              # Find paths from all nodes co-accessible to the target.
              if G.is_directed():
                  G = G.reverse(copy=False)
              if method == "unweighted":
                  paths = nx.single_source_shortest_path(G, target)
              elif method == "dijkstra":
                  paths = nx.single_source_dijkstra_path(
                      G, target, weight=weight
                  )
              else:  # method == 'bellman-ford':
                  paths = nx.single_source_bellman_ford_path(
                      G, target, weight=weight
                  )
              # Now flip the paths so they go from a source to the target.
              for target in paths:
                  paths[target] = list(reversed(paths[target]))
      else:
          if target is None:
              # Find paths to all nodes accessible from the source.
              if method == "unweighted":
                  paths = nx.single_source_shortest_path(G, source)
              elif method == "dijkstra":
                  paths = nx.single_source_dijkstra_path(
                      G, source, weight=weight
                  )
              else:  # method == 'bellman-ford':
                  paths = nx.single_source_bellman_ford_path(
                      G, source, weight=weight
                  )
          else:
              # Find shortest source-target path.
              if method == "unweighted":
                  paths = nx.bidirectional_shortest_path(
                      G, source, target
                  )
              elif method == "dijkstra":
                  _, paths = nx.bidirectional_dijkstra(
                      G, source, target, weight
                  )
              else:  # method == 'bellman-ford':
                  paths = nx.bellman_ford_path(
                      G, source, target, weight
                  )
      return paths
  
file_path: networkx/networkx/algorithms/shortest_paths/generic.py
imports: import warnings
  import networkx
label_desc: Use image node_id 2 for calling a global function with example usage: shortest_path(G, source, target, weight, method) and returns: paths