# HG changeset patch
# User Nathann Cohen
# Date 1249573009 25200
# Node ID 3b89427f6ea37f47e08750fb167dec887edd46f8
# Parent fd8e478f4c9d226e5b6df665da65a8bd82e71c5b
Graph.dominating_set
diff -r fd8e478f4c9d -r 3b89427f6ea3 sage/graphs/graph.py
--- a/sage/graphs/graph.py Wed Dec 16 12:07:35 2009 +0100
+++ b/sage/graphs/graph.py Thu Aug 06 08:36:49 2009 -0700
@@ -3254,6 +3254,90 @@
v = pv
return B, C
+ def dominating_set(self, independent=False, value_only=False,log=0):
+ r"""
+ Returns a minimum dominating set of the graph
+ ( cf. http://en.wikipedia.org/wiki/Dominating_set )
+ represented by the list of its vertices.
+
+ A minimum dominating set `S` of a graph `G` is
+ a set of its vertices of minimal cardinality such
+ that any vertex of `G` is in `S` or has one of its neighbors
+ in `S`.
+
+ As an optimization problem, it can be expressed as :
+
+ .. MATH::
+ \mbox{Minimize : }&\sum_{v\in G} b_v\\
+ \mbox{Such that : }&\forall v \in G, b_v+\sum_{(u,v)\in G.edges()} b_u\geq 1\\
+ &\forall x\in G, b_x\mbox{ is a binary variable}
+
+ INPUT:
+
+ - ``value_only`` (boolean)
+
+ - If ``True``, only the cardinality of a minimum
+ dominating set is returned.
+ - If ``False`` ( default ), a minimum dominating set
+ is returned as the list of its vertices.
+
+ - ``independent`` (boolean)
+ - If ``True``, computes a minimum independent
+ dominating set.
+
+ - ``log`` (integer)
+ As minimum dominating set is a `NP`-complete problem, its
+ solving may take some time depending on the graph. Use
+ ``log`` to define the level of verbosity you want from the linear program solver.
+
+ By default ``log=0``, meaning that there will be no message printed by the solver.
+
+ EXAMPLE:
+
+ A basic illustration on a ``PappusGraph`` ::
+
+ sage: g=graphs.PappusGraph()
+ sage: g.dominating_set(value_only=True) # optional - requires Glpk or COIN-OR/CBC
+ 5.0
+
+ If we build a graph from two disjoint stars, then link their centers
+ we will find a difference between the cardinality of an independent set
+ and a stable independent set ::
+
+ sage: g = 2 * graphs.StarGraph(5)
+ sage: g.add_edge(0,6)
+ sage: len(g.dominating_set()) # optional - requires Glpk or COIN-OR/CBC
+ 2
+ sage: len(g.dominating_set(independent=True)) # optional - requires Glpk or COIN-OR/CBC
+ 6
+
+ """
+ from sage.numerical.mip import MixedIntegerLinearProgram
+ g=self
+ p=MixedIntegerLinearProgram(maximization=False)
+ b=p.new_variable()
+
+ # For any vertex v, one of its neighbors or v itself is in
+ # the minimum dominating set
+ [p.add_constraint(b[v]+sum([b[u] for u in g.neighbors(v)]),min=1) for v in g.vertices()]
+
+
+ if independent:
+ # no two adjacent vertices are in the set
+ [p.add_constraint(b[u]+b[v],max=1) for (u,v) in g.edges(labels=None)]
+
+ # Minimizes the number of vertices used
+ p.set_objective(sum([b[v] for v in g.vertices()]))
+
+ p.set_integer(b)
+
+ if value_only:
+ return p.solve(objective_only=True,log=log)
+ else:
+ obj=p.solve(log=log)
+ b=p.get_values(b)
+ return [v for v in g.vertices() if b[v]==1]
+
### Vertex handlers
def add_vertex(self, name=None):