Ticket #12385: trac_12385-all-simple-paths.2.patch

File trac_12385-all-simple-paths.2.patch, 3.0 KB (added by kini, 10 years ago)

apply to $SAGE_ROOT/devel/sage

  • sage/graphs/digraph.py

    # HG changeset patch
    # Parent 7b7d196de69ba30a5bcccc8e3a6e462abae9afe7
    # User Keshav Kini <keshav.kini@gmail.com>
    # Date 1327986206 -28800
    
    more premature optimization
    
    diff --git a/sage/graphs/digraph.py b/sage/graphs/digraph.py
    a b  
    17841784            return
    17851785
    17861786        # Start with the empty path; we will try all extensions of it
    1787         queue = [[vertex]]
    1788         while queue:
    1789             path = queue.pop(0)     # get a path
    1790 
    1791             if path[-1] in ending_vertices and (trivial or len(path) > 1):
     1787        queue = []
     1788        path = [vertex]
     1789
     1790        if trivial and vertex in ending_vertices:
     1791            yield path
     1792        while True:
     1793            # Build next generation of paths, one arc longer; max_length refers
     1794            # to edges and not vertices, hence <= and not <
     1795            if len(path) <= max_length:
     1796
     1797                # We try all possible extensions
     1798                if simple:
     1799                    # We only keep simple extensions. One exception: when we
     1800                    # have found a simple cycle. In this case, we yield it
     1801                    # immediately, but don't consider it for further extension.
     1802                    # See trac #12385.
     1803                    for neighbor in self.neighbor_out_iterator(path[-1]):
     1804                        if neighbor not in path:
     1805                            queue.append(path + [neighbor])
     1806                        elif ( neighbor == path[0] and
     1807                               neighbor in ending_vertices ):
     1808                            yield path + [neighbor]
     1809
     1810                else:
     1811                    # Non-simple paths requested: we add all of them
     1812                    for neighbor in self.neighbor_out_iterator(path[-1]):
     1813                        queue.append(path + [neighbor])
     1814
     1815            if not queue:
     1816                break
     1817            path = queue.pop(0)     # get the next path
     1818
     1819            if path[-1] in ending_vertices:
    17921820                yield path      # yield good path
    17931821
    1794             # Build next generation of paths, one arc longer
    1795             if len(path) <= max_length:
    1796 
    1797                 # We try all possible extensions
    1798                 for neighbor in self.neighbor_out_iterator(path[-1]):
    1799                     if simple:
    1800                         # We only keep simple extensions. One exception : when
    1801                         # we have found a simple cycle. In this case, we return
    1802                         # it immediately.
    1803                         if neighbor in path:
    1804                             if neighbor == path[0] and neighbor in ending_vertices:
    1805                                 yield path + [neighbor]
    1806                         else:
    1807                             queue.append(path + [neighbor])
    1808 
    1809                     else:
    1810                         # Non-simple paths : we add all of them
    1811                         queue.append(path + [neighbor])
    18121822
    18131823    def all_paths_iterator(self, starting_vertices=None, ending_vertices=None,
    18141824                           simple=False, max_length=None, trivial=False):