Hello, dear friend, you can consult us at any time if you have any questions, add WeChat: THEend8_
(10 marks + 2 demo marks)
This assignment involves an extension to the single source - single destination shortest path
problem.
The Program
Your program should:
1. Open the text file “ass3.txt”. (Note: “ass3.txt” should be a hardcoded as a constant.)
2. Read a graph from the file.
3. Find the shortest path between the start and goal vertices specified in the file.
4. Print out the vertices on the path, in order from start to goal.
5. Print out the length of this path and the total number of vertices visited.
6. Devise a strategy for determining the second shortest path between the vertices.
7. Find the second shortest path between the start and goal vertices specified in the file.
8. Print out the vertices on the path, in order from start to goal.
9. Print out the length of this path and the total number of vertices visited.
10. Optimize your shortest and second shortest path algorithms to improve their performance.
The data files are constructed as follows:
? Two integers: nVertices and nEdges, the number of vertices and edges in the graph.
? nVertices triples consisting of the label and the x- and y-coordinates of each vertex.
? nEdges triples consisting of the labels of the start and end vertices of each edge, along
with its weight. Note: the weight associated with an edge will be greater than or equal to
the Euclidean distance between its start and end vertices as determined by their
coordinates.
? Two labels, the indicating the start and goal vertices for which the paths are required.
A proposed solution to the second shortest path problem is as follows:
For each edge ei
on the shortest path:
Find the shortest path on (V, E – {ei}). // shortest path without edge ei
The shortest of these is the second shortest path.
Questions
Think about this! Is this proposed solution correct always?
? What if we require that the second shortest path be longer than the shortest path?
? What if the graph contains cycles?
? What if the graph is undirected?
Explain your answers. If necessary explain how you might modify the proposed algorithm to
address any issues that you identify.
Note: you may implement either the proposed solution or any modification you develop. You are
not required to implement a modified proposal if you do not wish to do so.
2
Step-1 (Week-10 demo, 2 marks)
For step 1, you should read the data file into adjacency lists, or an adjacency matrix, and print
on the screen the first 5 vertices and the vertices they are connected to together with their
weights. e.g.:
a: c(35) d(27) e(48)
b: d(35) g(27)
c: b(125) e(20) f(56) h(31)
. . .
Note: The data shown above is for format purposes only.
Step-2 (Discovering the Shortest Path) (2 marks)