| 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 | |