Ticket #12385: trac_12385_review.2.patch

File trac_12385_review.2.patch, 2.2 KB (added by kini, 10 years ago)

apply to $SAGE_ROOT/devel/sage

  • sage/graphs/digraph.py

    # HG changeset patch
    # User Nathann Cohen <nathann.cohen@gmail.com>
    # Date 1327925993 -3600
    # Node ID 331be5dbd2c9920bb23db71d78a038727cf3ed89
    # Parent 6acb76c94e97aab4350d98d621048d29145083e2
    reviewer patch
    
    diff --git a/sage/graphs/digraph.py b/sage/graphs/digraph.py
    a b  
    17871787        queue = [[vertex]]
    17881788        while queue:
    17891789            path = queue.pop(0)     # get a path
    1790             cycle = False
    1791             if simple and path.count(path[-1]) > 1:
    1792                 # We found a path which has formed a cycle, and simple=True, so
    1793                 # we don't want to extend this path, and we don't want to yield
    1794                 # it unless it IS a cycle (beginning = end). See trac #12385.
    1795                 if path[0] == path[-1]:
    1796                     cycle = True
    1797                 else:
    1798                     continue
     1790
    17991791            if path[-1] in ending_vertices and (trivial or len(path) > 1):
    18001792                yield path      # yield good path
    18011793
    1802             if cycle:
    1803                 # We only yielded this path because it was a cycle; it should
    1804                 # not be extended.
    1805                 continue
    1806 
    18071794            # Build next generation of paths, one arc longer
    18081795            if len(path) <= max_length:
     1796
     1797                # We try all possible extensions
    18091798                for neighbor in self.neighbor_out_iterator(path[-1]):
    1810                     queue.append(path + [neighbor])
     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])
    18111812
    18121813    def all_paths_iterator(self, starting_vertices=None, ending_vertices=None,
    18131814                           simple=False, max_length=None, trivial=False):