-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathgraph_dijkstra.rs
More file actions
63 lines (53 loc) · 1.51 KB
/
graph_dijkstra.rs
File metadata and controls
63 lines (53 loc) · 1.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
// Copyright © https://github.com/microwind All rights reserved.
// @author: jarryli@gmail.com
// @version: 1.0
/// Dijkstra最短路径算法实现
/// 使用贪心策略,适用于非负权重的图
///
/// 时间复杂度: O(V²),使用优先队列可优化至O((V+E)logV)
/// 空间复杂度: O(V),用于存储距离和访问标记
const V: usize = 6;
const INF: i32 = i32::MAX;
/// 找到距离最小的未处理顶点
fn min_distance(dist: &[i32], visited: &[bool]) -> usize {
let mut min = INF;
let mut min_index = 0;
for v in 0..V {
if !visited[v] && dist[v] <= min {
min = dist[v];
min_index = v;
}
}
min_index
}
/// Dijkstra算法实现
pub fn dijkstra(graph: &[[i32; V]; V], src: usize) {
let mut dist = [INF; V];
let mut visited = [false; V];
dist[src] = 0;
for _ in 0..V - 1 {
let u = min_distance(&dist, &visited);
visited[u] = true;
for v in 0..V {
if !visited[v] && graph[u][v] != 0 &&
dist[u] != INF && dist[u] + graph[u][v] < dist[v] {
dist[v] = dist[u] + graph[u][v];
}
}
}
println!("Vertex \t Distance from Source");
for i in 0..V {
println!("{} \t {}", i, dist[i]);
}
}
pub fn test_dijkstra() {
let graph = [
[0, 4, 0, 0, 0, 0],
[4, 0, 8, 0, 0, 0],
[0, 8, 0, 7, 0, 4],
[0, 0, 7, 0, 9, 14],
[0, 0, 0, 9, 0, 10],
[0, 0, 4, 14, 10, 0],
];
dijkstra(&graph, 0);
}