-
-
Notifications
You must be signed in to change notification settings - Fork 2.4k
/
Copy pathsteinhaus_johnson_trotter.rs
61 lines (56 loc) · 1.85 KB
/
steinhaus_johnson_trotter.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
/// <https://en.wikipedia.org/wiki/Steinhaus%E2%80%93Johnson%E2%80%93Trotter_algorithm>
pub fn steinhaus_johnson_trotter_permute<T: Clone>(array: &[T]) -> Vec<Vec<T>> {
let len = array.len();
let mut array = array.to_owned();
let mut inversion_vector = vec![0; len];
let mut i = 1;
let mut res = Vec::with_capacity((1..=len).product());
res.push(array.clone());
while i < len {
if inversion_vector[i] < i {
if i % 2 == 0 {
array.swap(0, i);
} else {
array.swap(inversion_vector[i], i);
}
res.push(array.to_vec());
inversion_vector[i] += 1;
i = 1;
} else {
inversion_vector[i] = 0;
i += 1;
}
}
res
}
#[cfg(test)]
mod tests {
use quickcheck_macros::quickcheck;
use crate::general::permutations::steinhaus_johnson_trotter::steinhaus_johnson_trotter_permute;
use crate::general::permutations::tests::{
assert_permutations, assert_valid_permutation, NotTooBigVec,
};
#[test]
fn test_3_different_values() {
let original = vec![1, 2, 3];
let res = steinhaus_johnson_trotter_permute(&original);
assert_eq!(res.len(), 6); // 3!
for permut in res {
assert_valid_permutation(&original, &permut)
}
}
#[test]
fn test_3_times_the_same_value() {
let original = vec![1, 1, 1];
let res = steinhaus_johnson_trotter_permute(&original);
assert_eq!(res.len(), 6); // 3!
for permut in res {
assert_valid_permutation(&original, &permut)
}
}
#[quickcheck]
fn test_some_elements(NotTooBigVec { inner: original }: NotTooBigVec) {
let permutations = steinhaus_johnson_trotter_permute(&original);
assert_permutations(&original, &permutations)
}
}