-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvec_graph_utils.rs
More file actions
49 lines (45 loc) · 1.38 KB
/
vec_graph_utils.rs
File metadata and controls
49 lines (45 loc) · 1.38 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
use std::collections::VecDeque;
pub type VecGraph = Vec<Vec<usize>>;
pub fn reversed_graph(graph: &VecGraph) -> VecGraph {
let mut reversed = vec![Vec::new(); graph.len()];
for (node, parents) in graph.iter().enumerate() {
for &parent in parents {
reversed[parent].push(node);
}
}
reversed
}
pub fn count_parents(graph: &VecGraph) -> Vec<u32> {
graph
.iter()
.map(|parents| parents.len() as u32)
.collect::<Vec<_>>()
}
pub fn bfs(enter: usize, exit: usize, graph: &VecGraph) -> Option<Vec<usize>> {
let mut previous = vec![None; graph.len()];
previous[enter] = Some(usize::MAX);
let mut queue = VecDeque::new();
queue.push_back(enter);
while let Some(vertex) = queue.pop_front() {
if vertex == exit {
let mut path = Vec::new();
let mut previous_vertex = Some(exit);
while let Some(vertex) = previous_vertex {
path.push(vertex);
if vertex == enter {
break;
}
previous_vertex = previous[vertex];
}
path.reverse();
return Some(path);
}
for &parent in &graph[vertex] {
if previous[parent].is_none() {
previous[parent] = Some(vertex);
queue.push_back(parent);
}
}
}
None
}