Ticket #14618: trac_14618-rev.patch

File trac_14618-rev.patch, 5.6 KB (added by ncohen, 9 years ago)
  • sage/graphs/graph_generators.py

    # HG changeset patch
    # User Nathann Cohen <nathann.cohen@gmail.com>
    # Date 1369656720 -7200
    # Node ID fa5aae0b1bc13165dfbe3b4a145e6377e62b242a
    # Parent  0206caa9027833bd0ea5782a1c6237aa8933ea60
    Add generators for fullerenes -- reviewer's patch
    
    diff --git a/sage/graphs/graph_generators.py b/sage/graphs/graph_generators.py
    a b  
    928928        with an embedding set. These will be simple graphs: no loops, no
    929929        multiple edges, no directed edges.
    930930
     931        .. SEEALSO::
     932
     933            - :meth:`~sage.graphs.generic_graph.GenericGraph.set_embedding`,
     934              :meth:`~sage.graphs.generic_graph.GenericGraph.get_embedding` --
     935              get/set methods for embeddings.
     936
    931937        EXAMPLES:
    932938
    933         There are 1812 isomers of `\textrm{C}_60`, i.e., 1812 fullerene graphs
     939        There are 1812 isomers of `\textrm{C}_{60}`, i.e., 1812 fullerene graphs
    934940        on 60 vertices:  ::
    935941
    936942            sage: gen = graphs.fullerenes(60)  # optional buckygen
     
    946952            sage: gen.next()  # optional buckygen
    947953            Traceback (most recent call last):
    948954            ...
    949             StopIteration: Exhausted list of graphs from buckygen
     955            StopIteration
    950956
    951957        The unique fullerene graph on 20 vertices is isomorphic to the dodecahedron
    952958        graph. ::
    953959
    954960            sage: gen = graphs.fullerenes(20)  # optional buckygen
    955961            sage: g = gen.next()  # optional buckygen
     962            sage: g.is_isomorphic(graphs.DodecahedralGraph()) # optional buckygen
     963            True
    956964            sage: g.get_embedding()  # optional buckygen
    957965            {1: [2, 3, 4],
    958966             2: [1, 5, 6],
     
    979987        REFERENCE:
    980988
    981989        .. [buckygen] G. Brinkmann, J. Goedgebeur and B.D. McKay, Generation of Fullerenes,
    982           Journal of Chemical Information and Modeling, 52(11):2910-2918, 2012. 
     990          Journal of Chemical Information and Modeling, 52(11):2910-2918, 2012.
    983991        """
    984992        from sage.misc.package import is_package_installed
    985993        if not is_package_installed("buckygen"):
    986             raise TypeError, "the optional buckygen package is not installed"
     994            raise TypeError("the optional buckygen package is not installed")
    987995
    988         #number of vertices should be positive
     996        # number of vertices should be positive
    989997        if order < 0:
    990             raise ValueError("Number of vertices should be positive 20.")
    991         #buckygen can only handle fullerenes on up to 300 vertices
     998            raise ValueError("Number of vertices should be positive.")
     999
     1000        # buckygen can only handle fullerenes on up to 300 vertices
    9921001        if order > 300:
    9931002            raise ValueError("Number of vertices should be at most 300.")
    994         #fullerenes only exist for an even number of vertices
    995         if order % 2 == 1:
    996             raise ValueError("Number of vertices should be even.")
    9971003
    998         #fullerenes only exist for numbers larger than 20 and different from 22
    999         if order < 20 or order == 22:
    1000             raise StopIteration("Exhausted list of graphs from buckygen")
     1004        # fullerenes only exist for an even number of vertices, larger than 20
     1005        # and different from 22
     1006        if order % 2 == 1 or order < 20 or order == 22:
     1007            return
    10011008
    1002         if ipr:
    1003             command = 'buckygen -Id {0}d'.format(order)
    1004         else:
    1005             command = 'buckygen -d {0}d'.format(order)
     1009        command = 'buckygen -'+('I' if ipr else '')+'d {0}d'.format(order)
    10061010
    10071011        import subprocess
    10081012        sp = subprocess.Popen(command, shell=True,
     
    10121016
    10131017        #start of code to read planar code
    10141018
    1015         #read header
    1016         header = out.read(13)
    1017 
    1018         assert header == '>>planar_code', 'Not a valid planar code header'
    1019 
    1020         c = out.read(1)
    1021 
    1022         while c != '<':
    1023             c = out.read(1)
    1024 
    1025         #one more character to read full header
    1026         c = out.read(1)
     1019        header = out.read(15)
     1020        assert header == '>>planar_code<<', 'Not a valid planar code header'
    10271021
    10281022        #read graph per graph
    1029         c = out.read(1)
    10301023        while True:
     1024            c = out.read(1)
    10311025            if len(c)==0:
    1032                 raise StopIteration("Exhausted list of graphs from buckygen")
     1026                return
     1027
     1028            # Each graph is stored in the following way :
     1029            #
     1030            # The first character is the number of vertices, followed by
     1031            # n11,...,n1k,null character,n21,...,n2k',null character, ...
     1032            #
     1033            # where the n1* are all neighbors of n1 and all n2* are the
     1034            # neighbors of n2, ...
     1035            #
     1036            # Besides, these neighbors are enumerated in clockwise order.
    10331037            order = ord(c)
    10341038
    10351039            zeroCount = 0
    10361040
    1037             l = []
    1038             g = {}
     1041            g = [[] for i in range(order)]
    10391042
    10401043            while zeroCount < order:
    10411044                c = out.read(1)
    10421045                if ord(c)==0:
    10431046                    zeroCount += 1
    1044                     g[zeroCount] = l
    1045                     l = []
    10461047                else:
    1047                     l.append(ord(c))
     1048                    g[zeroCount].append(ord(c))
    10481049
    10491050            #construct graph based on g
     1051            g = {i+1:di for i,di in enumerate(g)}
    10501052            G = graph.Graph(g)
    10511053            G.set_embedding(g)
    10521054            yield(G)
    10531055
    1054 
    1055             #prepare for next graph
    1056             g = {}
    1057 
    1058             c = out.read(1)
    1059 
    10601056###########################################################################
    10611057# Chessboard graphs
    10621058###########################################################################