Ticket #13961: trac_13961-update_is_line_graph.patch

File trac_13961-update_is_line_graph.patch, 6.0 KB (added by ncohen, 8 years ago)
  • sage/graphs/line_graph.py

    # 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  
    11r"""
    22Line graphs
    33
    4 This modules gather everything which is related to line graphs. Right now, this
     4This module gather everything which is related to line graphs. Right now, this
    55amounts to the following functions :
    66
    77.. csv-table::
     
    116116    Even though the root graph is *NOT UNIQUE* for the triangle, this method
    117117    returns `K_{1,3}` (and not `K_3`) in this case. Pay *very close* attention
    118118    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
    120120    possible answers.
    121121
    122122.. [Whitney32] Congruent graphs and the connectivity of graphs,
     
    145145
    146146    INPUT:
    147147
    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.
    153159
    154160    .. TODO::
    155161
    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`
    160183
    161184    EXAMPLES:
    162185
     
    173196
    174197    This is indeed the subgraph returned::
    175198
    176         sage: C = graphs.PetersenGraph().is_line_graph(certificate = True)
     199        sage: C = graphs.PetersenGraph().is_line_graph(certificate = True)[1]
    177200        sage: C.is_isomorphic(graphs.ClawGraph())
    178201        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
    179227    """
    180228    from sage.graphs.graph_generators import graphs
    181229
     
    183231        h = g.subgraph_search(fg, induced = True)
    184232        if h is not None:
    185233            if certificate:
    186                 return h
     234                return (False,h)
    187235            else:
    188236                return False
    189237
    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)
    191254
    192255def line_graph(self, labels=True):
    193256    """
     
    219282        code will fail if edge labels are not hashable. You can also set the
    220283        argument ``labels=False`` to ignore labels.
    221284
     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
    222294    EXAMPLES::
    223295
    224296        sage: g = graphs.CompleteGraph(4)
     
    357429          cliques, and that can take a while for general graphs. As soon as
    358430          there is a way to iterate over maximal cliques without first building
    359431          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 !
    361433
    362434    TESTS:
    363435