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
use crate::interface::DynamicGraph;
use rand::seq::IteratorRandom;
use rand::Rng;
pub fn create_binary_tree<Graph: DynamicGraph>(
graph: &mut Graph,
depth: i32,
) -> Option<Graph::NodeIndex>
where
Graph::NodeData: Default,
Graph::EdgeData: Default,
{
if depth < 0 {
return None;
}
let root = graph.add_node(Default::default());
create_binary_tree_recursively(graph, depth - 1, root);
Some(root)
}
fn create_binary_tree_recursively<Graph: DynamicGraph>(
graph: &mut Graph,
depth: i32,
root: Graph::NodeIndex,
) where
Graph::NodeData: Default,
Graph::EdgeData: Default,
{
if depth < 0 {
return;
}
let l = graph.add_node(Default::default());
let r = graph.add_node(Default::default());
graph.add_edge(root, l, Default::default());
graph.add_edge(root, r, Default::default());
create_binary_tree_recursively(graph, depth - 1, l);
create_binary_tree_recursively(graph, depth - 1, r);
}
pub fn compute_m_from_n_and_c(n: usize, c: f64) -> usize {
let node_amount_f64 = n as f64;
let target_edge_amount =
c * node_amount_f64 * (node_amount_f64.ln().max(1.0) + node_amount_f64.ln().ln().max(0.0));
target_edge_amount.round() as usize
}
pub fn create_random_hamiltonian_graph<Graph: DynamicGraph, Random: Rng>(
graph: &mut Graph,
node_amount: usize,
c: f64,
random: &mut Random,
) where
Graph::NodeData: Default,
Graph::EdgeData: Default,
{
if node_amount == 0 {
return;
}
for _ in 0..node_amount {
graph.add_node(Default::default());
}
for (n1, n2) in graph
.node_indices()
.take(graph.node_count() - 1)
.zip(graph.node_indices().skip(1))
{
graph.add_edge(n1, n2, Default::default());
}
graph.add_edge(
graph.node_indices().last().unwrap(),
graph.node_indices().next().unwrap(),
Default::default(),
);
let target_edge_amount = compute_m_from_n_and_c(node_amount, c);
debug_assert!(
target_edge_amount >= node_amount && target_edge_amount <= node_amount * (node_amount - 1),
"node_amount <= target_edge_amount <= node_amount * (node_amount - 1): {} <= {} <= {} (c: {})",
node_amount,
target_edge_amount,
node_amount * (node_amount - 1),
c,
);
while graph.edge_count() < target_edge_amount {
let n1 = graph.node_indices().choose(random).unwrap();
let n2 = graph.node_indices().choose(random).unwrap();
if n1 != n2 && !graph.contains_edge_between(n1, n2) {
graph.add_edge(n1, n2, Default::default());
}
}
}
pub fn create_random_graph<Graph: DynamicGraph, Random: Rng>(
graph: &mut Graph,
node_amount: usize,
c: f64,
random: &mut Random,
) where
Graph::NodeData: Default,
Graph::EdgeData: Default,
{
if node_amount == 0 {
return;
}
for _ in 0..node_amount {
graph.add_node(Default::default());
}
let target_edge_amount = compute_m_from_n_and_c(node_amount, c);
debug_assert!(
target_edge_amount >= node_amount && target_edge_amount <= node_amount * (node_amount - 1)
);
while graph.edge_count() < target_edge_amount {
let n1 = graph.node_indices().choose(random).unwrap();
let n2 = graph.node_indices().choose(random).unwrap();
if n1 != n2 && !graph.contains_edge_between(n1, n2) {
graph.add_edge(n1, n2, Default::default());
}
}
}
#[cfg(test)]
mod tests {
use super::create_binary_tree;
use crate::implementation::petgraph_impl;
use crate::interface::ImmutableGraphContainer;
#[test]
fn test_create_binary_tree_2() {
let mut graph = petgraph_impl::new::<(), ()>();
create_binary_tree(&mut graph, 2);
debug_assert_eq!(graph.node_count(), 7);
debug_assert_eq!(graph.edge_count(), 6);
}
}