# HG changeset patch
# User Nathann Cohen <nathann.cohen@gmail.com>
# Date 1259138586 -3600
# Node ID 8ca939870ef50e8f8ec3d94a0b9508d32489867c
# Parent 18a4ec53e18a7dd51639dc0ea4f3bd9541430e32
trac 7527: Adds graph_coloring to the reference manual, plus docstring fixes
diff -r 18a4ec53e18a -r 8ca939870ef5 doc/en/reference/graphs.rst
a
|
b
|
|
6 | 6 | |
7 | 7 | sage/graphs/cliquer |
8 | 8 | sage/graphs/graph |
| 9 | sage/graphs/graph_coloring |
9 | 10 | sage/graphs/graph_generators |
10 | 11 | sage/graphs/graph_database |
11 | 12 | sage/graphs/graph_list |
diff -r 18a4ec53e18a -r 8ca939870ef5 sage/graphs/graph_coloring.py
a
|
b
|
|
22 | 22 | from graph_generators import GraphGenerators |
23 | 23 | |
24 | 24 | def all_graph_colorings(G,n,count_only=False): |
25 | | """ |
26 | | Computes all n-colorings of the graph G by casting the graph |
| 25 | r""" |
| 26 | Computes all `n`-colorings of the graph `G` by casting the graph |
27 | 27 | coloring problem into an exact cover problem, and passing this |
28 | 28 | into an implementation of the Dancing Links algorithm described |
29 | 29 | by Knuth (who attributes the idea to Hitotumatu and Noshita). |
30 | 30 | |
31 | 31 | The construction works as follows: |
32 | 32 | (columns) |
33 | | * The first |V| columns correspond to a vertex -- a 1 in this |
| 33 | * The first `|V|` columns correspond to a vertex -- a `1` in this |
34 | 34 | column indicates that that vertex has a color. |
35 | | * After those |V| columns, we add n*|E| columns -- a 1 in |
| 35 | * After those `|V|` columns, we add `n*|E|` columns -- a `1` in |
36 | 36 | these columns indicate that a particular edge is |
37 | 37 | incident to a vertex with a certain color. |
38 | 38 | |
39 | 39 | (rows) |
40 | | * For each vertex, add n rows; one for each color c. Place |
41 | | a 1 in the column corresponding to the vertex, and a 1 |
| 40 | * For each vertex, add `n` rows; one for each color `c`. Place |
| 41 | a `1` in the column corresponding to the vertex, and a `1` |
42 | 42 | in the appropriate column for each edge incident to the |
43 | 43 | vertex, indicating that that edge is incident to the |
44 | | color c. |
45 | | * If n > 2, the above construction cannot be exactly covered |
| 44 | color `c`. |
| 45 | * If `n > 2`, the above construction cannot be exactly covered |
46 | 46 | since each edge will be incident to only two vertices |
47 | | (and hence two colors) - so we add n*|E| rows, each one |
48 | | containing a 1 for each of the n*|E| columns. These |
| 47 | (and hence two colors) - so we add `n*|E|` rows, each one |
| 48 | containing a `1` for each of the `n*|E|` columns. These |
49 | 49 | get added to the cover solutions "for free" during the |
50 | 50 | backtracking. |
51 | 51 | |
52 | | Note that this construction results in n*|V| + 2*n*|E| + n*|E| |
| 52 | Note that this construction results in `n*|V| + 2*n*|E| + n*|E|` |
53 | 53 | entries in the matrix. The Dancing Links algorithm uses a |
54 | | sparse representation, so if the graph is simple, |E| <= |V|^2 |
55 | | and n <= |V|, this construction runs in O(|V|^3) time. |
| 54 | sparse representation, so if the graph is simple, `|E| \leq |V|^2` |
| 55 | and `n <= |V|`, this construction runs in `O(|V|^3)` time. |
56 | 56 | Back-conversion to a coloring solution is a simple scan of the |
57 | | solutions, which will contain |V| + (n-2)*|E| entries, so |
58 | | runs in O(|V|^3) time also. For most graphs, the conversion |
| 57 | solutions, which will contain `|V| + (n-2)*|E|` entries, so |
| 58 | runs in `O(|V|^3)` time also. For most graphs, the conversion |
59 | 59 | will be much faster -- for example, a planar graph will be |
60 | | transformed for 4-coloring in linear time since |E| = O(|V|). |
| 60 | transformed for `4`-coloring in linear time since `|E| = O(|V|)`. |
61 | 61 | |
62 | 62 | REFERENCES: |
63 | 63 | http://www-cs-staff.stanford.edu/~uno/papers/dancing-color.ps.gz |
64 | 64 | |
65 | | EXAMPLES: |
| 65 | EXAMPLES:: |
| 66 | |
66 | 67 | sage: from sage.graphs.graph_coloring import all_graph_colorings |
67 | 68 | sage: G = Graph({0:[1,2,3],1:[2]}) |
68 | 69 | sage: n = 0 |
… |
… |
|
78 | 79 | sage: print "G has %s 3-colorings."%n |
79 | 80 | G has 12 3-colorings. |
80 | 81 | |
81 | | TESTS: |
| 82 | TESTS:: |
| 83 | |
82 | 84 | sage: G = Graph({0:[1,2,3],1:[2]}) |
83 | 85 | sage: for C in all_graph_colorings(G,0): print C |
84 | 86 | sage: for C in all_graph_colorings(G,-1): print C |
… |
… |
|
172 | 174 | return C.values() |
173 | 175 | |
174 | 176 | def number_of_n_colorings(G,n): |
175 | | """ |
176 | | Given a graph G and a natural number n, returns the number of |
177 | | n-colorings of the graph. |
| 177 | r""" |
| 178 | Given a graph `G` and a natural number `n`, returns the number of |
| 179 | `n`-colorings of the graph. |
178 | 180 | |
179 | | EXAMPLES: |
| 181 | EXAMPLES:: |
| 182 | |
180 | 183 | sage: from sage.graphs.graph_coloring import number_of_n_colorings |
181 | 184 | sage: G = Graph({0:[1,2,3],1:[2]}) |
182 | 185 | sage: number_of_n_colorings(G,3) |
… |
… |
|
198 | 201 | return m |
199 | 202 | |
200 | 203 | def numbers_of_colorings(G): |
201 | | """ |
202 | | Returns the number of n-colorings of the graph G for n from |
203 | | 0 to |V|. |
| 204 | r""" |
| 205 | Returns the number of `n`-colorings of the graph `G` for `n` from |
| 206 | `0` to `|V|`. |
204 | 207 | |
205 | | EXAMPLES: |
| 208 | EXAMPLES:: |
| 209 | |
206 | 210 | sage: from sage.graphs.graph_coloring import numbers_of_colorings |
207 | 211 | sage: G = Graph({0:[1,2,3],1:[2]}) |
208 | 212 | sage: numbers_of_colorings(G) |
… |
… |
|
212 | 216 | return [number_of_n_colorings(G,i) for i in range(0,o+1)] |
213 | 217 | |
214 | 218 | def chromatic_number(G): |
215 | | """ |
| 219 | r""" |
216 | 220 | Returns the minimal number of colors needed to color the |
217 | | vertices of the graph G. |
| 221 | vertices of the graph `G`. |
218 | 222 | |
219 | | EXAMPLES: |
| 223 | EXAMPLES:: |
| 224 | |
220 | 225 | sage: from sage.graphs.graph_coloring import chromatic_number |
221 | 226 | sage: G = Graph({0:[1,2,3],1:[2]}) |
222 | 227 | sage: chromatic_number(G) |
… |
… |
|
645 | 650 | return g |
646 | 651 | |
647 | 652 | class Test: |
648 | | """ |
| 653 | r""" |
649 | 654 | This class performs randomized testing for all_graph_colorings. |
650 | 655 | Since everything else in this file is derived from |
651 | 656 | all_graph_colorings, this is a pretty good randomized tester for |
652 | | the entire file. Note that for a graph G, G.chromatic_polynomial() |
| 657 | the entire file. Note that for a graph `G`, ``G.chromatic_polynomial()`` |
653 | 658 | uses an entirely different algorithm, so we provide a good, |
654 | 659 | independent test. |
655 | 660 | """ |
656 | 661 | |
657 | 662 | def random(self,tests = 1000): |
658 | | """ |
659 | | Calls self.random_all_graph_colorings(). In the future, if |
| 663 | r""" |
| 664 | Calls ``self.random_all_graph_colorings()``. In the future, if |
660 | 665 | other methods are added, it should call them, too. |
661 | 666 | |
662 | | TESTS: |
| 667 | TESTS:: |
| 668 | |
663 | 669 | sage: from sage.graphs.graph_coloring import Test |
664 | 670 | sage: Test().random(1) |
665 | 671 | """ |
666 | 672 | self.random_all_graph_colorings(tests) |
667 | 673 | |
668 | 674 | def random_all_graph_colorings(self,tests = 1000): |
669 | | """ |
| 675 | r""" |
670 | 676 | Verifies the results of all_graph_colorings in three ways: |
671 | 677 | 1) all colorings are unique |
672 | | 2) number of m-colorings is P(m) (where P is the chromatic |
| 678 | 2) number of m-colorings is `P(m)` (where `P` is the chromatic |
673 | 679 | polynomial of the graph being tested) |
674 | 680 | 3) colorings are valid -- that is, that no two vertices of |
675 | 681 | the same color share an edge. |
676 | 682 | |
677 | | TESTS: |
| 683 | TESTS:: |
| 684 | |
678 | 685 | sage: from sage.graphs.graph_coloring import Test |
679 | 686 | sage: Test().random_all_graph_colorings(1) |
680 | 687 | """ |