/** * Execute Dijkstra's algorithm on the given graph to find the shortest path from {@code * sourceNode} to {@code destinationNode}. The length of the path is defined as the sum of the * edge weights, which are provided in the edge file. Every time the algorithm pulls a node into * the cloud, call {@link Logger#movedCharacter(String, String)} with the name {@link * #PATH_CHARACTER} and the location equal to the new cloud node. Every time the algorithm uses an * edge to relax the known distances to a node <b>which is not yet in the cloud</b>, call {@link * Logger#traversedEdge(String, String, String)} with the tag {@link #DIJKSTRA_TAG}, the node just * pulled into the cloud as the second argument, and the node whose known distance may be relaxed * as the third argument. Do this even if the known distance to the node doesn't decrease. For * testing purposes run the algorithm until all nodes are in the cloud and be sure to relax the * edges using alphabetical ordering of the nodes. * * @return the sequence of node GUIDs of the shortest path found, or null if there is no such path */ public List<Graph.Node> dijkstra(Graph.Node sourceNode, Graph.Node destinationNode) { HashMap<Graph.Node, Double> dist = new HashMap<Graph.Node, Double>(); HashMap<Graph.Node, Graph.Node> previous = new HashMap<Graph.Node, Graph.Node>(); for (Graph.Node node : g.getNodes()) { dist.put(node, Double.POSITIVE_INFINITY); previous.put(node, null); } dist.put(sourceNode, 0.0); Set<Graph.Node> nodes = g.getNodes(); while (!nodes.isEmpty()) { Graph.Node current = closest(nodes, dist); if (dist.get(current) == Double.POSITIVE_INFINITY) break; nodes.remove(current); if (current == destinationNode) { List<Graph.Node> sequence = new ArrayList<Graph.Node>(); while (previous.get(current) != null) { sequence.add(current); current = previous.get(current); } sequence.add(sourceNode); Collections.reverse(sequence); return sequence; } for (Graph.Node neighbor : current.getNeighbors().keySet()) { double alt = dist.get(current) + current.getNeighbors().get(neighbor); if (alt < dist.get(neighbor)) { dist.put(neighbor, alt); previous.put(neighbor, current); } } } return new ArrayList<Graph.Node>(); }
public List<Graph.Node> dfs(List<Graph.Node> marked, Graph.Node node, Graph.Node target) { marked.add(node); Set<Graph.Node> neighbors = node.getNeighbors().keySet(); for (Graph.Node neighbor : neighbors) { if (!marked.contains(neighbor)) { if (neighbor.equals(target)) { List<Graph.Node> list = new ArrayList<Graph.Node>(); list.add(target); return list; } else { List<Graph.Node> list = dfs(marked, neighbor, target); if (list.contains(target)) { list.add(neighbor); return list; } } } } return new ArrayList<Graph.Node>(); }
public HashMap<Graph.Node, Double> dijkstra2(Graph.Node sourceNode, Graph.Node destinationNode) { HashMap<Graph.Node, Double> dist = new HashMap<Graph.Node, Double>(); HashMap<Graph.Node, Graph.Node> previous = new HashMap<Graph.Node, Graph.Node>(); for (Graph.Node node : g.getNodes()) { dist.put(node, Double.POSITIVE_INFINITY); previous.put(node, null); } dist.put(sourceNode, 0.0); Set<Graph.Node> nodes = g.getNodes(); while (!nodes.isEmpty()) { Graph.Node current = closest(nodes, dist); if (dist.get(current) == Double.POSITIVE_INFINITY) break; nodes.remove(current); for (Graph.Node neighbor : current.getNeighbors().keySet()) { double alt = dist.get(current) + current.getNeighbors().get(neighbor); if (alt < dist.get(current)) { dist.put(neighbor, alt); previous.put(neighbor, current); } } } return dist; }