# HG changeset patch
# User Eviatar Bach <eviatarbach@gmail.com>
# Date 1294875715 28800
# Node ID 50830205b2f235e4424424c4538f54710dea5540
# Parent 1896a0311e7061fac80de9805e0258200b23336e
#10560: Fix spelling errors in generic_graph.py
diff -r 1896a0311e70 -r 50830205b2f2 sage/graphs/generic_graph.py
a
|
b
|
|
1958 | 1958 | |
1959 | 1959 | def eulerian_orientation(self): |
1960 | 1960 | r""" |
1961 | | Returns a DiGraph which is an eulerian orientation of the current graph. |
1962 | | |
1963 | | An eulerian graph being a graph such that any vertex has an even degree, |
1964 | | an eulerian orientation of a graph is an orientation of its edges such |
| 1961 | Returns a DiGraph which is an Eulerian orientation of the current graph. |
| 1962 | |
| 1963 | An Eulerian graph being a graph such that any vertex has an even degree, |
| 1964 | an Eulerian orientation of a graph is an orientation of its edges such |
1965 | 1965 | that each vertex `v` verifies `d^+(v)=d^-(v)=d(v)/2`, where `d^+` and |
1966 | 1966 | `d^-` respectively represent the out-degree and the in-degree of a vertex. |
1967 | 1967 | |
1968 | | If the graph is not eulerian, the orientation verifies for any vertex `v` |
| 1968 | If the graph is not Eulerian, the orientation verifies for any vertex `v` |
1969 | 1969 | that `| d^+(v)-d^-(v) | \leq 1`. |
1970 | 1970 | |
1971 | 1971 | ALGORITHM: |
… |
… |
|
1981 | 1981 | EXAMPLES: |
1982 | 1982 | |
1983 | 1983 | The CubeGraph with parameter 4, which is regular of even degree, has an |
1984 | | eulerian orientation such that `d^+=d^-`:: |
| 1984 | Eulerian orientation such that `d^+=d^-`:: |
1985 | 1985 | |
1986 | 1986 | sage: g=graphs.CubeGraph(4) |
1987 | 1987 | sage: g.degree() |
… |
… |
|
3853 | 3853 | |
3854 | 3854 | def vertex_cut(self, s, t, value_only=True, vertices=False, solver=None, verbose=0): |
3855 | 3855 | r""" |
3856 | | Returns a minimum vertex cut between non adjacent vertices `s` and `t` |
| 3856 | Returns a minimum vertex cut between non-adjacent vertices `s` and `t` |
3857 | 3857 | represented by a list of vertices. |
3858 | 3858 | |
3859 | | A vertex cut between two non adjacent vertices is a set `U` |
| 3859 | A vertex cut between two non-adjacent vertices is a set `U` |
3860 | 3860 | of vertices of self such that the graph obtained by removing |
3861 | 3861 | `U` from self is disconnected. For more information, see the |
3862 | 3862 | `Wikipedia article on cuts |
… |
… |
|
4160 | 4160 | only the size of a minimum vertex cover is returned. Otherwise, |
4161 | 4161 | a minimum vertex cover is returned as a list of vertices. |
4162 | 4162 | |
4163 | | - ``log`` -- non negative integer (default: ``0``). Set the level |
| 4163 | - ``log`` -- non-negative integer (default: ``0``). Set the level |
4164 | 4164 | of verbosity you want from the linear program solver. Since the |
4165 | 4165 | problem of computing a vertex cover is `NP`-complete, its solving |
4166 | 4166 | may take some time depending on the graph. A value of 0 means |
… |
… |
|
4380 | 4380 | |
4381 | 4381 | Given a graph (resp. a digraph) `G` with weighted edges, |
4382 | 4382 | the traveling salesman problem consists in finding a |
4383 | | hamiltonian cycle (resp. circuit) of the graph of |
| 4383 | Hamiltonian cycle (resp. circuit) of the graph of |
4384 | 4384 | minimum cost. |
4385 | 4385 | |
4386 | 4386 | This TSP is one of the most famous NP-Complete problems, |
… |
… |
|
4430 | 4430 | |
4431 | 4431 | EXAMPLES: |
4432 | 4432 | |
4433 | | The Heawood graph is known to be hamiltonian:: |
| 4433 | The Heawood graph is known to be Hamiltonian:: |
4434 | 4434 | |
4435 | 4435 | sage: g = graphs.HeawoodGraph() |
4436 | 4436 | sage: tsp = g.traveling_salesman_problem() |
… |
… |
|
4453 | 4453 | True |
4454 | 4454 | |
4455 | 4455 | On the other hand, the Petersen Graph is known not to |
4456 | | be hamiltonian:: |
| 4456 | be Hamiltonian:: |
4457 | 4457 | |
4458 | 4458 | sage: g = graphs.PetersenGraph() |
4459 | 4459 | sage: tsp = g.traveling_salesman_problem() |
4460 | 4460 | Traceback (most recent call last): |
4461 | 4461 | ... |
4462 | | ValueError: The given graph is not hamiltonian |
| 4462 | ValueError: The given graph is not Hamiltonian |
4463 | 4463 | |
4464 | 4464 | One easy way to change is is obviously to add to this |
4465 | | graph the edges corresponding to a hamiltonian cycle. |
| 4465 | graph the edges corresponding to a Hamiltonian cycle. |
4466 | 4466 | |
4467 | 4467 | If we do this by setting the cost of these new edges |
4468 | 4468 | to `2`, while the others are set to `1`, we notice |
… |
… |
|
4609 | 4609 | return tsp |
4610 | 4610 | |
4611 | 4611 | except MIPSolverException: |
4612 | | raise ValueError("The given graph is not hamiltonian") |
| 4612 | raise ValueError("The given graph is not Hamiltonian") |
4613 | 4613 | |
4614 | 4614 | |
4615 | 4615 | def hamiltonian_cycle(self, algorithm='tsp' ): |
4616 | 4616 | r""" |
4617 | | Returns a hamiltonian cycle/circuit of the current graph/digraph |
4618 | | |
4619 | | A graph (resp. digraph) is said to be hamiltonian |
| 4617 | Returns a Hamiltonian cycle/circuit of the current graph/digraph |
| 4618 | |
| 4619 | A graph (resp. digraph) is said to be Hamiltonian |
4620 | 4620 | if it contains as a subgraph a cycle (resp. a circuit) |
4621 | 4621 | going through all the vertices. |
4622 | 4622 | |
4623 | | Computing a hamiltonian cycle/circuit being NP-Complete, |
| 4623 | Computing a Hamiltonian cycle/circuit being NP-Complete, |
4624 | 4624 | this algorithm could run for some time depending on |
4625 | 4625 | the instance. |
4626 | 4626 | |
… |
… |
|
4636 | 4636 | |
4637 | 4637 | OUTPUT: |
4638 | 4638 | |
4639 | | If using the 'tsp' algorithm, returns a hamiltonian cycle/circuit if it |
| 4639 | If using the 'tsp' algorithm, returns a Hamiltonian cycle/circuit if it |
4640 | 4640 | exists; otherwise, raises a ``ValueError`` exception. If using the |
4641 | 4641 | 'backtrack' algorithm, returns a pair (B,P). If B is True then P is a |
4642 | | hamiltonian cycle and if B is False, P is a longest path found by the |
4643 | | algorithm. Observe that if B is False, the graph may still be hamiltonian. |
| 4642 | Hamiltonian cycle and if B is False, P is a longest path found by the |
| 4643 | algorithm. Observe that if B is False, the graph may still be Hamiltonian. |
4644 | 4644 | The 'backtrack' algorithm is only implemented for undirected |
4645 | 4645 | graphs. |
4646 | 4646 | |
… |
… |
|
4651 | 4651 | |
4652 | 4652 | NOTE: |
4653 | 4653 | |
4654 | | This function, as ``is_hamiltonian``, computes a hamiltonian |
| 4654 | This function, as ``is_hamiltonian``, computes a Hamiltonian |
4655 | 4655 | cycle if it exists : the user should *NOT* test for |
4656 | | hamiltonicity using ``is_hamiltonian`` before calling this |
| 4656 | Hamiltonicity using ``is_hamiltonian`` before calling this |
4657 | 4657 | function, as it would result in computing it twice. |
4658 | 4658 | |
4659 | 4659 | The backtrack algorithm is only implemented for undirected graphs. |
4660 | 4660 | |
4661 | 4661 | EXAMPLES: |
4662 | 4662 | |
4663 | | The Heawood Graph is known to be hamiltonian :: |
| 4663 | The Heawood Graph is known to be Hamiltonian :: |
4664 | 4664 | |
4665 | 4665 | sage: g = graphs.HeawoodGraph() |
4666 | 4666 | sage: g.hamiltonian_cycle() |
… |
… |
|
4672 | 4672 | sage: g.hamiltonian_cycle() |
4673 | 4673 | Traceback (most recent call last): |
4674 | 4674 | ... |
4675 | | ValueError: The given graph is not hamiltonian |
| 4675 | ValueError: The given graph is not Hamiltonian |
4676 | 4676 | |
4677 | 4677 | Now, using the backtrack algorithm in the Heawood graph :: |
4678 | 4678 | |
… |
… |
|
4686 | 4686 | sage: G.hamiltonian_cycle(algorithm='backtrack') |
4687 | 4687 | (False, [6, 8, 5, 0, 1, 2, 7, 9, 4, 3]) |
4688 | 4688 | |
4689 | | Finally, we test the algorithm in a cube graph, which is hamiltonian :: |
| 4689 | Finally, we test the algorithm in a cube graph, which is Hamiltonian :: |
4690 | 4690 | |
4691 | 4691 | sage: G=graphs.CubeGraph(3) |
4692 | 4692 | sage: G.hamiltonian_cycle(algorithm='backtrack') |
… |
… |
|
4699 | 4699 | try: |
4700 | 4700 | return self.traveling_salesman_problem(weighted = False) |
4701 | 4701 | except MIPSolverException: |
4702 | | raise ValueError("The given graph is not hamiltonian") |
| 4702 | raise ValueError("The given graph is not Hamiltonian") |
4703 | 4703 | elif algorithm=='backtrack': |
4704 | 4704 | from sage.graphs.generic_graph_pyx import find_hamiltonian as fh |
4705 | 4705 | return fh( self ) |
… |
… |
|
11910 | 11910 | def layout_tree(self, tree_orientation = "down", tree_root = None, dim = 2, **options): |
11911 | 11911 | """ |
11912 | 11912 | Computes an ordered tree layout for this graph, which should |
11913 | | be a tree (no non oriented cycles). |
| 11913 | be a tree (no non-oriented cycles). |
11914 | 11914 | |
11915 | 11915 | INPUT: |
11916 | 11916 | |
… |
… |
|
13964 | 13964 | |
13965 | 13965 | def is_hamiltonian(self): |
13966 | 13966 | r""" |
13967 | | Tests whether the current graph is hamiltonian |
13968 | | |
13969 | | A graph (resp. digraph) is said to be hamiltonian |
| 13967 | Tests whether the current graph is Hamiltonian. |
| 13968 | |
| 13969 | A graph (resp. digraph) is said to be Hamiltonian |
13970 | 13970 | if it contains as a subgraph a cycle (resp. a circuit) |
13971 | 13971 | going through all the vertices. |
13972 | 13972 | |
13973 | | Testing for hamiltonicity being NP-Complete, this |
| 13973 | Testing for Hamiltonicity being NP-Complete, this |
13974 | 13974 | algorithm could run for some time depending on |
13975 | 13975 | the instance. |
13976 | 13976 | |
… |
… |
|
13980 | 13980 | |
13981 | 13981 | OUTPUT: |
13982 | 13982 | |
13983 | | Returns ``True`` if a hamiltonian cycle/circuit exists, and |
| 13983 | Returns ``True`` if a Hamiltonian cycle/circuit exists, and |
13984 | 13984 | ``False`` otherwise. |
13985 | 13985 | |
13986 | 13986 | NOTE: |
13987 | 13987 | |
13988 | 13988 | This function, as ``hamiltonian_cycle`` and |
13989 | | ``traveling_salesman_problem``, computes a hamiltonian |
| 13989 | ``traveling_salesman_problem``, computes a Hamiltonian |
13990 | 13990 | cycle if it exists : the user should *NOT* test for |
13991 | | hamiltonicity using ``is_hamiltonian`` before calling |
| 13991 | Hamiltonicity using ``is_hamiltonian`` before calling |
13992 | 13992 | ``hamiltonian_cycle`` or ``traveling_salesman_problem`` |
13993 | 13993 | as it would result in computing it twice. |
13994 | 13994 | |
13995 | 13995 | EXAMPLES: |
13996 | 13996 | |
13997 | | The Heawood Graph is known to be hamiltonian :: |
| 13997 | The Heawood Graph is known to be Hamiltonian :: |
13998 | 13998 | |
13999 | 13999 | sage: g = graphs.HeawoodGraph() |
14000 | 14000 | sage: g.is_hamiltonian() |