generated from habedi/template-rust-project
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathalgorithms.rs
279 lines (268 loc) · 8.75 KB
/
algorithms.rs
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
// File: src/links/algorithms.rs
//! Link Prediction Algorithms
//!
//! This module implements several link prediction algorithms. Each function returns a vector of
//! pairs ((u, v), score). The optional parameter `ebunch` allows you to supply a slice of node pairs
//! for which the scores should be computed. If `ebunch` is None, the functions default to using all
//! unordered node pairs.
use crate::core::types::{BaseGraph, GraphConstructor, NodeId};
use std::collections::HashSet;
/// Helper: If no ebunch is provided, generate all unordered pairs of nodes.
fn default_ebunch<A, W, Ty>(graph: &BaseGraph<A, W, Ty>) -> Vec<(NodeId, NodeId)>
where
Ty: crate::core::types::GraphConstructor<A, W>,
{
let nodes: Vec<NodeId> = graph.nodes().map(|(u, _)| u).collect();
let mut ebunch = Vec::new();
for i in 0..nodes.len() {
for j in (i + 1)..nodes.len() {
ebunch.push((nodes[i], nodes[j]));
}
}
ebunch
}
/// Resource Allocation Index (RA)
/// For each pair (u, v), RA = sum_{w in N(u) ∩ N(v)} (1 / degree(w))
pub fn resource_allocation_index<A, Ty>(
graph: &BaseGraph<A, f64, Ty>,
ebunch: Option<&[(NodeId, NodeId)]>,
) -> Vec<((NodeId, NodeId), f64)>
where
Ty: GraphConstructor<A, f64>,
{
let pairs = match ebunch {
Some(p) => p.to_vec(),
None => default_ebunch(graph),
};
let mut results = Vec::new();
for (u, v) in pairs {
let neighbors_u: HashSet<_> = graph.neighbors(u).collect();
let neighbors_v: HashSet<_> = graph.neighbors(v).collect();
let common: Vec<_> = neighbors_u.intersection(&neighbors_v).cloned().collect();
let score: f64 = common
.iter()
.map(|w| {
let deg = graph.neighbors(*w).count();
if deg > 0 {
1.0 / deg as f64
} else {
0.0
}
})
.sum();
results.push(((u, v), score));
}
results
}
/// Jaccard Coefficient
/// For each pair (u, v), Jaccard = |N(u) ∩ N(v)| / |N(u) ∪ N(v)|
pub fn jaccard_coefficient<A, Ty>(
graph: &BaseGraph<A, f64, Ty>,
ebunch: Option<&[(NodeId, NodeId)]>,
) -> Vec<((NodeId, NodeId), f64)>
where
Ty: GraphConstructor<A, f64>,
{
let pairs = match ebunch {
Some(p) => p.to_vec(),
None => default_ebunch(graph),
};
let mut results = Vec::new();
for (u, v) in pairs {
let set_u: HashSet<_> = graph.neighbors(u).collect();
let set_v: HashSet<_> = graph.neighbors(v).collect();
let intersection = set_u.intersection(&set_v).count();
let union = set_u.union(&set_v).count();
let score = if union > 0 {
intersection as f64 / union as f64
} else {
0.0
};
results.push(((u, v), score));
}
results
}
/// Adamic–Adar Index
/// For each pair (u, v), AA = sum_{w in N(u) ∩ N(v)} (1 / log(degree(w)))
pub fn adamic_adar_index<A, Ty>(
graph: &BaseGraph<A, f64, Ty>,
ebunch: Option<&[(NodeId, NodeId)]>,
) -> Vec<((NodeId, NodeId), f64)>
where
Ty: GraphConstructor<A, f64>,
{
let pairs = match ebunch {
Some(p) => p.to_vec(),
None => default_ebunch(graph),
};
let mut results = Vec::new();
for (u, v) in pairs {
let set_u: HashSet<_> = graph.neighbors(u).collect();
let set_v: HashSet<_> = graph.neighbors(v).collect();
let common: Vec<_> = set_u.intersection(&set_v).cloned().collect();
let score: f64 = common
.iter()
.filter_map(|w| {
let deg = graph.neighbors(*w).count();
if deg > 1 {
Some(1.0 / (deg as f64).ln())
} else {
None
}
})
.sum();
results.push(((u, v), score));
}
results
}
/// Preferential Attachment
/// For each pair (u, v), PA = degree(u) * degree(v)
pub fn preferential_attachment<A, Ty>(
graph: &BaseGraph<A, f64, Ty>,
ebunch: Option<&[(NodeId, NodeId)]>,
) -> Vec<((NodeId, NodeId), f64)>
where
Ty: GraphConstructor<A, f64>,
{
let pairs = match ebunch {
Some(p) => p.to_vec(),
None => default_ebunch(graph),
};
let mut results = Vec::new();
for (u, v) in pairs {
let deg_u = graph.neighbors(u).count();
let deg_v = graph.neighbors(v).count();
let score = (deg_u * deg_v) as f64;
results.push(((u, v), score));
}
results
}
/// CN Soundarajan–Hopcroft
/// For each pair (u, v), returns the number of common neighbors w such that
/// community(u) == community(v) == community(w).
pub fn cn_soundarajan_hopcroft<A, Ty, F, C>(
graph: &BaseGraph<A, f64, Ty>,
ebunch: Option<&[(NodeId, NodeId)]>,
community: F,
) -> Vec<((NodeId, NodeId), f64)>
where
Ty: GraphConstructor<A, f64>,
F: Fn(NodeId) -> C,
C: Eq,
{
let pairs = match ebunch {
Some(p) => p.to_vec(),
None => default_ebunch(graph),
};
let mut results = Vec::new();
for (u, v) in pairs {
let set_u: Vec<NodeId> = graph.neighbors(u).collect();
let set_v: Vec<NodeId> = graph.neighbors(v).collect();
let common: Vec<NodeId> = set_u.into_iter().filter(|w| set_v.contains(w)).collect();
let score = common
.into_iter()
.filter(|w| community(u) == community(*w) && community(v) == community(*w))
.count() as f64;
results.push(((u, v), score));
}
results
}
/// RA Index Soundarajan–Hopcroft
/// For each pair (u, v), returns the resource allocation index over common neighbors w
/// for which community(u)==community(v)==community(w).
pub fn ra_index_soundarajan_hopcroft<A, Ty, F, C>(
graph: &BaseGraph<A, f64, Ty>,
ebunch: Option<&[(NodeId, NodeId)]>,
community: F,
) -> Vec<((NodeId, NodeId), f64)>
where
Ty: GraphConstructor<A, f64>,
F: Fn(NodeId) -> C,
C: Eq,
{
let pairs = match ebunch {
Some(p) => p.to_vec(),
None => default_ebunch(graph),
};
let mut results = Vec::new();
for (u, v) in pairs {
let set_u: Vec<NodeId> = graph.neighbors(u).collect();
let set_v: Vec<NodeId> = graph.neighbors(v).collect();
let common: Vec<NodeId> = set_u.into_iter().filter(|w| set_v.contains(w)).collect();
let score: f64 = common
.into_iter()
.filter_map(|w| {
if community(u) == community(w) && community(v) == community(w) {
let deg = graph.neighbors(w).count();
if deg > 0 {
Some(1.0 / (deg as f64))
} else {
None
}
} else {
None
}
})
.sum();
results.push(((u, v), score));
}
results
}
/// Within–Inter Cluster Ratio
/// For each pair (u, v), computes the ratio:
/// (within-cluster common neighbors + delta) / (inter-cluster common neighbors + delta)
/// where “within” means common neighbor w with community(u)==community(v)==community(w).
pub fn within_inter_cluster<A, Ty, F, C>(
graph: &BaseGraph<A, f64, Ty>,
ebunch: Option<&[(NodeId, NodeId)]>,
community: F,
delta: f64,
) -> Vec<((NodeId, NodeId), f64)>
where
Ty: GraphConstructor<A, f64>,
F: Fn(NodeId) -> C,
C: Eq,
{
let pairs = match ebunch {
Some(p) => p.to_vec(),
None => default_ebunch(graph),
};
let mut results = Vec::new();
for (u, v) in pairs {
let set_u: Vec<NodeId> = graph.neighbors(u).collect();
let set_v: Vec<NodeId> = graph.neighbors(v).collect();
let common: Vec<NodeId> = set_u.into_iter().filter(|w| set_v.contains(w)).collect();
let within = common
.iter()
.filter(|&&w| community(u) == community(w) && community(v) == community(w))
.count() as f64;
let inter = (common.len() as f64) - within;
let score = (within + delta) / (inter + delta);
results.push(((u, v), score));
}
results
}
/// Common Neighbor Centrality (CCPA)
/// For each pair (u, v), returns (|N(u) ∩ N(v)|)^alpha.
pub fn common_neighbor_centrality<A, Ty>(
graph: &BaseGraph<A, f64, Ty>,
ebunch: Option<&[(NodeId, NodeId)]>,
alpha: f64,
) -> Vec<((NodeId, NodeId), f64)>
where
Ty: GraphConstructor<A, f64>,
{
let pairs = match ebunch {
Some(p) => p.to_vec(),
None => default_ebunch(graph),
};
let mut results = Vec::new();
for (u, v) in pairs {
let set_u: HashSet<_> = graph.neighbors(u).collect();
let set_v: HashSet<_> = graph.neighbors(v).collect();
let common = set_u.intersection(&set_v).count();
let score = (common as f64).powf(alpha);
results.push(((u, v), score));
}
results
}