# Ticket #10560: 15206.patch

File 15206.patch, 9.5 KB (added by eviatarbach, 11 years ago)
• ## sage/graphs/generic_graph.py

# HG changeset patch
# User Eviatar Bach <eviatarbach@gmail.com>
# Date 1294875715 28800
# Node ID 50830205b2f235e4424424c4538f54710dea5540
# Parent  1896a0311e7061fac80de9805e0258200b23336e
Trac 10560: Updated patch.

diff -r 1896a0311e70 -r 50830205b2f2 sage/graphs/generic_graph.py
 a def eulerian_orientation(self): r""" Returns a DiGraph which is an eulerian orientation of the current graph. An eulerian graph being a graph such that any vertex has an even degree, an eulerian orientation of a graph is an orientation of its edges such Returns a DiGraph which is an Eulerian orientation of the current graph. An Eulerian graph being a graph such that any vertex has an even degree, an Eulerian orientation of a graph is an orientation of its edges such that each vertex v verifies d^+(v)=d^-(v)=d(v)/2, where d^+ and d^- respectively represent the out-degree and the in-degree of a vertex. If the graph is not eulerian, the orientation verifies for any vertex v If the graph is not Eulerian, the orientation verifies for any vertex v that | d^+(v)-d^-(v) | \leq 1. ALGORITHM: EXAMPLES: The CubeGraph with parameter 4, which is regular of even degree, has an eulerian orientation such that d^+=d^-:: Eulerian orientation such that d^+=d^-:: sage: g=graphs.CubeGraph(4) sage: g.degree() def vertex_cut(self, s, t, value_only=True, vertices=False, solver=None, verbose=0): r""" Returns a minimum vertex cut between non adjacent vertices s and t Returns a minimum vertex cut between non-adjacent vertices s and t represented by a list of vertices. A vertex cut between two non adjacent vertices is a set U A vertex cut between two non-adjacent vertices is a set U of vertices of self such that the graph obtained by removing U from self is disconnected. For more information, see the Wikipedia article on cuts only the size of a minimum vertex cover is returned. Otherwise, a minimum vertex cover is returned as a list of vertices. - log -- non negative integer (default: 0). Set the level - log -- non-negative integer (default: 0). Set the level of verbosity you want from the linear program solver. Since the problem of computing a vertex cover is NP-complete, its solving may take some time depending on the graph. A value of 0 means Given a graph (resp. a digraph) G with weighted edges, the traveling salesman problem consists in finding a hamiltonian cycle (resp. circuit) of the graph of Hamiltonian cycle (resp. circuit) of the graph of minimum cost. This TSP is one of the most famous NP-Complete problems, EXAMPLES: The Heawood graph is known to be hamiltonian:: The Heawood graph is known to be Hamiltonian:: sage: g = graphs.HeawoodGraph() sage: tsp = g.traveling_salesman_problem() True On the other hand, the Petersen Graph is known not to be hamiltonian:: be Hamiltonian:: sage: g = graphs.PetersenGraph() sage: tsp = g.traveling_salesman_problem() Traceback (most recent call last): ... ValueError: The given graph is not hamiltonian ValueError: The given graph is not Hamiltonian One easy way to change is is obviously to add to this graph the edges corresponding to a hamiltonian cycle. graph the edges corresponding to a Hamiltonian cycle. If we do this by setting the cost of these new edges to 2, while the others are set to 1, we notice return tsp except MIPSolverException: raise ValueError("The given graph is not hamiltonian") raise ValueError("The given graph is not Hamiltonian") def hamiltonian_cycle(self, algorithm='tsp' ): r""" Returns a hamiltonian cycle/circuit of the current graph/digraph A graph (resp. digraph) is said to be hamiltonian Returns a Hamiltonian cycle/circuit of the current graph/digraph A graph (resp. digraph) is said to be Hamiltonian if it contains as a subgraph a cycle (resp. a circuit) going through all the vertices. Computing a hamiltonian cycle/circuit being NP-Complete, Computing a Hamiltonian cycle/circuit being NP-Complete, this algorithm could run for some time depending on the instance. OUTPUT: If using the 'tsp' algorithm, returns a hamiltonian cycle/circuit if it If using the 'tsp' algorithm, returns a Hamiltonian cycle/circuit if it exists; otherwise, raises a ValueError exception. If using the 'backtrack' algorithm, returns a pair (B,P). If B is True then P is a hamiltonian cycle and if B is False, P is a longest path found by the algorithm. Observe that if B is False, the graph may still be hamiltonian. Hamiltonian cycle and if B is False, P is a longest path found by the algorithm. Observe that if B is False, the graph may still be Hamiltonian. The 'backtrack' algorithm is only implemented for undirected graphs. NOTE: This function, as is_hamiltonian, computes a hamiltonian This function, as is_hamiltonian, computes a Hamiltonian cycle if it exists : the user should *NOT* test for hamiltonicity using is_hamiltonian before calling this Hamiltonicity using is_hamiltonian before calling this function, as it would result in computing it twice. The backtrack algorithm is only implemented for undirected graphs. EXAMPLES: The Heawood Graph is known to be hamiltonian :: The Heawood Graph is known to be Hamiltonian :: sage: g = graphs.HeawoodGraph() sage: g.hamiltonian_cycle() sage: g.hamiltonian_cycle() Traceback (most recent call last): ... ValueError: The given graph is not hamiltonian ValueError: The given graph is not Hamiltonian Now, using the backtrack algorithm in the Heawood graph :: sage: G.hamiltonian_cycle(algorithm='backtrack') (False, [6, 8, 5, 0, 1, 2, 7, 9, 4, 3]) Finally, we test the algorithm in a cube graph, which is hamiltonian :: Finally, we test the algorithm in a cube graph, which is Hamiltonian :: sage: G=graphs.CubeGraph(3) sage: G.hamiltonian_cycle(algorithm='backtrack') try: return self.traveling_salesman_problem(weighted = False) except MIPSolverException: raise ValueError("The given graph is not hamiltonian") raise ValueError("The given graph is not Hamiltonian") elif algorithm=='backtrack': from sage.graphs.generic_graph_pyx import find_hamiltonian as fh return fh( self ) def layout_tree(self, tree_orientation = "down", tree_root = None, dim = 2, **options): """ Computes an ordered tree layout for this graph, which should be a tree (no non oriented cycles). be a tree (no non-oriented cycles). INPUT: def is_hamiltonian(self): r""" Tests whether the current graph is hamiltonian A graph (resp. digraph) is said to be hamiltonian Tests whether the current graph is Hamiltonian. A graph (resp. digraph) is said to be Hamiltonian if it contains as a subgraph a cycle (resp. a circuit) going through all the vertices. Testing for hamiltonicity being NP-Complete, this Testing for Hamiltonicity being NP-Complete, this algorithm could run for some time depending on the instance. OUTPUT: Returns True if a hamiltonian cycle/circuit exists, and Returns True if a Hamiltonian cycle/circuit exists, and False otherwise. NOTE: This function, as hamiltonian_cycle and traveling_salesman_problem, computes a hamiltonian traveling_salesman_problem, computes a Hamiltonian cycle if it exists : the user should *NOT* test for hamiltonicity using is_hamiltonian before calling Hamiltonicity using is_hamiltonian before calling hamiltonian_cycle or traveling_salesman_problem` as it would result in computing it twice. EXAMPLES: The Heawood Graph is known to be hamiltonian :: The Heawood Graph is known to be Hamiltonian :: sage: g = graphs.HeawoodGraph() sage: g.is_hamiltonian()