def get_certificate(gg): """ Assuming that gg is not a line graph, return a forbidden induced subgraph. """ from sage.graphs.graph_generators import graphs for fg in graphs.line_graph_forbidden_subgraphs(): h = gg.subgraph_search(fg, induced=True) if h is not None: return h
def get_certificate(gg): """ Assuming that gg is not a line graph, return a forbidden induced subgraph. """ from sage.graphs.graph_generators import graphs for fg in graphs.line_graph_forbidden_subgraphs(): h = gg.subgraph_search(fg, induced=True) if h is not None: return h
def is_line_graph(g, certificate=False): r""" Tests wether the graph is a line graph. INPUT: - ``certificate`` (boolean) -- whether to return a certificate along with the boolean result. Here is what happens when ``certificate = True``: - If the graph is not a line graph, the method returns a pair ``(b, subgraph)`` where ``b`` is ``False`` and ``subgraph`` is a subgraph isomorphic to one of the 9 forbidden induced subgraphs of a line graph. - If the graph is a line graph, the method returns a triple ``(b,R,isom)`` where ``b`` is ``True``, ``R`` is a graph whose line graph is the graph given as input, and ``isom`` is a map associating an edge of ``R`` to each vertex of the graph. .. TODO:: This method sequentially tests each of the forbidden subgraphs in order to know whether the graph is a line graph, which is a very slow method. It could eventually be replaced by :func:`~sage.graphs.line_graph.root_graph` when this method will not require an exponential time to run on general graphs anymore (see its documentation for more information on this problem)... and if it can be improved to return negative certificates ! .. NOTE:: This method wastes a bit of time when the input graph is not connected. If you have performance in mind, it is probably better to only feed it with connected graphs only. .. SEEALSO:: - The :mod:`line_graph <sage.graphs.line_graph>` module. - :meth:`~sage.graphs.graph_generators.GraphGenerators.line_graph_forbidden_subgraphs` -- the forbidden subgraphs of a line graph. - :meth:`~sage.graphs.generic_graph.GenericGraph.line_graph` EXAMPLES: A complete graph is always the line graph of a star:: sage: graphs.CompleteGraph(5).is_line_graph() True The Petersen Graph not being claw-free, it is not a line graph:: sage: graphs.PetersenGraph().is_line_graph() False This is indeed the subgraph returned:: sage: C = graphs.PetersenGraph().is_line_graph(certificate = True)[1] sage: C.is_isomorphic(graphs.ClawGraph()) True The house graph is a line graph:: sage: g = graphs.HouseGraph() sage: g.is_line_graph() True But what is the graph whose line graph is the house ?:: sage: is_line, R, isom = g.is_line_graph(certificate = True) sage: R.sparse6_string() ':DaHI~' sage: R.show() sage: isom {0: (0, 1), 1: (0, 2), 2: (1, 3), 3: (2, 3), 4: (3, 4)} TESTS: Disconnected graphs:: sage: g = 2*graphs.CycleGraph(3) sage: gl = g.line_graph().relabel(inplace = False) sage: new_g = gl.is_line_graph(certificate = True)[1] sage: g.line_graph().is_isomorphic(gl) True """ g._scream_if_not_simple() from sage.graphs.graph_generators import graphs for fg in graphs.line_graph_forbidden_subgraphs(): h = g.subgraph_search(fg, induced=True) if h is not None: if certificate: return (False, h) else: return False if not certificate: return True if g.is_connected(): R, isom = root_graph(g) else: from sage.graphs.graph import Graph R = Graph() for gg in g.connected_components_subgraphs(): RR, _ = root_graph(gg) R = R + RR _, isom = g.is_isomorphic(R.line_graph(labels=False), certify=True) return (True, R, isom)
def is_line_graph(g, certificate = False): r""" Tests wether the graph is a line graph. INPUT: - ``certificate`` (boolean) -- whether to return a certificate along with the boolean result. Here is what happens when ``certificate = True``: - If the graph is not a line graph, the method returns a pair ``(b, subgraph)`` where ``b`` is ``False`` and ``subgraph`` is a subgraph isomorphic to one of the 9 forbidden induced subgraphs of a line graph. - If the graph is a line graph, the method returns a triple ``(b,R,isom)`` where ``b`` is ``True``, ``R`` is a graph whose line graph is the graph given as input, and ``isom`` is a map associating an edge of ``R`` to each vertex of the graph. .. TODO:: This method sequentially tests each of the forbidden subgraphs in order to know whether the graph is a line graph, which is a very slow method. It could eventually be replaced by :func:`~sage.graphs.line_graph.root_graph` when this method will not require an exponential time to run on general graphs anymore (see its documentation for more information on this problem)... and if it can be improved to return negative certificates ! .. NOTE:: This method wastes a bit of time when the input graph is not connected. If you have performance in mind, it is probably better to only feed it with connected graphs only. .. SEEALSO:: - The :mod:`line_graph <sage.graphs.line_graph>` module. - :meth:`~sage.graphs.graph_generators.GraphGenerators.line_graph_forbidden_subgraphs` -- the forbidden subgraphs of a line graph. - :meth:`~sage.graphs.generic_graph.GenericGraph.line_graph` EXAMPLES: A complete graph is always the line graph of a star:: sage: graphs.CompleteGraph(5).is_line_graph() True The Petersen Graph not being claw-free, it is not a line graph:: sage: graphs.PetersenGraph().is_line_graph() False This is indeed the subgraph returned:: sage: C = graphs.PetersenGraph().is_line_graph(certificate = True)[1] sage: C.is_isomorphic(graphs.ClawGraph()) True The house graph is a line graph:: sage: g = graphs.HouseGraph() sage: g.is_line_graph() True But what is the graph whose line graph is the house ?:: sage: is_line, R, isom = g.is_line_graph(certificate = True) sage: R.sparse6_string() ':DaHI~' sage: R.show() sage: isom {0: (0, 1), 1: (0, 2), 2: (1, 3), 3: (2, 3), 4: (3, 4)} TESTS: Disconnected graphs:: sage: g = 2*graphs.CycleGraph(3) sage: gl = g.line_graph().relabel(inplace = False) sage: new_g = gl.is_line_graph(certificate = True)[1] sage: g.line_graph().is_isomorphic(gl) True """ g._scream_if_not_simple() from sage.graphs.graph_generators import graphs for fg in graphs.line_graph_forbidden_subgraphs(): h = g.subgraph_search(fg, induced = True) if h is not None: if certificate: return (False,h) else: return False if not certificate: return True if g.is_connected(): R, isom = root_graph(g) else: from sage.graphs.graph import Graph R = Graph() for gg in g.connected_components_subgraphs(): RR, _ = root_graph(gg) R = R + RR _, isom = g.is_isomorphic(R.line_graph(labels = False), certificate = True) return (True, R, isom)