广度优先算法之狄克斯特拉算法
package cn.wizzer.common.util;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class A {
// the graph
private static Map<String, Map<String, Double>> graph = new HashMap<>();
private static List<String> processed = new ArrayList<>();
private static String findLowestCostNode(Map<String, Double> costs) {
Double lowestCost = Double.POSITIVE_INFINITY;
String lowestCostNode = null;
// Go through each node
for (Map.Entry<String, Double> node : costs.entrySet()) {
Double cost = node.getValue();
// If it's the lowest cost so far and hasn't been processed yet...
if (cost < lowestCost && !processed.contains(node.getKey())) {
// ... set it as the new lowest-cost node.
lowestCost = cost;
lowestCostNode = node.getKey();
}
}
return lowestCostNode;
}
public static void main(String[] args) {
graph.put("start", new HashMap<>());
graph.get("start").put("a", 5.0);
graph.get("start").put("b", 2.0);
graph.put("a", new HashMap<>());
graph.get("a").put("fin", 1.0);
//增加c点
graph.get("a").put("c", 3.0);
graph.put("b", new HashMap<>());
graph.get("b").put("a", 3.0);
graph.get("b").put("c", 5.0);
//graph.get("b").put("fin", 5.0);
graph.put("c", new HashMap<>());
graph.get("c").put("fin", 4.0);
// graph.get("b").put("c", 5.0);
graph.put("fin", new HashMap<>());
// The costs table
Map<String, Double> costs = new HashMap<>();
costs.put("a", 5.0);
costs.put("b", 2.0);
costs.put("c", Double.POSITIVE_INFINITY);
costs.put("fin", Double.POSITIVE_INFINITY);
// the parents table
Map<String, String> parents = new HashMap<>();
parents.put("a", "start");
parents.put("b", "start");
parents.put("fin", null);
String node = findLowestCostNode(costs);
while (node != null) {
Double cost = costs.get(node);
// Go through all the neighbors of this node
Map<String, Double> neighbors = graph.get(node);
for (String n : neighbors.keySet()) {
double newCost = cost + neighbors.get(n);
// If it's cheaper to get to this neighbor by going through this node
if (newCost<costs.get(n) ) {
// ... update the cost for this node
costs.put(n, newCost);
// This node becomes the new parent for this neighbor.
parents.put(n, node);
}
}
// Mark the node as processed
processed.add(node);
// Find the next node to process, and loop
node = findLowestCostNode(costs);
}
System.out.println("Cost from the start to each node:");
System.out.println(costs); // { a: 5, b: 2, fin: 6 }
System.out.println(parents); // { a: 5, b: 2, fin: 6 }
}
}