Skip to content

Commit 22c6dc6

Browse files
author
aric
committed
Split shortest paths algorithms and provide simpler interface
that allows weighted keyword. New interface still needs tests. Addresses #196 and fixes #308 --HG-- extra : convert_revision : svn%3A3ed01bd8-26fb-0310-9e4c-ca1a4053419f/networkx/trunk%401594
1 parent 9d08c31 commit 22c6dc6

7 files changed

Lines changed: 1201 additions & 975 deletions

File tree

‎doc/source/reference/algorithms.shortest_paths.rst‎

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,31 +6,56 @@ Shortest Paths
66
.. toctree::
77
:maxdepth: 2
88

9-
Shortest Paths
10-
--------------
11-
.. automodule:: networkx.algorithms.shortest_paths.path
9+
.. automodule:: networkx.algorithms.shortest_paths.generic
1210
.. currentmodule:: networkx
1311

1412
.. autosummary::
1513
:toctree: generated/
1614

17-
average_shortest_path_length
1815
shortest_path
1916
shortest_path_length
17+
average_shortest_path_length
18+
19+
20+
21+
Advanced Interface
22+
------------------
23+
24+
.. automodule:: networkx.algorithms.shortest_paths.unweighted
25+
.. currentmodule:: networkx
26+
27+
.. autosummary::
28+
:toctree: generated/
29+
2030
single_source_shortest_path
2131
single_source_shortest_path_length
2232
all_pairs_shortest_path
2333
all_pairs_shortest_path_length
34+
predecessor
35+
floyd_warshall
36+
37+
38+
.. automodule:: networkx.algorithms.shortest_paths.weighted
39+
.. currentmodule:: networkx
40+
41+
.. autosummary::
42+
:toctree: generated/
43+
2444
dijkstra_path
2545
dijkstra_path_length
2646
single_source_dijkstra_path
2747
single_source_dijkstra_path_length
48+
all_pairs_dijkstra_path
49+
all_pairs_dijkstra_path_length
2850
single_source_dijkstra
2951
bidirectional_dijkstra
3052
bidirectional_shortest_path
3153
dijkstra_predecessor_and_distance
32-
predecessor
33-
floyd_warshall
54+
55+
56+
57+
A* Algorithm
58+
------------
3459

3560
.. automodule:: networkx.algorithms.shortest_paths.astar
3661
.. currentmodule:: networkx

‎networkx/algorithms/centrality.py‎

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,6 @@
2525

2626

2727
import heapq
28-
from networkx.algorithms.shortest_paths.path import predecessor, \
29-
single_source_shortest_path_length, \
30-
single_source_dijkstra_path_length, \
31-
dijkstra_predecessor_and_distance
3228
import networkx
3329

3430

@@ -231,9 +227,9 @@ def _node_betweenness(G,source,cutoff=False,normalized=True,weighted_edges=False
231227

232228
# get the predecessor and path length data
233229
if weighted_edges:
234-
(pred,length)=dijkstra_predecessor_and_distance(G,source)
230+
(pred,length)=networkx.dijkstra_predecessor_and_distance(G,source)
235231
else:
236-
(pred,length)=predecessor(G,source,cutoff=cutoff,return_seen=True)
232+
(pred,length)=networkx.predecessor(G,source,cutoff=cutoff,return_seen=True)
237233

238234
# order the nodes by path length
239235
onodes = [ (l,vert) for (vert,l) in length.items() ]
@@ -488,7 +484,7 @@ def _edge_betweenness(G,source,nodes,cutoff=False):
488484
between={}
489485
# get the predecessor data
490486
#(pred,length)=_fast_predecessor(G,source,cutoff=cutoff)
491-
(pred,length)=predecessor(G,source,cutoff=cutoff,return_seen=True)
487+
(pred,length)=networkx.predecessor(G,source,cutoff=cutoff,return_seen=True)
492488
# order the nodes by path length
493489
onodes = [ nn for dd,nn in sorted( (dist,n) for n,dist in length.iteritems() )]
494490
# intialize betweenness, doesn't account for any edge weights
@@ -646,9 +642,9 @@ def closeness_centrality(G,v=None,weighted_edges=False):
646642
647643
"""
648644
if weighted_edges:
649-
path_length=single_source_dijkstra_path_length
645+
path_length=networkx.single_source_dijkstra_path_length
650646
else:
651-
path_length=single_source_shortest_path_length
647+
path_length=networkx.single_source_shortest_path_length
652648

653649
if v is None:
654650
closeness_centrality={}

‎networkx/algorithms/component.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323

2424

2525
import networkx
26-
from networkx.algorithms.shortest_paths.path import \
26+
from networkx.algorithms.shortest_paths.unweighted import \
2727
single_source_shortest_path,\
2828
single_source_shortest_path_length
2929
from networkx.algorithms.traversal.depth_first_search import \
Lines changed: 166 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,166 @@
1+
# -*- coding: utf-8 -*-
2+
"""
3+
Shortest paths.
4+
"""
5+
__author__ = """Aric Hagberg ([email protected])"""
6+
# Copyright (C) 2004-2010 by
7+
# Aric Hagberg <[email protected]>
8+
# Dan Schult <[email protected]>
9+
# Pieter Swart <[email protected]>
10+
# All rights reserved.
11+
# BSD license.
12+
13+
__all__ = ['shortest_path',
14+
'shortest_path_length',
15+
'average_shortest_path_length']
16+
17+
18+
import networkx
19+
#use deque only if networkx requires python 2.4
20+
#from collections import deque
21+
import heapq
22+
23+
24+
def shortest_path(G,source=None,target=None,weighted=False):
25+
"""Compute the shortest path.
26+
27+
There may be more than one shortest path. This returns only one.
28+
29+
Parameters
30+
----------
31+
G : NetworkX graph
32+
33+
source : node, optional
34+
Starting node for path
35+
36+
target : node, optional
37+
Ending node for path
38+
39+
weighted : bool, optional
40+
If True consider weighted edges when finding shortest path.
41+
42+
Returns
43+
-------
44+
45+
Examples
46+
--------
47+
>>> G=nx.path_graph(5)
48+
>>> print nx.shortest_path(G,0,4)
49+
[0, 1, 2, 3, 4]
50+
51+
"""
52+
if source is None:
53+
if target is None:
54+
if weighted:
55+
paths=networkx.all_pairs_dijkstra_path(G)
56+
else:
57+
paths=networkx.all_pairs_shortest_path(G)
58+
else:
59+
raise networkx.NetworkXError("Target given but no source specified.")
60+
else: # source specified
61+
if target is None:
62+
if weighted:
63+
paths=networkx.single_source_dijkstra_path(G,source)
64+
else:
65+
paths=networkx.single_source_shortest_path(G,source)
66+
else:
67+
# shortest source-target path
68+
if weighted:
69+
paths=networkx.dijkstra_shortest_path(G,source,target)
70+
else:
71+
paths=networkx.bidirectional_shortest_path(G,source,target)
72+
73+
return paths
74+
75+
76+
def shortest_path_length(G,source=None,target=None,weighted=False):
77+
"""Compute the shortest path length.
78+
79+
Raise an exception if no path exists.
80+
81+
Parameters
82+
----------
83+
G : NetworkX graph
84+
85+
source : node, optional
86+
Starting node for path
87+
88+
target : node, optional
89+
Ending node for path
90+
91+
weighted : bool, optional
92+
If True consider weighted edges when finding shortest path.
93+
94+
Returns
95+
-------
96+
97+
Raises
98+
------
99+
NetworkXError
100+
If no path exists between source and target.
101+
102+
Examples
103+
--------
104+
>>> G=nx.path_graph(5)
105+
>>> print nx.shortest_path_length(G,0,4)
106+
4
107+
108+
Notes
109+
-----
110+
111+
"""
112+
if source is None:
113+
if target is None:
114+
if weighted:
115+
paths=networkx.all_pairs_dijkstra_path_length(G)
116+
else:
117+
paths=networkx.all_pairs_shortest_path_length(G)
118+
else:
119+
raise networkx.NetworkXError("Target given but no source specified.")
120+
else: # source specified
121+
if target is None:
122+
if weighted:
123+
paths=networkx.single_source_dijkstra_path_length(G,source)
124+
else:
125+
paths=networkx.single_source_shortest_path_length(G,source)
126+
else:
127+
# shortest source-target path
128+
if weighted:
129+
paths=networkx.dijkstra_shortest_path_length(G,source,target)
130+
else:
131+
p=networkx.bidirectional_shortest_path(G,source,target)
132+
if p is False:
133+
raise networkx.NetworkXError("no path from %s to %s"%(source,target))
134+
paths=len(p)-1
135+
return paths
136+
137+
138+
139+
def average_shortest_path_length(G,weighted=False):
140+
""" Return the average shortest path length.
141+
142+
Parameters
143+
----------
144+
G : NetworkX graph
145+
146+
weighted : bool, optional, default=False
147+
If True use edge weights on path.
148+
149+
Examples
150+
--------
151+
>>> G=nx.path_graph(4)
152+
>>> print nx.average_shortest_path_length(G)
153+
1.25
154+
155+
"""
156+
if weighted:
157+
path_length=networkx.single_source_dijkstra_path_length
158+
else:
159+
path_length=networkx.single_source_shortest_path_length
160+
avg=0.0
161+
for n in G:
162+
l=path_length(G,n).values()
163+
avg+=float(sum(l))/len(l)
164+
return avg/len(G)
165+
166+

0 commit comments

Comments
 (0)