networkx 2.8.2 Pypi GitHub Homepage
Other Docs
NotesParametersRaisesReturnsBackRef
shortest_path(G, source=None, target=None, weight=None, method='dijkstra')

Notes

There may be more than one shortest path between a source and target. This returns only one of them.

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 :None:None:`weight` is None, unweighted graph methods are used, and this suggestion is ignored.

Raises

NodeNotFound

If :None:None:`source` is not in G.

ValueError

If :None:None:`method` is not among the supported options.

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].

Compute shortest paths in the graph.

See Also

all_pairs_bellman_ford_path
all_pairs_dijkstra_path
all_pairs_shortest_path
single_source_bellman_ford_path
single_source_dijkstra_path
single_source_shortest_path

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 = nx.shortest_path(G)  # source, target not specified
... p[2][4] # shortest path from source=2 to target=4 [2, 3, 4]
See :

Back References

The following pages refer to to this document either explicitly or contain code examples using this.

networkx.algorithms.simple_paths._bidirectional_dijkstra networkx.algorithms.simple_paths.all_simple_edge_paths networkx.algorithms.shortest_paths.generic.all_shortest_paths networkx.algorithms.shortest_paths.unweighted.bidirectional_shortest_path networkx.algorithms.simple_paths.all_simple_paths networkx.algorithms.shortest_paths.generic.shortest_path networkx.algorithms.simple_paths._bidirectional_shortest_path networkx.algorithms.shortest_paths.astar.astar_path networkx.algorithms.shortest_paths.weighted.bidirectional_dijkstra networkx.algorithms.shortest_paths.unweighted.single_source_shortest_path networkx.algorithms.shortest_paths.unweighted.single_target_shortest_path networkx.algorithms.simple_paths.shortest_simple_paths networkx.algorithms.flow.gomory_hu.gomory_hu_tree networkx.algorithms.flow.networksimplex.network_simplex networkx.algorithms.shortest_paths.generic._build_paths_from_predecessors

Local connectivity graph

Hover to see nodes names; edges to Self not shown, Caped at 50 nodes.

Using a canvas is more power efficient and can get hundred of nodes ; but does not allow hyperlinks; , arrows or text (beyond on hover)

SVG is more flexible but power hungry; and does not scale well to 50 + nodes.

All aboves nodes referred to, (or are referred from) current nodes; Edges from Self to other have been omitted (or all nodes would be connected to the central node "self" which is not useful). Nodes are colored by the library they belong to, and scaled with the number of references pointing them


GitHub : /networkx/algorithms/shortest_paths/generic.py#39
type: <class 'function'>
Commit: