pub fn bidir<T: PartialEq + Debug>(edges: &mut Vec<([usize; 2], T)>)Expand description
Filters edges such that only those who are equal in both directions remains.
Removes redundant edges and edges which only exist in one direction.
Does not preserve the order of edges. The order of the edges is unsorted afterwards.
Assumes that there are maximum two edges between nodes.
Examples found in repository?
examples/eq.rs (line 98)
39fn main() {
40 // Change this to control the number of terms in the equation.
41 let n: usize = if let Some(x) = std::env::args_os().skip(1).take(1).next() {
42 usize::from_str(&x.into_string().expect("number")).unwrap()
43 } else {
44 3
45 };
46 // Change this to control how many
47 let solution_terms: usize = if let Some(x) = std::env::args_os().skip(2).take(1).next() {
48 usize::from_str(&x.into_string().expect("number")).unwrap()
49 } else {
50 1
51 };
52
53 // Putting all terms except the last one
54 let start = Eq {
55 side: {
56 if solution_terms == 1 && n > 0 {
57 let mut res = vec![true; n-1];
58 res.push(false);
59 res
60 } else {
61 vec![true; n]
62 }
63 },
64 positive: vec![true; n],
65 };
66
67 // Swap side and sign on the chosen term.
68 let f = |eq: &Eq, ind: usize| {
69 let mut eq = eq.clone();
70 eq.side[ind] = !eq.side[ind];
71 eq.positive[ind] = !eq.positive[ind];
72 Ok((eq, Swap(vec![ind])))
73 };
74 // Filter nodes to those with the specified number of solutions.
75 let g = |eq: &Eq| eq.len_right() == solution_terms;
76 // Join operations.
77 // Since these swap operations are commutative, require order.
78 let h = |a: &Swap, b: &Swap| if a.0 >= b.0 {Err(None)} else {Ok(Swap({
79 let mut a = a.0.clone();
80 a.extend_from_slice(&b.0);
81 a.sort();
82 a
83 }))};
84
85 let settings = GenerateSettings {
86 max_nodes: 1000,
87 max_edges: 1000,
88 };
89
90 let seed = (vec![start], vec![]);
91 // Generate graph.
92 let (eqs, mut edges) = match gen(seed, n, f, g, h, &settings) {
93 Ok(x) => x,
94 Err((x, ())) => x,
95 };
96
97 // Remove all edges that are not bidirectional.
98 bidir(&mut edges);
99 edges.sort();
100 for i in 0..eqs.len() {
101 println!("{}: {}", i, eqs[i]);
102 }
103 for i in 0..edges.len() {
104 println!("{:?}", edges[i]);
105 }
106
107 println!("(nodes, edges): ({}, {})", eqs.len(), edges.len());
108}