1
- use crate::graph::types::{BaseGraph, GraphConstructor, MyEdgeType};
2
- use petgraph::EdgeType;
1
+ use crate :: graph:: types:: { BaseGraph , GraphConstructor } ;
3
2
use rand:: rngs:: StdRng ;
4
3
use rand:: { Rng , SeedableRng } ;
5
4
@@ -13,23 +12,23 @@ use rand::{Rng, SeedableRng};
13
12
///
14
13
/// # Type Parameters
15
14
///
16
- /// * `Ty` - The edge type of the graph, which must implement
17
- /// `GraphConstructor<i32, f32> + MyEdgeType + EdgeType`.
15
+ /// * `Ty` - The edge type of the graph, which must implement `GraphConstructor`.
18
16
///
19
17
/// # Returns
20
18
///
21
- /// * `BaseGraph<i32, f32, Ty>` - The generated graph.
22
- pub fn erdos_renyi_graph<Ty>(n: usize, p: f64, seed: u64) -> BaseGraph<i32, f32, Ty>
23
- where
24
- Ty: GraphConstructor<i32, f32> + MyEdgeType + EdgeType,
25
- {
26
- let mut graph = BaseGraph::<i32, f32, Ty>::new();
19
+ /// * `BaseGraph<u32, f32, Ty>` - The generated graph.
20
+ pub fn erdos_renyi_graph < Ty : GraphConstructor < u32 , f32 > > (
21
+ n : usize ,
22
+ p : f64 ,
23
+ seed : u64 ,
24
+ ) -> BaseGraph < u32 , f32 , Ty > {
25
+ let mut graph = BaseGraph :: < u32 , f32 , Ty > :: new ( ) ;
27
26
let mut nodes = Vec :: with_capacity ( n) ;
28
27
for i in 0 ..n {
29
- nodes.push(graph.add_node(i as i32 ));
28
+ nodes. push ( graph. add_node ( i as u32 ) ) ;
30
29
}
31
30
let mut rng = StdRng :: seed_from_u64 ( seed) ;
32
- if <Ty as GraphConstructor<i32 , f32>>::is_directed() {
31
+ if <Ty as GraphConstructor < u32 , f32 > >:: is_directed ( ) {
33
32
for i in 0 ..n {
34
33
for j in 0 ..n {
35
34
if i != j && rng. random_bool ( p) {
@@ -50,16 +49,25 @@ where
50
49
}
51
50
52
51
/// Generates a complete graph.
53
- pub fn complete_graph<Ty>(n: usize) -> BaseGraph<i32, f32, Ty>
54
- where
55
- Ty: GraphConstructor<i32, f32> + MyEdgeType + EdgeType,
56
- {
57
- let mut graph = BaseGraph::<i32, f32, Ty>::new();
52
+ ///
53
+ /// # Arguments
54
+ ///
55
+ /// * `n` - The number of nodes.
56
+ ///
57
+ /// # Type Parameters
58
+ ///
59
+ /// * `Ty` - The edge type of the graph, which must implement `GraphConstructor`.
60
+ ///
61
+ /// # Returns
62
+ ///
63
+ /// * `BaseGraph<u32, f32, Ty>` - The generated graph.
64
+ pub fn complete_graph < Ty : GraphConstructor < u32 , f32 > > ( n : usize ) -> BaseGraph < u32 , f32 , Ty > {
65
+ let mut graph = BaseGraph :: < u32 , f32 , Ty > :: new ( ) ;
58
66
let mut nodes = Vec :: with_capacity ( n) ;
59
67
for i in 0 ..n {
60
- nodes.push(graph.add_node(i as i32 ));
68
+ nodes. push ( graph. add_node ( i as u32 ) ) ;
61
69
}
62
- if <Ty as GraphConstructor<i32 , f32>>::is_directed() {
70
+ if <Ty as GraphConstructor < u32 , f32 > >:: is_directed ( ) {
63
71
for i in 0 ..n {
64
72
for j in 0 ..n {
65
73
if i != j {
@@ -78,18 +86,35 @@ where
78
86
}
79
87
80
88
/// Generates a bipartite graph.
81
- pub fn bipartite_graph<Ty>(n1: usize, n2: usize, p: f64, seed: u64) -> BaseGraph<i32, f32, Ty>
82
- where
83
- Ty: GraphConstructor<i32, f32> + MyEdgeType + EdgeType,
84
- {
85
- let mut graph = BaseGraph::<i32, f32, Ty>::new();
89
+ ///
90
+ /// # Arguments
91
+ ///
92
+ /// * `n1` - The number of nodes in the first set.
93
+ /// * `n2` - The number of nodes in the second set.
94
+ /// * `p` - The probability of edge creation.
95
+ /// * `seed` - The seed for the random number generator.
96
+ ///
97
+ /// # Type Parameters
98
+ ///
99
+ /// * `Ty` - The edge type of the graph, which must implement `GraphConstructor`.
100
+ ///
101
+ /// # Returns
102
+ ///
103
+ /// * `BaseGraph<u32, f32, Ty>` - The generated graph.
104
+ pub fn bipartite_graph < Ty : GraphConstructor < u32 , f32 > > (
105
+ n1 : usize ,
106
+ n2 : usize ,
107
+ p : f64 ,
108
+ seed : u64 ,
109
+ ) -> BaseGraph < u32 , f32 , Ty > {
110
+ let mut graph = BaseGraph :: < u32 , f32 , Ty > :: new ( ) ;
86
111
let mut group1 = Vec :: with_capacity ( n1) ;
87
112
let mut group2 = Vec :: with_capacity ( n2) ;
88
113
for i in 0 ..n1 {
89
- group1.push(graph.add_node(i as i32 ));
114
+ group1. push ( graph. add_node ( i as u32 ) ) ;
90
115
}
91
116
for j in 0 ..n2 {
92
- group2.push(graph.add_node((n1 + j) as i32 ));
117
+ group2. push ( graph. add_node ( ( n1 + j) as u32 ) ) ;
93
118
}
94
119
let mut rng = StdRng :: seed_from_u64 ( seed) ;
95
120
for & u in & group1 {
@@ -103,34 +128,52 @@ where
103
128
}
104
129
105
130
/// Generates a star graph.
106
- pub fn star_graph<Ty>(n: usize) -> BaseGraph<i32, f32, Ty>
107
- where
108
- Ty: GraphConstructor<i32, f32> + MyEdgeType + EdgeType,
109
- {
110
- let mut graph = BaseGraph::<i32, f32, Ty>::new();
131
+ ///
132
+ /// # Arguments
133
+ ///
134
+ /// * `n` - The number of nodes.
135
+ ///
136
+ /// # Type Parameters
137
+ ///
138
+ /// * `Ty` - The edge type of the graph, which must implement `GraphConstructor`.
139
+ ///
140
+ /// # Returns
141
+ ///
142
+ /// * `BaseGraph<u32, f32, Ty>` - The generated graph.
143
+ pub fn star_graph < Ty : GraphConstructor < u32 , f32 > > ( n : usize ) -> BaseGraph < u32 , f32 , Ty > {
144
+ let mut graph = BaseGraph :: < u32 , f32 , Ty > :: new ( ) ;
111
145
if n == 0 {
112
146
return graph;
113
147
}
114
148
let center = graph. add_node ( 0 ) ;
115
149
for i in 1 ..n {
116
- let node = graph.add_node(i as i32 );
150
+ let node = graph. add_node ( i as u32 ) ;
117
151
graph. add_edge ( center, node, 1.0 ) ;
118
152
}
119
153
graph
120
154
}
121
155
122
156
/// Generates a cycle graph.
123
- pub fn cycle_graph<Ty>(n: usize) -> BaseGraph<i32, f32, Ty>
124
- where
125
- Ty: GraphConstructor<i32, f32> + MyEdgeType + EdgeType,
126
- {
127
- let mut graph = BaseGraph::<i32, f32, Ty>::new();
157
+ ///
158
+ /// # Arguments
159
+ ///
160
+ /// * `n` - The number of nodes.
161
+ ///
162
+ /// # Type Parameters
163
+ ///
164
+ /// * `Ty` - The edge type of the graph, which must implement `GraphConstructor`.
165
+ ///
166
+ /// # Returns
167
+ ///
168
+ /// * `BaseGraph<u32, f32, Ty>` - The generated graph.
169
+ pub fn cycle_graph < Ty : GraphConstructor < u32 , f32 > > ( n : usize ) -> BaseGraph < u32 , f32 , Ty > {
170
+ let mut graph = BaseGraph :: < u32 , f32 , Ty > :: new ( ) ;
128
171
if n == 0 {
129
172
return graph;
130
173
}
131
174
let mut nodes = Vec :: with_capacity ( n) ;
132
175
for i in 0 ..n {
133
- nodes.push(graph.add_node(i as i32 ));
176
+ nodes. push ( graph. add_node ( i as u32 ) ) ;
134
177
}
135
178
for i in 0 ..n {
136
179
let j = ( i + 1 ) % n;
@@ -140,26 +183,44 @@ where
140
183
}
141
184
142
185
/// Generates a Watts–Strogatz small-world graph.
143
- pub fn watts_strogatz_graph<Ty>(n: usize, k: usize, beta: f64, seed: u64) -> BaseGraph<i32, f32, Ty>
144
- where
145
- Ty: GraphConstructor<i32, f32> + MyEdgeType + EdgeType,
146
- {
186
+ ///
187
+ /// # Arguments
188
+ ///
189
+ /// * `n` - The number of nodes.
190
+ /// * `k` - Each node is joined with its `k` nearest neighbors in a ring topology.
191
+ /// * `beta` - The probability of rewiring each edge.
192
+ /// * `seed` - The seed for the random number generator.
193
+ ///
194
+ /// # Type Parameters
195
+ ///
196
+ /// * `Ty` - The edge type of the graph, which must implement `GraphConstructor`.
197
+ ///
198
+ /// # Returns
199
+ ///
200
+ /// * `BaseGraph<u32, f32, Ty>` - The generated graph.
201
+ pub fn watts_strogatz_graph < Ty : GraphConstructor < u32 , f32 > > (
202
+ n : usize ,
203
+ k : usize ,
204
+ beta : f64 ,
205
+ seed : u64 ,
206
+ ) -> BaseGraph < u32 , f32 , Ty > {
147
207
// Watts–Strogatz is defined for undirected graphs.
148
- let mut graph = BaseGraph::<i32 , f32, Ty>::new();
208
+ let mut graph = BaseGraph :: < u32 , f32 , Ty > :: new ( ) ;
149
209
let mut nodes = Vec :: with_capacity ( n) ;
150
210
for i in 0 ..n {
151
- nodes.push(graph.add_node(i as i32 ));
211
+ nodes. push ( graph. add_node ( i as u32 ) ) ;
152
212
}
153
213
let mut rng = StdRng :: seed_from_u64 ( seed) ;
154
214
let half_k = k / 2 ;
155
- // Create ring lattice.
215
+ // Create ring lattice
156
216
for i in 0 ..n {
157
217
for j in 1 ..=half_k {
158
218
let neighbor = ( i + j) % n;
159
219
graph. add_edge ( nodes[ i] , nodes[ neighbor] , 1.0 ) ;
160
220
}
161
221
}
162
222
// Rewire each edge with probability beta.
223
+ // For simplicity, we add new edges for rewired connections.
163
224
for i in 0 ..n {
164
225
for _ in 1 ..=half_k {
165
226
if rng. random_bool ( beta) {
@@ -178,19 +239,34 @@ where
178
239
}
179
240
180
241
/// Generates a Barabási–Albert scale-free graph.
181
- pub fn barabasi_albert_graph<Ty>(n: usize, m: usize, seed: u64) -> BaseGraph<i32, f32, Ty>
182
- where
183
- Ty: GraphConstructor<i32, f32> + MyEdgeType + EdgeType,
184
- {
242
+ ///
243
+ /// # Arguments
244
+ ///
245
+ /// * `n` - The number of nodes.
246
+ /// * `m` - The number of edges to attach from a new node to existing nodes.
247
+ /// * `seed` - The seed for the random number generator.
248
+ ///
249
+ /// # Type Parameters
250
+ ///
251
+ /// * `Ty` - The edge type of the graph, which must implement `GraphConstructor`.
252
+ ///
253
+ /// # Returns
254
+ ///
255
+ /// * `BaseGraph<u32, f32, Ty>` - The generated graph.
256
+ pub fn barabasi_albert_graph < Ty : GraphConstructor < u32 , f32 > > (
257
+ n : usize ,
258
+ m : usize ,
259
+ seed : u64 ,
260
+ ) -> BaseGraph < u32 , f32 , Ty > {
185
261
// Barabási–Albert is defined for undirected graphs.
186
- let mut graph = BaseGraph::<i32 , f32, Ty>::new();
262
+ let mut graph = BaseGraph :: < u32 , f32 , Ty > :: new ( ) ;
187
263
if n < m || m == 0 {
188
264
return graph;
189
265
}
190
266
// Start with a complete graph of m nodes.
191
267
let mut nodes = Vec :: with_capacity ( n) ;
192
268
for i in 0 ..m {
193
- nodes.push(graph.add_node(i as i32 ));
269
+ nodes. push ( graph. add_node ( i as u32 ) ) ;
194
270
}
195
271
for i in 0 ..m {
196
272
for j in ( i + 1 ) ..m {
@@ -201,7 +277,7 @@ where
201
277
let mut degrees: Vec < usize > = vec ! [ m - 1 ; m] ;
202
278
let mut total_degree = m * ( m - 1 ) ;
203
279
for i in m..n {
204
- let new_node = graph.add_node(i as i32 );
280
+ let new_node = graph. add_node ( i as u32 ) ;
205
281
nodes. push ( new_node) ;
206
282
let mut targets = Vec :: new ( ) ;
207
283
while targets. len ( ) < m {
0 commit comments