# HG changeset patch
# Parent 7b7d196de69ba30a5bcccc8e3a6e462abae9afe7
# User Keshav Kini
# Date 1327986206 28800
more premature optimization
diff git a/sage/graphs/digraph.py b/sage/graphs/digraph.py
 a/sage/graphs/digraph.py
+++ b/sage/graphs/digraph.py
@@ 1784,31 +1784,41 @@
return
# Start with the empty path; we will try all extensions of it
 queue = [[vertex]]
 while queue:
 path = queue.pop(0) # get a path

 if path[1] in ending_vertices and (trivial or len(path) > 1):
+ queue = []
+ path = [vertex]
+
+ if trivial and vertex in ending_vertices:
+ yield path
+ while True:
+ # Build next generation of paths, one arc longer; max_length refers
+ # to edges and not vertices, hence <= and not <
+ if len(path) <= max_length:
+
+ # We try all possible extensions
+ if simple:
+ # We only keep simple extensions. One exception: when we
+ # have found a simple cycle. In this case, we yield it
+ # immediately, but don't consider it for further extension.
+ # See trac #12385.
+ for neighbor in self.neighbor_out_iterator(path[1]):
+ if neighbor not in path:
+ queue.append(path + [neighbor])
+ elif ( neighbor == path[0] and
+ neighbor in ending_vertices ):
+ yield path + [neighbor]
+
+ else:
+ # Nonsimple paths requested: we add all of them
+ for neighbor in self.neighbor_out_iterator(path[1]):
+ queue.append(path + [neighbor])
+
+ if not queue:
+ break
+ path = queue.pop(0) # get the next path
+
+ if path[1] in ending_vertices:
yield path # yield good path
 # Build next generation of paths, one arc longer
 if len(path) <= max_length:

 # We try all possible extensions
 for neighbor in self.neighbor_out_iterator(path[1]):
 if simple:
 # We only keep simple extensions. One exception : when
 # we have found a simple cycle. In this case, we return
 # it immediately.
 if neighbor in path:
 if neighbor == path[0] and neighbor in ending_vertices:
 yield path + [neighbor]
 else:
 queue.append(path + [neighbor])

 else:
 # Nonsimple paths : we add all of them
 queue.append(path + [neighbor])
def all_paths_iterator(self, starting_vertices=None, ending_vertices=None,
simple=False, max_length=None, trivial=False):