



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Community
Ask the community for help and clear up your study doubts
Discover the best universities in your country according to Docsity users
Free resources
Download our free guides on studying techniques, anxiety management strategies, and thesis advice from Docsity tutors
Instructions for assignment 5 of cs 2210a, data structures and algorithms. Students are required to write a java program that finds a path between two specified locations in a roadmap with certain conditions, such as the maximum number of private and construction roads that the path can use. The required classes, methods, and input file format. It also includes hints and code provided for testing.
Typology: Study Guides, Projects, Research
1 / 6
This page cannot be seen from the preview
Don't miss anything!
Due Date: December 7, 11:55 PM Total marks: 20
For this assignment you need to write a program that finds a path between two specified locations in a roadmap that satisfies certain conditions specified below. The roadmap has 3 kinds of roads: public roads, private roads that have a fee to be used, and construction roads that are under repair, so the traffic through them is slow. Your program will receive as input a file with a description of the roadmap, the starting point s, the destination e, the maximum number p of private roads that the path from s to e can use, and the maximum number c of construction roads that the path from s to e can use. Your program then will try to find a path from s to e as specified. You must store the roadmap as an undirected graph. Every edge of the graph represents a road and every node represents either the intersection of two roads or the end of a road. There are two special nodes in this graph denoting the starting point s and the destination e. A modified depth first search traversal, for example, can be used to find a path as required, if one exists.
You are to implement at least four Java classes: Node, Edge, Graph, and MyMap. You can implement more classes if you need to, as long as you follow good program design and information-hiding principles. You must write all code yourself. You cannot use code from the textbook, the Internet, other students, or any other sources. However, you are allowed to use the algorithms discussed in class. For each one of the classes below, you can implement more private methods if you want to, but you cannot implement additional public methods.
This class represent a node of the graph. You must implement these public methods:
This class represents an edge of the graph. You must implement these public methods:
This class represents an undirected graph. You must use an adjacency matrix or an adjacency list representation for the graph. For this class, you must implement all the public methods specified in the provided GraphADT interface plus the constructor. Hence, this class must be declared as follows:
public class Graph implements GraphADT The public methods in this class are described below.
The last four methods throw a GraphException if u or v are not nodes of the graph.
This class represents the roadmap. A graph will be used to store the map and to try to find a path from the starting point to the destination satisfying the conditions stated above. For this class you must implement the following public methods:
For the rest of the input file, R can be any of the following characters: ’+’ or ’B’. H could be ’B’, ’V’, ’C’, or ’P’. The meaning of the above characters is as follows:
Each line of the file (except the first eight lines) must have the same length because, as mentioned above, the only roadmaps that we will consider are grid roadmaps. Here is an example of an input file:
30 0 10 4 3 1 1 +P+B+V+ PBCBPBP +V+V+P+ BBVBPBP +P+V+V+
This input represents the following roadmap
starting point
destination
private road
construction road
For the above example, a path from node with id 0 to node with id 10 is sought that uses at most one private and one construction roads.
The roadmap is represented as a graph in which node ids are numbered consecutively, starting at zero from left to right and top to bottom. For example, the above roadmap is represented with this graph:
where dotted edges represent private roads, solid edges represent public roads, and wavy edges represent construction roads. In the MyMap class you need to keep a reference to the starting and destination nodes.
Your program must find any path from the starting vertex to the destination vertex that uses at most the specified number of private and construction roads. The path can use any number of public roads. If there are several such paths, your program might return any one of them. The solution can be found, for example, by using a modified DFS traversal. While traversing the graph, your algorithm needs to keep track of the vertices along the path that the DFS traversal has followed. If the current path has already used all allowed private roads, then no more private-road edges can be added to it. Similarly, if the current path has already used all allowed construction roads, no more construction-road edges can be added to the path. For example, consider the above graph and let the maximum allowed number of private roads be 1 and the maximum allowed number of construction roads be 1. Assume that the algorithm visits vertex 0, then 4, and then 5. As the algorithm traverses the graph, all visited vertices get marked. While at vertex 5, the algorithm cannot next visit vertices 6 or 9, since then two private roads would have been used by the current path. Hence, the algorithm goes next to vertex 1. However, the destination cannot be reached from here, so the algorithm must go back to vertex 5, and then back to vertices 4 and 0. Note that vertices 1, 5 and 4 must be unmarked when the algorithm steps back, as otherwise the algorithm will not be able to find a solution. Next, the algorithm will move from vertex 0 to vertex 1, and then to 5. Since edge (1, 5) represents a construction road, the current path has used 1 construction road and no private roads. The algorithm can then move to node 6 and then to 10 as the rest of the path uses only one private road. Therefore, the solution produced by the algorithm is: 0, 1, 5, 6, and 10. You do not have to implement the above algorithm if you do not want to. Please feel free to design your own solution for the problem.
You can download from the course’s website the following files: TestDict.java, DrawMap.java, Board.java, Path.java, GraphADT.java, GraphException, MapException, and several image files. The main method is in class Path.java , so to run your program you will type java Path input file