CSC 226 ALGORITHMS AND DATA STRUCTURES II ASSIGNMENT 3 – PROGRAM AND WRITTEN

$25.00

Category: You will Instantly receive a download link for .zip solution file upon Payment || To Order Original Work Click Custom Order?

Description

5/5 - (4 votes)

1 Programming Assignment
The assignment is to implement Kruskal’s algorithm to find a minimum weight spanning tree of an
edge-weighted graph. A Java template has been provided containing an empty method MWST, which
takes a single argument consisting of a weighted adjacency matrix for an edge-weighted graph 𝐺. The
expected behavior of the method is as follows:
Input: An 𝑛 Γ— 𝑛 array 𝐺 representing an edge-weighted graph.
Output: An integer value corresponding to the total weight of a minimum weight spanning tree
of 𝐺.
A correct implementation of the MWST function will use Kruskal’s algorithm to find a minimum weight
spanning tree, then sum the weights of each selected edge to produce the return value. For example,
consider the edge-weighted graph below.
The darkened edges of the graph form a minimum weight spanning tree, and the total weight is 35.
You must use the provided Java template as the basis of your submission, and put your implementation
inside the MWST method in the template. You may not change the name, return type or parameters of
the MWST method. You may add additional methods as needed. The main method in the template
contains code to help you test your implementation by entering test data or reading it from a file. You
may modify the main method to help with testing, but only the contents of the MWST method (and any
methods you have added) will be marked, since the main function will be deleted before marking begins.
Please read through the comments in the template file before starting.
3
6
5 4
10
7
9
8
11
1
12
13 2
2
2 Input Format
The testing code in the main function of the template reads a sequence of graphs in a weighted adjacency
matrix format and uses the MWST function to compute the weight of a minimum weight spanning tree
for each. A weighted adjacency matrix 𝐴 for an edge-weighted graph 𝐺 on 𝑛 vertices is an 𝑛 Γ— 𝑛 matrix
where entry (𝑖,𝑗) gives the weight of the edge between vertices 𝑖 and 𝑗 (or 0 if no edge exists). For
example, the matrix
𝐴 =
[

 

 

 

0 0 0 0 0 12 13 0
0 0 6 0 0 0 0 3
0 6 0 4 0 0 0 5
0 0 4 0 10 0 0 7
0 0 0 10 0 11 8 9
12 0 0 0 11 0 1 0
13 0 0 0 8 1 0 2
0 3 5 7 9 0 2 0]

 

 

 

corresponds to the edge-weighted graph below. Note that the weighted adjacency matrix for an
undirected graph is always symmetric.
The input format used by the testing code in main consists of the number of vertices 𝑛 followed by the
𝑛 Γ— 𝑛 weighted adjacency matrix. The graph above would be specified as follows:
8
0 0 0 0 0 12 13 0
0 0 6 0 0 0 0 3
0 6 0 4 0 0 0 5
0 0 4 0 10 0 0 7
0 0 0 10 0 11 8 9
12 0 0 0 11 0 1 0
13 0 0 0 8 1 0 2
0 3 5 7 9 0 2 0
1 2
3
7
6
0
5 4
3
6
5 4
10
7
9
8
11
1
12
13 2
3
3 Test Datasets
A collection of randomly generated edge-weighted graphs has been uploaded to conneX. Your
assignment will be tested on graphs similar but not identical to the uploaded graphs. You are encouraged
to create your own test inputs to ensure that your implementation functions correctly in all cases.
4 Sample Run
The output of a model solution on the graph above is given in the listing below. Console input is shown
in blue.
Reading input values from stdin.
Reading graph 1
8
0 0 0 0 0 12 13 0
0 0 6 0 0 0 0 3
0 6 0 4 0 0 0 5
0 0 4 0 10 0 0 7
0 0 0 10 0 11 8 9
12 0 0 0 11 0 1 0
13 0 0 0 8 1 0 2
0 3 5 7 9 0 2 0
Graph 1: Total weight is 35
Processed 1 graph.
Average Time (seconds): 0.00
5 Evaluation Criteria
The programming assignment will be marked out of 25, based on a combination of automated testing
and human inspection. To receive full marks, the running time of the implemented algorithm should be
at most 𝑂(𝑛
2 + π‘š log π‘š) on a graph 𝐺 with 𝑛 vertices and π‘š edges.
Score (/50) Description
0 βˆ’ 5 Submission does not compile or does not conform to the
provided template.
5 βˆ’ 15 The implemented algorithm is not Kruskal’s algorithm or
is substantially inaccurate on the tested inputs.
15 βˆ’ 20 The implemented algorithm is accurate on all tested
inputs.
20 βˆ’ 25 The implemented algorithm is accurate on all tested
inputs and has a 𝑂(𝑛
2 + π‘š log π‘š) running time.
To be properly tested, every submission must compile correctly as submitted, and must be based on the
provided template. You may only submit one source file. If your submission does not compile for any
reason (even trivial mistakes like typos), or was not based on the template, it will receive at most
5 out of 25. The best way to make sure your submission is correct is to download it from conneX after
4
submitting and test it. You are not permitted to revise your submission after the due date, and late
submissions will not be accepted, so you should ensure that you have submitted the correct version of
your code before the due date. conneX will allow you to change your submission before the due date if
you notice a mistake. After submitting your assignment, conneX will automatically send you a
confirmation email. If you do not receive such an email, your submission was not received. If you have
problems with the submission process, send an email to the instructor before the due date.

CSC 226 FALL 2016
ALGORITHMS AND DATA STRUCTURES II
ASSIGNMENT 2 – WRITTEN
UNIVERSITY OF VICTORIA
1. Suppose G is a connected graph with distinct positive integer edge weights. Prove that G has exactly
one minimum spanning tree T. You may invoke the cut property and/or the cycle property if needed.
2. Modify the statement of the cut property so that it is true, even for the case of edge weights that are
not necessarily distinct. Prove that your modified statement is true when edge weights may be
repeated.
3. Consider the following graph 𝐺 = (𝑉, 𝐸), where each edge 𝑒 = (𝑒, 𝑣) has a weight 𝑀(𝑒, 𝑣), 𝑉 =
{π‘Ž, 𝑏, 𝑐, 𝑑, 𝑒} and 𝐸 = {(π‘Ž, 𝑏), (𝑏, 𝑐), (𝑐, 𝑑), (𝑑, π‘Ž), (𝑏, 𝑒), (𝑐, 𝑒), (𝑑, 𝑒)}. The weights on the edges
are:
We define the profile of a graph 𝐺 to be the list of its edge weights in non-decreasing order. For
example, in the graph 𝐺 above, the profile is 1,1,2,2,3,3,3. The profile of an MST 𝑇 is the list of its
edge weights in non-decreasing order.
Find every MST 𝑇 for the graph 𝐺 above: for each 𝑇, list the edges in alphabetical order, give the
weight of 𝑇 and its profile 𝑝. We know that the weights of all the MSTs, by definition, must be the
same. Based on the results can you make a conjecture about profiles of MST’s?
4. In the Algorithms, 4th Edition textbook by Sedgewick, in Chapter 1.5 he develops an implementation
of the union-find data structure using an integer array of size 𝑁 for 𝑁 vertices called the site-indexed
id[] array. Show the contents of the id[] array along with the corresponding forest of trees
representation (see example on page 225) for each input pair 9-0 3-4 5-8 7-2 2-1 5-7 0-3 4-2 when you
use the weighted quick-union algorithm (page 228). This algorithm corresponds to the union-by-size
algorithm discussed in lecture. (This is actually exercise 1.5.3 in the text.)
5. For that matter, why don’t you do exercise 1.5.9 from the textbook while you’re at it. Draw the tree
corresponding to the id[] array given below:
i 0 1 2 3 4 5 6 7 8 9
id[i] 1 1 3 1 5 6 1 3 4 5
Can this be the result of running weighted quick-union? Explain why this is impossible or give a
sequence of operations that results in this array.
1
1
3
3
3
c
b
d e
a
2
2