MCPcopy Create free account
hub / github.com/PolyhedraZK/ExpanderCompilerCollection / topo_order

Function topo_order

expander_compiler/src/utils/misc.rs:21–51  ·  view source on GitHub ↗

must be a DAG

(vertices: &HashSet<usize>, edges: &HashMap<usize, HashSet<usize>>)

Source from the content-addressed store, hash-verified

19
20// must be a DAG
21pub fn topo_order(vertices: &HashSet<usize>, edges: &HashMap<usize, HashSet<usize>>) -> Vec<usize> {
22 let mut queue: Vec<usize> = Vec::new();
23 let mut in_deg: HashMap<usize, usize> = HashMap::new();
24 for &from in vertices.iter() {
25 in_deg.insert(from, 0);
26 }
27 for tos in edges.values() {
28 for &to in tos.iter() {
29 in_deg.entry(to).and_modify(|e| *e += 1);
30 }
31 }
32 for from in vertices.iter() {
33 if in_deg[from] == 0 {
34 queue.push(*from);
35 }
36 }
37 let mut i = 0;
38 while i < queue.len() {
39 let from = queue[i];
40 i += 1;
41 if let Some(tos) = edges.get(&from) {
42 for &to in tos.iter() {
43 in_deg.entry(to).and_modify(|e| *e -= 1);
44 if in_deg[&to] == 0 {
45 queue.push(to);
46 }
47 }
48 }
49 }
50 queue
51}

Callers 2

topo_order_and_is_dagFunction · 0.85
topo_orderMethod · 0.85

Calls 4

iterMethod · 0.80
pushMethod · 0.80
lenMethod · 0.45
getMethod · 0.45

Tested by

no test coverage detected