Skip to content
This repository has been archived by the owner on Jan 30, 2023. It is now read-only.

Commit

Permalink
trac #26823: reviewer's comments
Browse files Browse the repository at this point in the history
  • Loading branch information
dcoudert committed Dec 7, 2018
1 parent 9dd97bd commit 6c67fc0
Showing 1 changed file with 34 additions and 26 deletions.
60 changes: 34 additions & 26 deletions src/sage/graphs/generic_graph.py
Expand Up @@ -14042,7 +14042,7 @@ def eccentricity(self, v=None, by_weight=False, algorithm=None,
sage: G.eccentricity(by_weight = True, algorithm = 'Floyd-Warshall-Cython')
Traceback (most recent call last):
...
ValueError: Algorithm 'Floyd-Warshall-Cython' does not work with weights.
ValueError: algorithm 'Floyd-Warshall-Cython' does not work with weights

An algorithm that computes the all-pair-shortest-paths when not all
vertices are needed::
Expand Down Expand Up @@ -15275,7 +15275,7 @@ def all_paths(self, start, end):
act_path = [] # the current path
act_path_iter = [] # the neighbor/successor-iterators of the current path
done = False
s=start
s = start
while not done:
if s == end: # if path completes, add to list
all_paths.append(act_path + [s])
Expand All @@ -15286,12 +15286,12 @@ def all_paths(self, start, end):
s = None
while (s is None) and not done:
try:
s = next(act_path_iter[-1]) # try to get the next neighbor/successor, ...
except (StopIteration): # ... if there is none ...
act_path.pop() # ... go one step back
s = next(act_path_iter[-1]) # try to get the next neighbor/successor, ...
except (StopIteration): # ... if there is none ...
act_path.pop() # ... go one step back
act_path_iter.pop()
if not act_path: # there is no other vertex ...
done = True # ... so we are done
if not act_path: # there is no other vertex ...
done = True # ... so we are done
return all_paths


Expand Down Expand Up @@ -15548,20 +15548,21 @@ def shortest_path(self, u, v, by_weight=False, algorithm=None,
return self.shortest_paths(u, by_weight, algorithm, weight_function, check_weight)[v]

if weight_function is None and by_weight:
weight_function = lambda e: e[2]
def weight_function(e):
return e[2]

if u == v: # to avoid a NetworkX bug
if u == v: # to avoid a NetworkX bug
return [u]


if by_weight:
if algorithm == 'BFS_Bid':
raise ValueError("the 'BFS_Bid' algorithm does not "
"work on weighted graphs")
if check_weight:
self._check_weight_function(weight_function)
else:
weight_function = lambda e: 1
def weight_function(e):
return 1

if algorithm == "Dijkstra_Bid":
return self._backend.bidirectional_dijkstra(u, v, weight_function)
Expand Down Expand Up @@ -15704,14 +15705,12 @@ def shortest_path_length(self, u, v, by_weight=False, algorithm=None,
...
ValueError: vertex '6' is not in the (di)graph
"""
if not self.has_vertex(u):
raise ValueError("vertex '{}' is not in the (di)graph".format(u))
if not self.has_vertex(u):
raise ValueError("vertex '{}' is not in the (di)graph".format(u))
if not self.has_vertex(v):
raise ValueError("vertex '{}' is not in the (di)graph".format(v))

if u == v: # to avoid a NetworkX bug
if u == v: # to avoid a NetworkX bug
return 0

if weight_function is not None:
Expand All @@ -15721,7 +15720,8 @@ def shortest_path_length(self, u, v, by_weight=False, algorithm=None,
algorithm = 'Dijkstra_Bid' if by_weight else 'BFS_Bid'

if weight_function is None and by_weight:
weight_function = lambda e: e[2]
def weight_function(e):
return e[2]

if algorithm in ['BFS', 'Dijkstra_NetworkX', 'Bellman-Ford_Boost']:
return self.shortest_path_lengths(u, by_weight, algorithm, weight_function, check_weight)[v]
Expand All @@ -15733,7 +15733,8 @@ def shortest_path_length(self, u, v, by_weight=False, algorithm=None,
if check_weight:
self._check_weight_function(weight_function)
else:
weight_function = lambda e: 1
def weight_function(e):
return 1

if algorithm == "Dijkstra_Bid":
return self._backend.bidirectional_dijkstra(u, v, weight_function, distance_flag=True)
Expand Down Expand Up @@ -15944,9 +15945,11 @@ def shortest_paths(self, u, by_weight=False, algorithm=None,
if weight_function is not None:
by_weight = True
elif by_weight:
weight_function = lambda e: e[2]
def weight_function(e):
return e[2]
else:
weight_function = lambda e: 1
def weight_function(e):
return 1

if algorithm is None and not by_weight:
algorithm = 'BFS'
Expand Down Expand Up @@ -15981,7 +15984,7 @@ def shortest_paths(self, u, by_weight=False, algorithm=None,

elif algorithm in ['Dijkstra_Boost', 'Bellman-Ford_Boost', None]:
from sage.graphs.base.boost_graph import shortest_paths
_,pred = shortest_paths(self, u, weight_function, algorithm)
_, pred = shortest_paths(self, u, weight_function, algorithm)
paths = {}
for v in pred.keys():
w = v
Expand Down Expand Up @@ -16164,9 +16167,11 @@ def shortest_path_lengths(self, u, by_weight=False, algorithm=None,
if weight_function is not None:
by_weight = True
elif by_weight:
weight_function = lambda e: e[2]
def weight_function(e):
return e[2]
else:
weight_function = lambda e: 1
def weight_function(e):
return 1

if algorithm is None and not by_weight:
algorithm = 'BFS'
Expand Down Expand Up @@ -16378,7 +16383,7 @@ def shortest_path_all_pairs(self, by_weight=False, algorithm=None,
sage: g.shortest_path_all_pairs(by_weight=True)
Traceback (most recent call last):
...
ValueError: The graph contains a negative cycle.
ValueError: the graph contains a negative cycle

Unreachable vertices are not present in the dictionaries::

Expand Down Expand Up @@ -16447,7 +16452,8 @@ def shortest_path_all_pairs(self, by_weight=False, algorithm=None,
if weight_function is not None:
by_weight = True
elif by_weight:
weight_function = lambda e: e[2]
def weight_function(e):
return e[2]

if algorithm is None:
if by_weight:
Expand Down Expand Up @@ -16487,9 +16493,10 @@ def shortest_path_all_pairs(self, by_weight=False, algorithm=None,
dist = dict()
pred = dict()
if by_weight and weight_function is None:
weight_function = lambda e:e[2]
def weight_function(e):
return e[2]
for u in self:
dist[u],pred[u] = shortest_paths(self, u, weight_function, algorithm)
dist[u], pred[u] = shortest_paths(self, u, weight_function, algorithm)
return dist, pred

elif algorithm == "Dijkstra_NetworkX":
Expand All @@ -16513,7 +16520,8 @@ def shortest_path_all_pairs(self, by_weight=False, algorithm=None,

if by_weight:
if weight_function is None:
weight_function = lambda e: e[2]
def weight_function(e):
return e[2]
if check_weight:
self._check_weight_function(weight_function)

Expand Down

0 comments on commit 6c67fc0

Please sign in to comment.