public static void dijkstra( ListenableUndirectedWeightedGraph<String, DefaultWeightedEdge> g, String initNode, String endNode) { // Variable creation/assignment resultNum++; int loopCnt = 0; // Keep count of loops String currNode = initNode; // always starts as initial node Set<String> allVerts = new LinkedHashSet<String>(); allVerts = g.vertexSet(); // All vertices in the graph Set<DefaultWeightedEdge> allEdges = new LinkedHashSet<DefaultWeightedEdge>(); allEdges = g.edgeSet(); // All edges in the graph Set<String> visited = new LinkedHashSet<String>(); // List of visited vertices Set<String> unvisited = new LinkedHashSet<String>(); // List of unvisited vertices for (String v : allVerts) // adding the rest of the nodes to unvisited set { if (v != initNode) { unvisited.add(v); } } Map<String, Double> vertVals = new HashMap<String, Double>(); for (String v : allVerts) // setting a value of INFINITY to all vertices. { vertVals.put(v, Double.POSITIVE_INFINITY); } vertVals.put(initNode, 0.0); // Setting the initial node value to 0 visited.add(initNode); // adding initial node to visited set List<String> path = new ArrayList<String>(); path.add(initNode); // adding initial node to path // END Variable creation/assignment System.out.println("Result " + resultNum + ":"); while (!visited.contains(endNode) && loopCnt < 1000000) { loopCnt++; for (DefaultWeightedEdge e : allEdges) // Set weight values for neighbors of current node { if (g.getEdgeSource(e) == currNode && g.getEdgeWeight(e) < (vertVals.get(g.getEdgeTarget(e)) + vertVals.get(currNode)) && !visited.contains(g.getEdgeTarget(e))) // if the edge comes from the currentNode AND the value of that edges target is greater than // the distance to the current node AND the target hasn't been visited { vertVals.put( g.getEdgeTarget(e), g.getEdgeWeight(e) + vertVals.get(currNode)); // Update value of that vertex } else if (g.getEdgeTarget(e) == currNode && g.getEdgeWeight(e) < (vertVals.get(g.getEdgeSource(e)) + vertVals.get(currNode)) && !visited.contains(g.getEdgeSource(e))) // if the edge comes to the currentNode AND the value of that edges source is greater than // the distance from the starting node to the current node AND the source hasn't been // visited { vertVals.put( g.getEdgeSource(e), g.getEdgeWeight(e) + vertVals.get(currNode)); // Update value of that vertex } } Double lowest = Double.POSITIVE_INFINITY; // used to find lowest of the current neighbor vertices String n = ""; // temporary string for storing vertex names for (DefaultWeightedEdge e : allEdges) // finding the lowest of the current neighbor vertices to set to currentNode { if (g.getEdgeSource(e) == currNode && g.getEdgeWeight(e) < lowest && !visited.contains(g.getEdgeTarget(e))) // if the edge comes from the currentNode AND it's the smallest vertex value that has not // been visited and . { lowest = g.getEdgeWeight(e); n = g.getEdgeTarget(e); } else if (g.getEdgeTarget(e) == currNode && g.getEdgeWeight(e) < lowest && !visited.contains(g.getEdgeSource(e))) { lowest = g.getEdgeWeight(e); n = g.getEdgeSource(e); } } if (n != "") { currNode = n; visited.add(currNode); path.add(currNode); } else { currNode = path.get(path.size() - 2); visited.add(currNode); } } System.out.println("Shortest Path: " + path); System.out.println("Cost: " + vertVals.get(endNode)); System.out.println(); }