Package networkx :: Module path
[hide private]
[frames] | no frames]

Module path

source code

Shortest path algorithms.


Author: Aric Hagberg (hagberg@lanl.gov)

Functions [hide private]
 
shortest_path_length(G, source, target)
Return the shortest path length in the graph G between the source and target.
source code
 
single_source_shortest_path_length(G, source, cutoff=None)
Shortest path length from source to all reachable nodes.
source code
 
all_pairs_shortest_path_length(G, cutoff=None)
Return dictionary of shortest path lengths between all nodes in G.
source code
 
shortest_path(G, source, target)
Return a list of nodes in G for a shortest path between source and target.
source code
 
bidirectional_shortest_path(G, source, target)
Return list of nodes in a shortest path between source and target.
source code
 
_bidirectional_pred_succ(G, source, target)
Bidirectional shortest path helper.
source code
 
single_source_shortest_path(G, source, cutoff=None)
Return list of nodes in a shortest path between source and all other nodes in G reachable from source.
source code
 
all_pairs_shortest_path(G, cutoff=None)
Return dictionary of shortest paths between all nodes in G.
source code
 
dijkstra_path(G, source, target)
Returns the shortest path from source to target in a weighted graph G.
source code
 
dijkstra_path_length(G, source, target)
Returns the shortest path length from source to target in a weighted graph G.
source code
 
bidirectional_dijkstra(G, source, target)
Dijkstra's algorithm for shortest paths using bidirectional search.
source code
 
single_source_dijkstra_path(G, source)
Returns the shortest paths from source to all other reachable nodes in a weighted graph G.
source code
 
single_source_dijkstra_path_length(G, source)
Returns the shortest path lengths from source to all other reachable nodes in a weighted graph G.
source code
 
single_source_dijkstra(G, source, target=None)
Dijkstra's algorithm for shortest paths in a weighted graph G.
source code
 
dijkstra_predecessor_and_distance(G, source)
Same algorithm as for single_source_dijsktra, but returns two dicts representing a list of predecessors of a node and the distance to each node respectively.
source code
 
floyd_warshall_array(graph)
The Floyd-Warshall algorithm for all pairs shortest paths.
source code
 
floyd_warshall(G, huge=inf)
The Floyd-Warshall algorithm for all pairs shortest paths.
source code
 
predecessor(G, source, target=None, cutoff=None, return_seen=None)
Returns dictionary of predecessors for the path from source to all nodes in G.
source code
 
_test_suite() source code
Variables [hide private]
  ___revision__ = ''
Function Details [hide private]

shortest_path_length(G, source, target)

source code 

Return the shortest path length in the graph G between the source and target. Raise an exception if no path exists.

G is treated as an unweighted graph. For weighted graphs see dijkstra_path_length.

single_source_shortest_path_length(G, source, cutoff=None)

source code 

Shortest path length from source to all reachable nodes.

Returns a dictionary of shortest path lengths keyed by target.

>>> G=path_graph(5)
>>> length=single_source_shortest_path_length(G,1)
>>> length[4]
3
>>> print length
{0: 1, 1: 0, 2: 1, 3: 2, 4: 3}

cutoff is optional integer depth to stop the search - only paths of length <= cutoff are returned.

all_pairs_shortest_path_length(G, cutoff=None)

source code 

Return dictionary of shortest path lengths between all nodes in G.

The dictionary only has keys for reachable node pairs. >>> G=path_graph(5) >>> length=all_pairs_shortest_path_length(G) >>> print length[1][4] 3 >>> length[1] {0: 1, 1: 0, 2: 1, 3: 2, 4: 3}

cutoff is optional integer depth to stop the search - only paths of length <= cutoff are returned.

shortest_path(G, source, target)

source code 

Return a list of nodes in G for a shortest path between source and target.

There may be more than one shortest path. This returns only one.

bidirectional_shortest_path(G, source, target)

source code 

Return list of nodes in a shortest path between source and target. Return False if no path exists.

Also known as shortest_path.

_bidirectional_pred_succ(G, source, target)

source code 

Bidirectional shortest path helper.

Returns (pred,succ,w) where pred is a dictionary of predecessors from w to the source, and succ is a dictionary of successors from w to the target.

single_source_shortest_path(G, source, cutoff=None)

source code 

Return list of nodes in a shortest path between source and all other nodes in G reachable from source.

There may be more than one shortest path between the source and target nodes - this routine returns only one.

cutoff is optional integer depth to stop the search - only paths of length <= cutoff are returned.

See also shortest_path and bidirectional_shortest_path.

all_pairs_shortest_path(G, cutoff=None)

source code 

Return dictionary of shortest paths between all nodes in G.

The dictionary only has keys for reachable node pairs.

cutoff is optional integer depth to stop the search - only paths of length <= cutoff are returned.

See also floyd_warshall.

dijkstra_path(G, source, target)

source code 

Returns the shortest path from source to target in a weighted graph G. Uses a bidirectional version of Dijkstra's algorithm.

Edge data must be numerical values for XGraph and XDiGraphs. The weights are assigned to be 1 for Graphs and DiGraphs.

See also bidirectional_dijkstra for more information about the algorithm.

dijkstra_path_length(G, source, target)

source code 

Returns the shortest path length from source to target in a weighted graph G. Uses a bidirectional version of Dijkstra's algorithm.

Edge data must be numerical values for XGraph and XDiGraphs. The weights are assigned to be 1 for Graphs and DiGraphs.

See also bidirectional_dijkstra for more information about the algorithm.

bidirectional_dijkstra(G, source, target)

source code 

Dijkstra's algorithm for shortest paths using bidirectional search.

Returns a two-tuple (d,p) where d is the distance and p is the path from the source to the target.

Distances are calculated as sums of weighted edges traversed.

Edges must hold numerical values for XGraph and XDiGraphs. The weights are set to 1 for Graphs and DiGraphs.

In practice bidirectional Dijkstra is much more than twice as fast as ordinary Dijkstra.

Ordinary Dijkstra expands nodes in a sphere-like manner from the source. The radius of this sphere will eventually be the length of the shortest path. Bidirectional Dijkstra will expand nodes from both the source and the target, making two spheres of half this radius. Volume of the first sphere is pi*r*r while the others are 2*pi*r/2*r/2, making up half the volume.

This algorithm is not guaranteed to work if edge weights are negative or are floating point numbers (overflows and roundoff errors can cause problems).

single_source_dijkstra_path(G, source)

source code 

Returns the shortest paths from source to all other reachable nodes in a weighted graph G. Uses Dijkstra's algorithm.

Returns a dictionary of shortest path lengths keyed by source.

Edge data must be numerical values for XGraph and XDiGraphs. The weights are assigned to be 1 for Graphs and DiGraphs.

See also single_source_dijkstra for more information about the algorithm.

single_source_dijkstra_path_length(G, source)

source code 

Returns the shortest path lengths from source to all other reachable nodes in a weighted graph G. Uses Dijkstra's algorithm.

Returns a dictionary of shortest path lengths keyed by source.

Edge data must be numerical values for XGraph and XDiGraphs. The weights are assigned to be 1 for Graphs and DiGraphs.

See also single_source_dijkstra for more information about the algorithm.

single_source_dijkstra(G, source, target=None)

source code 

Dijkstra's algorithm for shortest paths in a weighted graph G.

Use:

single_source_dijkstra_path() - shortest path list of nodes

single_source_dijkstra_path_length() - shortest path length

Returns a tuple of two dictionaries keyed by node. The first stores distance from the source. The second stores the path from the source to that node.

Distances are calculated as sums of weighted edges traversed. Edges must hold numerical values for XGraph and XDiGraphs. The weights are 1 for Graphs and DiGraphs.

Optional target argument stops the search when target is found.

Based on the Python cookbook recipe (119466) at http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/119466

This algorithm is not guaranteed to work if edge weights are negative or are floating point numbers (overflows and roundoff errors can cause problems).

See also 'bidirectional_dijkstra_path'

dijkstra_predecessor_and_distance(G, source)

source code 

Same algorithm as for single_source_dijsktra, but returns two dicts representing a list of predecessors of a node and the distance to each node respectively. The list of predecessors contains more than one element only when there are more than one shortest paths to the key node.

This routine is intended for use with the betweenness centrality algorithms in centrality.py.

floyd_warshall_array(graph)

source code 

The Floyd-Warshall algorithm for all pairs shortest paths.

Returns a tuple (distance,path) containing two arrays of shortest distance and paths as a predecessor matrix.

This differs from floyd_warshall only in the types of the return values. Thus, path[i,j] gives the predecessor at j on a path from i to j. A value of None indicates that no path exists. A predecessor of i indicates the beginning of the path. The advantage of this implementation is that, while running time is O(n^3), running space is O(n^2).

This algorithm handles negative weights.

floyd_warshall(G, huge=inf)

source code 

The Floyd-Warshall algorithm for all pairs shortest paths.

Returns a tuple (distance,path) containing two dictionaries of shortest distance and predecessor paths.

This algorithm is most appropriate for dense graphs. The running time is O(n^3), and running space is O(n^2) where n is the number of nodes in G.

For sparse graphs, see

all_pairs_shortest_path all_pairs_shortest_path_length

which are based on Dijkstra's algorithm.

predecessor(G, source, target=None, cutoff=None, return_seen=None)

source code 

Returns dictionary of predecessors for the path from source to all nodes in G.

Optional target returns only predecessors between source and target. Cutoff is a limit on the number of hops traversed.

Example for the path graph 0-1-2-3

>>> G=path_graph(4)
>>> print G.nodes()
[0, 1, 2, 3]
>>> predecessor(G,0)
{0: [], 1: [0], 2: [1], 3: [2]}