Contact
CoCalc Logo Icon
StoreFeaturesDocsShareSupport News AboutSign UpSign In
| Download
Views: 1428
Kernel: SageMath (stable)
%html <h1>Dictionaries and Graph Theory</h1> <h2>Dictionaries<br /></h2> <p>A <em>dictionary</em> is another builtin datatype. Unlike lists or tuples, which are indexed by a range of numbers, dictionaries are indexed by <em>keys</em>, which can be any immutable type. Strings and numbers can always be keys. Dictionaries are sometimes called "associative arrays" in other programming languages.</p> <p>There are several ways to define dictionaries. Below are three equivalent ways to define a dictionary that pairs 'key1' with 'value1' and 'key2' with 'value2'.</p>
d = {'key1':'value1', 'key2':'value2'} d
d = dict(key1='value1', key2='value2') d
d = dict([('key1','value1'), ('key2','value2')]) d
d = {1:[2,2], 'a':(6,5), 0:{1:2}} d
%html <p>Dictionaries behave as lists, tuples, and strings for several important operations.</p> <p> <table style="text-align: center;" border="0" cellspacing="2" cellpadding="10" align="center"> <tbody> <tr> <td style="text-align: center;"><em><strong>Operation</strong></em></td> <td><em><strong>Syntax for lists</strong></em></td> <td><em><strong>Syntax for dictionaries</strong></em></td> </tr> <tr> <td>Accessing elements<br /></td> <td><strong>L[3]</strong></td> <td><strong>D[3]</strong></td> </tr> <tr> <td>Length</td> <td><strong>len(L)</strong></td> <td><strong>len(D)</strong></td> </tr> <tr> <td>Modifying</td> <td><strong>L[3] = 17</strong></td> <td><strong>D[3] = 17</strong></td> </tr> <tr> <td>Deleting items</td> <td><strong>del L[3]</strong></td> <td><strong>del D[3]</strong></td> </tr> </tbody> </table> </p>
%html <p><strong>Exercise:</strong> In the directed graph below, the vertex 1 points to the vertices in the list [2, 3].</p> <p><img src="images/graph0.png" alt="" /></p> <p>Create a dictionary with keys the vertices of the above directed graph, and with values the lists of vertices pointed to by the vertex.</p>
%html <p>&nbsp;</p> <p><strong>Exercise:</strong> Use the <strong>DiGraph</strong> command to contruct the above directed graph, and plot the directed graph (<em>Hint</em>: In the documentation for <strong>DiGraph</strong>, take a look at the '<em>dictionary of lists</em>' example.)</p>
%html <p>&nbsp;</p> <p><strong>Exercise:</strong> Find the <em>adjacency matrix</em> of the graph you constructed above.</p>
%html <p>&nbsp;</p> <p><strong>Exercise:</strong> Compute the square of the adjacency matrix. Give a graph-theoretic intepretation of the numbers in this matrix. Does your intepretation hold for the cube of the adjacency matrix?</p>
%html <h2>The Seven Bridges of K&ouml;nigsberg</h2> <p style="text-align: left;"><strong>Exercise:</strong> The <em>Seven Bridges of K&ouml;nigsberg</em> is the following famous historical problem solved by Leonhard Euler in 1735. This is the problem that started graph theory.</p> <blockquote> <p>"The city of K&ouml;nigsberg in Prussia (now Kaliningrad, Russia) was set on both sides of the Pregel River, and included two large islands which were connected to each other and the mainland by seven bridges.</p> <p>The problem was to find a walk through the city that would cross each bridge once and only once. The islands could not be reached by any route other than the bridges, and every bridge must have been crossed completely every time (one could not walk halfway onto the bridge and then turn around to come at it from another side)."</p> <p style="text-align: center;"><img src="images/euler.png" alt="" width="845" height="178" /></p> </blockquote> <ol> <li>Enter the graph on the right into Sage (use the <strong>Graph</strong> command, not the <strong>DiGraph</strong> command).</li> <li>Solve the problem; that is, does such a walk exist? (<em>Hint:</em> Take a look at the documentation for the <strong>eulerian_circuit</strong> method; look up <em>Eulerian circuit</em> in Wikipedia if you don't know its definition.)</li> </ol> <p style="text-align: right;"><span style="font-size: small;"><em> (The quotation and the image are from the Wikipedia page <a href="http://en.wikipedia.org/wiki/Seven_Bridges_of_K%C3%B6nigsberg" target="_blank">Seven Bridges of K&ouml;nigsberg</a>;<br />the problem is from </em></span><em><span style="font-size: small;"><a href="http://modular.math.washington.edu/">William Stein's</a> </span></em><span style="font-size: small;"><em>Graph Theory Worksheet for <a href="http://wiki.wstein.org/09/480b" target="_blank">Math 480b [2009</a>])</em></span></p>
%html <h2>The Coxeter Graph<br /></h2> <p>The <em>Coxeter graph</em> is the graph with vertices 28 vertices $v_{i,j}$, for $0 \leq i \leq 3$ and $0 \leq j \leq 6$, and with edges described by the rules:</p> <ol> <li>$v_{0,j}$ is connected to $v_{1,j}, v_{2,j}, v_{3,j}$ for all $0\leq j \leq 6$;</li> <li>$v_{1,j}$ is connected to $v_{1, j+1 (mod\, 7)}$ for all $0\leq j \leq 6$;</li> <li>$v_{2,j}$ is connected to $v_{2, j+2 (mod\, 7)}$ for all $0\leq j \leq 6$;</li> <li>$v_{3,j}$ is connected to $v_{3, j+3 (mod\, 7)}$ for all $0\leq j \leq 6$.</li> </ol> <p><strong>Exercise:</strong> Construct a dictionary <strong>V</strong> such that <strong>V[(i,j)]</strong> is the list of vertices<strong> (r,s)</strong> that are connected to <strong>(i,j)</strong>. Use this dictionary to construct and plot the <em>Coxeter graph</em>. (<em>Hints:</em> Note that writing <strong>V[i,j]</strong> is shorthand for writing <strong>V[(i,j)]</strong>. You should be able to generate the lists of vertices by using loops and list comprehensions.)</p>
%html <p>&nbsp;</p> <h2>Spectrum of a graph<br /></h2> <p>The <em>spectrum</em> of a graph is the set of eigenvalues of the adjacency matrix of the graph. The <em>spectrum</em> of the Coxeter graph is</p> <ul> <li>$-1-\sqrt{2}$ with multiplicity 6,</li> <li>$-1$ with multiplicity 7,</li> <li>$\sqrt{2}-1$ with multiplicity 6, </li> <li>$2$ with multiplicity 8,</li> <li>$3$ with multiplicity 1.</li> </ul> <p>It turns out that no other graph has this same spectrum (in this case, we say that the graph <em>is determined by its spectrum</em>).</p> <p><strong>Exercise:</strong> Test to see that you correctly constructed the Coxeter graph in the previous exercise. That is, compute the <strong>adjacency matrix</strong> of the Coxeter graph, find the&nbsp;<strong>eigenvalues</strong> of the adjacency matrix, and then compare them with the above.</p>
%html <p>&nbsp;</p> <p><strong>Exercise:</strong> The command <strong>graphs(n)</strong> generates all the graphs on $n$ vertices (up to isomorphism). Use this command to test whether there are two graphs with less than 7 vertices that have the same spectrum.</p>
%html <h2>Birthday Paradox</h2> <p>In the following exercise, we will use Sage estimate the pobbiliy that in a group of $n$ people, two of them will have the same birthday.</p> <p><strong>Exercise</strong> Using th command <strong>graphs.RandomGNP</strong>, create a function that returns a graph with $n$ vertices and where the probability that any two of the vertices is connected is $1/365$.</p>
%html <p>&nbsp;</p> <p><strong>Exercise:</strong> Plot a graph <strong>g</strong> created by your function above using the <strong>g.plot(layout='circular')</strong>.</p>
%html <p>&nbsp;</p> <p><strong>Exercise:</strong> Create 100 random graphs (using your above function) with $n=23$ vertices. What ratio of them contains an edge? (<em>Hint: </em>For a graph <strong>g</strong>, the command <strong>g.num_edges()</strong> returns the number of edges in <strong>g</strong>.)</p>
%html <p><strong>Exercise:</strong> Repeat the above exercise with $n=57$ vertices.</p>
%html <p>&nbsp;</p> <p><strong>Exercise:</strong> Repeat the above exercises for all the values $1, 2, ..., 120$. Plot the results using a line graph.</p>