a b c d a b c d e 5 e 7

Similar documents
MC 302 GRAPH THEORY 10/1/13 Solutions to HW #2 50 points + 6 XC points

2. CONNECTIVITY Connectivity

STUDENT NUMBER: MATH Final Exam. Lakehead University. April 13, Dr. Adam Van Tuyl

γ(ɛ) (a, b) (a, d) (d, a) (a, b) (c, d) (d, d) (e, e) (e, a) (e, e) (a) Draw a picture of G.

6.001 Notes: Section 8.1

CSE 21 Spring 2016 Homework 5. Instructions

CSE 21 Spring 2016 Homework 5. Instructions

Programming with Python

Basic Combinatorics. Math 40210, Section 01 Fall Homework 4 Solutions

ASSIGNMENT 4 SOLUTIONS

Definition For vertices u, v V (G), the distance from u to v, denoted d(u, v), in G is the length of a shortest u, v-path. 1

Introduction to Programming in C Department of Computer Science and Engineering. Lecture No. #17. Loops: Break Statement

CPS 102: Discrete Mathematics. Quiz 3 Date: Wednesday November 30, Instructor: Bruce Maggs NAME: Prob # Score. Total 60

DO NOT RE-DISTRIBUTE THIS SOLUTION FILE

Elements of Graph Theory

Basics of Graph Theory

Algorithm Design and Analysis

Notes slides from before lecture. CSE 21, Winter 2017, Section A00. Lecture 9 Notes. Class URL:

Introduction to Computer Science and Programming for Astronomers

Week - 01 Lecture - 04 Downloading and installing Python

(Refer Slide Time: 01:12)

CS2 Practical 1 CS2A 22/09/2004

MATH10001 Mathematical Workshop. Graphs, Trees and Algorithms Part 2. Trees. From Trees to Prüfer Codes

Packet #6: Counting & Graph Theory. Applied Discrete Mathematics

Introduction to Graph Theory

Chapter 1 Summary. Chapter 2 Summary. end of a string, in which case the string can span multiple lines.

Module 11. Directed Graphs. Contents

14 More Graphs: Euler Tours and Hamilton Cycles

(Refer Slide Time 3:31)

Eulerian tours. Russell Impagliazzo and Miles Jones Thanks to Janine Tiefenbruck. April 20, 2016

1. Suppose you are given a magic black box that somehow answers the following decision problem in polynomial time:

Math 15 - Spring Homework 5.2 Solutions

Treewidth and graph minors

Problem One: A Quick Algebra Review

Math 202 Test Problem Solving, Sets, and Whole Numbers 19 September, 2008

Math 15 - Spring Homework 2.6 Solutions 1. (2.6 # 20) The following graph has 45 vertices. In Sagemath, we can define it like so:

An Introduction to Graph Theory

Spring 2012 Homework 10

1 Format. 2 Topics Covered. 2.1 Minimal Spanning Trees. 2.2 Union Find. 2.3 Greedy. CS 124 Quiz 2 Review 3/25/18

CS70 - Lecture 6. Graphs: Coloring; Special Graphs. 1. Review of L5 2. Planar Five Color Theorem 3. Special Graphs:

Graph Theory Questions from Past Papers

Algorithms CMSC Homework set #2 due January 21, 2015

Definition: A graph G = (V, E) is called a tree if G is connected and acyclic. The following theorem captures many important facts about trees.

Lab 7 1 Due Thu., 6 Apr. 2017

Starting Boolean Algebra

(Refer Slide Time 6:48)

MITOCW watch?v=kz7jjltq9r4

Homework 2. Sample Solution. Due Date: Thursday, May 31, 11:59 pm

Graph Theory: Starting Out

Ramsey s Theorem on Graphs

Solutions to relevant spring 2000 exam problems

Intro. Scheme Basics. scm> 5 5. scm>

Graphs (MTAT , 6 EAP) Lectures: Mon 14-16, hall 404 Exercises: Wed 14-16, hall 402

CSE : Python Programming

Programming Languages Third Edition

\n is used in a string to indicate the newline character. An expression produces data. The simplest expression

Computational Geometry: Lecture 5

HMC CS 158, Fall 2017 Problem Set 3 Programming: Regularized Polynomial Regression

Dr. Scheme evaluates expressions so we will start by using the interactions window and asking Dr. Scheme to evaluate some expressions.

MA 111 Review for Exam 3

SOFTWARE ENGINEERING DESIGN I

Privacy and Security in Online Social Networks Department of Computer Science and Engineering Indian Institute of Technology, Madras

Bases of topologies. 1 Motivation

6.001 Notes: Section 6.1

CS112 Lecture: Working with Numbers

Discrete Mathematics for CS Spring 2008 David Wagner Note 13. An Introduction to Graphs

Lecture Notes on Contracts

Statistics Case Study 2000 M. J. Clancy and M. C. Linn

GRAPH THEORY: AN INTRODUCTION

Discrete Mathematics and Probability Theory Spring 2016 Rao and Walrand Midterm 1

Definition 1.1. A matching M in a graph G is called maximal if there is no matching M in G so that M M.

Python review. 1 Python basics. References. CS 234 Naomi Nishimura

Graphs. The ultimate data structure. graphs 1

Week 8: The fundamentals of graph theory; Planar Graphs 25 and 27 October, 2017

Chapter 11: Graphs and Trees. March 23, 2008

Notes for Recitation 8

Discrete Mathematics and Probability Theory Summer 2016 Dinh, Psomas, and Ye HW 2

Solutions to Homework 10

Math 485, Graph Theory: Homework #3

11.2 Eulerian Trails

Building a system for symbolic differentiation

Lecture 5: Graphs. Rajat Mittal. IIT Kanpur

Lecture 10 Notes Linked Lists

Discrete Mathematics and Probability Theory Fall 2013 Vazirani Note 7

CS112 Lecture: Variables, Expressions, Computation, Constants, Numeric Input-Output

DO NOT RE-DISTRIBUTE THIS SOLUTION FILE

15-780: Problem Set #4

CS195H Homework 1 Grid homotopies and free groups. Due: February 5, 2015, before class

CHAPTER 2. Graphs. 1. Introduction to Graphs and Graph Isomorphism

6.001 Notes: Section 4.1

CS473-Algorithms I. Lecture 13-A. Graphs. Cevdet Aykanat - Bilkent University Computer Engineering Department

Homework #3 (Search) Out: 1/24/11 Due: 2/1/11 (at noon)

CSE 101 Homework 5. Winter 2015

Eulerian Paths and Cycles

CS 1803 Pair Homework 4 Greedy Scheduler (Part I) Due: Wednesday, September 29th, before 6 PM Out of 100 points

Characterizing Graphs (3) Characterizing Graphs (1) Characterizing Graphs (2) Characterizing Graphs (4)

Notebook Assignments

Lecture 3 - Template and Vectors

Introduction to Programming in C Department of Computer Science and Engineering. Lecture No. #06 Loops: Operators

Midterm I Practice Problems

Transcription:

COMPSCI 230 Homework 9 Due on April 5, 2016 Work on this assignment either alone or in pairs. You may work with different partners on different assignments, but you can only have up to one partner for any one assignment. You may not talk about this assignment with others until all of you have handed in their work. See Mechanics Homework on the class web page for details on the homework policy. This assignment comes with auxiliary files hw.tex and hw.py. Remember to put your name in the right place in the.tex file before starting to work with it. Usual formatting instructions hold, including instructions about proof style (see Homework 8 for those). 1. The.tex template file shows how to draw the graph G shown below and write its adjacency matrix in L A TEX. Vertex labels are placed on a 2 by 2 grid (with the same ampersand and double-backslash notation you would use in the tabular environment) and then the \ncline and \nccircle commands add edges. For instance, \ncline{1, 1}{1, 2} draws a line between vertex a (in position (1, 1) on the grid) and vertex b (in position (1, 2)); the command \nccircle draws a circular arc from a vertex to itself. The command right after each \ncline or \nccircle command places labels. You need not concern yourself with how to do all this, because this problem asks you only to remove existing edges. When you do so, remove both the line-drawing command and the corresponding labeling command (that is, remove the whole line in the hw.tex file). The file hw.tex has a drawing, an adjacency matrix for G, and an arbitrary path repeated where appropriate. Just modify those for your answers to this and the following questions. If you use L A TEX, you will notice that compilation is slow when graphs are involved. This is normal. Use your own favorite drawing program if you are not planning to use L A TEX. Several of the following questions admit more than one acceptable answer. However, you will be penalized for removing more edges than needed. A graph may or may not have an open Euler tour (an open tour is a tour that is not a circuit), an Euler circuit, an open Hamilton tour, or a Hamilton circuit. Let us call these four types of tour special. (a) Is the graph G above simple? Why or why not? e 2 a c e 4 e 3 e 1 e 6 b d e 5 e 7 a b c d a 0 1 1 1 b 1 0 1 1 c 1 1 0 1 d 1 1 1 1 (b) Without removing any vertices, remove as few edges from G as possible, if any, until the new graph G 1 has an open Euler tour that is not also a special tour of a different type (it is OK for G 1 to have other tours that are special, regardless of their types). If multiple open Euler tours are possible on G 1, give one of them. Show the drawing and adjacency matrix for G 1 as indicated in the file hw.pdf, and then show your tour as a sequence of the following form: (vertex, edge, vertex,..., edge, vertex). (c) Without removing any vertices, remove as few edges from G as possible, if any, until the new graph G 2 has an Euler circuit that is not also a special tour of a different type. If multiple Euler circuits are possible on G 2, give one of them. Show the drawing and adjacency matrix for G 2, and then show your circuit as a sequence. No explanations needed. (d) Without removing any vertices, remove as few edges from G as possible, if any, until the new graph G 3 has an open Hamilton tour that is not also a special tour of a different type. Show the drawing and adjacency matrix for G 3, and then show your tour as a sequence. No explanations needed. (e) Without removing any vertices, remove as few edges from G as possible, if any, until the new graph G 4 has a Hamilton circuit that is not also a special tour of a different type. Show the drawing and adjacency matrix for G 4, and then show your circuit as a sequence. No explanations needed. [Assignment continues on the next page]

2. Let A be the adjacency matrix of some graph G = (V, E). If G has n vertices numbered 1 through n, then A is n n and symmetric 1, and the entry a uv = a vu of A is the number of distinct edges between vertices u and v. We now prove that entry a (p) uv = a (p) vu of the p-th power of A { A p A if p = 1 = A A p 1 otherwise is the number of distinct paths of length p between vertices u and v. Recall that path length is measured by the number of edges, and that the product AB of two n n matrices A and B is another n n matrix C that is symmetric if both A and B are, and whose entry u, v is n c uv = a uk b kv. k=1 Your task is to fill the boxes in the proof below. The proof itself is repeated in the file hw.tex. Just put your text between braces wherever you see \answer{}. Do not remove the \answer{} command itself. This will put your text in a box, and will make it easier for us to find it in your solution. If you use something other than L A TEX, make sure that your text is boxed in a similar way. Be careful with math mode if you use L A TEX: If the \answer{} command appears inside an equation or another expression in math mode, do not put dollar signs around your formulas, since you are in math mode already. If the command is not inside text in math mode, you need to use dollar signs inside it to switch to/from math mode as appropriate. CAVEAT: We saw in class that it is safest to restate results so that a proof by induction is on a variable called n. However, choosing n to denote the number of vertices of a graph is a very widely accepted convention, so this proof will be by induction on p. Just be mindful of this. Proof. We prove this result by induction on p. Specifically, we want to prove that p 1 P (p) where the predicate P (p) means that if A is the adjacency matrix of a graph, then entry a (p) uv = a (p) vu of the symmetric matrix A p is the number of distinct paths of length p between vertices u and v. For the base case p = [your text here], the matrix A p is [your text here] by definition, and entry a uv = a vu of A is the number of [your text here]. A path of length 1 is [your text here], so for p = 1 the matrix A p is the number of distinct paths of length p between vertex u and vertex v. This proves [your text here]. For the inductive step, make the inductive assumption that for any k 1 the predicate P (k) holds, that is, [your text here]. Let us subdivide all the paths of length k + 1 from u to v into n groups. Group w contains all those paths that have edge (w, v) as their last edge. Let i w be the number of paths of length k + 1 in group w. [i w = [your text here] if there is no edge between w and v.] Paths in different groups are distinct from each other, because [your text here], so a (k+1) uv = [your text here]. We can determine i w as follows: By the inductive assumption, there are [your text here] paths of length k from u to w. To go from u to v over k + 1 edges and passing through w, we can choose one of [your text here] ways to reach w in k edges. For each of these choices, we can choose any one of the [your text here] edges to take the last step from w to v. Thus, we have i w = [your text here]. In conclusion, since the number of paths from u to v is the same as that from v to u, we can combine the last two equations to obtain a (k+1) uv = a (k+1) vu = [your text here], which is entry u, v of [your text here] by the definition of this matrix and of matrix product. This establishes the inductive step P (k) P (k + 1) for k 1 and completes the proof. [Assignment continues on the next page] 1 A matrix A with entries a uv is symmetric if a uv = a vu for all u and v.

3. In this problem you will implement an algorithm in Python to find an Euler tour (either open or a circuit) for a given graph G. The output is in the form of a Python list that represents the desired tour. If no such tour exists, the algorithm returns the empty list. The algorithm is due to a Monsieur Fleury, who devised it in 1883. (a) Suppose that a graph G is given through its adjacency matrix A. In light of the result you proved in the previous problem, what is a simple way to check if the graph G is connected, without running any graph search? Recall that a graph is connected if for all pairs (u, v) of distinct vertices in G there is a path from u to v. Prove your statement. [Hint: A graph is not connected iff there is a pair of vertices for which there is no path of length 1, and no path of length 2,.... When do the dots stop? In other words, what is the longest possible shortest path between any two distinct vertices of a graph G with n vertices, and why?] (b) Write a Python function def isconnected(a): that takes an adjacency matrix A and returns True if the graph A represents is connected and returns False otherwise. An empty graph or a single vertex, regardless of whether there are edges back to itself or not, is connected. You will get full credit for a correct and simple implementation that uses a good answer to the previous question. There are also more complicated ways to check for graph connectedness, based on searching the graph for paths between any two vertices. This type of solution is not recommended, and will only earn half credit, assuming that it is correct and well implemented. To help you with this programming exercise, the file hw.py contains implementations of the following items: A class called sdict that is exactly the same as Python s standard dictionary class dict (more on this below), except that sdict always prints dictionary keys in sorted order. This ordering makes it easier to inspect dictionaries while debugging code. 2 To take advantage of this feature, just use sdict wherever you would otherwise use dict. Do not use dict directly, since this would make our grading much harder. A Python dictionary dict is a set of (key, value) pairs. If you are not familiar with dict, please study the tutorial at http://www.python-course.eu/python3_dictionaries.php. Try some of the examples there, to make sure you understand how dictionaries work and what methods they provide. The file hw.py implements adjacency matrices as dictionaries of dictionaries. Consider for instance a graph with three vertices called 'a', 'b', 'c' with two edges between 'a' and 'b' and one edge between 'a' and 'c'. The adjacency matrix of this graph is a b c a 0 2 1 A =. b 2 0 0 c 1 0 0 We could enter A by hand as a dictionary of dictionaries by typing A = {'a': {'a': 0, 'b': 2, 'c': 1}, 'b': {'a': 2, 'b': 0, 'c': 0}, 'c': {'a': 1, 'b': 0, 'c': 0}} to the Python interpreter (where optional newline characters were added for readability). To access entry for vertex pair ('b', 'c') we would say A['b']['c']. So you can think of A as of a list of lists, A = [[0, 2, 1], [2, 0, 0], [1, 0, 0]] except that you can use something other than consecutive integers (single characters in the example) to index rows and columns. This will become crucial when we remove vertices from a graph, because the explicit key values 'a', 'b', 'c' keep track of which entry corresponds to which vertex as we manipulate A. To enter A as an sdict of sdicts, which will make inspecting variables easier, you would type A = sdict({'a': sdict({'a': 0, 'b': 2, 'c': 1}), 'b': sdict({'a': 2, 'b': 0, 'c': 0}), 'c': sdict({'a': 1, 'b': 0, 'c': 0})}) 2 You can also use the function adjprint provided in hw.py for prettier printing.

Functions def adjsum(a, B): def adjproduct(a, B): that respectively add and multiply (using the matrix product we saw in the previous problems) two adjacency matrices and return the resulting adjacency matrix. Functions def degrees(a): def nedges(a): def firstvertex(a): def oddvertices(a): def neighbors(a, u): that respectively return (i) a dictionary with the degrees of the vertices in A; (ii) the number of edges in A; (iii) the (key of the) first vertex in A (iv) a list (not a dictionary) of the (keys of the) vertices in A that have odd degree; and (v) a list of the (keys of the) vertices in A that are adjacent to vertex u. These functions are useful in answering this or subsequent questions, and also as examples of how to program with dictionaries. Refer back to the tutorial mentioned earlier or to the full Python 3 documentation for dictionaries if you have questions or doubts. (c) Write a function def iseuler(a): that returns True if the adjacency matrix A represents a graph with an Euler tour (either open or circuit), and returns False otherwise. A graph with no edges (including the empty graph) is Euler, and its Euler tour is the empty tour. A disconnected graph is not Euler. Include your code in both your.pdf and.py files, and show in your.pdf file the outputs on the test cases given in hw.py. [Hint: Remember that a characterization of these graphs was given in the March 24 lecture notes.] (d) Write functions def removeedge(a, u, v): def addedge(a, u, v): def removevertex(a, u): that respectively (i) remove one of the edges between vertices u and v; (ii) add one edge between vertices u and v; and (iii) remove vertex u. These functions do not return anything, but rather modify their first argument A. Also include an assert statement that ensures that the vertices and/or edges being passed as arguments are in A whenever appropriate. Include your code in both your.pdf and.py files, and show in your.pdf file the outputs on the test cases given in hw.py. [Hints: To remove or add the edge between vertices u and v you need to modify two entries of A, except when u and v are the same. The first two functions do not change the size of A. The third one makes A smaller by one in each dimension. To ensure that u is in A you say assert i in A.] (e) We saw both in FDM and in class that a path in a graph is specified in the form described in problem 1(b) above. For the current question, however, we specify a path as a list of edges for simplicity: (edge,..., edge). In addition, we specify each edge as a pair of vertices, listing the vertices in each pair in the order in which they are encountered in the path. For instance, referring to the graph G in problem 1, the path will be specified as (a, e 2, c, e 6, d, e 7, d) [(a, c), (c, d), (d, d)] where we use brackets rather than parentheses to remind us to use a Python list to represent the sequence. Each pair, on the other hand, is a Python tuple (use parentheses). With this representation, multiple edges between any two given vertices (including between a vertex and itself) are still allowed, but are treated as being indistinguishable from each other. For instance, if there are three edges between vertices a and b, then the edge (a, b) will appear three times in the Euler tour. [Assignment continues on the next page]

Algorithm 1 below is pseudocode for Fleury s algorithm. The general idea behind the algorithm is as follows: If the graph is not connected or does not satisfy the conditions for having an Euler tour (either an open Euler tour or an Euler circuit), then return the null object None. In all other cases, an Euler tour exists, and can be constructed as follows. Initialize the tour to the empty sequence. If the graph has no edges, return the tour. Otherwise, initialize a current vertex u to an appropriate vertex in the graph. If the graph admits an open Euler tour, there are two appropriate vertices one could start from (remember how you wrote your iseuler function), so just pick either one. If the graph admits an Euler circuit, you can start anywhere, so pick any vertex. Remove edges from the graph one at at time and add them to the tour as follows. As long as there are edges in the graph, For each neighbor v of the current vertex u, * Tentatively remove the edge (u, v) from the graph. * If the reduced graph is still connected, then (u, v) is a good edge to add to the tour, so stop considering other neighbors of u (that is, break out of the loop that examines the neighbors of u). * Otherwise, edge (u, v) cannot be used in the Euler path yet, so put it back into the graph. If, after examining all the neighbors v of u, no edge (u, v) was found whose removal leaves the graph connected, then it can be proven that u has exactly one neighbor v left, and the edge (u, v) is the next edge in the tour. In this case, remove both the edge (u, v) and the vertex u from the graph. Do not append (u, v) to the tour yet, you will do that next. At this point, the examination above has led to a new vertex v. Append the edge (u, v) to the tour and replace u with v. When the graph has no more edges, return the tour. Algorithm 1 Fleury s algorithm for finding an Euler tour in the graph specified by the given adjacency matrix. Input: The adjacency matrix A of a graph. Output: An Euler tour (either open or a circuit) if one exists, and the null object None otherwise. if the graph is not Euler then return None else tour [] if the graph has no edges then return tour if the graph has an open Euler tour then u either of the two appropriate starting vertices else u any vertex while there are edges in the graph do found False for v adjacent to u do Remove edge (u, v) from A if A is connected then found True break else Reinsert edge (u, v) into A end for if not found then Remove edge (u, v) from A Remove vertex u from A Append edge (u, v) to tour u v end while return tour

Use the functions you wrote in answer to the previous questions to write a Python function def fleury(a0): that takes the adjacency matrix A0 for a graph and uses Fleury s algorithm to return an Euler tour (either an open one or a circuit) if one exists, or the empty list otherwise. The tour is to be represented as discussed earlier. Include your code in both your.pdf and.py files, and show in your.pdf file the outputs on the test cases given in hw.py. You may have to add newlines manually to make the outputs print correctly on the page. PROGRAMMING NOTES: [Important!] The first order of business after having established that an Euler tour exists is to make a deep copy of A0 as follows: A = deepcopy(a0) and then work with A. In this way, we prevent fleury to have undesired side effects on A0. The function deepcopy ensures that the objects A and A0 and all the objects they contain in turn are unrelated to each other. This function is defined in the module copy, hence the import statement included in the file hw.py. Your code will be made mostly of calls to the functions provided and the ones you wrote earlier. The functions isconnected and iseuler you wrote are of course crucial for writing fleury. If you were not able to write those correctly, you can use ad hoc versions defined as follows to implement fleury: For each of the test cases in the assignment, determine by hand whether the function in question should return True or False. Then, make the function a long if... elif... statement that tests if A is empty and so forth, and returns the appropriate value in each case. If you adopt this solution, state so very explicitly in your file hw.pdf, and show your code in both hw.pdf and hw.py. Not doing so would lose you credit for this question too, and would be a violation of academic integrity.