// Another BFS path-finding implementation // This one doesn't rely on getting all reachable nodes through BFS // Instead, it stops once it finds the node during BFS. // Also, the queue keeps track of paths instead of individual nodes public static List<Node> BFS2(Node root, Node find) { Node.resetTree(root); LinkedList<ArrayList<Node>> queue = new LinkedList<ArrayList<Node>>(); root.setDistance(0); ArrayList<Node> path = new ArrayList<Node>(); path.add(root); queue.add(path); while (queue.size() > 0) { ArrayList<Node> curPath = queue.remove(); Node endNode = curPath.get(curPath.size() - 1); if (endNode == find) { return curPath; } else { List<Node> endNodeChildren = endNode.getChildren(); for (Node node : endNodeChildren) { if (node.getDistance() == Double.POSITIVE_INFINITY) { node.setDistance(endNode.getDistance() + 1); node.setParent(endNode); ArrayList<Node> newPath = new ArrayList<Node>(curPath); newPath.add(node); queue.add(newPath); } } } } return Collections.emptyList(); }
// Breadth first search // Returns a list of all reachable nodes in the order visited public static List<Node> BFS(Node root) { Node.resetTree(root); List<Node> reachable = new ArrayList<Node>(); LinkedList<Node> queue = new LinkedList<Node>(); root.setDistance(0); queue.add(root); reachable.add(root); while (queue.size() > 0) { Node cur = queue.remove(); List<Node> children = cur.getChildren(); for (Node node : children) { if (node.getDistance() == Double.POSITIVE_INFINITY) { node.setDistance(cur.getDistance() + 1); node.setParent(cur); queue.add(node); reachable.add(node); } } } return reachable; }
public static void main(String[] args) { Node<String> prez = Node.getTree(); List<Node> reachable = BFS(prez); System.out.println(reachable.size()); for (Node node : reachable) { System.out.println("Node: " + node + " distance from root: " + node.getDistance()); } // This is fragile, and might break if Node.getTree() changes. Node<String> pop = prez.getChildren().get(1).getChildren().get(0).getChildren().get(0); List<Node> path = BFS2(prez, pop); System.out.println(path.size()); for (Node node : path) { System.out.println("Node: " + node + " distance from root: " + node.getDistance()); } }
@Test( dataProvider = "doubleNodes", dataProviderClass = NodeProvider.class, dependsOnMethods = {"subtractionTest"}) public void distanceTest(Node a, Node b) { Node subtracted = a.subtract(b); double distance = a.getDistance(b); assert distance > 0.; assert distance > Math.abs(subtracted.getX()); assert distance > Math.abs(subtracted.getY()); assert distance < Math.abs(subtracted.getX()) + Math.abs(subtracted.getY()); }
@Override // Process every node that is gray and explore its neighbors public void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException { Node child; String nodeProperties = value.toString(); // Create a Node Node node = new Node(nodeProperties); if (node.getColor() == Node.Color.GRAY) { // 'G' int distance = node.getDistance(); int idParent = node.getId(); // Explore their neighbors ArrayList<Integer> neigh = node.getNeighbors(); for (int i : neigh) { child = new Node(i, distance + 1, idParent); // Node(IDP) // Writing the explored neighbors context.write(new IntWritable(child.getId()), new Text(child.toString())); } // Change the explored node to Black color node.setColor(Node.Color.BLACK); } // Black, White and Gray => Black nodes Written context.write(new IntWritable(node.getId()), new Text(node.toString())); } // End of map()
/* * Finds the shortest route from a specified start node to a specified end node */ private static void findShortestRoute(String inputString) { HashMap<Character, Node> nodeMapTemp = new HashMap<Character, Node>(nodeMap); HashMap<Character, Node> nodeMapDijkstra = new HashMap<Character, Node>(); PriorityQueue<Node> pq = new PriorityQueue<Node>(); Node current; Node currentTo; int shortestRoute = INFINITY; Set nodeSet; first = inputString.charAt(0); second = inputString.charAt(2); for (char key : nodeMap.keySet()) { nodeMap.get(key).setDistance(INFINITY); } if (first == second) { edgeList = nodeMapTemp.get(first).getEdges(); for (i = 0; i < edgeList.size(); i++) { current = nodeMapTemp.get(edgeList.get(i).getEdgeDestination()); current.setDistance(edgeList.get(i).getEdgeLength()); pq.add(current); } nodeMapTemp.remove(first); while (pq.size() != 0) { current = pq.peek(); edgeList = current.getEdges(); if (current.getName() == second) { break; } else { nodeMapDijkstra.put(pq.peek().getName(), pq.peek()); nodeMapTemp.remove(pq.poll().getName()); } for (i = 0; i < edgeList.size(); i++) { if (nodeMapTemp.get(edgeList.get(i).getEdgeDestination()) != null) { currentTo = nodeMapTemp.get(edgeList.get(i).getEdgeDestination()); if (currentTo.getDistance() > edgeList.get(i).getEdgeLength() + current.getDistance()) { currentTo.setDistance(edgeList.get(i).getEdgeLength() + current.getDistance()); } pq.add(currentTo); } else { } } } for (char key : nodeMapDijkstra.keySet()) { edgeList = nodeMapDijkstra.get(key).getEdges(); for (i = 0; i < edgeList.size(); i++) { if (edgeList.get(i).getEdgeDestination() == second) { if (nodeMapDijkstra.get(key).getDistance() + edgeList.get(i).getEdgeLength() < shortestRoute) { shortestRoute = nodeMapDijkstra.get(key).getDistance() + edgeList.get(i).getEdgeLength(); } } } } } else { edgeList = nodeMapTemp.get(first).getEdges(); for (i = 0; i < edgeList.size(); i++) { current = nodeMapTemp.get(edgeList.get(i).getEdgeDestination()); current.setDistance(edgeList.get(i).getEdgeLength()); pq.add(current); } nodeMapTemp.remove(first); while (nodeMapTemp.get(second) != null) { current = pq.peek(); edgeList = current.getEdges(); if (current.getName() == second) { break; } else { nodeMapTemp.remove(pq.poll().getName()); } for (i = 0; i < edgeList.size(); i++) { if (nodeMapTemp.get(edgeList.get(i).getEdgeDestination()) != null) { currentTo = nodeMapTemp.get(edgeList.get(i).getEdgeDestination()); if (currentTo.getDistance() > edgeList.get(i).getEdgeLength() + current.getDistance()) { currentTo.setDistance(edgeList.get(i).getEdgeLength() + current.getDistance()); } pq.add(currentTo); } } } shortestRoute = nodeMapTemp.get(second).getDistance(); } try { bw.write(Integer.toString(shortestRoute)); bw.newLine(); } catch (IOException e) { System.out.println(e); } }