Description
1 Programming Assignment
The assignment is to implement an algorithm to compute the shortest (i.e. lowest weight) path between
two vertices of an edge-weighted graph. A Java template has been provided containing an empty method
ShortestPath, which takes a single argument consisting of a weighted adjacency matrix for an edgeweighted 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 path from vertex
0 to vertex 1.
For full marks, the finished ShortestPath function must implement Dijkstraβs algorithm and use a heapbased priority queue to select the nest vertex at each step. It is not necessary to store a specific minimum
weight path, only to determine the total weight of such a path. For example, consider the edge-weighted
graph below.
The total weight of a minimum weight path from vertex 0 to vertex 1 is 18. One such minimum weight
path is highlighted (note that there may be more than one path with minimum weight.)
You must use the provided Java template as the basis of your submission, and put your implementation
inside the ShortestPath method in the template. You may not change the name, return type or parameters
of the ShortestPath 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 ShortestPath 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.
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 ShortestPath function to compute the weight of a minimum 0-1 path for
each graph. 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
corresponds to the edge-weighted graph in the previous section. 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
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: Minimum weight of a 0-1 path is 18
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, based on the criteria in the table below. The running times in the table assume
an input graph with π vertices and π edges.
Score (/25) Description
0 – 5 Submission does not compile or does not conform to the
provided template.
6 – 15 The implemented algorithm is not that of Dijkstra or is
substantially inaccurate on the tested inputs.
15 – 20 The implemented algorithm is accurate on all tested
inputs and has an π(π
2 + π log π) running time.
20 – 25 The implemented algorithm is accurate on all tested
inputs, uses a heap-based priority queue to select the next
vertex at each step and has an π(π
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
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. The conneX site will usually 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 4 – WRITTEN
UNIVERSITY OF VICTORIA
1. Write a pseudocode description of the printLCS() algorithm, which prints the longest common
subsequence of two strings π₯ and π¦. Your algorithm takes as input the completed llcs[][] integer
array of longest common subsequence lengths, and the two strings π₯ and π¦. (So, you do not have the
path[][] array – see Lecture 17, slides 98 and 99.) Your algorithm should start at llcs[n][m]
and work its way down to llcs[i][j] where either i = 0 or j = 0 and it should run in π(π +
π) time where π is the length of π₯ and π is the length of π¦.
2. Let π be a flow network, and let π be a flow for π. Prove that for any cut, ο£, of π, the value of π is
equal to the flow across cut ο£, that is, |π| = π(ο£).
3. Consider the following graph πΊ. The edges have costs and are undirected. There are 7 vertices and 10
edges. The edge list πΈ is as follows:
(π΄, π΅) (π΄,π·) (π΄, π) (π΅, πΈ) (π΅, πΉ) (π΅, π) (πΆ, πΉ) (πΆ, π) (π·, πΈ) (πΈ, πΉ)
The Bellman-Ford algorithm makes |π| β 1 = 7 β 1 = 6 passes through the edge list πΈ. Each pass
relaxes the edges in the order they appear in the edge list. As with Dijkstraβs algorithm, we record the
current best known cost π·[π] to reach each vertex π from the start vertex π. Initially π·[π] = 0 and
π·[π] = +β for all the other vertices π β π. Run Bellman-Ford on the given graph, starting at vertex
π, and using the order of set πΈ above, show me the contents of array π·[] after each iteration (6 arrays
in all.)
π΄
V
A
π
t
π΅
V
A
πΆ
V
C
V
A
C
π·
V
C
V
A
C
πΈ
V
C
V
A
C
πΉ
V
C
V
A
C
2
3
9
1
2
2
4
4
1
8