1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149
| public class Dijkstra {
static Map<String, Map<String,Integer>> graphMap = new HashMap<>(); static { Map<String,Integer> aNeighborMap = new HashMap<>(); graphMap.put("A",aNeighborMap); aNeighborMap.put("B",6); aNeighborMap.put("C",3);
Map<String,Integer> cNeighborMap = new HashMap<>(); graphMap.put("C",cNeighborMap); cNeighborMap.put("B",2); cNeighborMap.put("D",5);
Map<String,Integer> bNeighborMap = new HashMap<>(); graphMap.put("B",bNeighborMap); bNeighborMap.put("D",1);
Map<String,Integer> dNeighborMap = new HashMap<>(); graphMap.put("D",dNeighborMap); dNeighborMap.put("E",3); dNeighborMap.put("F",2);
Map<String,Integer> eNeighborMap = new HashMap<>(); graphMap.put("E",eNeighborMap); eNeighborMap.put("G",1);
Map<String,Integer> fNeighborMap = new HashMap<>(); graphMap.put("F",fNeighborMap); fNeighborMap.put("G",3);
}
static Map<String,Integer> costMap = new HashMap<>(); static { costMap.put("A",0); costMap.put("D",Integer.MAX_VALUE); costMap.put("C",Integer.MAX_VALUE); costMap.put("B",Integer.MAX_VALUE); costMap.put("E",Integer.MAX_VALUE); costMap.put("F",Integer.MAX_VALUE); costMap.put("G",Integer.MAX_VALUE); }
static Set<String> processed = new HashSet<>(); static { processed.add("A"); processed.add("G"); }
static Map<String,String> parentMap = new HashMap<>();
static String start = "A";
public static void main(String[] args) {
while (!processed.containsAll(graphMap.keySet())){ dijkstra(); }
System.out.println(costMap.get("G"));
String curNode = "G"; StringBuilder route = new StringBuilder(curNode);
while (!"A".equals(curNode)){ route.insert(0,parentMap.get(curNode)+"->"); curNode = parentMap.get(curNode); }
System.out.println(route.toString());
}
private static void dijkstra() { String nextLowerNode = findLowerNode(start); if("".equals(nextLowerNode)){ return; }
Integer cost = costMap.get(nextLowerNode);
int newCost = 0; Map<String, Integer> allNeighbors = graphMap.get(nextLowerNode); for (Map.Entry<String, Integer> entry : allNeighbors.entrySet()) { newCost = cost + entry.getValue(); if (costMap.get(entry.getKey()) > newCost) { costMap.put(entry.getKey(), newCost); parentMap.put(entry.getKey(), nextLowerNode); } } processed.add(nextLowerNode); }
public static String findLowerNode(String startNode){
Map<String, Integer> nextNodes = graphMap.get(startNode); if(processed.containsAll(nextNodes.keySet())){ for(Map.Entry<String,Integer> entry : nextNodes.entrySet()){ return findLowerNode(entry.getKey());
}
}else { int cost = Integer.MAX_VALUE;
String nextNode = "";
for(Map.Entry<String,Integer> entry : nextNodes.entrySet()){ if(processed.contains(entry.getKey())){ continue; } if(entry.getValue() < cost){ cost = entry.getValue(); nextNode = entry.getKey();
} } int newCost = costMap.get(startNode)+cost; if(newCost < costMap.get(nextNode)){ costMap.put(nextNode,newCost); parentMap.put(nextNode,startNode); } return nextNode; }
return ""; } }
|