參考鏈接:
- https://www.baeldung.com/java-dijkstra
- https://www.geeksforgeeks.org/dijkstras-shortest-path-algorithm-in-java-using-priorityqueue/

image.png
小頂堆實(shí)現(xiàn),查找unsettled中距離源點(diǎn)最小的節(jié)點(diǎn)時(shí)間復(fù)雜度較低,但是小頂堆里,一個(gè)節(jié)點(diǎn)可能同時(shí)存在不止一個(gè)。
import java.util.*;
public class Dijkstra {
public static void main(String[] args) {
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.distToSource = 0;
nodeA.adjacent.put(nodeB,10);
nodeA.adjacent.put(nodeC,15);
nodeB.adjacent.put(nodeD,12);
nodeB.adjacent.put(nodeF,15);
nodeC.adjacent.put(nodeE,10);
nodeD.adjacent.put(nodeE,2);
nodeD.adjacent.put(nodeF,1);
nodeF.adjacent.put(nodeE,5);
List<Node> nodes = new ArrayList<>(Arrays.asList(nodeA,nodeB,nodeC,nodeD,nodeE,nodeF));
Dijkstra obj = new Dijkstra();
int minDist = obj.dijkstra(nodes, nodeA, nodeE);
}
public int dijkstra(List<Node> nodes, Node source, Node destination){
Set<Node> settled = new HashSet<>();
PriorityQueue<Node> unSettled = new PriorityQueue<>((node1, node2) -> node1.distToSource - node2.distToSource);
unSettled.add(source);
while(unSettled.size()>0){
Node node = unSettled.poll();
if(settled.contains(node)){// 因?yàn)樾№敹牙?,一個(gè)節(jié)點(diǎn)可能同時(shí)存在不止一個(gè)
continue;
}
node.shortestPath = new ArrayList<>(node.shortestPath);
node.shortestPath.add(node.name);
settled.add(node);
if(node == destination){
return destination.distToSource;
}
for (Map.Entry<Node, Integer> entry : node.adjacent.entrySet()) {
Node adjacent = entry.getKey();
int edgeDist = entry.getValue();
if(!settled.contains(adjacent)){
if(node.distToSource+edgeDist < adjacent.distToSource){
adjacent.distToSource = node.distToSource+edgeDist;
adjacent.shortestPath = new ArrayList<>(node.shortestPath);
}
unSettled.offer(adjacent);
}
}
}
return destination.distToSource;
}
static class Node{
String name = null;
int distToSource = Integer.MAX_VALUE;
Map<Node, Integer> adjacent = new HashMap<>();
List<String> shortestPath = new LinkedList<>();
public Node(String name){
this.name = name;
}
}
}