Dijkstra 算法简介
Dijkstra 算法是一个经典的单源最短路径算法,用于计算从一个起点到图中所有其他节点的最短路径。适用于边权非负的图(如果有负权重就不能用这个了,要用 Bellman-Ford)。
算法原理(朴素版思路)
- 初始化:
- 设置起点 source 到自己的距离为 0,到其他点为 ∞
- 用一个 visited 集合记录已确定最短路径的点
- 每次从未访问的点中选取当前距离起点最短的那个点 u
- 将 u 加入 visited 集合
- 更新 u 的邻居节点:
- 如果 dist[u] + weight(u, v) < dist[v],就更新 dist[v]
- 重复步骤 2~4,直到所有点都访问过或没有可更新的点了
时间复杂度
- 朴素实现(邻接矩阵):O(V²)
- 优先队列 + 邻接表(堆优化):O((V+E) log V)
图解版:
- 假设有图:
A --1--> B --2--> C
\ |
\--4--> C
起点 A:
A 距离自己 0
B 初始无穷大
C 初始无穷大
第一次:
从 A 出发,更新 B=1,C=4
第二次:
取 B(距离1最小),从 B 更新 C:
C = min(4, 1+2) = 3
第三次:
剩下 C,访问完成
最终:
A 到 B 距离 1
A 到 C 距离 3
C++代码示例(堆优化版)
#include <iostream>
#include <vector>
#include <queue>
#include <utility>
using namespace std;
const int INF = 1e9;
typedef pair<int, int> P; // first: distance, second: node
void dijkstra(int start, vector<vector<P>>& graph, vector<int>& dist) {
priority_queue<P, vector<P>, greater<P>> pq;
dist[start] = 0;
pq.push({0, start});
while (!pq.empty()) {
auto [d, u] = pq.top();
pq.pop();
if (d > dist[u]) continue;
for (auto [v, w] : graph[u]) {
if (dist[v] > dist[u] + w) {
dist[v] = dist[u] + w;
pq.push({dist[v], v});
}
}
}
}
int main() {
int V = 5;
vector<vector<P>> graph(V);
graph[0].push_back({1, 10});
graph[0].push_back({2, 3});
graph[1].push_back({2, 1});
graph[1].push_back({3, 2});
graph[2].push_back({1, 4});
graph[2].push_back({3, 8});
graph[2].push_back({4, 2});
graph[3].push_back({4, 7});
graph[4].push_back({3, 9});
vector<int> dist(V, INF);
dijkstra(0, graph, dist);
for (int i = 0; i < V; ++i) {
cout << "Distance from 0 to " << i << ": " << dist[i] << endl;
}
return 0;
}
C++11 多线程 Dijkstra 核心实现
#include <iostream>
#include <vector>
#include <queue>
#include <unordered_map>
#include <climits>
#include <mutex>
#include <thread>
using namespace std;
unordered_map<int, int> dist;
priority_queue<pair<int, int>, vector<pair<int, int>>, greater<>> pq;
unordered_map<int, vector<pair<int, int>>> graph;
mutex pq_mutex, dist_mutex;
void relax(int u) {
for (auto [v, weight] : graph[u]) {
dist_mutex.lock();
if (dist[u] + weight < dist[v]) {
dist[v] = dist[u] + weight;
pq_mutex.lock();
pq.push({dist[v], v});
pq_mutex.unlock();
}
dist_mutex.unlock();
}
}
void dijkstra(int start, int thread_count = 4) {
for (const auto& node : graph) {
dist[node.first] = INT_MAX;
}
dist[start] = 0;
pq.push({0, start});
while (!pq.empty()) {
pq_mutex.lock();
if (pq.empty()) {
pq_mutex.unlock();
break;
}
auto [currDist, u] = pq.top();
pq.pop();
pq_mutex.unlock();
if (currDist > dist[u]) continue;
// 开多线程松弛邻居
vector<thread> threads;
for (int i = 0; i < thread_count; ++i) {
threads.emplace_back(relax, u);
}
for (auto& t : threads) t.join();
}
for (const auto& [node, d] : dist) {
cout << "到 " << node << " 的最短路径:" << d << endl;
}
}
int main() {
graph = {
{1, {{2, 1}, {3, 4}}},
{2, {{3, 2}}},
{3, {}}
};
dijkstra(1, 4);
return 0;
}
注意:
- 这个是伪并行,多线程松弛邻居节点,但 priority_queue 和 dist 还是得上锁。
- 线程数别太多,瓶颈在堆锁竞争,更高效方式是:
- 多队列 + 局部堆(类似 Boost.Graph dijkstra_parallel)
- 或直接上线程池/ TBB(Threading Building Blocks)
最优思路:
多线程 Dijkstra 的工业级并行版是:
- 每线程维护自己的局部优先队列
- 线程间协作式Work Stealing
- 或者delta-stepping算法(放宽堆顺序要求,提升并行度)