Dijkstra算法——从原理到实现
Dijkstra 算法是由荷兰计算机科学家 Edsger W. Dijkstra 在 1956 年提出的一种解决带权有向图中单个源点到所有其他节点最短路径问题的算法。该算法要求图中所有边的权值非负,其时间复杂度在使用优先队列优化后可以达到 O ((V+E) logV),其中 V 是节点数,E 是边数。本文将详细介绍 Dijkstra 算法的原理、实现以及应用场景,并给出 Python、Java 和
最短路径问题 ——Dijkstra 算法
一、引言
在现实生活中,我们经常会遇到需要寻找最短路径的问题,比如导航系统中寻找从起点到终点的最快路线、计算机网络中数据包的最优传输路径等。这些问题都可以抽象为图论中的最短路径问题。
Dijkstra 算法是由荷兰计算机科学家 Edsger W. Dijkstra 在 1956 年提出的一种解决带权有向图中单个源点到所有其他节点最短路径问题的算法。该算法要求图中所有边的权值非负,其时间复杂度在使用优先队列优化后可以达到 O ((V+E) logV),其中 V 是节点数,E 是边数。
本文将详细介绍 Dijkstra 算法的原理、实现以及应用场景,并给出 Python、Java 和 C++ 三种语言的实现代码。
二、Dijkstra 算法的基本原理
Dijkstra 算法采用贪心策略,从起点开始逐步扩展,每次选择距离起点最近且未被处理的节点,然后更新该节点的所有邻居的距离。具体步骤如下:
初始化:将起点的距离设为 0,其他所有节点的距离设为无穷大。同时维护一个优先队列(最小堆),用于存储待处理的节点及其距离。
选择节点:从优先队列中取出距离最小的节点,该节点即为当前处理的节点。
更新邻居:遍历当前节点的所有邻居,计算通过当前节点到达邻居的新距离。如果新距离比已知的距离更短,则更新邻居的距离,并将邻居加入优先队列。
重复步骤 2-3:直到优先队列为空,此时所有节点的最短距离都已确定。

Dijkstra 算法的正确性基于一个重要性质:在每次选择距离最小的节点时,该节点的最短路径已经确定。这是因为如果存在另一条更短的路径到达该节点,那么这条路径必然会经过其他节点,但其他节点的距离都比当前节点大,因此不可能通过其他节点得到更短的路径。
三、Dijkstra 算法的实现
下面分别给出 Python、Java 和 C++ 三种语言的 Dijkstra 算法实现。
Python 实现
import heapq
def dijkstra(graph, start):
"""
Dijkstra算法实现,计算从起点到所有其他节点的最短路径
参数:
graph -- 图的邻接表表示,格式为 {节点: {邻居: 边权重}}
start -- 起点节点
返回:
distances -- 从起点到各节点的最短距离字典
predecessors -- 前驱节点字典,用于构造最短路径
"""
# 初始化距离字典,将所有节点的距离设为无穷大
distances = {node: float('inf') for node in graph}
# 起点到自身的距离为0
distances[start] = 0
# 初始化前驱节点字典
predecessors = {node: None for node in graph}
# 优先队列,存储 (距离, 节点) 元组
priority_queue = [(0, start)]
while priority_queue:
# 从优先队列中取出距离最小的节点
current_distance, current_node = heapq.heappop(priority_queue)
# 如果当前距离大于已知的最短距离,跳过
if current_distance > distances[current_node]:
continue
# 遍历当前节点的所有邻居
for neighbor, weight in graph[current_node].items():
# 计算通过当前节点到达邻居的距离
distance = current_distance + weight
# 如果新的距离更短,则更新距离和前驱节点
if distance < distances[neighbor]:
distances[neighbor] = distance
predecessors[neighbor] = current_node
# 将新的距离加入优先队列
heapq.heappush(priority_queue, (distance, neighbor))
return distances, predecessors
def get_shortest_path(predecessors, start, end):
"""
根据前驱节点字典构造从起点到终点的最短路径
参数:
predecessors -- 前驱节点字典
start -- 起点节点
end -- 终点节点
返回:
path -- 最短路径列表,如果不存在则返回空列表
"""
path = []
current_node = end
while current_node is not None:
path.append(current_node)
current_node = predecessors[current_node]
# 如果路径中没有节点或者起点不在路径中,说明不存在路径
if len(path) == 0 or path[-1] != start:
return []
# 反转路径以获得正确的顺序
return path[::-1]
# 示例用法
if __name__ == "__main__":
# 定义一个带权有向图
graph = {
'A': {'B': 1, 'C': 4},
'B': {'C': 2, 'D': 5},
'C': {'D': 8},
'D': {'E': 2},
'E': {}
}
start_node = 'A'
distances, predecessors = dijkstra(graph, start_node)
print(f"从节点 {start_node} 到各节点的最短距离:")
for node, distance in distances.items():
print(f"到节点 {node} 的距离: {distance}")
print("\n最短路径示例:")
end_node = 'E'
path = get_shortest_path(predecessors, start_node, end_node)
if path:
print(f"从节点 {start_node} 到节点 {end_node} 的最短路径: {' -> '.join(path)}")
print(f"路径长度: {distances[end_node]}")
else:
print(f"从节点 {start_node} 到节点 {end_node} 不存在路径")
Java 实现
import java.util.*;
class Dijkstra {
// 图的邻接表表示
private Map<String, Map<String, Integer>> graph;
public Dijkstra(Map<String, Map<String, Integer>> graph) {
this.graph = graph;
}
public Map<String, Integer> dijkstra(String start) {
// 初始化距离字典,将所有节点的距离设为无穷大
Map<String, Integer> distances = new HashMap<>();
for (String node : graph.keySet()) {
distances.put(node, Integer.MAX_VALUE);
}
// 起点到自身的距离为0
distances.put(start, 0);
// 优先队列,存储 (距离, 节点) 元组
PriorityQueue<NodeDistance> priorityQueue = new PriorityQueue<>();
priorityQueue.add(new NodeDistance(start, 0));
while (!priorityQueue.isEmpty()) {
// 从优先队列中取出距离最小的节点
NodeDistance current = priorityQueue.poll();
String currentNode = current.getNode();
int currentDistance = current.getDistance();
// 如果当前距离大于已知的最短距离,跳过
if (currentDistance > distances.get(currentNode)) {
continue;
}
// 遍历当前节点的所有邻居
if (graph.containsKey(currentNode)) {
for (Map.Entry<String, Integer> neighborEntry : graph.get(currentNode).entrySet()) {
String neighbor = neighborEntry.getKey();
int weight = neighborEntry.getValue();
// 计算通过当前节点到达邻居的距离
int distance = currentDistance + weight;
// 如果新的距离更短,则更新距离并加入优先队列
if (distance < distances.get(neighbor)) {
distances.put(neighbor, distance);
priorityQueue.add(new NodeDistance(neighbor, distance));
}
}
}
}
return distances;
}
// 辅助类,用于存储节点及其距离,实现Comparable接口以便在优先队列中排序
static class NodeDistance implements Comparable<NodeDistance> {
private String node;
private int distance;
public NodeDistance(String node, int distance) {
this.node = node;
this.distance = distance;
}
public String getNode() {
return node;
}
public int getDistance() {
return distance;
}
@Override
public int compareTo(NodeDistance other) {
return Integer.compare(this.distance, other.distance);
}
}
public static void main(String[] args) {
// 定义一个带权有向图
Map<String, Map<String, Integer>> graph = new HashMap<>();
Map<String, Integer> aEdges = new HashMap<>();
aEdges.put("B", 1);
aEdges.put("C", 4);
graph.put("A", aEdges);
Map<String, Integer> bEdges = new HashMap<>();
bEdges.put("C", 2);
bEdges.put("D", 5);
graph.put("B", bEdges);
Map<String, Integer> cEdges = new HashMap<>();
cEdges.put("D", 8);
graph.put("C", cEdges);
Map<String, Integer> dEdges = new HashMap<>();
dEdges.put("E", 2);
graph.put("D", dEdges);
graph.put("E", new HashMap<>());
Dijkstra dijkstra = new Dijkstra(graph);
String startNode = "A";
Map<String, Integer> distances = dijkstra.dijkstra(startNode);
System.out.println("从节点 " + startNode + " 到各节点的最短距离:");
for (String node : distances.keySet()) {
System.out.println("到节点 " + node + " 的距离: " + distances.get(node));
}
}
C++ 实现
#include <iostream>
#include <vector>
#include <queue>
#include <unordered_map>
#include <limits>
#include <string>
using namespace std;
// 图的邻接表表示
using Graph = unordered_map<string, unordered_map<string, int>>;
// 用于优先队列的比较函数
struct Compare {
bool operator()(const pair<int, string>& a, const pair<int, string>& b) {
return a.first > b.first;
}
};
unordered_map<string, int> dijkstra(const Graph& graph, const string& start) {
// 初始化距离字典,将所有节点的距离设为无穷大
unordered_map<string, int> distances;
for (const auto& node : graph) {
distances[node.first] = numeric_limits<int>::max();
}
// 起点到自身的距离为0
distances[start] = 0;
// 优先队列,存储 (距离, 节点) 对
priority_queue<pair<int, string>, vector<pair<int, string>>, Compare> pq;
pq.push({0, start});
while (!pq.empty()) {
// 从优先队列中取出距离最小的节点
int currentDistance = pq.top().first;
string currentNode = pq.top().second;
pq.pop();
// 如果当前距离大于已知的最短距离,跳过
if (currentDistance > distances[currentNode]) {
continue;
}
// 遍历当前节点的所有邻居
for (const auto& neighbor : graph.at(currentNode)) {
string neighborNode = neighbor.first;
int weight = neighbor.second;
// 计算通过当前节点到达邻居的距离
int distance = currentDistance + weight;
// 如果新的距离更短,则更新距离并加入优先队列
if (distance < distances[neighborNode]) {
distances[neighborNode] = distance;
pq.push({distance, neighborNode});
}
}
}
return distances;
}
int main() {
// 定义一个带权有向图
Graph graph;
graph["A"] = {{"B", 1}, {"C", 4}};
graph["B"] = {{"C", 2}, {"D", 5}};
graph["C"] = {{"D", 8}};
graph["D"] = {{"E", 2}};
graph["E"] = {};
string startNode = "A";
unordered_map<string, int> distances = dijkstra(graph, startNode);
cout << "从节点 " << startNode << " 到各节点的最短距离:" << endl;
for (const auto& node : distances) {
cout << "到节点 " << node.first << " 的距离: " << node.second << endl;
}
return 0;
}
四、Dijkstra 算法的复杂度分析
时间复杂度:在使用优先队列优化的情况下,Dijkstra 算法的时间复杂度为 O ((V+E) logV),其中 V 是节点数,E 是边数。每次从优先队列中取出最小元素的时间复杂度是 O (logV),而每条边最多被处理一次,处理每条边的时间复杂度是 O (logV)。
空间复杂度:主要用于存储距离数组和优先队列,空间复杂度为 O (V)。
五、Dijkstra 算法的应用场景
Dijkstra 算法在现实生活中有广泛的应用,常见的场景包括:
导航系统:计算地图上两点之间的最短路径。
网络路由:在计算机网络中寻找数据包的最优传输路径。
社交网络:计算社交网络中两个用户之间的最短关系路径。
游戏开发:在游戏中计算角色从起点到终点的最短路径。
资源分配:在资源有限的情况下,寻找最优的分配路径。
六、Dijkstra 算法的注意事项
非负权值要求:Dijkstra 算法要求图中所有边的权值非负。如果图中存在负权边,Dijkstra 算法可能无法得到正确的结果,此时需要使用其他算法,如 Bellman-Ford 算法。
稀疏图与稠密图:对于稀疏图(边数远小于节点数的平方),使用优先队列优化的 Dijkstra 算法效率较高;对于稠密图(边数接近节点数的平方),可以考虑使用邻接矩阵和数组实现,时间复杂度为 O (V²)。
处理无向图:无向图可以看作是双向有向图,即每条边对应两条方向相反的有向边,权值相同。
七、总结
Dijkstra 算法是解决带权有向图中最短路径问题的经典算法,它采用贪心策略,通过不断扩展距离最小的节点来逐步确定最短路径。该算法的时间复杂度在使用优先队列优化后为 O ((V+E) logV),适用于边权非负的情况。
本文详细介绍了 Dijkstra 算法的原理、实现步骤,并给出了 Python、Java 和 C++ 三种语言的实现代码。通过学习和掌握 Dijkstra 算法,我们可以解决许多实际生活中的最短路径问题。
That’s all, thanks for reading!
你的点赞是我创作的动力,
收藏避免错过,
关注解锁更多深度内容!
魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。
更多推荐


所有评论(0)