Docsity

Labyrinth Implementation: Java Classes and Input File Format, Assignments of Data Structures and Algorithms

Toggle sidebar
bg1
CS 2210a — Data Structures and Algorithms
Assignment 5
Due Date: December 8, 11:55 pm
Total marks: 20
1 Overview
For this assignment you will write a program for finding an exit to a labyrinth. The program will receive as
input a file with a description of the labyrinth, and it will produce as output a path from the entrance to the
exit, if any exists. A labyrinth consists of a set of rooms connected by corridors, some of which are closed
by doors. Each door has associated a number that indicates its type. The program will be given a collection
of keys that will allow it to open some of the doors. Each key has also an associated number indicating its
type. A key can open any door whose type is less than or equal to the type of the key. For example, a key of
type 3 can open any door of types 3, 2, 1, or 0. The door and key types are integers between 0 and 9.
Each key can be used only once; to open a door the key must be left attached to the door and
once attached a key cannot be retrieved.
Your program will store the labyrinth as an undirected graph. Every node of the graph corresponds to a
room, and every edge corresponds to either a corridor that can be used to go from one room to another or to
a door that the program might decide to open. There are two special nodes in this graph corresponding to
the entrance and the exit. A modified depth first search traversal, for example, can be used to find a solution
for the labyrinth.
2 Classes to Implement
You are to implement at least four Java classes: Node, Edge, Graph, and Labyrinth. 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 the 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.
2.1 Class Node
This class represent a node of the graph. You must implement these public methods:

Node(int name): the constructor for the class. Creates a node with the given name. The name of a
node is an integer value between 0 and nāˆ’1, where nis the number of vertices in the graph to which
the node belongs. A node can be marked with a value that is either true or false using the following
method.

setMark(boolean mark): marks the node with the specified value.

boolean getMark(): returns the value with which the node has been marked.

int getName(): returns the name of the node.
2.2 Class Edge
This class represents an edge of the graph. You must implement these public methods:
pf3
pf4
pf5

Partial preview of the text

Download Labyrinth Implementation: Java Classes and Input File Format and more Assignments Data Structures and Algorithms in PDF only on Docsity!

CS 2210a — Data Structures and Algorithms

Assignment 5

Due Date: December 8, 11:55 pm Total marks: 20

1 Overview

For this assignment you will write a program for finding an exit to a labyrinth. The program will receive as input a file with a description of the labyrinth, and it will produce as output a path from the entrance to the exit, if any exists. A labyrinth consists of a set of rooms connected by corridors, some of which are closed by doors. Each door has associated a number that indicates its type. The program will be given a collection of keys that will allow it to open some of the doors. Each key has also an associated number indicating its type. A key can open any door whose type is less than or equal to the type of the key. For example, a key of type 3 can open any door of types 3, 2, 1, or 0. The door and key types are integers between 0 and 9. Each key can be used only once; to open a door the key must be left attached to the door and once attached a key cannot be retrieved. Your program will store the labyrinth as an undirected graph. Every node of the graph corresponds to a room, and every edge corresponds to either a corridor that can be used to go from one room to another or to a door that the program might decide to open. There are two special nodes in this graph corresponding to the entrance and the exit. A modified depth first search traversal, for example, can be used to find a solution for the labyrinth.

2 Classes to Implement

You are to implement at least four Java classes: Node, Edge, Graph, and Labyrinth. 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 the 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.

2.1 Class Node

This class represent a node of the graph. You must implement these public methods:

ˆ Node(int name): the constructor for the class. Creates a node with the given name. The name of a node is an integer value between 0 and n āˆ’ 1 , where n is the number of vertices in the graph to which the node belongs. A node can be marked with a value that is either true or false using the following method. ˆ setMark(boolean mark): marks the node with the specified value. ˆ boolean getMark(): returns the value with which the node has been marked. ˆ int getName(): returns the name of the node.

2.2 Class Edge

This class represents an edge of the graph. You must implement these public methods:

ˆ Edge(Node u, Node v, int type): the constructor for the class. The first two parameters are the endpoints of the edge. The last parameter is the type of the edge; when representing the labyrinth, if an edge represents a door the type of the edge represents the type of the door. ˆ Edge(Node u, Node v, int type, String label): another constructor for the class. The first two parameters are the endpoints of the edge. The last parameters are the type and label of the edge. When representing a labyrinth, you can use the label to distinguish between edges representing corridors and edges representing doors. ˆ Node firstEndpoint(): returns the first endpoint of the edge. ˆ Node secondEndpoint(): returns the second endpoint of the edge. ˆ int getType(): returns the type of the edge. ˆ setType(int newType): sets the type of the edge to the specified value. ˆ String getLabel(): returns the label of the edge. ˆ setLabel(String newLabel): sets the label of the edge to the specified value.

For example consider edge (u, v). The first endpoint of this edge is node u and the second endpoint is node v. The label of the edge could be "corridor" if the edge represents a corridor. The label of the edge could be "door" if the edge represents a door and the type of the edge would correspond to the type of the door.

2.3 Class Graph

This class represents an undirected graph. You must use an adjacency matrix or an adjacency list repre- sentation for the graph. For this class, you must implement all and only the public methods specified in the GraphADT interface and the constructor. Therefore, this class must be declared as follows:

public class Graph implements GraphADT

The public methods in this class are described below.

ˆ Graph(n): creates an empty graph with n nodes and no edges. This is the constructor for the class. The names of the nodes are 0 , 1 ,... , nāˆ’ 1. ˆ insertEdge(Node u, Node v, int edgeType): adds to the graph an edge connecting nodes u and v. The type for this new edge is as indicated by the last parameter. This method throws a GraphException if either node does not exist or if there is already an edge connecting the given nodes. ˆ insertEdge(Node u, Node v, int edgeType, String label): adds to the graph an edge connecting nodes u and v. The type and label for this new edge is as indicated by the last param- eters. This method throws a GraphException if either node does not exist or if there is already an edge connecting the given nodes. ˆ Node getNode(int name): returns the node with the specified name. If no node with this name exists, the method should throw a GraphException. ˆ Iterator incidentEdges(Node u): returns a Java Iterator storing all the edges incident on node u. It returns null if u does not have any edges incident on it. If u is not a node of the graph a GraphException must be thrown. ˆ Edge getEdge(Node u, Node v): returns the edge connecting nodes u and v. This method throws a GraphException if there is no edge between u and v or if u or v are not nodes of the graph. ˆ boolean areAdjacent(Node u, Node v): returns true if nodes u and v are adjacent; returns false otherwise. It throws a GraphException if u or v are not nodes of the graph.

ˆ ’x’: exit of the labyrinth ˆ ’i’: room ˆ ’c’: corridor ˆ ’w’: wall ˆ ’0’, ’1’, ... ’9’: door of type given by the digit; so ’0’ represents a door of type 0, ’1’ represents a door of type 1, and so on.

There is only one entrance and one exit, and each line of the file (except the first four lines) must have the same length. Here is an example of an input file:

30 4 3 0 1 0 1 0 0 0 0 0 0 s1iwi1i cwcwcwc i2i3ici ww4wcwc icicx3i This input represents the following labyrinth

entrance

exit

corridor

room

wall

door of type 1

1 1

2 3

3

4

In this labyrinth one door or type 0 or 1 could be open along with a second door of type 0, 1, 2, or 3 to try to reach the exit.

4 Graph Construction

The rooms (or nodes) are numbered consecutively, starting at zero. For example, the above labyrinth is represented with this graph:

entrance

exit

1

2

3

4

3

1

where dotted edges represent doors and solid edges represent corridors. In the Labyrinth class you need to keep a reference to the entrance and exit vertices.

5 Labyrinth solution

A solution for the labyrinth is any path from the entrance node to the exit node that uses only the specified number of keys of each type that are available to the algorithm. If there are several solutions for the labyrinth, your program can 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 nodes along the path that the DFS traversal has followed. If the current path already has used the maximum allowed number of keys of each type, then no more edges representing doors can be added to it. For example, consider the above graph and let the available number of keys of type 1 be 1 and the number of available keys of type 3 to be also 1. Assume that the algorithm visits first nodes 0, 4, and 5. Note that to move from node 4 to node 5 the algorithm must use the key of type 3 as the passage from node 4 to node 5 is barred by a door of type 2; hence, after reaching node 5 the algorithm has only a key of type 1 available. As the algorithm traverses the graph, all visited nodes get marked. While at node 5, the algorithm cannot next visit nodes 6 or 9, since the available key of type 1 cannot open a door of type 2 or a door of type 3. Hence, the only place where the algorithm can go next is node 1. However, the exit cannot be reached from here, so the algorithm must go back to node 5, and then to nodes 4 and 0. Note that nodes 1, 5 and 4 must be unmarked when DFS traces its steps back, otherwise the algorithm will not be able to find a solution. Next, the algorithm moves from vertex 0 to node 1 using the key of type 1 (whenever opening a door the algorithm must use the available key of smallest type, so in this case the algorithm should not use the key of type 3). From node 1 the algorithm moves to node 5 and then it uses the key of type 3 to go to node 6. At this point the algorithm has used all its keys, so no more doors could be open. Luckily, from node 6 the exit at node 10 can be reached without having to open any additional doors. So, the solution produced by the algorithm is: 0, 1, 5, 6, and 10. Note that the path 0, 1, 5, 9, 10 is not a valid solution as edge (5,9) represents a door of type 4 that cannot be opened with a key of type 3. 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.

ˆ Tests for the Graph class: 4 marks. ˆ Tests for the Labyrinth class: 4 marks. ˆ Coding style: 2 marks. ˆ Graph implementation: 4 marks. ˆ Labyrinth implementation: 4 marks.

10 Handing In Your Program

You must submit an electronic copy of your program through OWL. Please DO NOT put your code in sub-directories and DO NOT submit a compressed file (.zip, .tar, .gzip, ...). When you submit your program, we will receive a copy of it with a datestamp and timestamp. You can submit your program more than once if you need to. We will take the latest program submitted as the final version, and will deduct marks accordingly if it is late.