wasm4pm 26.7.1

High-performance process mining algorithms in WebAssembly for JavaScript/TypeScript
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
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
use std::collections::{BTreeMap, HashSet, VecDeque};
use std::fmt::Write as _;

/// Network metrics for social network analysis.
/// Computes centrality, clustering coefficient, and community detection.

#[derive(Clone)]
pub struct NetworkNode {
    pub id: String,
    pub label: Option<String>,
    pub workload: Option<usize>,
}

#[derive(Clone)]
pub struct NetworkEdge {
    pub from: String,
    pub to: String,
    pub weight: usize,
}

#[derive(Clone)]
pub struct SocialNetwork {
    pub nodes: Vec<NetworkNode>,
    pub edges: Vec<NetworkEdge>,
}

impl SocialNetwork {
    /// Compute degree centrality: proportion of other nodes connected to this node.
    pub fn degree_centrality(&self) -> BTreeMap<String, f64> {
        let n = self.nodes.len() as f64;
        if n <= 1.0 {
            return BTreeMap::new();
        }

        let mut degrees: BTreeMap<String, usize> = BTreeMap::new();
        for edge in &self.edges {
            *degrees.entry(edge.from.clone()).or_default() += 1;
            *degrees.entry(edge.to.clone()).or_default() += 1;
        }

        let mut centrality = BTreeMap::new();
        for node in &self.nodes {
            let degree = degrees.get(&node.id).copied().unwrap_or(0);
            centrality.insert(node.id.clone(), degree as f64 / (n - 1.0));
        }
        centrality
    }

    /// Compute betweenness centrality: measure of how often a node lies on shortest paths.
    /// Uses Brandes algorithm approximation for efficiency.
    pub fn betweenness_centrality(&self) -> BTreeMap<String, f64> {
        let n = self.nodes.len();
        if n <= 2 {
            return BTreeMap::new();
        }

        let mut betweenness: BTreeMap<String, f64> =
            self.nodes.iter().map(|n| (n.id.clone(), 0.0)).collect();

        // Build adjacency list
        let mut adj: BTreeMap<String, Vec<String>> = BTreeMap::new();
        for node in &self.nodes {
            adj.insert(node.id.clone(), Vec::new());
        }
        for edge in &self.edges {
            adj.entry(edge.from.clone())
                .or_default()
                .push(edge.to.clone());
            adj.entry(edge.to.clone())
                .or_default()
                .push(edge.from.clone());
        }

        // BFS from each node
        for source in &self.nodes {
            let mut queue = VecDeque::new();
            let mut visited: HashSet<String> = HashSet::new();
            let mut predecessors: BTreeMap<String, Vec<String>> = BTreeMap::new();
            let mut distances: BTreeMap<String, usize> = BTreeMap::new();

            queue.push_back(source.id.clone());
            visited.insert(source.id.clone());
            distances.insert(source.id.clone(), 0);

            while let Some(v) = queue.pop_front() {
                if let Some(neighbors) = adj.get(&v) {
                    for w in neighbors {
                        if !visited.contains(w) {
                            visited.insert(w.clone());
                            distances.insert(w.clone(), distances[&v] + 1);
                            queue.push_back(w.clone());
                        }
                        if distances.get(w).copied().unwrap_or(usize::MAX) == distances[&v] + 1 {
                            predecessors.entry(w.clone()).or_default().push(v.clone());
                        }
                    }
                }
            }

            // Accumulation phase (simplified)
            for node in &self.nodes {
                if node.id != source.id {
                    if let Some(preds) = predecessors.get(&node.id) {
                        for pred in preds {
                            if let Some(bc) = betweenness.get_mut(pred) {
                                *bc += 1.0 / (preds.len() as f64).max(1.0);
                            }
                        }
                    }
                }
            }
        }

        // Normalize
        let max_val = betweenness.values().copied().fold(0.0, f64::max).max(1.0);
        for bc in betweenness.values_mut() {
            *bc /= max_val;
        }
        betweenness
    }

    /// Compute closeness centrality: average shortest path distance to all other nodes.
    pub fn closeness_centrality(&self) -> BTreeMap<String, f64> {
        let n = self.nodes.len();
        if n <= 1 {
            return BTreeMap::new();
        }

        let mut closeness = BTreeMap::new();

        // Build adjacency list
        let mut adj: BTreeMap<String, Vec<String>> = BTreeMap::new();
        for node in &self.nodes {
            adj.insert(node.id.clone(), Vec::new());
        }
        for edge in &self.edges {
            adj.entry(edge.from.clone())
                .or_default()
                .push(edge.to.clone());
            adj.entry(edge.to.clone())
                .or_default()
                .push(edge.from.clone());
        }

        // BFS from each node
        for source in &self.nodes {
            let mut queue = VecDeque::new();
            let mut distances: BTreeMap<String, usize> = BTreeMap::new();

            queue.push_back(source.id.clone());
            distances.insert(source.id.clone(), 0);

            while let Some(v) = queue.pop_front() {
                if let Some(neighbors) = adj.get(&v) {
                    for w in neighbors {
                        if !distances.contains_key(w) {
                            distances.insert(w.clone(), distances[&v] + 1);
                            queue.push_back(w.clone());
                        }
                    }
                }
            }

            // Compute closeness (sum of reciprocal distances)
            let mut sum_reciprocals = 0.0;
            let mut reachable = 0;
            for node in &self.nodes {
                if node.id != source.id {
                    if let Some(dist) = distances.get(&node.id) {
                        if *dist > 0 {
                            sum_reciprocals += 1.0 / (*dist as f64);
                            reachable += 1;
                        }
                    }
                }
            }

            let c = if reachable > 0 {
                sum_reciprocals / (reachable as f64)
            } else {
                0.0
            };
            closeness.insert(source.id.clone(), c);
        }

        closeness
    }

    /// Compute clustering coefficient: measure of local clustering.
    /// Global coefficient = average of local coefficients.
    pub fn clustering_coefficient(&self) -> (f64, BTreeMap<String, f64>) {
        let mut local_coefficients: BTreeMap<String, f64> = BTreeMap::new();

        // Build adjacency list
        let mut adj: BTreeMap<String, HashSet<String>> = BTreeMap::new();
        for node in &self.nodes {
            adj.insert(node.id.clone(), HashSet::new());
        }
        for edge in &self.edges {
            adj.entry(edge.from.clone())
                .or_default()
                .insert(edge.to.clone());
            adj.entry(edge.to.clone())
                .or_default()
                .insert(edge.from.clone());
        }

        for node in &self.nodes {
            let neighbors: Vec<_> = adj
                .get(&node.id)
                .map(|s| s.iter().cloned().collect())
                .unwrap_or_default();

            if neighbors.len() <= 1 {
                local_coefficients.insert(node.id.clone(), 0.0);
                continue;
            }

            // Count edges among neighbors
            let mut edge_count = 0;
            for i in 0..neighbors.len() {
                for j in i + 1..neighbors.len() {
                    if let Some(neighbor_adj) = adj.get(&neighbors[i]) {
                        if neighbor_adj.contains(&neighbors[j]) {
                            edge_count += 1;
                        }
                    }
                }
            }

            let k = neighbors.len() as f64;
            let possible_edges = k * (k - 1.0) / 2.0;
            let coeff = if possible_edges > 0.0 {
                edge_count as f64 / possible_edges
            } else {
                0.0
            };
            local_coefficients.insert(node.id.clone(), coeff);
        }

        let global = if !local_coefficients.is_empty() {
            local_coefficients.values().sum::<f64>() / (local_coefficients.len() as f64)
        } else {
            0.0
        };

        (global, local_coefficients)
    }

    /// Detect communities using Louvain algorithm (simplified greedy version).
    /// Returns a map of node_id -> community_id.
    pub fn community_detection(&self) -> BTreeMap<String, usize> {
        let mut communities: BTreeMap<String, usize> = BTreeMap::new();
        for (idx, node) in self.nodes.iter().enumerate() {
            communities.insert(node.id.clone(), idx); // Start with each node in own community
        }

        // Build adjacency list with weights
        let mut adj: BTreeMap<String, Vec<(String, usize)>> = BTreeMap::new();
        for node in &self.nodes {
            adj.insert(node.id.clone(), Vec::new());
        }
        for edge in &self.edges {
            adj.entry(edge.from.clone())
                .or_default()
                .push((edge.to.clone(), edge.weight));
            adj.entry(edge.to.clone())
                .or_default()
                .push((edge.from.clone(), edge.weight));
        }

        // Greedy optimization (simplified Louvain)
        let mut improved = true;
        let mut iterations = 0;
        const MAX_ITERATIONS: usize = 10;

        while improved && iterations < MAX_ITERATIONS {
            improved = false;
            iterations += 1;

            for node in &self.nodes {
                let current_comm = communities[&node.id];
                let mut best_comm = current_comm;
                let mut best_gain = 0.0;

                // Try moving node to each neighbor's community
                let mut candidate_comms: HashSet<usize> = HashSet::new();
                candidate_comms.insert(current_comm);

                if let Some(neighbors) = adj.get(&node.id) {
                    for (neighbor, _) in neighbors {
                        candidate_comms.insert(communities[neighbor]);
                    }
                }

                for &candidate in &candidate_comms {
                    let gain =
                        self.modularity_gain(&node.id, current_comm, candidate, &adj, &communities);
                    if gain > best_gain {
                        best_gain = gain;
                        best_comm = candidate;
                        improved = true;
                    }
                }

                communities.insert(node.id.clone(), best_comm);
            }
        }

        // Relabel communities to be contiguous (0, 1, 2, ...)
        let mut mapping: std::collections::BTreeMap<usize, usize> =
            std::collections::BTreeMap::new();
        let mut next_label = 0;
        for node in &self.nodes {
            let old_label = communities[&node.id];
            if let std::collections::btree_map::Entry::Vacant(e) = mapping.entry(old_label) {
                e.insert(next_label);
                next_label += 1;
            }
        }

        for node in &self.nodes {
            let old_label = communities[&node.id];
            communities.insert(node.id.clone(), mapping[&old_label]);
        }

        communities
    }

    fn modularity_gain(
        &self,
        node_id: &str,
        old_comm: usize,
        new_comm: usize,
        adj: &BTreeMap<String, Vec<(String, usize)>>,
        communities: &BTreeMap<String, usize>,
    ) -> f64 {
        let neighbors = adj.get(node_id).cloned().unwrap_or_default();

        let mut old_edges = 0;
        let mut new_edges = 0;

        for (neighbor, weight) in neighbors {
            let neighbor_comm = communities[&neighbor];
            if neighbor_comm == old_comm {
                old_edges += weight;
            }
            if neighbor_comm == new_comm && old_comm != new_comm {
                new_edges += weight;
            }
        }

        (new_edges as f64) - (old_edges as f64)
    }
}

/// Convert network to GraphML format (for Gephi/Cytoscape).
pub fn network_to_graphml(network: &SocialNetwork) -> String {
    let mut xml = String::from("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
    xml.push_str("<graphml xmlns=\"http://graphml.graphdrawing.org/xmlns\">\n");
    xml.push_str(
        "  <key id=\"workload\" for=\"node\" attr.name=\"workload\" attr.type=\"long\"/>\n",
    );
    xml.push_str("  <key id=\"weight\" for=\"edge\" attr.name=\"weight\" attr.type=\"long\"/>\n");
    xml.push_str("  <graph edgedefault=\"undirected\">\n");

    for node in &network.nodes {
        let _ = write!(xml, "    <node id=\"{}\"", escape_xml(&node.id));
        if let Some(label) = &node.label {
            let _ = write!(xml, " label=\"{}\"", escape_xml(label));
        }
        xml.push_str(">\n");
        if let Some(workload) = node.workload {
            let _ = write!(xml, "      <data key=\"workload\">{}</data>\n", workload);
        }
        xml.push_str("    </node>\n");
    }

    for edge in &network.edges {
        let _ = write!(
            xml,
            "    <edge source=\"{}\" target=\"{}\">\n",
            escape_xml(&edge.from),
            escape_xml(&edge.to)
        );
        let _ = write!(xml, "      <data key=\"weight\">{}</data>\n", edge.weight);
        xml.push_str("    </edge>\n");
    }

    xml.push_str("  </graph>\n");
    xml.push_str("</graphml>\n");
    xml
}

/// Convert network to CSV edge list format.
pub fn network_to_csv(network: &SocialNetwork) -> String {
    let mut csv = String::from("from,to,weight\n");
    for edge in &network.edges {
        let _ = write!(csv, "{},{},{}\n", edge.from, edge.to, edge.weight);
    }
    csv
}

fn escape_xml(s: &str) -> String {
    s.replace("&", "&amp;")
        .replace("<", "&lt;")
        .replace(">", "&gt;")
        .replace("\"", "&quot;")
        .replace("'", "&apos;")
}

#[cfg(test)]
mod tests {
    use super::*;

    fn create_test_network() -> SocialNetwork {
        SocialNetwork {
            nodes: vec![
                NetworkNode {
                    id: "A".to_string(),
                    label: Some("Alice".to_string()),
                    workload: Some(5),
                },
                NetworkNode {
                    id: "B".to_string(),
                    label: Some("Bob".to_string()),
                    workload: Some(4),
                },
                NetworkNode {
                    id: "C".to_string(),
                    label: Some("Charlie".to_string()),
                    workload: Some(3),
                },
                NetworkNode {
                    id: "D".to_string(),
                    label: Some("Dana".to_string()),
                    workload: Some(2),
                },
            ],
            edges: vec![
                NetworkEdge {
                    from: "A".to_string(),
                    to: "B".to_string(),
                    weight: 5,
                },
                NetworkEdge {
                    from: "B".to_string(),
                    to: "C".to_string(),
                    weight: 3,
                },
                NetworkEdge {
                    from: "C".to_string(),
                    to: "A".to_string(),
                    weight: 2,
                },
                NetworkEdge {
                    from: "B".to_string(),
                    to: "D".to_string(),
                    weight: 1,
                },
            ],
        }
    }

    #[test]
    fn test_degree_centrality() {
        let network = create_test_network();
        let centrality = network.degree_centrality();

        // A connects to B, C: degree 2
        // B connects to A, C, D: degree 3
        // C connects to B, A: degree 2
        // D connects to B: degree 1
        assert!(centrality["A"] > 0.0);
        assert!(centrality["B"] > centrality["A"]); // B is more central
    }

    #[test]
    fn test_clustering_coefficient() {
        let network = create_test_network();
        let (global, local) = network.clustering_coefficient();

        assert!(global >= 0.0 && global <= 1.0);
        assert_eq!(local.len(), 4);

        // Triangle A-B-C exists
        assert!(local["A"] > 0.0);
    }

    #[test]
    fn test_community_detection() {
        let network = create_test_network();
        let communities = network.community_detection();

        assert_eq!(communities.len(), 4);
        // A, B, C should likely be in same community (triangle)
        assert_eq!(communities["A"], communities["B"]);
        assert_eq!(communities["B"], communities["C"]);
    }

    #[test]
    fn test_graphml_export() {
        let network = create_test_network();
        let graphml = network_to_graphml(&network);

        assert!(graphml.contains("<?xml"));
        assert!(graphml.contains("<graphml"));
        assert!(graphml.contains("<node id=\"A\""));
        assert!(graphml.contains("<edge source=\"A\" target=\"B\""));
    }
}