Attachment 'Dictionaries-GraphTheory_Solutions.rst'

Download

Dictionaries and Graph Theory

Authors: Franco Saliola
Mélodie Lapointe

Dictionaries

A dictionary is another builtin datatype. Unlike lists or tuples, which are indexed by a range of numbers, dictionaries are indexed by keys , which can be any immutable type. Strings and numbers can always be keys. Dictionaries are sometimes called "associative arrays" in other programming languages.

There are several ways to define dictionaries. Below are equivalent ways to define a dictionary that pairs 'key1' with 'value1' and 'key2' with 'value2'.

sage: d = {'key1':'value1', 'key2':'value2'}
sage: d
{'key2': 'value2', 'key1': 'value1'}
sage: d = dict(key1='value1', key2='value2')
sage: d
{'key2': 'value2', 'key1': 'value1'}
sage: d = dict([('key1','value1'), ('key2','value2')])
sage: d
{'key2': 'value2', 'key1': 'value1'}
sage: d = {1:[2,2], 'a':(6,5), 0:{1:2}}
sage: d
{'a': (6, 5), 1: [2, 2], 0: {1: 2}}

Dictionaries behave as lists, tuples, and strings for several important operations.

*Operation* *Syntax for lists* *Syntax for dictionaries*
Accessing elements L[3] D[3]
Length len(L) len(D)
Modifying L[3] = 17 D[3] = 17
Deleting items del L[3] del D[3]

Exercise: In the directed graph below, the vertex 1 points to the vertices in the list [2, 3].

./graph0.png

Create a dictionary with keys the vertices of the above directed graph, and with values the lists of vertices pointed to by the vertex.

sage: E = {0:[1,1,2],1:[2,3],2:[3],3:[4,4]};E
{0: [1, 1, 2], 1: [2, 3], 2: [3], 3: [4, 4]}

Exercise: Use the DiGraph command to contruct the above directed graph, and plot the directed graph ( Hint : In the documentation for DiGraph, take a look at the ' dictionary of lists ' example.)

sage: G = DiGraph(E)
....: G.show()

Exercise: Find the adjacency matrix of the graph you constructed above.

sage: G.adjacency_matrix()
[0 2 1 0 0]
[0 0 1 1 0]
[0 0 0 1 0]
[0 0 0 0 2]
[0 0 0 0 0]

Exercise: 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?

sage: (G.adjacency_matrix())^2
[0 0 2 3 0]
[0 0 0 1 2]
[0 0 0 0 2]
[0 0 0 0 0]
[0 0 0 0 0]

The Seven Bridges of Königsberg

Exercise: The Seven Bridges of Königsberg is the following famous historical problem solved by Leonhard Euler in 1735. This is the problem that started graph theory.

The city of Kö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. 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).

./euler.png
  1. Enter the graph on the right into Sage (use the Graph command, not the DiGraph command).
sage: D = {1:[2,2,3,4,4],2:[1,1,3],3:[1,2,4],4:[1,1,3]}
....: G = Graph(D)
....: G.show()
  1. Solve the problem; that is, does such a walk exist? ( Hint: Take a look at the documentation for the eulerian_circuit method; look up Eulerian circuit in Wikipedia if you don't know its definition.)
sage: G.eulerian_circuit()
False

(The quotation and the image are from the Wikipedia page `Seven Bridges of Königsberg <http://en.wikipedia.org/wiki/Seven_Bridges_of_K%C3%B6nigsberg>`_ ; the problem is from `William Stein's <http://modular.math.washington.edu/>`_ Graph Theory Worksheet for `Math 480b [2009 <http://wiki.wstein.org/09/480b>`_ ])

The Coxeter Graph

The Coxeter graph is the graph with 28 vertices vi, j, for 0 ≤ i, j,  ≤ 6, and with edges described by the rules:

  1. v0, i is connected to v1, i, v2, i, v3, i for all 0 ≤ i ≤ 6;
  2. v1, j is connected to v1, j + 1(mod 7) for all 0 ≤ j ≤ 6;
  3. v2, j is connected to v2, j + 2(mod 7) for all 0 ≤ j ≤ 6;
  4. v3, j is connected to v3, j + 3(mod 7) for all 0 ≤ j ≤ 6.

Exercise: Construct a dictionary V such that V[(i,j)] is the list of vertices (r,s) that are connected to (i,j). Use this dictionary to construct and plot the Coxeter graph . ( Hints: Note that writing V[i,j] is shorthand for writing V[(i,j)]. You should be able to generate the lists of vertices by using loops and list comprehensions.)

sage: V = {(0,j):[(i,j) for i in [1,2,3]] for j in range(7)}
....: V.update({(i,j):[(i,(j+i)%7)] for i in [1,2,3] for j in range(7)})
....: V

Spectrum of a graph

The spectrum of a graph is the set of eigenvalues of the adjacency matrix of the graph. The spectrum of the Coxeter graph is

  •  − 1 − (6) with multiplicity 6,
  •  − 1 with multiplicity 7,
  • (2) − 1 with multiplicity 6,
  • 2 with multiplicity 8,
  • 3 with multiplicity 1.

It turns out that no other graph has this same spectrum (in this case, we say that the graph is determined by its spectrum ).

Exercise: Test to see that you correctly constructed the Coxeter graph in the previous exercise. That is, compute the adjacency matrix of the Coxeter graph, find the eigenvalues of the adjacency matrix, and then compare them with the above.

sage: G = Graph(V)
....: sorted(G.adjacency_matrix().eigenvalues())

Exercise: The command graphs(n) 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.

sage: spectrum = set({})
....: compteur = 0
....: for i in range(1,4):
....:     for g in graphs(i):
....:         compteur += 1
....:         e = frozenset(g.adjacency_matrix().eigenvalues())
....:         if spectrum.isdisjoint(set({e})):
....:             spectrum.add(e)
....:         else:
....:             print('Two graphs have the same spectrum: ', e)
....:             break
('Two graphs have the same spectrum: ', frozenset([0]))

Birthday Paradox

In the following exercises, we will use Sage to estimate the probability that in a group of n people, two of them will have the same birthday.

Exercise: Using the command graphs.RandomGNP, 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.

sage: n = 23
....: G = graphs.RandomGNP(n,1/365)

Exercise: Plot a graph g created by your function above using the g.plot(layout='circular').

sage: G.plot(layout='circular')

Exercise: Create 100 random graphs (using your above function) with n = 23 vertices. What ratio of them contains an edge? ( Hint: For a graph g, the command g.num_edges() returns the number of edges in g.)

sage: compteur = 0
....: for i in range(100):
....:     G = graphs.RandomGNP(23,1/365)
....:     if G.num_edges() > 0:
....:         compteur += 1
....: compteur
51

Exercise: Repeat the above exercise with n = 57 vertices.

sage: compteur = 0
....: for i in range(100):
....:     G = graphs.RandomGNP(57,1/365)
....:     if G.num_edges() > 0:
....:         compteur += 1
....: compteur
98

Exercise: Repeat the above exercises for all the values 1, 2, ..., 120. Plot the results using a line graph.

sage: ratio = []
....: for n in range(1,121):
....:     compteur = 0
....:     for i in range(100):
....:         G = graphs.RandomGNP(n,1/365)
....:         if G.num_edges() > 0:
....:             compteur += 1
....:     ratio.append(compteur)
....: l = [(i+1,ratio[i]) for i in range(120)]
....: line(l)

Attached Files

To refer to attachments on a page, use attachment:filename, as shown below in the list of files. Do NOT use the URL of the [get] link, since this is subject to change and can break easily.
  • [get | view] (2018-08-12 22:01:42, 8.3 KB) [[attachment:CollatzConjecture.ipynb]]
  • [get | view] (2018-08-12 22:01:45, 100.9 KB) [[attachment:CollatzConjecture.pdf]]
  • [get | view] (2018-08-12 22:01:49, 4.8 KB) [[attachment:CollatzConjecture.rst]]
  • [get | view] (2018-08-12 22:01:53, 12.3 KB) [[attachment:Dictionaries-GraphTheory.ipynb]]
  • [get | view] (2018-08-12 22:01:56, 202.6 KB) [[attachment:Dictionaries-GraphTheory_Solutions.pdf]]
  • [get | view] (2018-08-12 22:02:00, 9.1 KB) [[attachment:Dictionaries-GraphTheory_Solutions.rst]]
  • [get | view] (2018-08-15 12:32:58, 16.0 KB) [[attachment:Fields-2018-flatsurf_and_surface_dynamics_demo.ipynb]]
  • [get | view] (2018-08-11 19:25:37, 15.0 KB) [[attachment:S_2_1.svg]]
  • [get | view] (2018-08-12 22:02:10, 13.4 KB) [[attachment:Strings-BWT.ipynb]]
  • [get | view] (2018-08-12 22:02:14, 72.4 KB) [[attachment:Strings-BWT.pdf]]
  • [get | view] (2018-08-12 22:02:17, 7.0 KB) [[attachment:Strings-BWT.rst]]
  • [get | view] (2018-08-11 19:28:42, 16.7 KB) [[attachment:chap1-first_steps.ipynb]]
  • [get | view] (2018-08-11 19:28:46, 103.1 KB) [[attachment:chap1-first_steps.pdf]]
  • [get | view] (2018-08-11 19:28:53, 7.8 KB) [[attachment:chap1-first_steps.rst]]
  • [get | view] (2018-08-11 19:29:05, 24.6 KB) [[attachment:chap1-first_steps_solutions.ipynb]]
  • [get | view] (2018-08-11 19:29:09, 108.7 KB) [[attachment:chap1-first_steps_solutions.pdf]]
  • [get | view] (2018-08-11 19:29:12, 12.0 KB) [[attachment:chap1-first_steps_solutions.rst]]
  • [get | view] (2018-08-11 19:29:17, 30.8 KB) [[attachment:chap2-list_and_for.ipynb]]
  • [get | view] (2018-08-11 19:29:23, 110.0 KB) [[attachment:chap2-list_and_for.pdf]]
  • [get | view] (2018-08-11 19:29:26, 17.6 KB) [[attachment:chap2-list_and_for.rst]]
  • [get | view] (2018-08-11 19:29:32, 42.0 KB) [[attachment:chap2-list_and_for_Solutions.ipynb]]
  • [get | view] (2018-08-11 19:29:35, 116.1 KB) [[attachment:chap2-list_and_for_Solutions.pdf]]
  • [get | view] (2018-08-11 19:29:38, 25.6 KB) [[attachment:chap2-list_and_for_Solutions.rst]]
  • [get | view] (2018-08-11 19:29:51, 21.1 KB) [[attachment:chap3-if-solutions.ipynb]]
  • [get | view] (2018-08-11 19:29:58, 95.4 KB) [[attachment:chap3-if-solutions.pdf]]
  • [get | view] (2018-08-11 19:29:55, 9.0 KB) [[attachment:chap3-if-solutions.rst]]
  • [get | view] (2018-08-11 19:29:41, 11.8 KB) [[attachment:chap3-if.ipynb]]
  • [get | view] (2018-08-11 19:29:45, 91.6 KB) [[attachment:chap3-if.pdf]]
  • [get | view] (2018-08-11 19:29:48, 5.7 KB) [[attachment:chap3-if.rst]]
  • [get | view] (2018-08-11 19:30:02, 4.4 KB) [[attachment:chap4-functions.ipynb]]
  • [get | view] (2018-08-11 19:30:06, 83.1 KB) [[attachment:chap4-functions.pdf]]
  • [get | view] (2018-08-11 19:30:09, 2.3 KB) [[attachment:chap4-functions.rst]]
  • [get | view] (2018-08-11 19:30:20, 3.2 KB) [[attachment:chap5-while.ipynb]]
  • [get | view] (2018-08-11 19:30:14, 62.1 KB) [[attachment:chap5-while.pdf]]
  • [get | view] (2018-08-11 19:30:39, 1.5 KB) [[attachment:chap5-while.rst]]
  • [get | view] (2018-08-11 19:30:47, 4.0 KB) [[attachment:chap6-advanced_exercises.ipynb]]
  • [get | view] (2018-08-11 19:30:53, 69.7 KB) [[attachment:chap6-advanced_exercises.pdf]]
  • [get | view] (2018-08-11 19:31:00, 2.0 KB) [[attachment:chap6-advanced_exercises.rst]]
  • [get | view] (2018-08-12 22:02:04, 90.9 KB) [[attachment:euler.png]]
  • [get | view] (2018-08-13 21:52:57, 1.1 KB) [[attachment:flipper_nf_conversion.py]]
  • [get | view] (2018-08-15 12:18:55, 55.4 KB) [[attachment:flipper_tutorial.pdf]]
  • [get | view] (2018-08-12 21:56:57, 16.1 KB) [[attachment:floating_point_and_stability.ipynb]]
  • [get | view] (2018-08-12 21:57:01, 78.0 KB) [[attachment:floating_point_and_stability.pdf]]
  • [get | view] (2018-08-12 21:57:04, 6.6 KB) [[attachment:floating_point_and_stability.rst]]
  • [get | view] (2018-08-12 22:02:07, 17.6 KB) [[attachment:graph0.png]]
  • [get | view] (2018-08-11 23:44:38, 23.2 KB) [[attachment:intro.en.ipynb]]
  • [get | view] (2018-08-11 23:44:45, 117.5 KB) [[attachment:intro.en.pdf]]
  • [get | view] (2018-08-11 23:44:53, 12.5 KB) [[attachment:intro.en.rst]]
  • [get | view] (2018-08-12 23:25:35, 62.3 KB) [[attachment:logistic_orbit_interact.png]]
  • [get | view] (2018-08-11 19:48:34, 9.6 KB) [[attachment:random_walk.ipynb]]
  • [get | view] (2018-08-11 19:48:38, 87.9 KB) [[attachment:random_walk.pdf]]
  • [get | view] (2018-08-11 19:48:44, 5.7 KB) [[attachment:random_walk.rst]]
  • [get | view] (2018-08-15 19:25:46, 67.3 KB) [[attachment:real_and_complex_numbers.ipynb]]
 All files | Selected Files: delete move to page copy to page

You are not allowed to attach a file to this page.