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
use crate::data_structure::list_graph::ListGraph;
use rand::rngs::StdRng;
use rand::{seq::SliceRandom, Rng, SeedableRng};
pub fn generate(mut n: usize, seed: Option<u64>) -> ListGraph {
#[cfg(feature = "debug_graph_generation")]
let mut counter = 0;
let max_edges = 5 * n;
let mut graph = ListGraph::k4();
let mut urn = Vec::with_capacity(max_edges);
let mut active = vec![false; max_edges];
let mut rng = match seed {
Some(seed) => StdRng::seed_from_u64(seed),
None => StdRng::from_entropy(),
};
for edge in graph.edge_indexes() {
urn.push(edge);
active[edge] = true;
}
while n > 4 {
let mut edge;
while {
edge = *urn.choose(&mut rng).unwrap();
urn.remove(urn.iter().position(|e| &edge == e).unwrap());
!active[edge]
} {}
urn.push(edge);
let (v_a, v_b) = graph.edge(edge).unwrap();
let vertex = if rng.gen_bool(0.5) { v_a } else { v_b };
let typ = *if graph.edges(vertex).unwrap().len() == 3 {
[3, 4].choose(&mut rng)
} else {
[3, 4, 5].choose(&mut rng)
}
.unwrap();
if typ >= 4 {
let succ_edge = graph.cyclic_incident_succ(edge, vertex).unwrap();
graph.remove_edge(succ_edge);
active[succ_edge] = false;
}
if typ == 5 {
let succ_edge = graph.cyclic_incident_succ(edge, vertex).unwrap();
graph.remove_edge(succ_edge);
active[succ_edge] = false;
}
let new_vertex = graph.new_vertex();
let mut current_edge = edge;
let mut current_vertex = vertex;
while {
let new_edge = graph.add_edge(new_vertex, current_vertex, None, Some(current_edge));
#[cfg(feature = "debug_graph_generation")]
debug_graph(
&graph,
vertex,
current_vertex,
new_vertex,
edge,
current_edge,
new_edge,
&mut counter,
);
active[new_edge] = true;
urn.push(new_edge);
current_vertex = graph.opposite(current_vertex, current_edge).unwrap();
current_edge = graph
.cyclic_incident_prev(current_edge, current_vertex)
.unwrap();
current_vertex != vertex
} {}
n -= 1;
}
graph
}
#[cfg(feature = "debug_graph_generation")]
use crate::data_structure::list_graph::{EdgeId, NodeId};
#[cfg(feature = "debug_graph_generation")]
#[allow(clippy::too_many_arguments)]
fn debug_graph(
graph: &ListGraph,
vertex: NodeId,
current_vertex: NodeId,
new_vertex: EdgeId,
edge: EdgeId,
current_edge: EdgeId,
new_edge: EdgeId,
counter: &mut usize,
) {
let mut node_color = std::collections::HashMap::new();
let mut edge_color = std::collections::HashMap::new();
node_color.insert(vertex, "green".to_string());
node_color.insert(current_vertex, "blue".to_string());
node_color.insert(new_vertex, "red".to_string());
edge_color.insert(edge, "green".to_string());
edge_color.insert(current_edge, "blue".to_string());
edge_color.insert(new_edge, "red".to_string());
crate::debug::list_graph::write_as_files(graph, &node_color, &edge_color, counter);
}
#[cfg(test)]
mod tests {
use super::generate;
#[test]
fn test_graph_generation_base() {
let graph = generate(4, Some(0));
assert_eq!(graph.node_indexes().count(), 4);
}
#[test]
fn test_graph_generation_min() {
let graph = generate(5, Some(0));
assert_eq!(graph.node_indexes().count(), 5);
}
#[test]
fn test_graph_generation_small() {
let graph = generate(10, Some(0));
assert_eq!(graph.node_indexes().count(), 10);
}
#[test]
fn test_graph_generation_medium() {
let graph = generate(50, Some(0));
assert_eq!(graph.node_indexes().count(), 50);
}
#[test]
fn test_graph_generation_large() {
let graph = generate(100, Some(0));
assert_eq!(graph.node_indexes().count(), 100);
}
}