-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathtopological_sort.rs
More file actions
74 lines (59 loc) · 1.95 KB
/
Copy pathtopological_sort.rs
File metadata and controls
74 lines (59 loc) · 1.95 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
64
65
66
67
68
69
70
71
72
73
74
// Copyright © https://github.com/microwind All rights reserved.
// @author: jarryli@gmail.com
// @version: 1.0
/// 拓扑排序 (Topological Sort)
/// 使用Kahn算法(基于BFS)对有向无环图进行拓扑排序
use std::collections::HashMap;
use std::collections::VecDeque;
/// Kahn算法实现拓扑排序
pub fn topological_sort(graph: &HashMap<usize, Vec<usize>>, num_vertices: usize) -> Vec<usize> {
let mut in_degree = vec![0; num_vertices];
// 计算每个顶点的入度
for neighbors in graph.values() {
for &v in neighbors {
in_degree[v] += 1;
}
}
// 将所有入度为0的顶点加入队列
let mut queue = VecDeque::new();
for i in 0..num_vertices {
if in_degree[i] == 0 {
queue.push_back(i);
}
}
let mut result = Vec::new();
while let Some(u) = queue.pop_front() {
result.push(u);
// 将u的所有邻居的入度减1
if let Some(neighbors) = graph.get(&u) {
for &v in neighbors {
in_degree[v] -= 1;
if in_degree[v] == 0 {
queue.push_back(v);
}
}
}
}
// 检查是否存在环
if result.len() != num_vertices {
return Vec::new(); // 存在环
}
result
}
fn main() {
let mut graph = HashMap::new();
graph.insert(0, vec![1]);
graph.insert(1, vec![2, 3]);
graph.insert(2, vec![3]);
graph.insert(3, vec![]);
let num_vertices = 4;
println!("==================================================");
println!("拓扑排序 (Topological Sort)");
println!("==================================================");
let result = topological_sort(&graph, num_vertices);
if !result.is_empty() {
println!("\n拓扑排序结果: {:?}", result);
} else {
println!("\n图中存在环,无法进行拓扑排序");
}
}