Programming Assignment

The road network of NewVille has been recently upgraded, thanks to your help. Now the Fireﬁghters can respond to emergencies more quickly than before. But the traﬃc condition is not always good and the ﬁre vehicles must ﬁnd the shortest paths to their destinations. Your job is to help them ﬁnd the shortest paths to all the places of the town from their headquarter.

Input: An edge-weighted graph of n nodes (representing places in NewVille) and the position of the Fireﬁghters’ headquarter (the source vertex). The weight on edge (u,v) represents the time required to go from u to v or v to u at a certain time. Output: The shortest paths to all the places (including the headquarter) from the headquarter. and the total time required to get there.

A Java template has been provided containing two empty functions. The function ShortestPath takes a two dimensional integer array G, that represents the graph, and an integer representing the source vertex, and calculates and stores the shortest path to all the vertices from the source vertex. The function PrintPaths takes the source vertex as an argument and prints out the paths in a speciﬁc format. Your task is to write both the functions. The input might be disconnected, but you can assume that the graphs are undirected. You must use the provided Java template as the basis of your submission, and put your implementation inside the ShortestPath and PrintPaths functions in the template. You may not change the name, return type or parameters of those functions. The main function in the template contains code to help you test your implementation by reading it from a ﬁle or from the console. A sample ﬁle is also provided. You may modify the main function or any other function, because your submission will be tested using a diﬀerent main function. You can use any helper methods or any helper classes. You can use any built-in class or write your own classes and data structures. We advise you to put all the classes you write in the same ﬁle, but no other class except the provided one should be declared as a public class.

2 Examples

The input format is exactly like Assignment 2. The ﬁrst line denotes the number of vertices n in the graph and the following n lines represent the n×n adjacency matrix. A 0 at row i and column j in the adjacency matrix means that there is no edge between vertices i and j, otherwise that number

1

denotes the edge weight between i and j. You can assume that the edge weights are between 1 and 1000.

Sample input: 3 0 5 6 5 0 7 6 7 0 6 0 7 9 0 0 14 7 0 10 15 0 0 9 10 0 11 0 2 0 15 11 0 6 0 0 0 0 6 0 9 14 0 2 0 9 0

Sample output:

Reading graph 1 The path from 0 to 0 is: 0 and the total distance is : 0 The path from 0 to 1 is: 0−− 1 and the total distance is : 5 The path from 0 to 2 is: 0−− 2 and the total distance is : 6 Reading graph 2 The path from 0 to 0 is: 0 and the total distance is : 0 The path from 0 to 1 is: 0−− 1 and the total distance is : 7 The path from 0 to 2 is: 0−− 2 and the total distance is : 9 The path from 0 to 3 is: 0−− 2−− 3 and the total distance is : 20 The path from 0 to 4 is: 0−− 2−− 5−− 4 and the total distance is : 20 The path from 0 to 5 is: 0−− 2−− 5 and the total distance is : 11 Processed 2 graphs. Average Time (seconds): 0.00

Sale!