Repository navigation
added induced minor function to graph algorithms - #36354
Conversation
dcoudert
left a comment
There was a problem hiding this comment.
Thank you for this new functionality. However, to avoid unnecessary code duplication, it is better to add parameter induced (default to False) to method minor and, when true, to add the extra constraints to the MIP.
dcoudert
left a comment
There was a problem hiding this comment.
a few more remarks. Sorry for that.
dcoudert
left a comment
There was a problem hiding this comment.
I hope these are my last comments. Sorry for all these Python style details.
dcoudert
left a comment
There was a problem hiding this comment.
Unfortunately I cannot push in this branch, so I give you the diff of the changes that are required due to remaining trailing white spaces, coding style and more importantly doctest errors (check that using ./sage -t --long src/sage/graphs/graph.py).
In particular, we can call randint but not random.randint. Furthermore, the call to g.add_edge(random.randint(0, 5), i) may raise an error when the value r of the random number is equal to i. Indeed, the graph g does not allow loops. Instead, we can use a call to g.add_vertex() which returns a new vertex with an unused integer label.
diff --git a/src/sage/graphs/graph.py b/src/sage/graphs/graph.py
index cea6ff99ba..4db0b324f2 100644
--- a/src/sage/graphs/graph.py
+++ b/src/sage/graphs/graph.py
@@ -4911,7 +4911,7 @@ class Graph(GenericGraph):
been merged to create a new graph `G'`, this new graph contains `H` as a
subgraph.
- When parameter ``induced`` is ``True``, this method returns an induced minor
+ When parameter ``induced`` is ``True``, this method returns an induced minor
isomorphic to `H`, if it exists.
We say that a graph `G` has an induced `H`-minor (or that it has a
@@ -4941,7 +4941,8 @@ class Graph(GenericGraph):
:meth:`MixedIntegerLinearProgram.get_values`.
- ``induced`` -- boolean (default: ``False``); if ``True``, returns an
- induced minor isomorphic to `H` if it exists, and :class:`ValueError` otherwise.
+ induced minor isomorphic to `H` if it exists, and raises a
+ :class:`ValueError` otherwise.
OUTPUT:
@@ -5001,21 +5002,24 @@ class Graph(GenericGraph):
...
ValueError: This graph has no minor isomorphic to H !
- Trying to find an induced minor for a graph with a C6 cycle::
+ Trying to find an induced minor isomorphic to `C_5` in a graph
+ containing an induced `C_6`::
- sage: g = graphs.CycleGraph(6) # Create a graph with 6 vertices forming a C6 cycle
- sage: for i in random.randint(10, 30):
- ....: g.add_edge(random.randint(0, 5), i)
- sage: h = graphs.CycleGraph(5) # Create a graph with 5 vertices forming a C5 cycle
- sage: L = g.minor(h, induced=True)
- sage: gg = g.subgraph(flatten(L.values(), max_level = 1))
- sage: _ = [gg.merge_vertices(l) for l in L.values() if len(l)>1]
+ sage: g = graphs.CycleGraph(6)
+ sage: for i in range(randint(10, 30)):
+ ....: g.add_edge(randint(0, 5), g.add_vertex())
+ sage: h = graphs.CycleGraph(5)
+ sage: L = g.minor(h, induced=True)
+ sage: gg = g.subgraph(flatten(L.values(), max_level=1))
+ sage: _ = [gg.merge_vertices(l) for l in L.values() if len(l) > 1]
sage: gg.is_isomorphic(h)
True
TESTS::
-
- sage: # a graph `g` may have minor but no induced minor isomorphic to given graph `h`
+
+ A graph `g` may have a minor isomorphic to a given graph `h` but no
+ induced minor isomorphic to `h`::
+
sage: g = Graph([(0, 1), (0, 2), (1, 2), (2, 3), (3, 4), (3, 5), (4, 5), (6, 5)])
sage: h = Graph([(9, 10), (9, 11), (9, 12), (9, 13)])
sage: l = g.minor(h, induced=False)
@@ -5024,11 +5028,14 @@ class Graph(GenericGraph):
...
ValueError: This graph has no induced minor isomorphic to H !
+ Checking that the returned induced minor is isomorphic to the given
+ graph::
+
sage: g = Graph([(0, 1), (0, 2), (1, 2), (2, 3), (3, 4), (3, 5), (4, 5), (6, 5)])
sage: h = Graph([(7, 8), (8, 9), (9, 10), (10, 11)])
sage: L = g.minor(h, induced=True)
- sage: gg = g.subgraph(flatten(L.values(), max_level = 1))
- sage: _ = [gg.merge_vertices(l) for l in L.values() if len(l)>1]
+ sage: gg = g.subgraph(flatten(L.values(), max_level=1))
+ sage: _ = [gg.merge_vertices(l) for l in L.values() if len(l) > 1]
sage: gg.is_isomorphic(h)
True
"""14aafcd to
c255d4b
Compare
|
Can you edit the PR description to add at the top: Fixes #36353 This way, the PR will be automatically linked to the issue and both will be closed simultaneously. This greatly ease the work of the release manager. Thanks. |
|
@dcoudert is the PR ready to merge? |
|
Yes. The release manager should merge it in the next beta release (or the following). |
|
this has broken our linters (trailing whitespaces and bad rst syntax) |
|
Sorry for missing these issues when reviewing this PR. |
sagemathgh-36461: fix the linters again Fix the broken linters again broken just recently in` graphs/graph.py` sagemath#36354 ### 📝 Checklist - [x] The title is concise, informative, and self-explanatory. - [x] The description explains in detail what this PR is about. URL: sagemath#36461 Reported by: Frédéric Chapoton Reviewer(s): Frédéric Chapoton, Kwankyu Lee
sagemathgh-36461: fix the linters again Fix the broken linters again broken just recently in` graphs/graph.py` sagemath#36354 ### 📝 Checklist - [x] The title is concise, informative, and self-explanatory. - [x] The description explains in detail what this PR is about. URL: sagemath#36461 Reported by: Frédéric Chapoton Reviewer(s): Frédéric Chapoton, Kwankyu Lee
Description
Fixes #36353
This pull request introduces the "induced_minor" function to the SageMath library for graph theory. The function allows users to find an induced minor isomorphic to a given target graph
Hin a graphG. It utilizes Mixed Integer Linear Programming (MILP) to solve this problem efficiently.Motivation
The "induced_minor" function is a valuable addition to SageMath as it addresses the need to find induced minors, a fundamental concept in graph theory. Users often encounter scenarios where they need to identify an induced minor isomorphic to a specific target graph, and this function streamlines the process.
Issue: #36353
Documentation Issue: ----
Implementation
Logic
An existing minor function had several constraints for finding an induced minor isomorphic to a given target graph
Hin a graph`G:vinGrepresents a vertexhinHusing binary variablesrs[h, v].Gform a connected tree structure using binary variablesedges[h, fuv]for edges(u, v)inG.hinHis exactly one less than the cardinality of its representative set.r_edges[h, (u, v)]variables and constraints.Gcorrespond to edges inHusingh_edgesvariables.For induced minor, we just had to add an additional constraint:
(h1, h2)in target graphH. If no such edge exists inH, it ensures that there is no edge between the representative sets of verticesh1andh2in graphG.Algorithm and Complexity
The "induced_minor" function uses MILP to find the minor and follows the algorithm described in [KKT2006]. The complexity of this operation is discussed in the docstring.
Example Usage
The PR includes examples demonstrating how to use the "induced_minor" function to find induced minors isomorphic to specific target graphs in various scenarios.
Tests
The code includes tests to ensure the correctness of the "induced_minor" function. These tests cover different use cases, including scenarios where no induced minor exists.
Documentation
The documentation will be updated in a separate PR to include information about the new "induced_minor" function.
📝 Checklist
⌛ Dependencies
N/A (There are no open PRs that this PR depends on.)