木其工作室 QQ 928900200
COMP3712/8712 GE COMPUTER PROGRAMMING 3 ASSIGNMENT 2
Assignment 2: Shortest Paths in Graphs
Write a Java program that implements Dijkstra's algorithm to find shortest paths to all vertices in a
directed weighted graph from a given starting vertex.
As mentioned in the lectures, choosing an implementation for the data structure representing a
graph is highly dependent on the sparsity of the graph. For dense graphs, an adjacency matrix
representation is preferable, but for sparse graphs, an adjacency list representation may be more
efficient. You are asked to implement either the adjacency list or adjacency matrix representation
for graphs in your program. If you choose to extend you implementation from Lab03 then it will
need account for weighted edges.
Your program should (a) read in data defining a graph from a text file, (b) build an adjacency list OR
matrix representation of the weighted graph and (c) use the graph representation to find shortest
paths from a specified vertex in the graph to all other vertices, using Dijkstra’s algorithm*.
In addition, you should (d) provide documentation for your program describing how to run it, and
explaining how the code works. You should also justify your choice of data structures*, and details of
your code implementation, in terms of space and time efficiency.
*(e) Use both a standard List and Java’s builtin PriorityQueue for the storing the list of distances
from the given starting vertex to other vertices compare the performance (e.g. runtime, memory,
etc).
(f) Implement your own Priority Queue class using a Heap as described in lectures and tutorials.
Compare your implementation with the List and built-in PriorityQueue compare. Implementing your
own priority queue should allow you to make optimizations specific to the Dijkstra’s Algorithm (e.g.
not being generic) that the Java PriorityQueue must maintain to be widely usable.
Marking Guide
It is expected that all students attempt parts (a)-(f). All students should be able to complete parts
(a)-(d) and most should be able to complete part (e). Part (f) is potentially more challenging. The
table below gives an indication as to the distribution of marks to the parts of the assignment
described above.
Part Weight (100%) Weight (15%)
a 5 2
b 20 3
c 25 3
d 20 3
e 20 3
f 10 2
Total 100 15 COMP3712/8712 GE COMPUTER PROGRAMMING 3 ASSIGNMENT 2
Data Format: There are five example files (in Assignment02-data.zip) provided as example
input files. Feel free to test your program with your own files as well. The data format is as follows:
The first line contains the number of vertices.
Every line after that represents all edges going out from a particular vertex. The first number on each
line is the number of the vertex. After that, there are a number of pairs of integers. The first integer
is the index of a vertex connected to the first vertex in the line. The second integer is the weight of
the edge connecting those two vertices.
For example, if the text file content is:
4
1 2 18 3 12
2 1 22
3 2 16 4 14 4 1 31
then this means that there are 4 vertices, numbered 1 to 4. Vertex 1 has an edge to vertex 2, with
weight 18, and an edge to vertex 3 with weight 12. Vertex 2 has an edge back to vertex 1, but its
weight is 22 (because this is a directed graph, if there is an edge from A to B, it doesn’t mean that
there is an edge from B to A, or if there is, the weights don’t need to be the same).
Output: The graph in graphPosLittle.txt has size of 10 vertices. If coded correctly, a call to find
shortest paths from vertex 1 should produce the following output:
Shortest path to 1: 1: cost = 0
Shortest path to 2: 1 2: cost = 6
Shortest path to 3: 1 7 9 10 4 8 5 3: cost = 35
Shortest path to 4: 1 7 9 10 4: cost = 21
Shortest path to 5: 1 7 9 10 4 8 5: cost = 28
Shortest path to 6: 1 7 9 10 4 8 5 3 6: cost = 37
Shortest path to 7: 1 7: cost = 6
Shortest path to 8: 1 7 9 10 4 8: cost = 25
Shortest path to 9: 1 7 9: cost = 11
Shortest path to 10: 1 7 9 10: cost = 15
Submission of Assignment
Submission will be via FLO. A single zip file containing all source code and required documents. If
there are any special requirements for running the program these should be detailed somewhere,
preferably in separate document about running your program. Additional documents should be in
PDF format. If you have utilised additional data files for your assignment and they are too large to
upload to FLO then provide a link in your documentation.