Download Part 1: Graphs and Adjacency Matrices

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts

Orthogonal matrix wikipedia , lookup

Perron–Frobenius theorem wikipedia , lookup

Non-negative matrix factorization wikipedia , lookup

Gaussian elimination wikipedia , lookup

Cayley–Hamilton theorem wikipedia , lookup

Matrix multiplication wikipedia , lookup

Matrix calculus wikipedia , lookup

Transcript
COMPSCI 230
Homework 8
Part 1: Graphs and Adjacency Matrices
As explained in the March 23 class notes, an undirected graph with nodes numbered from 0 to n − 1 can be represented with an adjacency
matrix, a symmetric n × n matrix A of nonnegative integers. The class notes considered graphs without loops. To include loops and to be
consistent with the class notes, we define entry auv in row u and column v of A to be the number of edge-ends that are incident to vertex
u and are on edges between vertices u and v. Thus, if u 6= v, then auv = avu , equal to the number of edges between vertices u and v. If
u = v, then auu is twice the number of loops on u. The empty graph has an empty matrix as an adjacency matrix, and an adjacency matrix
with all zeros represents a graph with no edges.
The choice to count edge-ends rather than edges in the adjacency matrix simplifies the computation of the degree of a node, because then
the degree of node u is the sum of the elements in row u (or column u) of A even if the graph has loops.
In the code for this assignment, we represent graphs (including trees) as Python lists of lists. For instance, the adjacency matrix on the left
below represents the graph drawn on the right, and is defined as follows in Python:
A = [[4, 2, 1, 0], [2, 6, 1, 3], [1, 1, 0, 2], [0, 3, 2, 2]]
0

4
 2

 1
0
2
6
1
3
1
1
0
2

0
3 

2 
2
3
1
2
The function ok provided in graph.py, which is in turn imported into hwcode.py, contains several assert statements that ckeck
that a given adjacency matrix has the proper format.
In addition, the function latexGraph in graph.py creates LATEX code to visualize simple graphs and adjacency matrices. For instance,
the figure above (including the adjacency matrix to its left) was created by running the Python command
latexGraph(A, ’A.tex’)
This created a LATEX file A.tex, which was then included in the assignment LATEX file with the command
\inputFileIfExists{A}
You may want to use these facilities to display graphs for matrices you create. However, the Appendix in this assignment shows the graphs
used for testing your code.
Problem 1.1 (tag: neighbors)
Write a Python function with header
def neighbors(u, A):
that returns a list of the neighbors (integers) of vertex u in A, where A is the adjacency matrix of a graph. Neighbors should be listed in
increasing order.
If there are multiple edges between vertices u and v, vertex v should be listed just once. If u has loops, then u should be listed once as its
own neighbor. A vertex is not its own neighbor otherwise.
The body of your function should start with an assert statement that checks that the integer u is between 0 (inclusive) and len(A)
(exclusive). As a consequence, calling neighbors on an empty graph would raise an exception. Do not call ok(A), since neighbors
will be called by a function in a later question, and that function will do the check.
What to turn in: Display your code and the results of the tests from hwcode.py.
Spring 2017
COMPSCI 230
Homework 8
Problem 1.2 (tag: traverse)
Write a recursive Python function with header
def traverse(A, u, visit = lambda u:
print(u)):
to traverse, in depth-first order, all the vertices in the graph with adjacency matrix A that are reachable from the given vertex u (including
u itself, even if there is no loop on u). When a vertex is visited, the function visit passed as argument should be executed. The graph is
not necessarily connected, so not all vertices may be reachable from u.
Do not return a spanning tree. Your function should first call ok(A) to check that A has a proper format.
[Hint: This function can be found verbatim, with name gdf, in the class notes on graph traversals, so this is easy credit. However,
points will be deducted if the output is incorrect, even if the issue is in your implementation of the neighbors function in the previous
problem.]
What to turn in: Display your code and the results of the tests from hwcode.py.
Problem 1.3 (tag: isconnected)
A graph is connected iff every vertex is reachable from every vertex. A vertex is reachable from itself, and the empty graph is connected.
Modify the function traverse from the previous problem to write an efficient recursive Python function with header
def isConnected(A):
that returns True if the graph with adjacency matrix A is connected, and False otherwise. In this context, “efficient” means that the
function visits each vertex in the graph once.
Your function should call ok(A) to check that A has a proper format.
What to turn in: Display your code and the results of the tests from hwcode.py.
Spring 2017
COMPSCI 230
Homework 8
Part 2: Trees
Problem 2.1 (tag: istree)
A tree is a simple connected graph with no polygons.
Modify the function isConnected from the previous problem to write an efficient recursive Python function with header
def isTree(A):
that returns True if the graph with adjacency matrix A is a tree, and False otherwise. In this context, “efficient” means that the function
visits each vertex in the graph once.
Your function should call ok(A) to check that A has a proper format.
[Hint: Seeing a node you visited earlier, other than the current node’s parent, is bad news.]
What to turn in: Display your code and the results of the tests from hwcode.py.
Problem 2.2 (tag: mn)
Let P (n) be the following predicate: A tree T with n vertices has m(n) = n − 1 edges.
Prove this predicate P (n) by induction on n for every integer n ≥ 1, using the definition of tree given in the previous problem. Be detailed
and precise in your arguments.
Spring 2017
COMPSCI 230
Homework 8
Part 3: Euler Tours
A graph is nonempty if it has at least one vertex. An Euler tour of a nonempty graph is a path in which every edge of the graph occurs
exactly once. An Euler tour is an Euler circuit iff it starts and ends at the same vertex.
The empty sequence is not a path. A graph with a single vertex v has an Euler circuit, regardless of the number of edges.
Euler’s theorem states the following:
An undirected graph has an Euler circuit iff the graph is connected and all its vertices have even degree. An undirected graph
has an Euler tour that is not a circuit iff the graph is connected and exactly two of its vertices have odd degree.
Problem 3.1 (tag: manual)
Find an Euler tour for each of the following two graphs. For each of them, also state if the tour is a circuit. In your answers, list the tours
as sequences of vertices, without listing the edges. Different valid solutions are possible. You help our graders if your sequence starts at a
vertex with as low a number as possible.
(a)
0

0
 1

 1
2
1
0
2
1
1
2
0
1

2
1 

1 
0
3
1
2
(b)
0

0
 1

 1
0
1
0
1
1
1
1
0
1

0
1 

1 
0
3
1
2
Problem 3.2 (tag: tours)
Write an efficient function with header
def hasEuler(A):
that takes the adjacency matrix A of an undirected graph and returns the following string:
• 'tour'if A has an Euler tour that is not a circuit
• 'circuit'if A has an Euler circuit
• 'none'if A has no Euler tour
Your function should call ok(A) to check that A has a proper format. It should then include an assert statement that checks if the input
graph is nonempty, and raise an exception otherwise.
What to turn in: Display your code and the results of the tests from hwcode.py.
Spring 2017
COMPSCI 230
Homework 8
Part 4: A Hamiltonian Puzzle
Problem 4.1 (tag: knight)
The word fragments below are written on a chessboard, one word per square, in the order shown. A re-entrant knight’s tour of the board
(starting with ‘life’) yields some English text. Reconstruct the text. You will have to add capitalization, blank spaces, and punctuation.
Just give the text, no need to show the tour.
life
le
en
imp
vic
her
a
but
se
not
tory
whi
imm
lose
des
to
the
is
che
in
ort
be
rat
pla
ss
even
is
an
eat
own
to
tiny
it
pie
ga
ant
the
ev
yer
her
nts
a
of
def
and
tes
for
or
tim
me
ce
pla
to
a
en
crea
is
eve
vic
not
in
yer
win
piece
[Hints: This is a pencil-and-paper puzzle that requires some patience. Reconstruct meaningful pieces of the text by trying various knight’s
moves, then connect the pieces. Re-entrant knight’s tours are defined in the March 23 class notes.]
Spring 2017
COMPSCI 230
Homework 8
Some Graphs and their Adjacency Matrices
empty:
[ ]
vertex0:
0
0
vertex1:
2
0
vertex2:
4
0
vertex3:
6
0
Spring 2017
COMPSCI 230
Homework 8
pair0:
0
0
0
0
0
1
pair1:
0
0
1
1
0
1
quadruple0:
0

0
 1

 1
0
1
0
1
0
1
1
0
0

0
0 

0 
0
3
1
2
Spring 2017
COMPSCI 230
Homework 8
quadruple1:
0

0
 1

 1
0
1
0
1
1
1
1
0
0

0
1 

0 
0
3
1
2
quadruple2:
0

0
 1

 1
0
1
0
1
1
1
1
0
1

0
1 

1 
0
3
1
2
quadruple3:
0

0
 1

 1
1
1
0
1
1
1
1
0
1

1
1 

1 
0
3
1
2
Spring 2017
COMPSCI 230
Homework 8
konigsberg:
0

0
 0

 2
1
0
0
2
1
2
2
0
1

1
1 

1 
0
3
1
2
euler1:














0
1
0
0
1
0
1
0
1
1
0
1
1
0
0
0
0
1
0
1
0
1
0
0
0
0
0
0
1
1
0
1
1
0
0
0
1
0
0
1
0
0
0
0
0
0
0
0
1
0
0
1
0
0
1
0
0
0
0
1
0
1
1
0
0
0
0
0
0
1
0
1
1
1
0
0
0
0
1
1
0
0

8













1
7
2
6
3
5
4
euler2:
0

0
 1

 1
2
1
0
2
1
1
2
0
1

2
1 

1 
0
3
1
2
Spring 2017
COMPSCI 230
Homework 8
euler3:
0

0
 1

 1
0
1
0
1
1
1
1
0
1

0
1 

1 
0
3
1
2
domino:
0










2
1
1
1
1
1
1
1
2
1
1
1
1
1
1
1
2
1
1
1
1
1
1
1
2
1
1
1
1
1
1
1
2
1
1
1
1
1
1
1
2
1
1
1
1
1
1
1
2

6









1
5
2
4
3
A:
0

4
 2

 1
0
2
6
1
3
1
1
0
2

0
3 

2 
2
3
1
2
Spring 2017
COMPSCI 230
Homework 8
split:














0
1
0
0
0
0
1
0
1
1
0
0
0
0
0
0
0
1
0
0
0
1
0
0
0
0
0
0
0
1
0
1
1
0
0
0
0
0
0
1
0
0
0
0
0
0
0
0
1
0
0
0
0
0
1
0
0
0
0
0
0
1
1
0
0
0
0
0
0
1
0
1
0

1
1
0
0
0
0
1
1
0
8













1
7
2
6
3
5
4
eight1:
0












0
0
0
0
0
0
0
1
0
0
0
1
0
0
0
0
0
0
0
1
0
0
0
0
0
1
1
0
1
0
0
0
0
0
0
1
0
1
0
0
0
0
0
0
1
0
1
1
0
0
0
0
0
1
0
0
1
0
0
0
0
1
0
0












7
1
6
2
5
3
4
eight2:
0












0
0
0
0
0
0
0
1
0
0
0
1
0
0
0
1
0
0
0
1
0
0
0
0
0
1
1
0
1
0
0
0
0
0
0
1
0
1
0
0
0
0
0
0
1
0
1
1
0
0
0
0
0
1
0
0
1
1
0
0
0
1
0
0












7
1
6
2
5
3
4
Spring 2017
COMPSCI 230
Homework 8
eight3:
0












0
0
0
0
0
0
0
1
0
0
0
1
0
0
0
0
0
0
0
1
0
0
0
0
0
1
1
0
1
0
0
0
0
0
0
1
0
0
0
0
0
0
0
0
0
0
1
1
0
0
0
0
0
1
0
0
1
0
0
0
0
1
0
0












7
1
6
2
5
3
4
eight4:
0












0
1
0
0
0
0
0
1
1
0
1
0
0
0
0
0
0
1
0
0
0
0
0
0
0
0
0
0
1
0
0
0
0
0
0
1
0
1
0
0
0
0
0
0
1
0
1
0
0
0
0
0
0
1
0
1
1
0
0
0
0
0
1
0












7
1
6
2
5
3
4
eight5:
0












0
1
0
0
0
0
0
1
1
0
1
0
0
0
0
0
0
1
0
1
0
0
0
0
0
0
1
0
1
0
0
0
0
0
0
1
0
1
0
0
0
0
0
0
1
0
1
0
0
0
0
0
0
1
0
1
1
0
0
0
0
0
1
0












7
1
6
2
5
3
4
Spring 2017
COMPSCI 230
Homework 8
eight6:
0












0
1
0
0
0
0
0
1
1
0
1
0
0
0
0
0
0
1
0
0
0
0
0
0
0
0
0
0
1
0
0
0
0
0
0
1
0
0
0
0
0
0
0
0
0
0
1
0
0
0
0
0
0
1
0
1
1
0
0
0
0
0
1
0












7
1
6
2
5
3
4
eight7:
0












0
0
1
0
0
0
0
0
0
0
1
0
0
0
0
0
1
1
0
0
0
0
1
1
0
0
0
0
1
0
0
0
0
0
0
1
0
1
0
0
0
0
0
0
1
0
1
0
0
0
1
0
0
1
0
0
0
0
1
0
0
0
0
0












7
1
6
2
5
3
4
eight8:
0












0
0
1
0
0
0
0
0
0
0
1
0
0
0
0
0
1
1
0
1
0
0
1
1
0
0
1
0
1
0
0
0
0
0
0
1
0
1
0
0
0
0
0
0
1
0
1
0
0
0
1
0
0
1
0
0
0
0
1
0
0
0
0
0












7
1
6
2
5
3
4
Spring 2017
COMPSCI 230
Homework 8
eight9:
0












0
0
1
0
0
0
0
0
0
0
1
0
0
0
0
0
1
1
0
0
0
0
1
1
0
0
0
0
1
0
0
0
0
0
0
1
0
1
0
0
0
0
0
0
1
0
0
0
0
0
1
0
0
0
0
0
0
0
1
0
0
0
0
0












7
1
6
2
5
3
4
Spring 2017