Hot-keys on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
r""" Graphic Matroids
Let `G = (V,E)` be a graph and let `C` be the collection of the edge sets of cycles in `G`. The corresponding graphic matroid `M(G)` has ground set `E` and circuits `C`.
Construction ============
The recommended way to create a graphic matroid is by using the :func:`Matroid() <sage.matroids.constructor.Matroid>` function, with a graph `G` as input. This function can accept many different kinds of input to get a graphic matroid if the ``graph`` keyword is used, similar to the :func:`Graph() <sage.graphs.graph.Graph>` constructor. However, invoking the class directly is possible too. To get access to it, type::
sage: from sage.matroids.advanced import *
See also :mod:`sage.matroids.advanced`.
Graphic matroids do not have a representation matrix or any of the functionality of regular matroids. It is possible to get an instance of the :class:`~sage.matroids.linear_matroid.RegularMatroid` class by using the ``regular`` keyword when constructing the matroid. It is also possible to cast a GraphicMatroid as a RegularMatroid with the :meth:`~sage.matroids.graphic_matroids.GraphicMatroid.regular_matroid` method::
sage: M1 = Matroid(graphs.DiamondGraph(), regular=True) sage: M2 = Matroid(graphs.DiamondGraph()) sage: M3 = M2.regular_matroid()
Below are some examples of constructing a graphic matroid.
::
sage: from sage.matroids.advanced import * sage: edgelist = [(0, 1, 'a'), (0, 2, 'b'), (1, 2, 'c')] sage: G = Graph(edgelist) sage: M1 = Matroid(G) sage: M2 = Matroid(graph=edgelist) sage: M3 = Matroid(graphs.CycleGraph(3)) sage: M1 == M3 False sage: M1.is_isomorphic(M3) True sage: M1.equals(M2) True sage: M1 == M2 True sage: isinstance(M1, GraphicMatroid) True sage: isinstance(M1, RegularMatroid) False
Note that if there is not a complete set of unique edge labels, and there are no parallel edges, then vertex tuples will be used for the ground set. The user may wish to override this by specifying the ground set, as the vertex tuples will not be updated if the matroid is modified.
sage: G = graphs.DiamondGraph() sage: M1 = Matroid(G) sage: N1 = M1.contract((0,1)) sage: N1.graph().edges_incident(0) [(0, 2, (0, 2)), (0, 2, (1, 2)), (0, 3, (1, 3))] sage: M2 = Matroid(range(G.num_edges()), G) sage: N2 = M2.contract(0) sage: N1.is_isomorphic(N2) True
AUTHORS:
- Zachary Gershkoff (2017-07-07): initial version
Methods ======= """ #***************************************************************************** # Copyright (C) 2017 Zachary Gershkoff <zgersh2@lsu.edu> # # # Distributed under the terms of the GNU General Public License (GPL) # as published by the Free Software Foundation; either version 2 of # the License, or (at your option) any later version. # http://www.gnu.org/licenses/ #*****************************************************************************
r""" The graphic matroid class.
INPUT:
- ``G`` -- a Graph - ``groundset`` -- (optional) a list in 1-1 correspondence with ``G.edge_iterator()``
OUTPUT:
A ``GraphicMatroid`` instance where the ground set elements are the edges of ``G``.
..NOTE::
If a disconnected graph is given as input, the instance of ``GraphicMatroid`` will connect the graph components and store this as its graph.
EXAMPLES::
sage: from sage.matroids.advanced import * sage: M = GraphicMatroid(graphs.BullGraph()); M Graphic matroid of rank 4 on 5 elements sage: N = GraphicMatroid(graphs.CompleteBipartiteGraph(3,3)); N Graphic matroid of rank 5 on 9 elements
A disconnected input will get converted to a connected graph internally::
sage: G1 = graphs.CycleGraph(3); G2 = graphs.DiamondGraph() sage: G = G1.disjoint_union(G2) sage: len(G) 7 sage: G.is_connected() False sage: M = GraphicMatroid(G) sage: M Graphic matroid of rank 5 on 8 elements sage: H = M.graph() sage: H Looped multi-graph on 6 vertices sage: H.is_connected() True sage: M.is_connected() False
You can still locate an edge using the vertices of the input graph::
sage: G1 = graphs.CycleGraph(3); G2 = graphs.DiamondGraph() sage: G = G1.disjoint_union(G2) sage: M = Matroid(G) sage: H = M.graph() sage: vm = M.vertex_map() sage: (u, v, l) = G.random_edge() sage: H.has_edge(vm[u], vm[v]) True """
# Necessary:
""" See class definition for full documentation.
EXAMPLES::
sage: from sage.matroids.advanced import * sage: G1 = graphs.CycleGraph(3); G2 = graphs.DiamondGraph() sage: G = G1.disjoint_union(G2) sage: M = GraphicMatroid(G) sage: M Graphic matroid of rank 5 on 8 elements sage: M.graph() Looped multi-graph on 6 vertices sage: M.graph().is_connected() True sage: M.is_connected() False
TESTS::
sage: TestSuite(M).run(verbose=True) running ._test_category() . . . pass running ._test_new() . . . pass running ._test_not_implemented_methods() . . . pass running ._test_pickling() . . . pass """
#Try to construct a ground set based on the edge labels. #If that fails, use range() to come up with a groundset.
# if the provided ground set is incomplete, it gets overwriten # invalidate `None` as label
# Map vertices on input graph to vertices in self._G
# Construct a graph and assign edge labels corresponding to the ground set # the ordering from edge_labels() respects edge_iterator() and not edges() self._vertex_map[e[1]], groundset[i])) # If the matroid is empty, have the internal graph be a single vertex data_structure='static_sparse') else: data_structure='static_sparse') # Map ground set elements to graph edges: # The the edge labels should already be the elements. (u, v, l) in self._G.edges()})
""" Return the ground set of the matroid as a frozenset.
EXAMPLES::
sage: M = Matroid(graphs.DiamondGraph()) sage: sorted(M.groundset()) [(0, 1), (0, 2), (1, 2), (1, 3), (2, 3)] sage: G = graphs.CompleteGraph(3).disjoint_union(graphs.CompleteGraph(4)) sage: M = Matroid(range(G.num_edges()), G); sorted(M.groundset()) [0, 1, 2, 3, 4, 5, 6, 7, 8] sage: M = Matroid(Graph([(0, 1, 'a'), (0, 2, 'b'), (0, 3, 'c')])) sage: sorted(M.groundset()) ['a', 'b', 'c'] """
""" Return the rank of a set ``X``.
This method does no checking on ``X``, and ``X`` may be assumed to have the same interface as ``frozenset``.
INPUT:
- ``X`` -- an object with Python's ``frozenset`` interface
OUTPUT:
The rank of `X` in the matroid.
EXAMPLES::
sage: from sage.matroids.advanced import * sage: edgelist = [(0,0,0), (0,1,1), (0,2,2), (0,3,3), (1,2,4), (1,3,5)] sage: M = GraphicMatroid(Graph(edgelist, loops=True, multiedges=True)) sage: M.rank([0]) 0 sage: M.rank([1,2]) 2 sage: M.rank([1,2,4]) 2 sage: M.rank(M.groundset()) 3 sage: edgelist = [(0,0,0), (1,2,1), (1,2,2), (2,3,3)] sage: M = GraphicMatroid(Graph(edgelist, loops=True, multiedges=True)) sage: M.rank(M.groundset()) 2 sage: M.rank([0,3]) 1
""" [v for (u, v, l) in edges]) # This counts components:
# Representation:
""" Returns a string representation of the matroid.
EXAMPLES::
sage: M = Matroid(graphs.CompleteGraph(5)) sage: M Graphic matroid of rank 4 on 10 elements sage: G = Graph([(0, 0), (0, 1), (0, 2), (1, 1), (2, 2)], loops=True) sage: M = Matroid(G) sage: M Graphic matroid of rank 2 on 5 elements """
# Comparison:
""" Computes the set of edge labels around each vertex.
Internal method for hashing purposes.
OUTPUT:
A ``frozenset`` of ``frozenset``s containing the edge labels around each vertex.
EXAMPLES::
sage: M = Matroid(range(5), graphs.DiamondGraph()) sage: sorted(M._vertex_stars()) [frozenset({0, 2, 3}), frozenset({1, 2, 4}), frozenset({3, 4}), frozenset({0, 1})]
sage: N = Matroid(range(5), graphs.BullGraph()) sage: sorted(N._vertex_stars()) [frozenset({0, 2, 3}), frozenset({4}), frozenset({1, 2, 4}), frozenset({3}), frozenset({0, 1})] """
r""" Return an invariant of the matroid.
This function is called when matroids are added to a set. It is very desirable to override it so it can distinguish matroids on the same groundset, which is a very typical use case!
.. WARNING::
This method is linked to __richcmp__ (in Cython) and __cmp__ or __eq__/__ne__ (in Python). If you override one, you should (and in Cython: MUST) override the other!
EXAMPLES::
sage: M = Matroid(graphs.CompleteGraph(3)) sage: N = Matroid(graphs.CycleGraph(3)) sage: O = Matroid(graphs.ButterflyGraph()) sage: hash(M) == hash(N) True sage: hash(O) == hash(N) False sage: P = Matroid(Graph([(0, 1, 'a'), (0, 2, 'b'), (1, 2, 'c')])) sage: hash(P) == hash(M) False """
""" Compare two matroids.
For two graphic matroids to be equal, all attributes of the underlying graphs must be equal.
INPUT:
- ``other`` -- a matroid
OUTPUT:
``True`` if ``self`` and ``other`` have the same graph; ``False`` otherwise.
EXAMPLES::
sage: M = Matroid(graphs.CompleteGraph(3)) sage: N = Matroid(graphs.CycleGraph(3)) sage: O = Matroid(graphs.ButterflyGraph()) sage: P = Matroid(Graph([(0, 1, 'a'), (0, 2, 'b'), (1, 2, 'c')])) sage: M == N True sage: M == O False sage: M == P False
A more subtle example where the vertex labels differ::
sage: G1 = Graph([(0,1,0),(0,2,1),(1,2,2)]) sage: G2 = Graph([(3,4,3),(3,5,4),(4,5,5),(4,6,6),(5,6,7)]) sage: G = G1.disjoint_union(G2) sage: H = G2.disjoint_union(G1) sage: Matroid(G) == Matroid(H) False sage: Matroid(G).equals(Matroid(H)) True
Same except for vertex labels::
sage: G1 = Graph([(0,1,0),(1,2,1),(2,0,2)]) sage: G2 = Graph([(3,4,0),(4,5,1),(5,3,2)]) sage: Matroid(G1) == Matroid(G2) False
""" # Graph.__eq__() will ignore edge labels unless we turn on weighted() # This will be done in __init__()
""" Compare two matroids.
INPUT:
- ``other`` -- a matroid
OUTPUT:
``False`` if ``self`` and ``other`` have the same graph; ``True`` otherwise.
EXAMPLES::
sage: M = Matroid(range(4), graphs.CycleGraph(4)) sage: N = Matroid(range(4), graphs.CompleteBipartiteGraph(2,2)) sage: O = Matroid(graphs.PetersenGraph()) sage: M != N True sage: M.equals(N) True sage: M != O True
"""
# Copying, loading, saving:
""" Create a shallow copy.
Creating a ``GraphicMatroid`` instance will build a new graph, so the copies have no attributes in common.
EXAMPLES::
sage: M = Matroid(graphs.PappusGraph()) sage: N = copy(M) sage: M == N True sage: M._G is N._G False """ N.rename(getattr(self, '__custom_name'))
""" Create a deep copy.
.. NOTE::
Since matroids are immutable, a shallow copy normally suffices.
EXAMPLES::
sage: M = Matroid(graphs.PetersenGraph()) sage: N = deepcopy(M) sage: N == M True """ # The only real difference between this and __copy__() is the memo N.rename(deepcopy(getattr(self, '__custom_name'), memo))
""" Save the matroid for later reloading.
EXAMPLES::
sage: M = Matroid(graphs.PetersenGraph()) sage: M == loads(dumps(M)) True sage: loads(dumps(M)) Graphic matroid of rank 9 on 15 elements """
# Overrides:
""" Return a minor.
INPUT:
- ``contractions`` -- frozenset; subset of ``self.groundset()`` to be contracted - ``deletions`` -- frozenset; subset of ``self.groundset()`` to be deleted
Assumptions: contractions are independent, deletions are coindependent, contractions and deletions are disjoint.
OUTPUT:
An instance of GraphicMatroid.
EXAMPLES::
sage: M = matroids.CompleteGraphic(5) sage: M._minor(deletions=frozenset([0,1,2])) Graphic matroid of rank 4 on 7 elements sage: M._minor(contractions=frozenset([0,1,2])) Graphic matroid of rank 1 on 7 elements sage: M = Matroid(range(15), graphs.PetersenGraph()) sage: N = M._minor(deletions = frozenset([0, 3, 5, 9]), contractions = ....: frozenset([1, 2, 11])); N Graphic matroid of rank 6 on 8 elements """ # deletions first so contractions don't mess up the vertices
""" Check if the matroid has a minor isomorphic to M(H).
INPUT:
- ``N`` - a matroid - ``certificate`` - (default: ``False``) if ``True``, returns the certificate isomorphism from the minor of ``self`` to ``N``
OUTPUT:
Boolean, or tuple if the ``certificate`` option is used. If ``certificate`` is ``True``, then the output will either be ``False, None`` or ``True, (X, Y, dic) where ``N`` is isomorphic to ``self.minor(X, Y)``, and ``dic`` is an isomorphism between ``N`` and ``self.minor(X, Y)``.
EXAMPLES::
sage: M = Matroid(range(9), graphs.CompleteBipartiteGraph(3, 3)) sage: N = Matroid(range(3), graphs.CycleGraph(3)) sage: N1 = Matroid(range(3), graph=graphs.CycleGraph(3), ....: regular=True) sage: _, cert = M._has_minor(N1, certificate=True) sage: Mp = M.minor(cert[0], cert[1]) sage: N.is_isomorphism(Mp, cert[2]) True sage: M._has_minor(N) True sage: M._has_minor(N1) True sage: _, cert = M._has_minor(N, certificate=True) sage: Mp = M.minor(cert[0], cert[1]) sage: N.is_isomorphism(Mp, cert[2]) True
::
sage: M = matroids.CompleteGraphic(6) sage: N = Matroid(range(8), graphs.WheelGraph(5)) sage: M._has_minor(N) True sage: _, cert = M._has_minor(N, certificate=True) sage: Mp = M.minor(cert[0], cert[1]) sage: N.is_isomorphism(Mp, cert[2]) True sage: N.has_minor(M) False sage: N.has_minor(M, certificate=True) (False, None)
If the matroids are not 3-connected, then the default matroid algorithms are used::
sage: M = matroids.CompleteGraphic(6) sage: N = Matroid(graphs.CycleGraph(4)) sage: M.has_minor(N) True sage: N.has_minor(M) False """ # The graph minor algorithm is faster but it doesn't make sense # to use it if M(H) is not 3-connected, because of all the possible # Whitney switches or 1-sums that will give the same matroid. # Graph.minor() does not work with multigraphs
# Graph.minor() returns a certificate if there is one # and a ValueError if there isn't. else:
# This is where it gets complicated. # The Graph.minor() method gives a dictionary of vertices # as its certificate. There is currently no easy way to # determine the edges. # From the dictionary, we can get an idea of what the # contractions are, and what vertices are not used. # So we'll merge the appropriate vertices, delete the # unused vertices, and pass to Matroid._has_minor().
# Determine contractions:
# determine deletions:
# take contractions and deletions with what we have so far # then use method from abstract matroid class
# elements is a tuple (contractions, deletions, dict) # There should be no more contractions
else: else: # otherwise send it to regular matroids
""" Return the corank of the set `X` in the matroid.
Internal version that does no input checking.
INPUT:
- ``X`` -- an iterable container of ground set elements
OUTPUT:
Integer.
EXAMPLES::
sage: M = Matroid(range(9), graphs.CompleteBipartiteGraph(3,3)) sage: M._corank([0,1,2]) 2 sage: M._corank([1,2,3]) 3 """
""" Test if input is a circuit.
INPUT:
- ``X`` -- an iterable container of ground set elements
OUTPUT:
Boolean.
EXAMPLES::
sage: M = Matroid(range(5), graphs.DiamondGraph()) sage: M._is_circuit([0,1,2]) True sage: M._is_circuit([0,1,2,3]) False sage: M._is_circuit([0,1,3]) False """
""" Return the closure of a set.
INPUT:
- ``X`` -- an iterable container of ground set elements
OUTPUT:
``frozenset`` instance containing a subset of the ground set.
EXAMPLES::
sage: M = Matroid(range(5), graphs.DiamondGraph()) sage: sorted(M._closure([0])) [0] sage: sorted(M._closure([0,1])) [0, 1, 2] sage: sorted(M._closure(M.groundset())) [0, 1, 2, 3, 4]
TESTS:
Make sure the closure gets loops::
sage: edgelist = [(0, 0), (0, 1), (0, 2), (0, 3), (1, 2), (1, 2)] sage: M = Matroid(range(6), Graph(edgelist, loops=True, multiedges=True)) sage: M.graph().edges() [(0, 0, 0), (0, 1, 1), (0, 2, 2), (0, 3, 3), (1, 2, 4), (1, 2, 5)] sage: sorted(M._closure([4])) [0, 4, 5]
""" # a non-loop edge is in the closure iff both its vertices are # in the induced subgraph, and the edge doesn't connect components else: g.delete_edge(e) # add all loops
""" Compute a maximal independent subset.
INPUT:
- ``X`` -- An object with Python's ``frozenset`` interface containing a subset of ``self.groundset()``
OUTPUT:
``frozenset`` instance containing a subset of the ground set.
EXAMPLES::
sage: M = Matroid(range(5), graphs.DiamondGraph()) sage: sorted(M._max_independent(M.groundset())) [0, 1, 3] sage: sorted(M._max_independent(frozenset([0,1,2]))) [0, 1] sage: sorted(M._max_independent(frozenset([3,4]))) [3, 4] sage: sorted(M._max_independent(frozenset([3]))) [3] sage: N = M.graphic_extension(0, element='a') sage: sorted(N._max_independent(frozenset(['a']))) [] """
""" Compute a maximal coindependent subset.
INPUT:
- ``X`` -- an iterable container of ground set elements
OUTPUT:
``frozenset`` instance containing a subset of the ground set.
EXAMPLES::
sage: M = Matroid(range(5), graphs.DiamondGraph()) sage: sorted(M._max_coindependent(M.groundset())) [2, 4] sage: sorted(M._max_coindependent([2,3,4])) [2, 4] sage: N = M.graphic_extension(0, element=5) sage: sorted(N.max_coindependent([0,1,2,5])) [1, 2, 5] """
else:
""" Return a minimal dependent subset.
INPUT:
- ``X`` -- an iterable container of ground set elements
OUTPUT:
``frozenset`` instance containing a subset of ``X``. A ``ValueError`` is raised if the set contains no circuit.
EXAMPLES::
sage: M = Matroid(range(5), graphs.DiamondGraph()) sage: sorted(M._circuit(M.groundset())) [0, 1, 2] sage: N = Matroid(range(9), graphs.CompleteBipartiteGraph(3,3)) sage: sorted(N._circuit([0, 1, 2, 6, 7, 8])) [0, 1, 6, 7] sage: N._circuit([0, 1, 2]) Traceback (most recent call last): ... ValueError: no circuit in independent set
TESTS:
With two disjoint cycles in the graph::
sage: edgelist = [(5,6), (0,1), (3,4), (1,2), (4,5), (2,0), (5,3)] sage: M = Matroid(range(7), Graph(edgelist)) sage: M Graphic matroid of rank 5 on 7 elements sage: sorted(M._circuit(M.groundset())) [0, 1, 2]
Giving it a long path before it finds a cycle::
sage: edgelist = [(0,1), (1,2), (2,3), (3,4), (4,5), (4,5)] sage: M = Matroid(Graph(edgelist, multiedges=True)) sage: M.graph().edges() [(0, 1, 0), (1, 2, 1), (2, 3, 2), (3, 4, 3), (4, 5, 4), (4, 5, 5)] sage: sorted(M._circuit(M.groundset())) [4, 5]
""" set([v for (u, v, l) in edges])) else: else:
+ [v for (u, v, l) in edge_set]) or vertex_list.count(v) == 1)] or vertex_list.count(v) == 1)]
""" Return the coclosure of a set.
INPUT:
- ``X`` -- an iterable container of ground set elements
OUTPUT:
``frozenset`` instance containing a subset of the groundset.
EXAMPLES::
sage: M = Matroid(range(5), graphs.DiamondGraph()) sage: sorted(M._coclosure([0])) [0, 1] sage: sorted(M._coclosure([0,1])) [0, 1] sage: N = M.graphic_extension(0, element=5) sage: sorted(N._coclosure([3])) [3, 4] sage: N = M.graphic_coextension(0, element=5) sage: sorted(N._coclosure([3])) [3, 4, 5] """
""" Test if input is a closed set.
INPUT:
- ``X`` -- an object with Python's ``frozenset`` interface containing a subset of ``self.groundset()``
OUTPUT:
Boolean.
EXAMPLES::
sage: edgelist = [(0, 0), (0, 1), (0, 2), (0, 3), (1, 2), (1, 2)] sage: M = Matroid(range(len(edgelist)), Graph(edgelist, loops=True, ....: multiedges=True)) sage: M._is_closed(frozenset([0,4,5])) True sage: M._is_closed(frozenset([0,4])) False sage: M._is_closed(frozenset([1, 2, 3, 4 ,5])) False """ # Take the set of vertices of the edges corresponding to the elements, # and check if there are other edges incident with two of those vertices. # Also, the must not be loops outside of X.
# Remove loops from input since we don't want to count them as components
""" Test if ``self`` is isomorphic to ``other``.
INPUT:
- ``other`` -- a matroid - ``certificate`` -- boolean
OUTPUT:
- If ``certificate`` is ``False``, Boolean. - If ``certificate`` is ``True``, a tuple containing a boolean and a dictionary giving the isomorphism or None.
EXAMPLES::
sage: M = Matroid(range(5), graphs.DiamondGraph()) sage: N = Matroid(graph=graphs.DiamondGraph(), regular=True) sage: M._is_isomorphic(N, certificate=True) (True, {0: (0, 1), 1: (0, 2), 2: (1, 2), 3: (1, 3), 4: (2, 3)}) sage: O = Matroid(graphs.WheelGraph(5)) sage: M._is_isomorphic(O, certificate=True) (False, None)
::
sage: M1 = Matroid(range(4), graphs.CycleGraph(4)) sage: M2 = Matroid(range(4), graphs.CompleteBipartiteGraph(2,2)) sage: M3 = matroids.Uniform(3,4) sage: M1._is_isomorphic(M2) True sage: M1._is_isomorphic(M3) True
::
sage: edgelist = [(0,1,'a'),(0,2,'b'),(0,3,'c'),(1,2,'d'),(1,3,'e'),(2,3,'f')] sage: M = Matroid(Graph(edgelist)) sage: N = Matroid(range(6), graphs.WheelGraph(4)) sage: M._is_isomorphic(N, certificate=True) (True, {'a': 2, 'b': 4, 'c': 5, 'd': 0, 'e': 1, 'f': 3}) sage: N._is_isomorphic(M, certificate=True) (True, {0: 'd', 1: 'e', 2: 'a', 3: 'f', 4: 'b', 5: 'c'}) sage: O = Matroid(range(6), graphs.CycleGraph(6)) sage: M._is_isomorphic(O) False """ # Check for 3-connectivity so we don't have to worry about Whitney twists
# If they are isomorphic and the user wants a certificate, # result[1] is a dictionary of vertices. # We need to translate this to edge labels.
else:
""" Return isomorphism from ``self`` to ``other``, if such an isomorphism exists.
Internal version that performs no checks on input.
INPUT:
- ``other`` -- a matroid
OUTPUT:
A dictionary, or ``None``.
EXAMPLES::
sage: M1 = Matroid(range(4), graphs.CycleGraph(4)) sage: M2 = Matroid(range(4), graphs.CompleteBipartiteGraph(2,2)) sage: M1._isomorphism(matroids.named_matroids.BetsyRoss()) sage: M1._isomorphism(M2) {0: 0, 1: 1, 2: 2, 3: 3} sage: M3 = matroids.Uniform(3,4) sage: M1._isomorphism(M3) {0: 0, 1: 1, 2: 2, 3: 3}
::
sage: edgelist = [(0,1,'a'),(0,2,'b'),(0,3,'c'),(1,2,'d'),(1,3,'e'),(2,3,'f')] sage: M = Matroid(Graph(edgelist)) sage: N = Matroid(range(6), graphs.WheelGraph(4)) sage: M._isomorphism(N) {'a': 2, 'b': 4, 'c': 5, 'd': 0, 'e': 1, 'f': 3} sage: O = Matroid(Graph(edgelist), regular=True) sage: M._isomorphism(O) {'a': 'a', 'b': 'c', 'c': 'b', 'd': 'e', 'e': 'd', 'f': 'f'} """
""" Test if the data obey the matroid axioms.
Since a graph is used for the data, this is always the case.
OUTPUT:
``True``.
EXAMPLES::
sage: M = matroids.CompleteGraphic(4); M M(K4): Graphic matroid of rank 3 on 6 elements sage: M.is_valid() True """
# Graphic methods:
""" Return the graph that represents the matroid.
The graph will always have loops and multiedges enabled.
OUTPUT:
A Graph.
EXAMPLES::
sage: M = Matroid(Graph([(0, 1, 'a'), (0, 2, 'b'), (0, 3, 'c')])) sage: M.graph().edges() [(0, 1, 'a'), (0, 2, 'b'), (0, 3, 'c')] sage: M = Matroid(graphs.CompleteGraph(5)) sage: M.graph() Looped multi-graph on 5 vertices """ # Return a mutable graph
""" Return a dictionary mapping the input vertices to the current vertices.
The graph for the matroid is alway connected. If the constructor is given a graph with multiple components, it will connect them. The Python dictionary given by this method has the vertices from the input graph as keys, and the corresponding vertex label after any merging as values.
OUTPUT:
A dictionary.
EXAMPLES::
sage: G = Graph([(0, 1), (0, 2), (1, 2), (3, 4), (3, 5), (4, 5), ....: (6, 7), (6, 8), (7, 8), (8, 8), (7, 8)], multiedges=True, loops=True) sage: M = Matroid(range(G.num_edges()), G) sage: M.graph().edges() [(0, 1, 0), (0, 2, 1), (1, 2, 2), (2, 4, 3), (2, 5, 4), (4, 5, 5), (5, 7, 6), (5, 8, 7), (7, 8, 8), (7, 8, 9), (8, 8, 10)] sage: M.vertex_map() {0: 0, 1: 1, 2: 2, 3: 2, 4: 4, 5: 5, 6: 5, 7: 7, 8: 8} """
""" Return a list of edges corresponding to a set of ground set elements.
INPUT:
- ``X`` -- a subset of the ground set
OUTPUT:
A list of graph edges.
EXAMPLES::
sage: M = Matroid(range(5), graphs.DiamondGraph()) sage: M.groundset_to_edges([2,3,4]) [(1, 2, 2), (1, 3, 3), (2, 3, 4)] sage: M.groundset_to_edges([2,3,4,5]) Traceback (most recent call last): ... ValueError: input must be a subset of the ground set """
""" Return a list of edges corresponding to a set of ground set elements.
INPUT:
- ``X`` -- a subset of the ground set
OUTPUT:
A list of graph edges.
EXAMPLES::
sage: M = Matroid(range(5), graphs.DiamondGraph()) sage: M._groundset_to_edges([2,3,4]) [(1, 2, 2), (1, 3, 3), (2, 3, 4)] """
""" Return the subgraph corresponding to the matroid restricted to `X`.
INPUT:
- ``X`` -- a subset of the ground set
OUTPUT:
A Graph.
EXAMPLES::
sage: M = Matroid(range(5), graphs.DiamondGraph()) sage: M.subgraph_from_set([0,1,2]) Looped multi-graph on 3 vertices sage: M.subgraph_from_set([3,4,5]) Traceback (most recent call last): ... ValueError: input must be a subset of the ground set """
""" Return the subgraph corresponding to `M` restricted to `X`.
INPUT:
- ``X`` -- a subset of the ground set
OUTPUT:
A Graph.
EXAMPLES::
sage: M = Matroid(range(5), graphs.DiamondGraph()) sage: M._subgraph_from_set([0,1,2]) Looped multi-graph on 3 vertices """
""" Return a graphic matroid extended by a new element.
A new edge will be added between ``u`` and ``v``. If ``v`` is not specified, then a loop is added on ``u``.
INPUT:
- ``u`` -- a vertex in the matroid's graph - ``v`` -- (optional) another vertex - ``element`` -- (optional) the label of the new element
OUTPUT:
A GraphicMatroid with the specified element added. Note that if ``v`` is not specifies or if ``v`` is ``u``, then the new element will be a loop. If the new element's label is not specified, it will be generated automatically.
EXAMPLES::
sage: M = matroids.CompleteGraphic(4) sage: M1 = M.graphic_extension(0,1,'a'); M1 Graphic matroid of rank 3 on 7 elements sage: M1.graph().edges() [(0, 1, 0), (0, 1, 'a'), (0, 2, 1), (0, 3, 2), (1, 2, 3), (1, 3, 4), (2, 3, 5)] sage: M2 = M1.graphic_extension(3); M2 Graphic matroid of rank 3 on 8 elements
::
sage: M = Matroid(range(10), graphs.PetersenGraph()) sage: M.graphic_extension(0, 'b', 'c').graph().vertices() [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 'b'] sage: M.graphic_extension('a', 'b', 'c').graph().vertices() Traceback (most recent call last): ... ValueError: u must be an existing vertex
TESTS::
sage: M = Matroid(graphs.EmptyGraph()) sage: M.graphic_extension(0) Graphic matroid of rank 0 on 1 elements sage: M.graphic_extension(0, 1, 'a') Graphic matroid of rank 1 on 1 elements
""" # This will possibly make a coloop if v is a new vertex raise ValueError("cannot extend by element already in ground set")
""" Return an iterable containing the graphic extensions.
This method iterates over the vertices in the input. If ``simple == False``, it first extends by a loop. It will then add an edge between every pair of vertices in the input, skipping pairs of vertices with an edge already between them if ``simple == True``.
This method only considers the current graph presentation, and does not take 2-isomorphism into account. Use :meth:`twist <sage.matroids.graphic_matroid.GraphicMatroid.twist>` or :meth:`one_sum <sage.matroids.graphic_matroid.GraphicMatroid.one_sum>` if you wish to change the graph presentation.
INPUT:
- ``element`` -- (optional) the name of the newly added element in each extension - ``vertices`` -- (optional) a set of vertices over which the extension may be taken - ``simple`` -- (default: ``False``) if true, extensions by loops and parallel elements are not taken
OUTPUT:
An iterable containing instances of GraphicMatroid. If ``vertices`` is not specified, every vertex is used.
.. NOTE::
The extension by a loop will always occur unless ``simple == True``. The extension by a coloop will never occur.
EXAMPLES::
sage: M = Matroid(range(5), graphs.DiamondGraph()) sage: I = M.graphic_extensions('a') sage: for N in I: ....: N.graph().edges() [(0, 0, 'a'), (0, 1, 0), (0, 2, 1), (1, 2, 2), (1, 3, 3), (2, 3, 4)] [(0, 1, 0), (0, 1, 'a'), (0, 2, 1), (1, 2, 2), (1, 3, 3), (2, 3, 4)] [(0, 1, 0), (0, 2, 1), (0, 2, 'a'), (1, 2, 2), (1, 3, 3), (2, 3, 4)] [(0, 1, 0), (0, 2, 1), (0, 3, 'a'), (1, 2, 2), (1, 3, 3), (2, 3, 4)] [(0, 1, 0), (0, 2, 1), (1, 2, 2), (1, 2, 'a'), (1, 3, 3), (2, 3, 4)] [(0, 1, 0), (0, 2, 1), (1, 2, 2), (1, 3, 3), (1, 3, 'a'), (2, 3, 4)] [(0, 1, 0), (0, 2, 1), (1, 2, 2), (1, 3, 3), (2, 3, 4), (2, 3, 'a')]
::
sage: M = Matroid(graphs.CompleteBipartiteGraph(3,3)) sage: I = M.graphic_extensions(simple=True) sage: sum (1 for i in I) 6 sage: I = M.graphic_extensions(vertices=[0,1,2]) sage: sum (1 for i in I) 4 """ raise ValueError("cannot extend by element already in ground set") raise ValueError("vertices are not all in the graph")
# First extend by a loop, then consider every pair of vertices. # Put the loop on the first vertex.
""" Return a matroid coextended by a new element.
A coextension in a graphic matroid is the opposite of contracting an edge; that is, a vertex is split, and a new edge is added between the resulting vertices. This method will create a new vertex `v` adjacent to `u`, and move the edges indicated by `X` from `u` to `v`.
INPUT:
- ``u`` -- the vertex to be split - ``v`` -- (optional) the name of the new vertex after splitting - ``X`` -- (optional) a list of the matroid elements corresponding to edges incident to ``u`` that move to the new vertex after splitting - ``element`` -- (optional) The name of the newly added element
OUTPUT:
An instance of GraphicMatroid coextended by the new element. If ``X`` is not specified, the new element will be a coloop.
.. NOTE::
A loop on ``u`` will stay a loop unless it is in ``X``.
EXAMPLES::
sage: G = Graph([(0, 1, 0), (0, 2, 1), (0, 3, 2), (0, 4, 3), (1, 2, 4), (1, 4, 5), (2, 3, 6), (3, 4, 7)]) sage: M = Matroid(G) sage: M1 = M.graphic_coextension(0, X=[1,2], element='a') sage: M1.graph().edges() [(0, 1, 0), (0, 4, 3), (0, 5, 'a'), (1, 2, 4), (1, 4, 5), (2, 3, 6), (2, 5, 1), (3, 4, 7), (3, 5, 2)]
TESTS::
sage: M = Matroid(range(3), graphs.CycleGraph(3)) sage: M = M.graphic_extension(0, element='a') sage: M.graph().edges() [(0, 0, 'a'), (0, 1, 0), (0, 2, 1), (1, 2, 2)] sage: M1 = M.graphic_coextension(0, X=[1], element='b') sage: M1.graph().edges() [(0, 0, 'a'), (0, 1, 0), (0, 3, 'b'), (1, 2, 2), (2, 3, 1)] sage: M2 = M.graphic_coextension(0, X=[1, 'a'], element='b') sage: M2.graph().edges() [(0, 1, 0), (0, 3, 'a'), (0, 3, 'b'), (1, 2, 2), (2, 3, 1)]
::
sage: M = Matroid(graphs.CycleGraph(3)) sage: M = M.graphic_coextension(u=2, element='a') sage: M.graph() Looped multi-graph on 4 vertices sage: M.graph().loops() [] sage: M = M.graphic_coextension(u=2, element='a') Traceback (most recent call last): ... ValueError: cannot extend by element already in ground set sage: M = M.graphic_coextension(u=4) Traceback (most recent call last): ... ValueError: u must be an existing vertex
TESTS::
sage: M = Matroid(graphs.EmptyGraph()) sage: M.graphic_coextension(u=0) Graphic matroid of rank 1 on 1 elements
sage: M = Matroid(graphs.DiamondGraph()) sage: N = M.graphic_coextension(0,'q') sage: N.graph().vertices() [0, 1, 2, 3, 'q']
::
sage: M = Matroid(range(5), graphs.DiamondGraph()) sage: N = M.graphic_coextension(u=3, v=5, element='a') sage: N.graph().edges() [(0, 1, 0), (0, 2, 1), (1, 2, 2), (1, 3, 3), (2, 3, 4), (3, 5, 'a')] sage: N = M.graphic_coextension(u=3, element='a') sage: N.graph().edges() [(0, 1, 0), (0, 2, 1), (1, 2, 2), (1, 3, 3), (2, 3, 4), (3, 4, 'a')] sage: N = M.graphic_coextension(u=3, v=3, element='a') Traceback (most recent call last): ... ValueError: u and v must be distinct """ else:
# To prevent an error for iterating over None:
raise ValueError("vertex is already in the graph") G.add_edge(u, v, element) return GraphicMatroid(G)
""" Return an iterator of graphic coextensions.
This method iterates over the vertices in the input. If ``cosimple == False``, it first coextends by a coloop and series edge for every edge incident with the vertices. For vertices of degree four or higher, it will consider the ways to partition the vertex into two sets of cardinality at least two, and these will be the edges incident with the vertices after splitting.
At most one series coextension will be taken for each series class.
INPUT:
- ``vertices`` -- (optional) the vertices to be split - ``v`` -- (optional) the name of the new vertex - ``element`` -- (optional) the name of the new element - ``cosimple`` -- (default: ``False``) if true, coextensions by a coloop or series elements will not be taken
OUTPUT:
An iterable containing instances of GraphicMatroid. If ``vertices`` is not specified, the method iterates over all vertices.
EXAMPLES::
sage: G = Graph([(0, 1), (0, 2), (0, 3), (0, 4), (1, 2), (1, 4), (2, 3), (3, 4)]) sage: M = Matroid(range(8), G) sage: I = M.graphic_coextensions(vertices=[0], element='a') sage: for N in I: ....: N.graph().edges_incident(0) [(0, 1, 0), (0, 2, 1), (0, 3, 2), (0, 4, 3), (0, 5, 'a')] [(0, 2, 1), (0, 3, 2), (0, 4, 3), (0, 5, 'a')] [(0, 1, 0), (0, 2, 1), (0, 3, 2), (0, 5, 'a')] [(0, 1, 0), (0, 3, 2), (0, 4, 3), (0, 5, 'a')] [(0, 1, 0), (0, 2, 1), (0, 4, 3), (0, 5, 'a')] [(0, 2, 1), (0, 3, 2), (0, 5, 'a')] [(0, 1, 0), (0, 3, 2), (0, 5, 'a')] [(0, 1, 0), (0, 2, 1), (0, 5, 'a')]
::
sage: N = Matroid(range(4), graphs.CycleGraph(4)) sage: I = N.graphic_coextensions(element='a') sage: for N1 in I: ....: N1.graph().edges() [(0, 1, 0), (0, 3, 1), (0, 4, 'a'), (1, 2, 2), (2, 3, 3)] [(0, 1, 0), (0, 3, 1), (1, 4, 2), (2, 3, 3), (2, 4, 'a')] sage: sum(1 for n in N.graphic_coextensions(cosimple=True)) 0
TESTS::
sage: M = Matroid(graphs.EmptyGraph()) sage: M.graphic_coextension(0) Graphic matroid of rank 1 on 1 elements sage: I = M.graphic_coextensions(element='a') sage: for m in I: ....: m.graph().edges() [(0, 1, 'a')] sage: N = Matroid(graphs.CycleGraph(4)) sage: I = N.graphic_coextensions(vertices=[3, 4], element='a') sage: next(I) Traceback (most recent call last): ... ValueError: vertices are not all in the graph
We expect 136 graphic coextensions of an 8-spoked wheel: 128 extensions from the center vertex because there are (256/2) ways to put the 8 center edges into 2 sets, and then 8 more for series extensions of the rims::
sage: M = Matroid(graphs.WheelGraph(9)) sage: I = M.graphic_coextensions() sage: sum(1 for N in I) 136 sage: I = M.graphic_coextensions(cosimple=True) sage: sum(1 for N in I) 119 sage: sum(1 for N in Matroid(graphs.WheelGraph(8)).graphic_coextensions()) 71
This graph has max degree 3, so the only series extensions should be non-cosimple, ie. a coloop and one for every coseries class. 12 total::
sage: edgedict = {0:[1,2,3], 1:[2,4], 2:[3], 3:[6], 4:[5,7], 5:[6,7], 6:[7]} sage: M = Matroid(range(12), Graph(edgedict)) sage: sorted(M.coclosure([4])) [4, 6] sage: sum(1 for N in M.graphic_coextensions()) 12 sage: sum(1 for N in M.graphic_coextensions(cosimple=True)) 0 """ raise ValueError("cannot extend by element already in groundset")
# we just need to know what the vertex's name will be elif v in G: raise ValueError("vertex is already in the graph")
# First extend by a coloop on the first vertex.
# Next add an edge in series, for every series class in the input # place the new element on v0 if v0 is a vertex from input else:
# If a vertex has degree 1, or 2, or 3, we already handled it. X=g, u=u, v=v, element=element)
""" Perform a Whitney twist on the graph.
`X` must be part of a 2-separation. The connectivity of `X` must be 1, and the subgraph induced by `X` must intersect the subgraph induced by the rest of the elements on exactly two vertices.
INPUT:
- ``X`` -- the set of elements to be twisted with respect to the rest of the matroid
OUTPUT:
An instance of ``GraphicMatroid`` isomorphic to this matroid but with a graph that is not necessarily isomorphic.
EXAMPLES::
sage: edgelist = [(0,1,0), (1,2,1), (1,2,2), (2,3,3), (2,3,4), (2,3,5), (3,0,6)] sage: M = Matroid(Graph(edgelist, multiedges=True)) sage: M1 = M.twist([0,1,2]); M1.graph().edges() [(0, 1, 1), (0, 1, 2), (0, 3, 6), (1, 2, 0), (2, 3, 3), (2, 3, 4), (2, 3, 5)] sage: M2 = M.twist([0,1,3]) Traceback (most recent call last): ... ValueError: the input must display a 2-separation that is not a 1-separation
TESTS::
sage: edgedict = {0: [1, 2], 1: [2, 3], 2: [3], 3: [4, 5], 4: [5]} sage: M = Matroid(range(8), Graph(edgedict)) sage: M.graph().edges() [(0, 1, 0), (0, 2, 1), (1, 2, 2), (1, 3, 3), (2, 3, 4), (3, 4, 5), (3, 5, 6), (4, 5, 7)] sage: M1 = M.twist([0, 1]); M1.graph().edges() [(0, 1, 1), (0, 2, 0), (1, 2, 2), (1, 3, 3), (2, 3, 4), (3, 4, 5), (3, 5, 6), (4, 5, 7)] sage: M2 = M1.twist([0, 1, 2]); M2.graph().edges() [(0, 1, 0), (0, 2, 1), (1, 2, 2), (1, 3, 3), (2, 3, 4), (3, 4, 5), (3, 5, 6), (4, 5, 7)] sage: M1 == M False sage: M2 == M True sage: M2.twist([3, 4]) Traceback (most recent call last): ... ValueError: too many vertices in the intersection """ # We require two things: # (1) The connectivity of X is 1, # (2) X intersects the rest of the graph on 2 vertices raise ValueError("X must be a subset of the ground set") + "that is not a 1-separation")
# Determine the vertices [e[1] for e in X_edges]) [e[1] for e in Y_edges])
u in vertices or v in vertices)] u = a
""" Arrange matroid components in the graph.
The matroid's graph must be connected even if the matroid is not connected, but if there are multiple matroid components, the user may choose how they are arranged in the graph. This method will take the block of the graph that represents `X` and attach it by vertex `u` to another vertex `v` in the graph.
INPUT:
- ``X`` -- a subset of the ground set - ``u`` -- a vertex spanned by the edges of the elements in ``X`` - ``v`` -- a vertex spanned by the edges of the elements not in ``X``
OUTPUT:
An instance of ``GraphicMatroid`` isomorphic to this matroid but with a graph that is not necessarily isomorphic.
EXAMPLES::
sage: edgedict = {0:[1, 2], 1:[2, 3], 2:[3], 3:[4, 5], 6:[4, 5]} sage: M = Matroid(range(9), Graph(edgedict)) sage: M.graph().edges() [(0, 1, 0), (0, 2, 1), (1, 2, 2), (1, 3, 3), (2, 3, 4), (3, 4, 5), (3, 5, 6), (4, 6, 7), (5, 6, 8)] sage: M1 = M.one_sum(u=3, v=1, X=[5, 6, 7, 8]) sage: M1.graph().edges() [(0, 1, 0), (0, 2, 1), (1, 2, 2), (1, 3, 3), (1, 4, 5), (1, 5, 6), (2, 3, 4), (4, 6, 7), (5, 6, 8)] sage: M2 = M.one_sum(u=4, v=3, X=[5, 6, 7, 8]) sage: M2.graph().edges() [(0, 1, 0), (0, 2, 1), (1, 2, 2), (1, 3, 3), (2, 3, 4), (3, 6, 7), (3, 7, 5), (5, 6, 8), (5, 7, 6)]
TESTS::
sage: M = matroids.CompleteGraphic(4) sage: M.one_sum(u=1, v=2, X=[0,1]) Traceback (most recent call last): ... ValueError: the input must display a 1-separation
::
sage: M = Matroid(range(5), graphs.BullGraph()) sage: M.graph().edges() [(0, 1, 0), (0, 2, 1), (1, 2, 2), (1, 3, 3), (2, 4, 4)] sage: M1 = M.one_sum(u=3, v=0, X=[3,4]) Traceback (most recent call last): ... ValueError: too many vertices in the intersection
sage: M1 = M.one_sum(u=3, v=2, X=[3]) sage: M1.graph().edges() [(0, 1, 0), (0, 2, 1), (1, 2, 2), (2, 4, 4), (2, 5, 3)]
sage: M2 = M1.one_sum(u=5, v=0, X=[3,4]) sage: M2.graph().edges() [(0, 1, 0), (0, 2, 1), (0, 3, 3), (1, 2, 2), (3, 4, 4)]
sage: M = Matroid(range(5), graphs.BullGraph()) sage: M.one_sum(u=0, v=1, X=[3]) Traceback (most recent call last): ... ValueError: first vertex must be spanned by the input
sage: M.one_sum(u=1, v=3, X=[3]) Traceback (most recent call last): ... ValueError: second vertex must be spanned by the rest of the graph """ # We require two things: # (1) The connectivity of X is 0, # (2) X intersects the rest of the graph on 1 vertex raise ValueError("X must be a subset of the ground set") raise ValueError("the vertices must already be in the graph")
# Determine the vertex [e[1] for e in X_edges]) [e[1] for e in Y_edges]) "the rest of the graph")
# If u was the cut vertex, u is now detached from our component # so we merge the new vertex. Otherwise we can merge u else:
""" Return an instance of RegularMatroid isomorphic to this GraphicMatroid.
EXAMPLES::
sage: M = matroids.CompleteGraphic(5); M M(K5): Graphic matroid of rank 4 on 10 elements sage: N = M.regular_matroid(); N Regular matroid of rank 4 on 10 elements with 125 bases sage: M.equals(N) True sage: M == N False """ regular=True) |