forked from TheAlgorithms/Rust
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtopological_sort.rs
62 lines (56 loc) · 1.8 KB
/
topological_sort.rs
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
use std::collections::{BTreeMap, VecDeque};
type Graph<V, E> = BTreeMap<V, Vec<(V, E)>>;
/// returns topological sort of the graph using Kahn's algorithm
pub fn topological_sort<V: Ord + Copy, E: Ord>(graph: &Graph<V, E>) -> Vec<V> {
let mut visited = BTreeMap::new();
let mut degree = BTreeMap::new();
for u in graph.keys() {
degree.insert(*u, 0);
for (v, _) in graph.get(u).unwrap() {
let entry = degree.entry(*v).or_insert(0);
*entry += 1;
}
}
let mut queue = VecDeque::new();
for (u, d) in degree.iter() {
if *d == 0 {
queue.push_back(*u);
visited.insert(*u, true);
}
}
let mut ret = Vec::new();
while let Some(u) = queue.pop_front() {
ret.push(u);
if let Some(from_u) = graph.get(&u) {
for (v, _) in from_u {
*degree.get_mut(v).unwrap() -= 1;
if *degree.get(v).unwrap() == 0 {
queue.push_back(*v);
visited.insert(*v, true);
}
}
}
}
ret
}
#[cfg(test)]
mod tests {
use std::collections::BTreeMap;
use super::{topological_sort, Graph};
fn add_edge<V: Ord + Copy, E: Ord>(graph: &mut Graph<V, E>, from: V, to: V, weight: E) {
let edges = graph.entry(from).or_insert(Vec::new());
edges.push((to, weight));
}
#[test]
fn it_works() {
let mut graph = BTreeMap::new();
add_edge(&mut graph, 1, 2, 1);
add_edge(&mut graph, 1, 3, 1);
add_edge(&mut graph, 2, 3, 1);
add_edge(&mut graph, 3, 4, 1);
add_edge(&mut graph, 4, 5, 1);
add_edge(&mut graph, 5, 6, 1);
add_edge(&mut graph, 6, 7, 1);
assert_eq!(topological_sort(&graph), vec![1, 2, 3, 4, 5, 6, 7]);
}
}