Ticket #9567: trac_9567-cycle_basis.patch

File trac_9567-cycle_basis.patch, 2.8 KB (added by ncohen, 10 years ago)
  • sage/graphs/generic_graph.py

    # HG changeset patch
    # User Nathann Cohen <nathann.cohen@gmail.com>
    # Date 1281583915 -28800
    # Node ID 3bab2e5f2a633d4a8dafc234f1f9ac1efcc97f44
    # Parent  cbafe93273bb7d6009768655eab131653b6a8449
    trac 9567 -- Adds the method Graph.cycle_basis
    
    diff -r cbafe93273bb -r 3bab2e5f2a63 sage/graphs/generic_graph.py
    a b  
    21222122            M[index,index]+=1
    21232123            return abs(M.determinant())   
    21242124
     2125    def cycle_basis(self):
     2126        r"""
     2127        Returns a list of cycles which form a basis of the cycle space
     2128        of ``self``.
     2129
     2130        A basis of cycles of a graph is a minimal collection of cycles
     2131        (considered as sets of edges) such that the edge set of any
     2132        cycle in the graph can be written as a `Z/2Z` sum of the
     2133        cycles in the basis.
     2134
     2135        OUTPUT:
     2136
     2137        A list of lists, each of them representing the vertices of a
     2138        cycle in a basis.
     2139
     2140        ALGORITHM:
     2141
     2142        Uses the NetworkX library.
     2143
     2144        EXAMPLE:
     2145
     2146        A cycle basis in Petersen's Graph ::
     2147
     2148            sage: g = graphs.PetersenGraph()
     2149            sage: g.cycle_basis()
     2150            [[1, 2, 7, 5, 0], [8, 3, 2, 7, 5], [4, 3, 2, 7, 5, 0], [4, 9, 7, 5, 0], [8, 6, 9, 7, 5], [1, 6, 9, 7, 5, 0]]
     2151
     2152        Checking the given cycles are algebraically free::
     2153
     2154            sage: g = graphs.RandomGNP(30,.4)
     2155            sage: basis = g.cycle_basis()
     2156
     2157        Building the space of (directed) edges over `Z/2Z`. On the way,
     2158        building a dictionary associating an unique vector to each
     2159        undirected edge::
     2160           
     2161            sage: m = g.size()
     2162            sage: edge_space = VectorSpace(FiniteField(2),m)
     2163            sage: edge_vector = dict( zip( g.edges(labels = False), edge_space.basis() ) )
     2164            sage: for (u,v),vec in edge_vector.items():
     2165            ...      edge_vector[(v,u)] = vec
     2166
     2167        Defining a lambda function associating a vector to the
     2168        vertices of a cycle::
     2169
     2170            sage: vertices_to_edges = lambda x : zip( x, x[1:] + [x[0]] )
     2171            sage: cycle_to_vector = lambda x : sum( edge_vector[e] for e in vertices_to_edges(x) )
     2172
     2173        Finally checking the cycles are a free set::
     2174
     2175            sage: basis_as_vectors = map( cycle_to_vector, basis )
     2176            sage: edge_space.span(basis_as_vectors).rank() == len(basis)
     2177            True
     2178        """
     2179
     2180        import networkx
     2181        return networkx.cycle_basis(self.networkx_graph(copy=False))
     2182
    21252183    def minimum_outdegree_orientation(self, use_edge_labels=False, solver=None, verbose=0):
    21262184        r"""
    21272185        Returns a DiGraph which is an orientation with the smallest
     
    22302288
    22312289        O.delete_edges(edges)
    22322290
    2233         return O       
     2291        return O
    22342292
    22352293    ### Planarity
    22362294