Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * A method that returns a Java Iterator that contains the list of Node objects that
- * states a path from the starting Node to the destination Node if such path exists.
- * The method also considers the amount of money the traveling car initial has and will
- * have during its travels. If such path doesn't exist, it will simply return null
- *
- * @param start the integer value that is the name of the Node object the car will
- * start at
- * @param destination the integer value that is the name of the Node object the car will
- * end at
- * @param initialMoney the integer value that is the initial amount of money the car will
- * start with
- * @return finalPath.iterator() the Java Iterator that holds the pathway of Nodes to get from the
- * starting Node to the destination Node
- * @throws GraphException when a specific Node is not in the Graph
- */
- public Iterator findPath(int start, int destination, int initialMoney) throws GraphException {
- // Sets the currentNode as the starting Node and sets the money the car current has
- Node currentNode = roadGraph.getNode(start);
- int money = initialMoney;
- // marks the currentNode to true to avoid going into an infinite loop
- currentNode.setMark(true);
- // adds the currentNode into the arrayList variable called finalPath
- finalPath.add(currentNode);
- // Checks if the name of the Node the method is currently on is the same as the name of the
- // destination Node. If true, ends the recursion, sets the boolean variable to state that the
- // a path has been detected and is ready to empty the execution stack
- if (start == destination) {
- pathFinish = true;
- return finalPath.iterator();
- }
- // Otherwise, continue
- else {
- // Retreive a Java Iterator that holds any available edges with the current node the method
- // is on
- Iterator availPath = roadGraph.incidentEdges(currentNode);
- // A while loop that continues until there aren't any Edge objects to look through
- while (availPath.hasNext()) {
- // Sets up a temporary Edge object to check to do a recursive call and see if it leads to
- // the destination Node
- Edge tempEdge = (Edge) availPath.next();
- Node nextNode = tempEdge.secondEndpoint();
- // Checks if the Node on the other end of the Edge it is temporarily checking is marked or
- // not. If it isn't, it continues to go and check what kind of Edge it is to adjust the money
- // the car will subsequently hold
- if (nextNode.getMark() == false) {
- if (tempEdge.getType() == 1 && money - this.toll >= 0)
- findPath(nextNode.getName(), destination, money - this.toll);
- else if (tempEdge.getType() == -1)
- findPath(nextNode.getName(), destination, money + this.gain);
- else if (tempEdge.getType() == 0)
- findPath(nextNode.getName(), destination, money);
- // Checks if a path has been made, and if true, simply return and empty the execution stack
- if (pathFinish == true)
- return finalPath.iterator();
- }
- }
- // From here, it indicates that a path was not found in the currentNode it was on and must be
- // removed from the finalPath arrayList and is ready to be unmarked for future use
- finalPath.remove(currentNode);
- currentNode.setMark(false);
- // Checks if the finalPath arrayList is empty and if so, return null
- if (finalPath.isEmpty())
- return null;
- // Otherwise return the Java Iterator
- return finalPath.iterator();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement