diff options
| author | Niels van Adrichem <n.l.m.vanadrichem@tudelft.nl> | 2015-11-16 16:15:18 +0100 |
|---|---|---|
| committer | Niels van Adrichem <n.l.m.vanadrichem@tudelft.nl> | 2015-11-16 16:15:18 +0100 |
| commit | 0c55748fa833b976962affea7f5e2c97abdbb2fc (patch) | |
| tree | c1abefcb86c881b53e701f666a0dc67d358c9480 | |
| parent | cf0a58bff4991cce88c283eb3c8287be71e19f30 (diff) | |
| download | networkx-0c55748fa833b976962affea7f5e2c97abdbb2fc.tar.gz | |
Changed function dijkstra_path_lengt() to stop computing at target
| -rw-r--r-- | networkx/algorithms/shortest_paths/weighted.py | 13 |
1 files changed, 12 insertions, 1 deletions
diff --git a/networkx/algorithms/shortest_paths/weighted.py b/networkx/algorithms/shortest_paths/weighted.py index 10254858..853f35b0 100644 --- a/networkx/algorithms/shortest_paths/weighted.py +++ b/networkx/algorithms/shortest_paths/weighted.py @@ -125,7 +125,18 @@ def dijkstra_path_length(G, source, target, weight='weight'): -------- bidirectional_dijkstra() """ - length = dict(single_source_dijkstra_path_length(G, source, weight=weight)) + + if source == target: + return 0 + + if G.is_multigraph(): + get_weight = lambda u, v, data: min( + eattr.get(weight, 1) for eattr in data.values()) + else: + get_weight = lambda u, v, data: data.get(weight, 1) + + length = dict(_dijkstra(G, source, get_weight, target=target)) + try: return length[target] except KeyError: |
