import java.util.*;
public class Dijkstra {
Set<Node> close = new HashSet<>();//存储访问过程的节点
Set<Node> open = new HashSet<>();//存储没有访问的节点
Map<String,Integer> path = new HashMap<>();//存储起始点到每个节点的长度
Map<String,String> pathInfo = new HashMap<>();//存储每个节点的信息
private static class Node {
private String name;
private Map<Node,Integer> map = new HashMap<>();//存储每个节点的直接关联的节点
public Node(String name) {
this.name = name;
}
public Node() {
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Map<Node, Integer> getMap() {
return map;
}
public void setMap(Map<Node, Integer> map) {
this.map = map;
}
}
public Node build() {//构建一个图
Node NodeA = new Node("A");
Node NodeB = new Node("B");
Node NodeC = new Node("C");
Node NodeD = new Node("D");
Node NodeE = new Node("E");
Node NodeF = new Node("F");
Node NodeG = new Node("G");
Node NodeH = new Node("H");
NodeA.getMap().put(NodeB,1);
NodeA.getMap().put(NodeC,1);
NodeA.getMap().put(NodeD,4);
NodeA.getMap().put(NodeF,2);
NodeA.getMap().put(NodeG,5);
NodeB.getMap().put(NodeA,1);
NodeB.getMap().put(NodeF,2);
NodeB.getMap().put(NodeH,4);
NodeC.getMap().put(NodeA,1);
NodeC.getMap().put(NodeG,3);
NodeD.getMap().put(NodeA,4);
NodeD.getMap().put(NodeE,1);
NodeE.getMap().put(NodeD,1);
NodeE.getMap().put(NodeF,1);
NodeF.getMap().put(NodeE,1);
NodeF.getMap().put(NodeA,2);
NodeF.getMap().put(NodeB,2);
NodeG.getMap().put(NodeA,5);
NodeG.getMap().put(NodeC,3);
NodeG.getMap().put(NodeH,1);
NodeH.getMap().put(NodeB,4);
NodeH.getMap().put(NodeG,1);
open.add(NodeB);
open.add(NodeC);
open.add(NodeD);
open.add(NodeE);
open.add(NodeF);
open.add(NodeG);
open.add(NodeH);
close.add(NodeA);
return NodeA;
}
public Node init() {//初始化起始节点
Node start = build();
path.put("B", 1);
pathInfo.put("B", "A->B");
path.put("C", 1);
pathInfo.put("C", "A->C");
path.put("D", 4);
pathInfo.put("D", "A->D");
path.put("E", Integer.MAX_VALUE);
pathInfo.put("E", "A");
path.put("F", 2);
pathInfo.put("F", "A->F");
path.put("G", 5);
pathInfo.put("G", "A->G");
path.put("H", Integer.MAX_VALUE);
pathInfo.put("H", "A");
return start;
}
public void computePath(Node start) {//计算到达每个节点最短路径
Node nester = getShortPath(start);
if(nester == null)
return;
close.add(nester);
open.remove(nester);
Map<Node, Integer> map = nester.getMap();
Set<Node> set = map.keySet();
for (Node node : set) {
if(open.contains(node)) {
int tmp = path.get(nester.getName()) + map.get(node);
if (tmp < path.get(node.getName())) {
path.put(node.getName(),tmp);
pathInfo.put(node.getName(),pathInfo.get(nester.getName())+"->"+node.getName());
}
}
}
computePath(start);
computePath(nester);
}
private Node getShortPath(Node start) {//获取直接到达的最小路径节点
int min = Integer.MAX_VALUE;
Node nester = null;
Map<Node, Integer> map = start.getMap();
Set<Node> set = map.keySet();
Iterator<Node> iterator = set.iterator();
while (iterator.hasNext()) {
Node node = iterator.next();
if(open.contains(node)) {
int tmp = map.get(node);
if(tmp < min) {
min = tmp;
nester = node;
}
}
}
return nester;
}
public void print() {//打印到达从起始节点到达每个节点的最短路径
Set<Map.Entry<String, String>> entries = pathInfo.entrySet();
Iterator<Map.Entry<String, String>> iterator = entries.iterator();
while(iterator.hasNext()) {
Map.Entry<String, String> next = iterator.next();
System.out.println(next.getKey()+":"+next.getValue());
}
}
public static void main(String[] args) {
Dijkstra dijkstra = new Dijkstra();
dijkstra.computePath(dijkstra.init());
dijkstra.print();
System.out.println(dijkstra.path.get("G"));
}
}