summaryrefslogtreecommitdiff
path: root/src/incremental.rs
blob: 3f55216981f257d6e6d7862208584fd4edf04ede (plain)
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
//! This file implements the suggested solution from the professor,
//! incrementally updating the existing graph, instead of creating new
//! graphs.

use super::*;

/// We keep a list of graphs, one for each round.  Since each node in
/// each graph has exactly one edge out of it, we can simply represent
/// the graph as a list of integers.
type Graph = Vec<usize>;

/// Returns an empty graph, where every node is associated with an
/// index that is out of bounds.
fn default_graph(n: usize) -> Graph {
    std::iter::repeat(n).take(n).collect()
}

fn add_edge(graph: &mut Graph, source: usize, target: usize) {
    let len = graph.len();

    if source >= len {
        panic!("source = {source} out of bound: {len}");
    }

    if target >= len {
        panic!("target = {target} out of bound: {len}");
    }

    *graph.get_mut(source).unwrap() = target;
    *graph.get_mut(target).unwrap() = source;
}

// NOTE: This seems to be unnecessary now.
/// Returns the associated number.
///
/// To be more precise, if `node` is compared with a number `n` at
/// round `round`, then return `Some(round)`, otherwise return `None`.
///
/// If either `round` or `node` is out of bounds, this function
/// panics.
#[allow(dead_code)]
fn assoc(graphs: &[Graph], round: usize, node: usize) -> Option<usize> {
    if let Some(graph) = graphs.get(round) {
        if let Some(n) = graph.get(node).copied() {
            if n >= graph.len() {
                None
            } else {
                Some(n)
            }
        } else {
            panic!("node = {node} out of bound = {}", graph.len());
        }
    } else {
        panic!("round = {round} out of bound = {}", graphs.len());
    }
}

/// Initial **MIN** algorithm.
fn smi_init<T: PartialOrd>(a: &[T], graphs: &mut Vec<Graph>, count: &mut usize) -> usize {
    if a.is_empty() {
        panic!("invalid empty input");
    }

    let n = a.len();
    let logn_ceil = (n as f32).log2().ceil() as usize;

    let mut indices: Vec<_> = (0..n).collect();

    let mut upper = n;

    graphs.clear();
    graphs
        .try_reserve(logn_ceil)
        .unwrap_or_else(|e| panic!("reserving memory fails: {e}"));

    while upper > 1 {
        let mut graph = default_graph(n);

        let div = upper.div_euclid(2);

        for i in 0..div {
            *count += 1;

            let x = indices.get(2 * i).unwrap();
            let y = indices.get(2 * i + 1).unwrap();

            let ax = a.get(*x).unwrap();
            let ay = a.get(*y).unwrap();

            add_edge(graph.borrow_mut(), *x, *y);

            match ax.partial_cmp(ay) {
                Some(Ordering::Less) | Some(Ordering::Equal) => {
                    *indices.get_mut(i).unwrap() = *x;
                }
                Some(_) => {
                    *indices.get_mut(i).unwrap() = *y;
                }
                None => {
                    *count += 1;

                    if ax.partial_cmp(ax).is_some() {
                        *indices.get_mut(i).unwrap() = *x;
                    } else {
                        *indices.get_mut(i).unwrap() = *y;
                    }
                }
            }
        }

        let offset = upper.rem_euclid(2);

        if offset == 1 {
            *indices.get_mut(div).unwrap() = *indices.get(upper - 1).unwrap();
        }

        upper = div + offset;

        graphs.push(graph);
    }

    assert_eq!(graphs.len(), logn_ceil);

    *indices.first().unwrap()
}

fn smi_step<T: PartialOrd>(
    a: &[T],
    previous_min_index: usize,
    added_indices: &HashSet<usize>,
    graphs: &mut Vec<Graph>,
    count: &mut usize,
) -> usize {
    let n = a.len();

    if previous_min_index >= n {
        panic!("invalid min index: {previous_min_index} with n = {n}");
    }

    if graphs.is_empty() {
        panic!("invalid empty round");
    }

    let mut to_compare: Option<usize> = None;

    for graph in graphs.iter_mut() {
        let assoc = {
            if let Some(assoc) = graph.get(previous_min_index).copied() {
                if assoc >= graph.len() {
                    continue;
                } else {
                    assoc
                }
            } else {
                panic!(
                    "prev = {previous_min_index}, graph length = {}",
                    graph.len()
                );
            }
        };

        if let Some(cand) = to_compare {
            let acand = a.get(cand).unwrap();
            let a_assoc = a.get(assoc).unwrap();

            let cand_already_added = added_indices.contains(&cand);
            let assoc_already_added = added_indices.contains(&assoc);

            if cand_already_added && assoc_already_added {
                to_compare = None;
                continue;
            } else if cand_already_added {
                to_compare = Some(assoc);
                continue;
            } else if assoc_already_added {
                to_compare = Some(cand);
                continue;
            }

            *count += 1;

            add_edge(graph.borrow_mut(), cand, assoc);

            match acand.partial_cmp(a_assoc) {
                Some(Ordering::Less) | Some(Ordering::Equal) => {}
                _ => {
                    to_compare = Some(assoc);
                }
            }
        } else {
            to_compare = Some(assoc);
        }
    }

    if to_compare.is_none() {
        let mut unadded: Vec<_> = Vec::new();

        for i in 0..n {
            if !added_indices.contains(&i) {
                unadded.push(i);
            }
        }

        dbg!(previous_min_index, &unadded);

        return *unadded.first().unwrap();
    }

    to_compare.unwrap()
}

pub fn smi<T: PartialOrd>(a: &[T], count: &mut usize) -> Vec<usize> {
    if a.is_empty() {
        return Vec::new();
    }

    let n = a.len();

    let mut graphs: Vec<Graph> = Vec::with_capacity((n.ilog2() as usize) + 1);

    let mut result = Vec::with_capacity(n);
    let mut added_indices = HashSet::with_capacity(n);

    let min = smi_init(a, &mut graphs, count);

    result.push(min);
    added_indices.insert(min);

    let mut step_min = min;

    for _ in 1..n {
        step_min = smi_step(a, step_min, &added_indices, &mut graphs, count);

        result.push(step_min);
        added_indices.insert(step_min);
    }

    result
}

#[cfg(test)]
mod test {
    use super::*;

    use rand::{
        distributions::{Distribution, Uniform},
        thread_rng,
    };

    #[test]
    fn test_smi_init_and_one_step() {
        let input = vec![1, 2, 3, 0, 9, 2, 3];

        let mut count = 0;

        let mut graphs = Vec::with_capacity((input.len().ilog2() as usize) + 1);

        let min = smi_init(&input, &mut graphs, &mut count);

        let mut added_indices = HashSet::with_capacity(7);

        added_indices.insert(min);

        assert_eq!(min, 3);

        let second_min = smi_step(&input, min, &added_indices, &mut graphs, &mut count);

        assert_eq!(second_min, 0);
    }

    #[test]
    fn test_smi_itself() {
        for i in 1..=5 {
            let two_i = 1 << i;

            let mut rng = thread_rng();

            let input = {
                let uniform = Uniform::new(-100i32, 100i32);
                uniform
                    .sample_iter(&mut rng)
                    .take(10 * two_i)
                    .collect::<Vec<_>>()
            };

            let n = input.len();

            // println!("input = {input:?}");
            println!("input length = {n}");

            let nlogn = n * ((n as f32).log2().ceil() as usize);

            let mut count = 0;

            let sort_result = smi(&input, &mut count);

            println!("count = {count}, nlog(n) = {nlogn}");
            // println!("sort_result = {sort_result:?}");

            let sort_result_numbers: Vec<_> = sort_result
                .iter()
                .copied()
                .map(|x| *input.get(x).unwrap())
                .collect();

            // println!("sort_result_numbers = {sort_result_numbers:?}");

            let mut answer = input.clone();

            answer.sort_unstable();

            assert_eq!(answer, sort_result_numbers);

            println!("correctly sorted");
            println!();
        }
    }
}