package com.xl.test01;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
public class Node {
private String name;
private Map<Node, Integer> child = new HashMap<Node, Integer>();
public Node(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Map<Node, Integer> getChild() {
return child;
}
public void setChild(Map<Node, Integer> child) {
this.child = child;
}
}
class MapBuilder {
public Node build(Set<Node> open, Set<Node> close) {
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");
nodeA.getChild().put(nodeB, 6);
nodeA.getChild().put(nodeC, 3);
nodeB.getChild().put(nodeA, 6);
nodeB.getChild().put(nodeC, 2);
nodeB.getChild().put(nodeD, 5);
nodeC.getChild().put(nodeA, 3);
nodeC.getChild().put(nodeB, 2);
nodeC.getChild().put(nodeD, 3);
nodeC.getChild().put(nodeE, 4);
nodeD.getChild().put(nodeB, 5);
nodeD.getChild().put(nodeC, 3);
nodeD.getChild().put(nodeF, 3);
nodeD.getChild().put(nodeE, 2);
nodeE.getChild().put(nodeC, 4);
nodeE.getChild().put(nodeD, 2);
nodeE.getChild().put(nodeF, 5);
nodeF.getChild().put(nodeD, 3);
nodeF.getChild().put(nodeE, 5);
open.add(nodeB);
open.add(nodeC);
open.add(nodeD);
open.add(nodeE);
open.add(nodeF);
close.add(nodeA);
return nodeA;
}
}
class Dijkstra {
Set<Node> open = new HashSet<Node>();
Set<Node> close = new HashSet<Node>();
Map<String, Integer> path = new HashMap<String, Integer>();
Map<String, String> pathInfo = new HashMap<String, String>();
public Node init() {
path.put("B", 6);
pathInfo.put("B", "A->B");
path.put("C", 3);
pathInfo.put("C", "A->C");
path.put("D", Integer.MAX_VALUE);
pathInfo.put("D", "A->D");
path.put("E", Integer.MAX_VALUE);
pathInfo.put("E", "A->E");
path.put("F", Integer.MAX_VALUE);
pathInfo.put("F", "A->F");
Node start = new MapBuilder().build(open, close);
return start;
}
private Node getShortestPath(Node node) {
Node res = null;
int minDis = Integer.MAX_VALUE;
Map<Node, Integer> childs = node.getChild();
for (Node child : childs.keySet()) {
if (open.contains(child)) {
int distance = childs.get(child);
if (distance < minDis) {
minDis = distance;
res = child;
}
}
}
return res;
}
public void computePath(Node start) {
Node nearest = getShortestPath(start);
if (nearest == null) {
return;
}
close.add(nearest);
open.remove(nearest);
Map<Node, Integer> childs = nearest.getChild();
for (Node child : childs.keySet()) {
if (open.contains(child)) {
Integer newCompute = path.get(nearest.getName()) + childs.get(child);
if (newCompute < path.get(child.getName())) {
path.put(child.getName(), newCompute);
pathInfo.put(child.getName(), pathInfo.get(nearest.getName()) + "->" + child.getName());
}
}
}
computePath(start);
computePath(nearest);
}
public void printPathInfo() {
Set<Map.Entry<String, String>> pathInfos = pathInfo.entrySet();
for (Map.Entry<String, String> pathInfo : pathInfos) {
System.out.println(pathInfo.getKey() + ":" + pathInfo.getValue());
}
}
}
class DijkstraTest {
public static void main(String args[]) throws Exception {
Dijkstra test = new Dijkstra();
Node start = test.init();
test.computePath(start);
test.printPathInfo();
}
}