snark-tool 0.4.0

snark-tool library contains structures and algorithm for (mainly) cubic graph analysis
Documentation
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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
use crate::graph::edge::Edge;
use crate::graph::graph::Graph;
use crate::graph::vertex::Vertex;
use crate::service::colour::colouriser::Colouriser;
use std::collections::VecDeque;

///
/// Colorizer for cubic graphs only
///
/// not working for subcubic graphs
///
#[derive(Debug, Clone)]
pub struct BFSColourizerImproved {}

struct BFSColourizerImprovedGraph {
    // pair - (neighbor, color)
    vertices: Vec<[(usize, usize); 3]>,
    one_edge_vert: Vec<usize>,
    non_colored_edges: Vec<usize>,
    non_colored_edges_of_graph: usize,

    to_visit: VecDeque<usize>,
}

impl Colouriser for BFSColourizerImproved {
    fn is_colorable<G: Graph>(graph: &G) -> bool {
        let mut vertices = Vec::with_capacity(graph.size());
        // create local graph
        for vertex in graph.vertices() {
            let mut neighbors = [(0, 0); 3];
            let mut i = 0;
            for edge in graph.edges_of_vertex(vertex.index()) {
                neighbors[i].1 = 1;
                if edge.from() == vertex.index() {
                    neighbors[i].0 = edge.to();
                } else {
                    neighbors[i].0 = edge.from();
                }
                i += 1;
            }
            vertices.push(neighbors);
        }
        let mut color_graph = BFSColourizerImprovedGraph {
            vertices,
            one_edge_vert: vec![],
            non_colored_edges: vec![],
            non_colored_edges_of_graph: 0,
            to_visit: VecDeque::new(),
        };

        // precolor
        let colors = [3, 4, 5];
        let first_vertex;
        let mut index = 0;
        for vertex in color_graph.vertices.clone() {
            // if vertex has only 2 neighbors - skip
            if vertex[2].1 == 0 {
                index += 1;
                continue;
            }

            let mut i = 0;
            for neighbor in vertex.iter() {
                if neighbor.1 == 1 {
                    color_graph.set_edge_color(index, neighbor.0, colors[i]);
                    color_graph.to_visit.push_back(neighbor.0);
                    i += 1;
                }
            }
            index += 1;
            if i != 0 {
                break;
            }
        }
        let mut non_colored_edges_of_graph = 0;
        let mut non_colored_edges_of_vertex_count = vec![0; graph.size()];
        let mut one_edge_vert = vec![];
        let mut i = 0;
        for vertex in &color_graph.vertices {
            for neighbor in vertex.iter() {
                if neighbor.1 == 1 {
                    non_colored_edges_of_vertex_count[i] += 1;
                    non_colored_edges_of_graph += 1;
                }
            }
            if non_colored_edges_of_vertex_count[i] == 1 {
                if vertex[2].1 != 0 {
                    one_edge_vert.push(i);
                }
            }
            i += 1;
        }
        non_colored_edges_of_graph = non_colored_edges_of_graph / 2;

        color_graph.one_edge_vert = one_edge_vert;
        color_graph.non_colored_edges = non_colored_edges_of_vertex_count;
        color_graph.non_colored_edges_of_graph = non_colored_edges_of_graph;
        first_vertex = color_graph.to_visit.pop_front().unwrap();
        color_graph.color(first_vertex)
    }

    fn new() -> Self {
        BFSColourizerImproved {}
    }
}

impl BFSColourizerImprovedGraph {
    fn color(&mut self, vertex: usize) -> bool {
        let color_vars = [(4, 5), (3, 5), (3, 4)];

        let mut neighbor1 = 0;
        let mut neighbor2 = 0;
        let mut colored_sum: usize = 0;
        for neighbor in self.vertices[vertex].iter() {
            let color = neighbor.1;
            if color == 1 {
                neighbor2 = neighbor1;
                neighbor1 = neighbor.0;
            } else {
                colored_sum += color;
            }
        }

        match self.non_colored_edges[vertex] {
            0 => {
                if self.non_colored_edges_of_graph == 0 {
                    return true;
                } else {
                    // if let Some(next) = self.to_visit.pop_front(){
                    //     return self.color(next);
                    // }
                    while let Some(next) = self.to_visit.pop_front() {
                        if (self.non_colored_edges[next] == 1)
                            || (self.non_colored_edges[next] == 2)
                        {
                            return self.color(next);
                        }
                    }

                    let mut vert = 0;
                    // why skipping vertices with 3 non coloured edges? - algorithm wouldn't handle this scenario
                    while (self.non_colored_edges[vert] != 1) && (self.non_colored_edges[vert] != 2)
                    {
                        vert += 1;
                    }
                    return self.color(vert);
                }
            }
            1 => {
                self.non_colored_edges_of_graph -= 1;
                self.non_colored_edges[vertex as usize] -= 1;
                self.non_colored_edges[neighbor1 as usize] -= 1;
                // let mut change = false;
                let mut from_to_visit = false;
                let mut from_one_vert = false;
                let next_vertex;

                // not working for subcubic graphs

                if self.non_colored_edges[neighbor1 as usize] == 1 {
                    next_vertex = neighbor1;
                } else if !self.one_edge_vert.is_empty() {
                    next_vertex = self.one_edge_vert.pop().unwrap();
                    self.to_visit.push_back(neighbor1);
                    from_one_vert = true;
                } else {
                    self.to_visit.push_back(neighbor1);
                    next_vertex = self.to_visit.pop_front().unwrap();
                    from_to_visit = true;
                }

                // self.to_visit.push_back(neighbor1);
                // next_vertex = self.to_visit.pop_front().unwrap();

                match colored_sum {
                    0 => {
                        if self.set_edge_check_and_color_rest(neighbor1, vertex, 3, next_vertex) {
                            return true;
                        }
                        if self.set_edge_check_and_color_rest(neighbor1, vertex, 4, next_vertex) {
                            return true;
                        }
                        if self.set_edge_check_and_color_rest(neighbor1, vertex, 5, next_vertex) {
                            return true;
                        }
                    }
                    3 => {
                        if self.set_edge_check_and_color_rest(neighbor1, vertex, 4, next_vertex) {
                            return true;
                        }
                        if self.set_edge_check_and_color_rest(neighbor1, vertex, 5, next_vertex) {
                            return true;
                        }
                    }
                    4 => {
                        if self.set_edge_check_and_color_rest(neighbor1, vertex, 3, next_vertex) {
                            return true;
                        }
                        if self.set_edge_check_and_color_rest(neighbor1, vertex, 5, next_vertex) {
                            return true;
                        }
                    }
                    5 => {
                        if self.set_edge_check_and_color_rest(neighbor1, vertex, 3, next_vertex) {
                            return true;
                        }
                        if self.set_edge_check_and_color_rest(neighbor1, vertex, 4, next_vertex) {
                            return true;
                        }
                    }
                    _ => {
                        // vertex has 3 edges and two of them are colored
                        if self.set_edge_check_and_color_rest(
                            neighbor1,
                            vertex,
                            12 - colored_sum,
                            next_vertex,
                        ) {
                            return true;
                        }
                    }
                }
                // revert changes
                if from_to_visit {
                    self.to_visit.pop_back();
                    self.to_visit.push_front(next_vertex);
                }
                if from_one_vert {
                    self.to_visit.pop_back();
                    self.one_edge_vert.push(next_vertex);
                }
                // self.to_visit.pop_back();
                // self.one_edge_vert.push(next_vertex);

                self.non_colored_edges_of_graph += 1;
                self.non_colored_edges[vertex as usize] += 1;
                self.non_colored_edges[neighbor1 as usize] += 1;
                self.set_edge_color(vertex, neighbor1, 1);
                return false;
            }
            2 => {
                self.non_colored_edges_of_graph -= 2;
                self.non_colored_edges[vertex as usize] -= 2;
                self.non_colored_edges[neighbor1 as usize] -= 1;
                self.non_colored_edges[neighbor2 as usize] -= 1;
                let mut one_edge_neighbors = 0;
                let mut next_from_queue = false;
                let next_vertex;

                // what if not added to neither one edge neither to visit?

                if self.non_colored_edges[neighbor1 as usize] == 1 {
                    if self.vertices[neighbor1 as usize][2].1 != 0 {
                        one_edge_neighbors += 1;
                        self.one_edge_vert.push(neighbor1);
                    } else {
                        self.to_visit.push_back(neighbor1);
                    }
                } else {
                    self.to_visit.push_back(neighbor1);
                }
                if self.non_colored_edges[neighbor2 as usize] == 1 {
                    if self.vertices[neighbor2 as usize][2].1 != 0 {
                        one_edge_neighbors += 1;
                        self.one_edge_vert.push(neighbor2);
                    } else {
                        self.to_visit.push_back(neighbor2);
                    }
                } else {
                    self.to_visit.push_back(neighbor2);
                }
                if !self.one_edge_vert.is_empty() {
                    next_from_queue = true;
                    next_vertex = self.one_edge_vert.pop().unwrap();
                } else {
                    if let Some(next) = self.to_visit.pop_front() {
                        next_vertex = next;
                    } else {
                        panic!("here");
                    }

                    // next_vertex = self.to_visit.pop_front().unwrap();
                }

                // self.to_visit.push_back(neighbor1);
                // self.to_visit.push_back(neighbor2);
                // next_vertex = self.to_visit.pop_front().unwrap();

                match colored_sum {
                    0 => {
                        for num in 3..6 {
                            self.set_edge_color(vertex, neighbor1, color_vars[num - 3].0);
                            self.set_edge_color(vertex, neighbor2, color_vars[num - 3].1);
                            if self.are_vertices_without_conflict(neighbor1, neighbor2)
                                && self.color(next_vertex)
                            {
                                return true;
                            }

                            self.set_edge_color(vertex, neighbor1, color_vars[num - 3].1);
                            self.set_edge_color(vertex, neighbor2, color_vars[num - 3].0);
                            if self.are_vertices_without_conflict(neighbor1, neighbor2)
                                && self.color(next_vertex)
                            {
                                return true;
                            }
                        }
                    }
                    _ => {
                        self.set_edge_color(vertex, neighbor1, color_vars[colored_sum - 3].0);
                        self.set_edge_color(vertex, neighbor2, color_vars[colored_sum - 3].1);
                        if self.are_vertices_without_conflict(neighbor1, neighbor2)
                            && self.color(next_vertex)
                        {
                            return true;
                        }

                        self.set_edge_color(vertex, neighbor1, color_vars[colored_sum - 3].1);
                        self.set_edge_color(vertex, neighbor2, color_vars[colored_sum - 3].0);
                        if self.are_vertices_without_conflict(neighbor1, neighbor2)
                            && self.color(next_vertex)
                        {
                            return true;
                        }
                    }
                }
                // revert changes
                if one_edge_neighbors == 2 {
                    self.one_edge_vert.pop();
                }
                if one_edge_neighbors == 1 {
                    self.to_visit.pop_back();
                }
                if next_from_queue && one_edge_neighbors == 0 {
                    self.one_edge_vert.push(next_vertex);
                    self.to_visit.pop_back();
                    self.to_visit.pop_back();
                }
                if !next_from_queue && one_edge_neighbors == 0 {
                    self.to_visit.push_front(next_vertex);
                    self.to_visit.pop_back();
                    self.to_visit.pop_back();
                }

                // self.to_visit.push_front(next_vertex);
                // self.to_visit.pop_back();
                // self.to_visit.pop_back();

                self.non_colored_edges_of_graph += 2;
                self.non_colored_edges[vertex as usize] += 2;
                self.non_colored_edges[neighbor1 as usize] += 1;
                self.non_colored_edges[neighbor2 as usize] += 1;
                self.set_edge_color(vertex, neighbor1, 1);
                self.set_edge_color(vertex, neighbor2, 1);
                return false;
            }
            _ => {
                panic!("More than 2 uncolored edges");
            }
        }
    }

    fn set_edge_color(&mut self, from: usize, to: usize, color: usize) {
        for neighbor in self.vertices[from].iter_mut() {
            if neighbor.0 == to {
                neighbor.1 = color;
                break;
            }
        }
        for neighbor in self.vertices[to].iter_mut() {
            if neighbor.0 == from {
                neighbor.1 = color;
                break;
            }
        }
    }

    // ttt2
    fn is_vertex_without_conflict(&self, neighbors: &[(usize, usize); 3]) -> bool {
        is_without_conflict(neighbors[0].1, neighbors[1].1, neighbors[2].1)
    }

    // cond1
    fn set_color_and_check_validity(&mut self, from: usize, to: usize, color: usize) -> bool {
        self.set_edge_color(from, to, color);
        self.is_vertex_without_conflict(&self.vertices[from])
    }

    // cond2
    fn are_vertices_without_conflict(&self, first: usize, second: usize) -> bool {
        self.is_vertex_without_conflict(&self.vertices[first])
            && self.is_vertex_without_conflict(&self.vertices[second])
    }

    fn set_edge_check_and_color_rest(
        &mut self,
        from: usize,
        to: usize,
        color: usize,
        next_vertex: usize,
    ) -> bool {
        self.set_color_and_check_validity(from, to, color) && self.color(next_vertex)
    }
}

fn is_without_conflict(color1: usize, color2: usize, color3: usize) -> bool {
    // ttt
    if (color1 == color2) && (color1 > 1) {
        return false;
    }
    if (color1 == color3) && (color1 > 1) {
        return false;
    }
    if (color3 == color2) && (color3 > 1) {
        return false;
    }
    return true;
}