# HG changeset patch
# User Nathann Cohen <nathann.cohen@gmail.com>
# Date 1358333174 -3600
# Node ID 9158e2efcb18fcce76891c472c95a4a259fd0a52
# Parent cfb66b8e35c261fc81c110acd0b9794da8071637
Compute the root graph of a graph -- update is_line_graph
diff --git a/sage/graphs/line_graph.py b/sage/graphs/line_graph.py
a
|
b
|
|
1 | 1 | r""" |
2 | 2 | Line graphs |
3 | 3 | |
4 | | This modules gather everything which is related to line graphs. Right now, this |
| 4 | This module gather everything which is related to line graphs. Right now, this |
5 | 5 | amounts to the following functions : |
6 | 6 | |
7 | 7 | .. csv-table:: |
… |
… |
|
116 | 116 | Even though the root graph is *NOT UNIQUE* for the triangle, this method |
117 | 117 | returns `K_{1,3}` (and not `K_3`) in this case. Pay *very close* attention |
118 | 118 | to that, for this answer is not theoretically correct : there is no unique |
119 | | answer in this case, and we deal with this case by returning one of the two |
| 119 | answer in this case, and we deal with it by returning one of the two |
120 | 120 | possible answers. |
121 | 121 | |
122 | 122 | .. [Whitney32] Congruent graphs and the connectivity of graphs, |
… |
… |
|
145 | 145 | |
146 | 146 | INPUT: |
147 | 147 | |
148 | | - ``certificate`` (boolean) -- whether to return a certificate when the |
149 | | graph is *not* a line graph. When ``certificate`` is set to ``True``, and |
150 | | if the graph is not a line graph, the method returns a subgraph isomorphic |
151 | | to one of the 9 forbidden induced subgraphs of a line graph (instead of |
152 | | the usual ``False``) |
| 148 | - ``certificate`` (boolean) -- whether to return a certificate along with |
| 149 | the boolean result. Here is what happens when ``certificate = True``: |
| 150 | |
| 151 | - If the graph is not a line graph, the method returns a pair ``(b, |
| 152 | subgraph)`` where ``b`` is ``False`` and ``subgraph`` is a subgraph |
| 153 | isomorphic to one of the 9 forbidden induced subgraphs of a line graph. |
| 154 | |
| 155 | - If the graph is a line graph, the method returns a triple ``(b,R,isom)`` |
| 156 | where ``b`` is ``True``, ``R`` is a graph whose line graph is the graph |
| 157 | given as input, and ``isom`` is a map associating an edge of ``R`` to |
| 158 | each vertex of the graph. |
153 | 159 | |
154 | 160 | .. TODO:: |
155 | 161 | |
156 | | This methods sequentially tests each of the forbidden subgraphs, which |
157 | | is a very slow method. There exist much better algorithms, including |
158 | | those which are actually able to return a graph whose line graph is |
159 | | isomorphic to the given graph. |
| 162 | This method sequentially tests each of the forbidden subgraphs in order |
| 163 | to know whether the graph is a line graph, which is a very slow |
| 164 | method. It could eventually be replaced by :meth:`root_graph` when this |
| 165 | method will not require an exponential time to run on general graphs |
| 166 | anymore (see its documentation for more information on this problem)... |
| 167 | and if it can be improved to return negative certificates ! |
| 168 | |
| 169 | .. NOTE:: |
| 170 | |
| 171 | This method wastes a bit of time when the input graph is not |
| 172 | connected. If you have performance in mind, it is probably better to |
| 173 | only feed it with connected graphs only. |
| 174 | |
| 175 | .. SEEALSO:: |
| 176 | |
| 177 | - The :mod:`line_graph <sage.graphs.line_graph>` module. |
| 178 | |
| 179 | - :meth:`~sage.graphs.graph_generators.GraphGenerators.line_graph_forbidden_subgraphs` |
| 180 | -- the forbidden subgraphs of a line graph. |
| 181 | |
| 182 | - :meth:`~sage.graphs.generic_graph.GenericGraph.line_graph` |
160 | 183 | |
161 | 184 | EXAMPLES: |
162 | 185 | |
… |
… |
|
173 | 196 | |
174 | 197 | This is indeed the subgraph returned:: |
175 | 198 | |
176 | | sage: C = graphs.PetersenGraph().is_line_graph(certificate = True) |
| 199 | sage: C = graphs.PetersenGraph().is_line_graph(certificate = True)[1] |
177 | 200 | sage: C.is_isomorphic(graphs.ClawGraph()) |
178 | 201 | True |
| 202 | |
| 203 | The house graph is a line graph:: |
| 204 | |
| 205 | sage: g = graphs.HouseGraph() |
| 206 | sage: g.is_line_graph() |
| 207 | True |
| 208 | |
| 209 | But what is the graph whose line graph is the house ?:: |
| 210 | |
| 211 | sage: is_line, R, isom = g.is_line_graph(certificate = True) |
| 212 | sage: R.sparse6_string() |
| 213 | ':DaHI~' |
| 214 | sage: R.show() |
| 215 | sage: isom |
| 216 | {0: (0, 1), 1: (0, 2), 2: (1, 3), 3: (2, 3), 4: (3, 4)} |
| 217 | |
| 218 | TESTS: |
| 219 | |
| 220 | Disconnected graphs:: |
| 221 | |
| 222 | sage: g = 2*graphs.CycleGraph(3) |
| 223 | sage: gl = g.line_graph().relabel(inplace = False) |
| 224 | sage: new_g = gl.is_line_graph(certificate = True)[1] |
| 225 | sage: g.line_graph().is_isomorphic(gl) |
| 226 | True |
179 | 227 | """ |
180 | 228 | from sage.graphs.graph_generators import graphs |
181 | 229 | |
… |
… |
|
183 | 231 | h = g.subgraph_search(fg, induced = True) |
184 | 232 | if h is not None: |
185 | 233 | if certificate: |
186 | | return h |
| 234 | return (False,h) |
187 | 235 | else: |
188 | 236 | return False |
189 | 237 | |
190 | | return True |
| 238 | if not certificate: |
| 239 | return True |
| 240 | |
| 241 | if g.is_connected(): |
| 242 | R, isom = root_graph(g) |
| 243 | else: |
| 244 | from sage.graphs.graph import Graph |
| 245 | R = Graph() |
| 246 | |
| 247 | for gg in g.connected_components_subgraphs(): |
| 248 | RR, _ = root_graph(gg) |
| 249 | R = R + RR |
| 250 | |
| 251 | _, isom = g.is_isomorphic(R.line_graph(labels = False), certify = True) |
| 252 | |
| 253 | return (True, R, isom) |
191 | 254 | |
192 | 255 | def line_graph(self, labels=True): |
193 | 256 | """ |
… |
… |
|
219 | 282 | code will fail if edge labels are not hashable. You can also set the |
220 | 283 | argument ``labels=False`` to ignore labels. |
221 | 284 | |
| 285 | .. SEEALSO:: |
| 286 | |
| 287 | - The :mod:`line_graph <sage.graphs.line_graph>` module. |
| 288 | |
| 289 | - :meth:`~sage.graphs.graph_generators.GraphGenerators.line_graph_forbidden_subgraphs` |
| 290 | -- the forbidden subgraphs of a line graph. |
| 291 | |
| 292 | - :meth:`~Graph.is_line_graph` -- tests whether a graph is a line graph. |
| 293 | |
222 | 294 | EXAMPLES:: |
223 | 295 | |
224 | 296 | sage: g = graphs.CompleteGraph(4) |
… |
… |
|
357 | 429 | cliques, and that can take a while for general graphs. As soon as |
358 | 430 | there is a way to iterate over maximal cliques without first building |
359 | 431 | the (long) list of them this implementation can be updated, and will |
360 | | deal reasonably with non-line grph too ! |
| 432 | deal reasonably with non-line graphs too ! |
361 | 433 | |
362 | 434 | TESTS: |
363 | 435 | |