本文整理汇总了Python中sage.graphs.graph.Graph.line_graph方法的典型用法代码示例。如果您正苦于以下问题:Python Graph.line_graph方法的具体用法?Python Graph.line_graph怎么用?Python Graph.line_graph使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类sage.graphs.graph.Graph
的用法示例。
在下文中一共展示了Graph.line_graph方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: is_line_graph
# 需要导入模块: from sage.graphs.graph import Graph [as 别名]
# 或者: from sage.graphs.graph.Graph import line_graph [as 别名]
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
#.........这里部分代码省略.........
示例2: root_graph
# 需要导入模块: from sage.graphs.graph import Graph [as 别名]
# 或者: from sage.graphs.graph.Graph import line_graph [as 别名]
def root_graph(g, verbose = False):
r"""
Computes the root graph corresponding to the given graph
See the documentation of :mod:`sage.graphs.line_graph` to know how it works.
INPUT:
- ``g`` -- a graph
- ``verbose`` (boolean) -- display some information about what is happening
inside of the algorithm.
.. NOTE::
It is best to use this code through
:meth:`~sage.graphs.graph.Graph.is_line_graph`, which first checks that
the graph is indeed a line graph, and deals with the disconnected
case. But if you are sure of yourself, dig in !
.. WARNING::
* This code assumes that the graph is connected.
* If the graph is *not* a line graph, this implementation will take a
loooooong time to run. Its first step is to enumerate all maximal
cliques, and that can take a while for general graphs. As soon as
there is a way to iterate over maximal cliques without first building
the (long) list of them this implementation can be updated, and will
deal reasonably with non-line graphs too !
TESTS:
All connected graphs on 6 vertices::
sage: from sage.graphs.line_graph import root_graph
sage: def test(g):
....: gl = g.line_graph(labels = False)
....: d=root_graph(gl)
sage: for i,g in enumerate(graphs(6)): # long time
....: if not g.is_connected(): # long time
....: continue # long time
....: test(g) # long time
Non line-graphs::
sage: root_graph(graphs.PetersenGraph())
Traceback (most recent call last):
...
ValueError: This graph is not a line graph !
Small corner-cases::
sage: from sage.graphs.line_graph import root_graph
sage: root_graph(graphs.CompleteGraph(3))
(Complete bipartite graph: Graph on 4 vertices, {0: (0, 1), 1: (0, 2), 2: (0, 3)})
sage: root_graph(graphs.OctahedralGraph())
(Complete graph: Graph on 4 vertices, {0: (0, 1), 1: (0, 2), 2: (0, 3), 3: (1, 2), 4: (1, 3), 5: (2, 3)})
sage: root_graph(graphs.DiamondGraph())
(Graph on 4 vertices, {0: (0, 3), 1: (0, 1), 2: (0, 2), 3: (1, 2)})
sage: root_graph(graphs.WheelGraph(5))
(Diamond Graph: Graph on 4 vertices, {0: (1, 2), 1: (0, 1), 2: (0, 2), 3: (2, 3), 4: (1, 3)})
"""
from sage.graphs.digraph import DiGraph
if isinstance(g, DiGraph):
raise ValueError("g cannot be a DiGraph !")
if g.has_multiple_edges():
raise ValueError("g cannot have multiple edges !")
if not g.is_connected():
raise ValueError("g is not connected !")
# Complete Graph ?
if g.is_clique():
from sage.graphs.generators.basic import CompleteBipartiteGraph
return (CompleteBipartiteGraph(1,g.order()),
{v : (0,1+i) for i,v in enumerate(g)})
# Diamond Graph ?
elif g.order() == 4 and g.size() == 5:
from sage.graphs.graph import Graph
root = Graph([(0,1),(1,2),(2,0),(0,3)])
return (root,
g.is_isomorphic(root.line_graph(labels = False), certificate = True)[1])
# Wheel on 5 vertices ?
elif g.order() == 5 and g.size() == 8 and min(g.degree()) == 3:
from sage.graphs.generators.basic import DiamondGraph
root = DiamondGraph()
return (root,
g.is_isomorphic(root.line_graph(labels = False), certificate = True)[1])
# Octahedron ?
elif g.order() == 6 and g.size() == 12 and g.is_regular(k=4):
from sage.graphs.generators.platonic_solids import OctahedralGraph
if g.is_isomorphic(OctahedralGraph()):
from sage.graphs.generators.basic import CompleteGraph
root = CompleteGraph(4)
return (root,
g.is_isomorphic(root.line_graph(labels = False), certificate = True)[1])
#.........这里部分代码省略.........
示例3: is_line_graph
# 需要导入模块: from sage.graphs.graph import Graph [as 别名]
# 或者: from sage.graphs.graph.Graph import line_graph [as 别名]
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.
.. 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()
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
if g.is_connected():
try:
R, isom = root_graph(g)
if certificate:
return True, R, isom
else:
return True
except ValueError:
# g is not a line graph
if certificate:
#.........这里部分代码省略.........