# HG changeset patch
# User Jason Grout <jasonsage@creativetrax.com>
# Date 1253179860 18000
# Node ID 013f7f3feb56a414172a14950c44b7fa80986c36
# Parent 3369a618ad1987b1658e4b34394b3466d89d93da
Typos, punctuation fixes, and minor editing.
diff r 3369a618ad19 r 013f7f3feb56 doc/en/tutorial/tour_graphtheory.rst
a

b


5  5   
6  6  
7  7  A Graph is a set of vertices connected by edges 
8   (see `Wikipedia <http://en.wikipedia.org/wiki/Graph_(mathematics)>`_ ) 
 8  (see `Wikipedia <http://en.wikipedia.org/wiki/Graph_(mathematics)>`_). 
9  9  
10   One can very easily create a graph in sage by typing :: 
 10  One can very easily create a graph in sage by typing:: 
11  11  
12  12  sage: g=Graph() 
13  13  
… 
… 

17  17  sage: g 
18  18  Graph on 0 vertices 
19  19  
20   This graph is not very interesting as it is by default the empty graph.. But 
21  20  Sage contains a large collection of predefined graph classes 
22   that can be listed this way : 
 21  that can be listed this way: 
23  22  
24   * type in Sage : ``graphs.`` 
25   ( do not press "Enter", and do not forget the final "." ) 
26   * hit "tabulation" two times in a row 
 23  1. type in Sage: ``graphs.`` (do not press "Enter", and do not 
 24  forget the final "."), then 
 25  
 26  2. hit the tab key two times in a row 
27  27  
28  28  You will see the list of methods defined in the class ``graphs``, 
29   all of which generate graphs you can play with ! 
 29  all of which generate graphs you can play with! 
30  30  
31   If you want to see what they look like, begin this way :: 
 31  If you want to see what they look like, you can plot the graphs:: 
32  32  
33  33  sage: g=graphs.PetersenGraph() 
34  34  sage: g.plot() 
35   
36   or:: 
37   
38  35  sage: g=graphs.ChvatalGraph() 
39  36  sage: g.plot() 
40  37  
41   If you are curious about what these graphs are, for example 
42   if you wonder what ``RandomGNP`` actually is, you but have to type :: 
 38  If you are curious about what these graphs are, for example, if you 
 39  wonder what ``RandomGNP`` actually is, use the standard Sage help:: 
43  40  
44  41  sage: graphs.RandomGNP? 
45  42  
46  43  Once you have defined the graph you want, you can begin 
47   to work on it by using thealmost 200 functions on graphs 
48   in the Sage library !If your graph is named ``g``, you can 
49   list these functions as previously this way : 
 44  to work on it by using the hundreds of functions for graphs 
 45  in the Sage library. If your graph is named ``g``, you can 
 46  list these functions using tab completion: 
50  47  
51   * type in Sage : ``g.`` 
52   ( do not press "Enter", and do not forget the final "." ) 
53   * hit "tabulation" two times in a row 
 48  1. type in Sage: ``g.`` (do not press "Enter", and do not forget 
 49  the final "."), then 
 50  
 51  2. hit the tab key two times in a row 
54  52  
55  53  As usual, you can get some information about what these 
56   functions do by typing( if you want to know about the ``diameter()`` 
57   method ):: 
 54  functions do by typing (if you want to know about the :meth:`diameter()` 
 55  method):: 
58  56  
59  57  sage: g.diameter? 
60  58  
61   If you have defined a graph ``g`` having several connected 
62   components ( = which is notconnected... Type ``g.is_connected()`` 
63   to know whether your graph is ), you canprint each one of 
64   its connected components with only two lines : 
 59  To find out if a graph is connected, use :meth:`is_connected`:: 
65  60  
66   ( if you do not have such a graph G, here is one 
67   for free : ``g=graphs.RandomGNP(30,.05)`` ):: 
 61  sage: g=graphs.RandomGNP(100, 0.01) 
 62  sage: g.is_connected() 
 63  
 64  You can plot each connected component separately:: 
68  65  
69  66  sage: for component in g.connected_components(): 
70  67  ... g.subgraph(component).plot() 
… 
… 

75  72  
76  73  The first elements you want to find in a graph are its vertices 
77  74  and its edges. The vertices of a graph ``g`` are returned 
78   by ``g.vertices()`` and its edges are returned by ``g.edges()``. 
 75  by :meth:`g.vertices` and its edges are returned by :meth:`g.edges`. 
79  76  In Sage, the edges of a graph are represented as tuples ``(u,v,l)`` 
80  77  where ``u`` and ``v`` are vertices, and ``l`` a label attached 
81   to the edge ( this label can be of any type, even though 
82   several functions expect it to be a real value ). 
 78  to the edge (this label can be of any type, even though 
 79  several functions expect it to be a real value). 
83  80  
84   The methods ``g.order()`` and ``g.size()`` respectively return the number 
 81  The methods :meth:`g.order` and :meth:`g.size` respectively return the number 
85  82  of vertices and the number of edges. 
86  83  
87  84  At any moment, you can display the adjacency matrix of you graph 
88   by using the method ``g.am()`` and plot the graph with ``g.plot()``. 
 85  by using the method :meth:`g.adjacency_matrix` and plot the graph with :meth:`g.plot`. 
89  86  
90   What interesting things can you do with Graphs in Sage ? 
 87  What interesting things can you do with Graphs in Sage? 
91  88   
92  89  
93   Computing matching 
 90  Compute maximum matchings 
94  91  ^^^^^^^^^^^^^^^^^^^ 
95  92  
96   Maximum Matching a polynomial problem in Graph Theory, and 
97   have a thousand of different applications. This, just to 
98   mean that the following list of examples is not (yet?) exhaustive. 
 93  Maximum Matchings, a polynomial problem in Graph Theory, has many 
 94  different applications. In the subsections that follow, we will look 
 95  at a few of these applications. 
99  96  
100   For more information on matching : 
101   `Matching <http://en.wikipedia.org/wiki/Matching>`_ 
 97  For more information on matchings, see `Matching 
 98  <http://en.wikipedia.org/wiki/Matching>`_. 
102  99  
103  100  Small company 
104  101  """""""""""""""" 
105  102  
106  103  Let us say that you are in charge of a small company with 4 employees 
107  104  `\{e_1,e_2,e_3,e_4\}` and have several tasks `\{t_1,t_2,t_3,t_4\}` 
108   to give them. Unfortunately, no worker is skilled enough to do all of them : 
 105  to give them. Unfortunately, no worker is skilled enough to do all of them: 
109  106  
110  107  * `e_1` can do `t_1, t_3, t_4` 
111  108  * `e_2` can do `t_1, t_3, t_5` 
… 
… 

117  114  You are lucky if you do not know how to solve this problem manually, 
118  115  because this is typically an application of matching in graphs 
119  116  (and if you have found the solution, I assure you it gets harder 
120   when you have more of them) ! 
 117  when you have more of employees). 
121  118  
122   To solve this problem, you but have to create the graph corresponding 
123   to the information above, and solve the matching problem ::: 
 119  To solve this problem, create the graph corresponding to the 
 120  information above, and solve the matching problem:: 
124  121  
125  122  sage: g=Graph({"e0":['t1', 't3', 't4'],"e1":['t1', 't3', 
126  123  't5'],"e2":['t1', 't2', 't3', 't4', 't5'], 
… 
… 

129  126  [('e2', 't4', None), ('e3', 't5', None), ('e0', 't3', None), 
130  127  ('e1', 't1', None), ('e4', 't2', None)] 
131  128  
132   If you prefer to "see" the result, you can also type :: 
 129  If you prefer to "see" the result, you can also type:: 
133  130  
134  131  sage: g.plot(edge_colors={"red":g.max_matching()}) 
135  132  
136   Sage 1 : Pain 0 
 133  Wasn't that simple? 
137  134  
138  135  Summer camp 
139  136  """""""""""" 
140  137  
141   You know have under your responsibility 5 rooms and 10 children 
142   `\{c_0,...,c_9\}`. You need to decide those of them who will 
 138  You know have under your responsibility five rooms and ten children 
 139  `\{c_0,...,c_9\}`. You need to decide which of them will 
143  140  sleep in the same rooms, but you do not want two of them to be 
144  141  together if they do not like each other or if you expect trouble 
145   from the pair... Here are the constraints : 
 142  from the pair. Here are the constraints: 
146  143  
147  144  * `c_0` can sleep with `c_5` 
148  145  * `c_1` can sleep with `c_5, c_8` 
… 
… 

153  150  * `c_6` can sleep with `c_7, c_9` 
154  151  * `c_7` can sleep with `c_9` 
155  152  
156   As previously, this defines a graph whose adjacency matrix has 
157   just been defined ! You now but have to create it in Sage, and 
158   look for a maximum matching...:: 
 153  As done previously, this defines a graph. Now create it in Sage, and 
 154  ask for a maximum matching:: 
159  155  
160  156  sage: g=Graph({'c0':['c5'],'c1':['c5', 'c8'],'c2':['c3', 
161  157  'c8', 'c9'],'c3':['c9'],'c4':['c9'],'c5':['c9'], 
… 
… 

164  160  [('c0', 'c5', None), ('c6', 'c7', None), ('c2', 'c3', None), 
165  161  ('c4', 'c9', None), ('c1', 'c8', None)] 
166  162  
167   If you prefer to "see" the result, you can also type :: 
 163  If you prefer to "see" the result, you can also type:: 
168  164  
169  165  sage: g.plot(edge_colors={"red":g.max_matching()}) 
170  166  
171   And this is another problem Sage solved for you ! 
172   
173   
 167  And this is another problem Sage solved for you! 
174  168  
175  169  Vertex coloring 
176  170  ^^^^^^^^^^^^^^^ 
177  171  
178  172  You are in front of a map of Western Europe that you would like 
179  173  to color. Obviously, you can not color both France and Italy 
180   with the same color as they have a common boundary, and you would 
181   not like to mix the two.. Actually, you want to color : 
 174  with the same color, as they have a common boundary, and you would 
 175  not like to mix the two. Actually, you want to color: 
182  176  
183  177  * Austria 
184  178  * Belgium 
… 
… 

194  188  * United Kingdom 
195  189  
196  190  And would like to know how many colors you need, and how to color 
197   them. Well, as Sage was specially built to help you solve this 
198   kind of tremendously exciting questions, here is the way to solve them : 
 191  them. Well, as Sage was especially built to help you solve this 
 192  kind of tremendously exciting question, here is the way to solve them: 
199  193  
200   * First, create the graph of Western Europe in Sage 
201   * Use the ``vertex_coloring()`` method 
 194  1. Create the graph of Western Europe in Sage 
 195  2. Use the :meth:`vertex_coloring` method 
202  196  
203   In Sage :: 
 197  In Sage:: 
204  198  
205  199  sage: g=Graph({"France":["Italy","Spain","Swiss","Luxembourg","Belgium", 
206  200  "Germany","Austria"], 
… 
… 

216  210  ['Belgium', 'Swiss'], 
217  211  ['Luxembourg', 'Italy']] 
218  212  
219   You can now look for your pens. 4 of them :) 
 213  You can now look for your pensfour of them. 
220  214  
221  215  For more information on graph 
222   coloring : `Graph coloring <http://en.wikipedia.org/wiki/Graph_coloring>`_ 
 216  coloring, see `Graph coloring <http://en.wikipedia.org/wiki/Graph_coloring>`_. 
223  217  
224   For more informations on why it could not have required more pens : 
225   `Four color theorem <http://en.wikipedia.org/wiki/Four_color_theorem>`_ 
 218  For more informations on why it could not have required more pens, see the 
 219  `Four color theorem 
 220  <http://en.wikipedia.org/wiki/Four_color_theorem>`_. 
226  221  
227  222  Edge coloring 
228  223  ^^^^^^^^^^^^^^ 
229  224  
230   You are organizing a soccer tournament ( or table tennis if you 
231   do not like soccer, but this is not really relevant ), with 10 
232   different teams that are to play against each other. Besides, 
233   the teams will play every Wednesday and will not be able to play 
234   two times the same day. How can you schedule them in such a way 
235   that the tournament will not last for too long ? 
 225  You are organizing a soccer tournament, with ten different teams that 
 226  are to play against each other. The teams will play every Wednesday 
 227  and will not be able to play two times on the same day. How can you 
 228  schedule them in such a way that the tournament will not last for too 
 229  long? 
236  230  
237  231  This is an easy application of the Edge Coloring problem on a 
238  232  complete graph. If you number your teams as `1,...,10`, here 
239   is how you can obtain your scheduling :: 
 233  is how you can obtain your scheduling:: 
240  234  
241  235  sage: g=graphs.CompleteGraph(10) 
242  236  sage: g.edge_coloring() 
… 
… 

250  244  [(0, 5, None), (1, 3, None), (6, 7, None), (2, 4, None), (8, 9, None)], 
251  245  [(3, 8, None), (4, 6, None), (1, 9, None), (0, 7, None), (2, 5, None)]] 
252  246  
253   And each line you see is the set of games being played on a particular 
254   day. If you prefer to plot the result, try :: 
 247  Each line you see is the set of games being played on a particular 
 248  day. If you prefer to plot the result, try:: 
255  249  
256  250  sage: g.plot(edge_colors=g.edge_coloring(hex_colors=True)) 
257  251  
258   Pretty, isn´t it ? Each day has its own color. 
 252  Pretty, isn't it? Each day has its own color. 
259  253  
260   
261   
262   Two links for more information : 
 254  Two links for more information: 
263  255  * `About edge coloring <http://en.wikipedia.org/wiki/Edge_coloring>`_ 
264  256  * `About the scheduling of tournaments <http://en.wikipedia.org/wiki/Roundrobin>`_ 
265  257  