Shortest path problem for an unweighted digraph

advertisement
CPE 103: Fundamentals of Computer Science III
Dr. Hasmik Gharibyan
Lab 21
Shortest path problem for an unweighted digraph
1. Create a new directory for this lab and call it Lab21.
2. Define a public class PathInfo to hold two int values: one for distance (or length of the
path), second for the second-from-last vertex in the path – see the handout.
3. Define a class DiGraphSP exactly as DiGraph class in Lab20, with the only difference
that this new class provides means for solving shortest path problem in an unweighted
graph.
Define a new class DiGraphSP by adding the following methods to DiGraph’s definition:
- a public method shortestPaths, has one int parameter for a given vertex. Returns an array of
PathInfo objects (information of shortest paths going from parameter vertex to all other
vertexes in the graph).
- a public method isTherePath, has two parameters for two given vertexes. Returns true if
there is a path from the first vertex to the second, and false otherwise.
- a public method lengthOfPath, has two parameters for two given vertexes. Returns an
integer – the distance of the second vertex from the first one.
- a public method printPath with two int parameters for two given vertexes. This method
should arrange the printing of the shortest path going from the first vertex to the second.
Attention: This method uses the private support method printPath.
- a private method printPath with three parameters: one PathInfo type array, and two int
parameters for two given vertexes. This method arranges the recursive printing of the shortest
path going from the first parameter vertex to the second.
4. Define an application class DiGraphSPTest by making the necessary additions to the
menu and execution segment of DiGraphTest class from Lab20 to include the following
options:
- for given two vertexes find out if there is a path going from the first vertex to the second.
- for the given two vertexes find the length of the shortest path going from the first vertex
to the second.
- for the given two vertexes print the shortest path going from the first vertex to the
second, if such path exists; and output an informative message otherwise.
- for the given vertex x print shortest distances of every node in the graph from x.
E.g. your output may have the following format:
Distances from vertex x.
Vertex distance
0
3
1
2
2
1
……………….
N-1
2
Attention: invoke shortestPaths method once and arrange the output using the returned array.
5. Edit, compile and execute your program, testing it thoroughly.
Download