Skip to content

Commit

Permalink
Simplify shortest path computation.
Browse files Browse the repository at this point in the history
  • Loading branch information
jfinkels committed May 10, 2015
1 parent 5b1c5e0 commit d3990a4
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 33 deletions.
40 changes: 17 additions & 23 deletions networkx/algorithms/shortest_paths/unweighted.py
Expand Up @@ -66,15 +66,16 @@ def single_source_shortest_path_length(G,source,cutoff=None):
return seen # return all path lengths as dictionary


def all_pairs_shortest_path_length(G,cutoff=None):
""" Compute the shortest path lengths between all nodes in G.
def all_pairs_shortest_path_length(G, cutoff=None):
"""Computes the shortest path lengths between all nodes in ``G``.
Parameters
----------
G : NetworkX graph
cutoff : integer, optional
depth to stop the search. Only paths of length <= cutoff are returned.
Depth at which to stop the search. Only paths of length at most
``cutoff`` are returned.
Returns
-------
Expand All @@ -87,20 +88,17 @@ def all_pairs_shortest_path_length(G,cutoff=None):
Examples
--------
>>> G=nx.path_graph(5)
>>> length=nx.all_pairs_shortest_path_length(G)
>>> G = nx.path_graph(5)
>>> length = nx.all_pairs_shortest_path_length(G)
>>> print(length[1][4])
3
>>> length[1]
{0: 1, 1: 0, 2: 1, 3: 2, 4: 3}
"""
paths={}
for n in G:
paths[n]=single_source_shortest_path_length(G,n,cutoff=cutoff)
return paths


length = single_source_shortest_path_length
# TODO This can be trivially parallelized.
return {n: length(G, n, cutoff=cutoff) for n in G}


def bidirectional_shortest_path(G,source,target):
Expand Down Expand Up @@ -258,15 +256,16 @@ def single_source_shortest_path(G,source,cutoff=None):
return paths


def all_pairs_shortest_path(G,cutoff=None):
""" Compute shortest paths between all nodes.
def all_pairs_shortest_path(G, cutoff=None):
"""Compute shortest paths between all nodes.
Parameters
----------
G : NetworkX graph
cutoff : integer, optional
Depth to stop the search. Only paths of length <= cutoff are returned.
Depth at which to stop the search. Only paths of length at most
``cutoff`` are returned.
Returns
-------
Expand All @@ -275,8 +274,8 @@ def all_pairs_shortest_path(G,cutoff=None):
Examples
--------
>>> G=nx.path_graph(5)
>>> path=nx.all_pairs_shortest_path(G)
>>> G = nx.path_graph(5)
>>> path = nx.all_pairs_shortest_path(G)
>>> print(path[0][4])
[0, 1, 2, 3, 4]
Expand All @@ -285,12 +284,8 @@ def all_pairs_shortest_path(G,cutoff=None):
floyd_warshall()
"""
paths={}
for n in G:
paths[n]=single_source_shortest_path(G,n,cutoff=cutoff)
return paths


# TODO This can be trivially parallelized.
return {n: single_source_shortest_path(G, n, cutoff=cutoff) for n in G}


def predecessor(G,source,target=None,cutoff=None,return_seen=None):
Expand Down Expand Up @@ -357,4 +352,3 @@ def predecessor(G,source,target=None,cutoff=None,return_seen=None):
return (pred,seen)
else:
return pred

16 changes: 6 additions & 10 deletions networkx/algorithms/shortest_paths/weighted.py
Expand Up @@ -460,11 +460,9 @@ def all_pairs_dijkstra_path_length(G, cutoff=None, weight='weight'):
The dictionary returned only has keys for reachable node pairs.
"""
paths = {}
for n in G:
paths[n] = single_source_dijkstra_path_length(G, n, cutoff=cutoff,
weight=weight)
return paths
length = single_source_dijkstra_path_length
# TODO This can be trivially parallelized.
return {n: length(G, n, cutoff=cutoff, weight=weight) for n in G}


def all_pairs_dijkstra_path(G, cutoff=None, weight='weight'):
Expand Down Expand Up @@ -502,11 +500,9 @@ def all_pairs_dijkstra_path(G, cutoff=None, weight='weight'):
floyd_warshall()
"""
paths = {}
for n in G:
paths[n] = single_source_dijkstra_path(G, n, cutoff=cutoff,
weight=weight)
return paths
path = single_source_dijkstra_path
# TODO This can be trivially parallelized.
return {n: path(G, n, cutoff=cutoff, weight=weight) for n in G}


def bellman_ford(G, source, weight='weight'):
Expand Down

0 comments on commit d3990a4

Please sign in to comment.