# Ticket #14434: trac_14434-doctest.patch

File trac_14434-doctest.patch, 4.4 KB (added by ncohen, 6 years ago)
• ## sage/graphs/generic_graph.py

```# HG changeset patch
# User Nathann Cohen <nathann.cohen@gmail.com>
# Date 1370370929 -7200
# Node ID c68e0561b7b2dc0e52dbbaa778314a2da5c1da3a
# Parent  5c50292d1c5d93ee90297c3fdcb182bb9436819e
Implement feedback_vertex_set for graphs -- broken doctest

diff --git a/sage/graphs/generic_graph.py b/sage/graphs/generic_graph.py```
 a height = p.new_variable(dim = 2) # cut[e] represents whether e is in the cut cut = p.new_variable() cut = p.new_variable(binary = True) # Reorder R = lambda x,y : (x,y) if x .5, self.edge_iterator()) else: return filter(lambda (u,v,l) : cut[R(u,v)] > .5, self.edge_iterator()) return filter(lambda (u,v,l) : cut[u,v] == 1, self.edge_iterator()) else: return filter(lambda (u,v,l) : cut[R(u,v)] ==1, self.edge_iterator()) def max_cut(self, value_only=True, use_edge_labels=False, vertices=False, solver=None, verbose=0): vertex_used = p.get_values(vertex_used) if self._directed: g = self.subgraph( vertices=(v for v in self if vertex_used[v] >= 0.5), vertices=(v for v in self if vertex_used[v] == 1), edges=((u,v,l) for u, v, l in self.edges() if edge_used[(u,v)] >= 0.5)) if edge_used[(u,v)] == 1)) else: g = self.subgraph( vertices=(v for v in self if vertex_used[v] >= 0.5), vertices=(v for v in self if vertex_used[v] == 1), edges=((u,v,l) for u, v, l in self.edges() if f_edge_used(u,v) >= 0.5)) if f_edge_used(u,v) == 1)) if use_edge_labels: return sum(map(weight, g.edge_labels())), g else: # We build the DiGraph representing the current solution h = DiGraph() for u,v,l in g.edges(): if p.get_values(b[u][v]) > .5: if p.get_values(b[u][v]) == 1: h.add_edge(u,v,l) # If there is only one circuit, we are done ! # We build the DiGraph representing the current solution h = Graph() for u,v,l in g.edges(): if p.get_values(B(u,v)) > .5: if p.get_values(B(u,v)) == 1: h.add_edge(u,v,l) # If there is only one circuit, we are done ! The necessary example:: sage: g = graphs.PetersenGraph() sage: g.feedback_vertex_set() [1, 3, 5] sage: fvs = g.feedback_vertex_set() sage: len(fvs) 3 sage: g.delete_vertices(fvs) sage: g.is_forest() True In a digraph built from a graph, any edge is replaced by arcs going in the two opposite directions, thus creating a cycle of length two. # Building the graph without the edges removed by the LP h = self.subgraph(vertices = [v for v in self if p.get_values(b[v]) < .5]) [v for v in self if p.get_values(b[v]) == 0]) # Is the graph acyclic ? if self.is_directed(): else: # listing the edges contained in the MFVS return [v for v in self if p.get_values(b[v]) > .5] return [v for v in self if p.get_values(b[v]) == 1] else: