从零开始学习Java编程,最近学到Dijkstra算法很头大
想请教如果是Java开发中想通过Dijkstra算法实现最短路径的查找,那么Java程序的实体类属性应该根据什么去定义
import java.util.*;
public class Dijkstra {
public static void main(String[] args) {
Graph graph = new Graph();
Node s = graph.addNode("S");
Node a = graph.addNode("A");
Node b = graph.addNode("B");
Node c = graph.addNode("C");
Node d = graph.addNode("D");
Node e = graph.addNode("E");
// 添加节点之间的边
s.addNeighbor(a, 1);
s.addNeighbor(b, 4);
a.addNeighbor(b, 2);
a.addNeighbor(c, 5);
b.addNeighbor(c, 1);
c.addNeighbor(e, 4);
b.addNeighbor(d, 3);
d.addNeighbor(e, 1);
// 计算最短路径并打印结果
graph.computeShortestPath(s, e);
List<Node> path = graph.getShortestPath(e);
System.out.println("Shortest path from S to E: " + path);
}
// 节点类
static class Node {
private String id;
private Map<Node, Integer> neighbors; // 邻居节点及其到该节点的距离
private int distance; // 距离起点的最短距离
private Node predecessor; // 到该节点的最短路径上的前缀节点
public Node(String id) {
this.id = id;
this.neighbors = new HashMap<>();
this.distance = Integer.MAX_VALUE; // 初始化为无穷大
this.predecessor = null; // 初始化为null
}
public String getId() {
return id;
}
public void addNeighbor(Node neighbor, int weight) {
this.neighbors.put(neighbor, weight);
}
public Map<Node, Integer> getNeighbors() {
return neighbors;
}
public int getDistance() {
return distance;
}
public void setDistance(int distance) {
this.distance = distance;
}
public Node getPredecessor() {
return predecessor;
}
public void setPredecessor(Node predecessor) {
this.predecessor = predecessor;
}
@Override
public String toString() {
return id;
}
}
// 图类
static class Graph {
private Map<String, Node> nodes; // 节点列表
public Graph() {
this.nodes = new HashMap<>();
}
// 添加节点
public Node addNode(String id) {
Node node = new Node(id);
nodes.put(id, node);
return node;
}
// 获取节点
public Node getNode(String id) {
return nodes.get(id);
}
// 计算从起点到目标节点的最短路径
public void computeShortestPath(Node source, Node target) {
source.setDistance(0); // 将起点的距离设置为0
Queue<Node> unvisitedNodes = new PriorityQueue<>((n1, n2) -> Integer.compare(n1.getDistance(), n2.getDistance()));
// 使用优先队列维护未访问的节点,并按照距离排序
unvisitedNodes.add(source);
while (!unvisitedNodes.isEmpty()) {
Node currentNode = unvisitedNodes.poll(); // 获取距离起点最近的未访问节点
if (currentNode == target) { // 如果当前节点是目标节点,结束循环
return;
}
for (Map.Entry<Node, Integer> neighborEntry : currentNode.getNeighbors().entrySet()) {
Node neighbor = neighborEntry.getKey(); // 获取邻居节点
int weight = neighborEntry.getValue(); // 获取到邻居节点的边的权重
int distanceThroughCurrent = currentNode.getDistance() + weight; // 通过当前节点到达邻居节点的距离
if (distanceThroughCurrent < neighbor.getDistance()) { // 如果通过当前节点到达邻居节点的距离更短
unvisitedNodes.remove(neighbor); // 从未访问节点中删除邻居节点
neighbor.setDistance(distanceThroughCurrent); // 更新邻居节点的距离
neighbor.setPredecessor(currentNode); // 更新邻居节点的前缀节点
unvisitedNodes.add(neighbor); // 将邻居节点添加到未访问节点中
}
}
}
}
// 获取从起点到目标节点的最短路径
public List<Node> getShortestPath(Node target) {
List<Node> path = new ArrayList<>();
for (Node node = target; node != null; node = node.getPredecessor()) {
path.add(node); // 将节点添加到路径中
}
Collections.reverse(path); // 反转路径,使其从起点到目标节点
return path;
}
}
}