arrow-graph 0.6.2

Arrow-native graph processing engine with SQL interface
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
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
use arrow::record_batch::RecordBatch;
use arrow::array::{StringArray, UInt32Array, Float64Array};
use arrow::datatypes::{DataType, Field, Schema};
use std::sync::Arc;
use rand::{Rng, SeedableRng};
use rand::seq::SliceRandom;
use rand_pcg::Pcg64;
use crate::algorithms::{GraphAlgorithm, AlgorithmParams};
use crate::graph::ArrowGraph;
use crate::error::{GraphError, Result};

/// Random Walk implementation for graph sampling and ML feature generation
pub struct RandomWalk;

impl RandomWalk {
    /// Perform random walks starting from specified nodes
    fn compute_random_walks(
        &self,
        graph: &ArrowGraph,
        start_nodes: &[String],
        walk_length: usize,
        num_walks: usize,
        seed: Option<u64>,
    ) -> Result<Vec<Vec<String>>> {
        if walk_length == 0 {
            return Err(GraphError::invalid_parameter(
                "walk_length must be greater than 0"
            ));
        }

        if num_walks == 0 {
            return Err(GraphError::invalid_parameter(
                "num_walks must be greater than 0"
            ));
        }

        let mut rng = match seed {
            Some(s) => Pcg64::seed_from_u64(s),
            None => Pcg64::from_entropy(),
        };

        let mut all_walks = Vec::new();

        for start_node in start_nodes {
            if !graph.has_node(start_node) {
                return Err(GraphError::node_not_found(start_node.clone()));
            }

            for _ in 0..num_walks {
                let walk = self.single_random_walk(graph, start_node, walk_length, &mut rng)?;
                all_walks.push(walk);
            }
        }

        Ok(all_walks)
    }

    /// Perform a single random walk from a starting node
    fn single_random_walk(
        &self,
        graph: &ArrowGraph,
        start_node: &str,
        walk_length: usize,
        rng: &mut Pcg64,
    ) -> Result<Vec<String>> {
        let mut walk = Vec::with_capacity(walk_length);
        let mut current_node = start_node.to_string();

        walk.push(current_node.clone());

        for _ in 1..walk_length {
            let neighbors = match graph.neighbors(&current_node) {
                Some(neighbors) => neighbors,
                None => break, // Dead end - end walk early
            };

            if neighbors.is_empty() {
                break; // No neighbors - end walk early
            }

            // Choose random neighbor
            let next_node = neighbors.choose(rng).unwrap();
            current_node = next_node.clone();
            walk.push(current_node.clone());
        }

        Ok(walk)
    }
}

impl GraphAlgorithm for RandomWalk {
    fn execute(&self, graph: &ArrowGraph, params: &AlgorithmParams) -> Result<RecordBatch> {
        let walk_length: usize = params.get("walk_length").unwrap_or(10);
        let num_walks: usize = params.get("num_walks").unwrap_or(10);
        let seed: Option<u64> = params.get("seed");
        
        // Get start nodes - if not specified, use all nodes
        let start_nodes: Vec<String> = if let Some(nodes) = params.get::<Vec<String>>("start_nodes") {
            nodes
        } else {
            graph.node_ids().cloned().collect()
        };

        let walks = self.compute_random_walks(graph, &start_nodes, walk_length, num_walks, seed)?;

        // Convert walks to Arrow format
        let schema = Arc::new(Schema::new(vec![
            Field::new("walk_id", DataType::UInt32, false),
            Field::new("step", DataType::UInt32, false),
            Field::new("node_id", DataType::Utf8, false),
        ]));

        let mut walk_ids = Vec::new();
        let mut steps = Vec::new();
        let mut node_ids = Vec::new();

        for (walk_id, walk) in walks.iter().enumerate() {
            for (step, node_id) in walk.iter().enumerate() {
                walk_ids.push(walk_id as u32);
                steps.push(step as u32);
                node_ids.push(node_id.clone());
            }
        }

        RecordBatch::try_new(
            schema,
            vec![
                Arc::new(UInt32Array::from(walk_ids)),
                Arc::new(UInt32Array::from(steps)),
                Arc::new(StringArray::from(node_ids)),
            ],
        ).map_err(GraphError::from)
    }

    fn name(&self) -> &'static str {
        "random_walk"
    }

    fn description(&self) -> &'static str {
        "Generate random walks from specified nodes for graph sampling and ML feature generation"
    }
}

/// Node2Vec-style biased random walks with return parameter p and in-out parameter q
pub struct Node2VecWalk;

impl Node2VecWalk {
    /// Perform Node2Vec-style biased random walks
    fn compute_node2vec_walks(
        &self,
        graph: &ArrowGraph,
        start_nodes: &[String],
        walk_length: usize,
        num_walks: usize,
        p: f64, // Return parameter (controls likelihood of returning to previous node)
        q: f64, // In-out parameter (controls likelihood of exploring vs. staying local)
        seed: Option<u64>,
    ) -> Result<Vec<Vec<String>>> {
        if walk_length < 2 {
            return Err(GraphError::invalid_parameter(
                "walk_length must be at least 2 for Node2Vec walks"
            ));
        }

        if p <= 0.0 || q <= 0.0 {
            return Err(GraphError::invalid_parameter(
                "p and q parameters must be positive"
            ));
        }

        let mut rng = match seed {
            Some(s) => Pcg64::seed_from_u64(s),
            None => Pcg64::from_entropy(),
        };

        let mut all_walks = Vec::new();

        for start_node in start_nodes {
            if !graph.has_node(start_node) {
                return Err(GraphError::node_not_found(start_node.clone()));
            }

            for _ in 0..num_walks {
                let walk = self.single_node2vec_walk(graph, start_node, walk_length, p, q, &mut rng)?;
                all_walks.push(walk);
            }
        }

        Ok(all_walks)
    }

    /// Perform a single Node2Vec-style biased random walk
    fn single_node2vec_walk(
        &self,
        graph: &ArrowGraph,
        start_node: &str,
        walk_length: usize,
        p: f64,
        q: f64,
        rng: &mut Pcg64,
    ) -> Result<Vec<String>> {
        let mut walk = Vec::with_capacity(walk_length);
        walk.push(start_node.to_string());

        // First step is uniform random
        let first_neighbors = match graph.neighbors(start_node) {
            Some(neighbors) if !neighbors.is_empty() => neighbors,
            _ => return Ok(walk), // No neighbors - return single node walk
        };

        let second_node = first_neighbors.choose(rng).unwrap().clone();
        walk.push(second_node.clone());

        // Subsequent steps use biased probabilities
        for _ in 2..walk_length {
            let current_node = &walk[walk.len() - 1];
            let previous_node = &walk[walk.len() - 2];

            let neighbors = match graph.neighbors(current_node) {
                Some(neighbors) if !neighbors.is_empty() => neighbors,
                _ => break, // No neighbors - end walk
            };

            let next_node = self.choose_next_node_biased(
                graph, previous_node, current_node, &neighbors, p, q, rng
            )?;
            walk.push(next_node);
        }

        Ok(walk)
    }

    /// Choose next node based on Node2Vec biased probabilities
    fn choose_next_node_biased(
        &self,
        graph: &ArrowGraph,
        previous_node: &str,
        current_node: &str,
        neighbors: &[String],
        p: f64,
        q: f64,
        rng: &mut Pcg64,
    ) -> Result<String> {
        let mut probabilities = Vec::new();
        let mut cumulative_prob = 0.0;

        // Get neighbors of previous node for distance calculation
        let previous_neighbors: std::collections::HashSet<_> = match graph.neighbors(previous_node) {
            Some(neighbors) => neighbors.iter().collect(),
            None => std::collections::HashSet::new(),
        };

        for neighbor in neighbors {
            let weight = if neighbor == previous_node {
                // Return to previous node - controlled by p
                1.0 / p
            } else if previous_neighbors.contains(neighbor) {
                // Stay within local neighborhood - weight = 1
                1.0
            } else {
                // Explore further - controlled by q
                1.0 / q
            };

            // Apply edge weight if available
            let edge_weight = graph.edge_weight(current_node, neighbor).unwrap_or(1.0);
            let final_weight = weight * edge_weight;

            cumulative_prob += final_weight;
            probabilities.push((neighbor, cumulative_prob));
        }

        // Sample from the probability distribution
        let random_val = rng.gen::<f64>() * cumulative_prob;
        
        for (neighbor, cum_prob) in probabilities {
            if random_val <= cum_prob {
                return Ok(neighbor.clone());
            }
        }

        // Fallback - should not happen with proper probability distribution
        Ok(neighbors[0].clone())
    }
}

impl GraphAlgorithm for Node2VecWalk {
    fn execute(&self, graph: &ArrowGraph, params: &AlgorithmParams) -> Result<RecordBatch> {
        let walk_length: usize = params.get("walk_length").unwrap_or(80);
        let num_walks: usize = params.get("num_walks").unwrap_or(10);
        let p: f64 = params.get("p").unwrap_or(1.0);
        let q: f64 = params.get("q").unwrap_or(1.0);
        let seed: Option<u64> = params.get("seed");
        
        // Get start nodes - if not specified, use all nodes
        let start_nodes: Vec<String> = if let Some(nodes) = params.get::<Vec<String>>("start_nodes") {
            nodes
        } else {
            graph.node_ids().cloned().collect()
        };

        let walks = self.compute_node2vec_walks(graph, &start_nodes, walk_length, num_walks, p, q, seed)?;

        // Convert walks to Arrow format with additional Node2Vec metadata
        let schema = Arc::new(Schema::new(vec![
            Field::new("walk_id", DataType::UInt32, false),
            Field::new("step", DataType::UInt32, false),
            Field::new("node_id", DataType::Utf8, false),
            Field::new("p_param", DataType::Float64, false),
            Field::new("q_param", DataType::Float64, false),
        ]));

        let mut walk_ids = Vec::new();
        let mut steps = Vec::new();
        let mut node_ids = Vec::new();
        let mut p_params = Vec::new();
        let mut q_params = Vec::new();

        for (walk_id, walk) in walks.iter().enumerate() {
            for (step, node_id) in walk.iter().enumerate() {
                walk_ids.push(walk_id as u32);
                steps.push(step as u32);
                node_ids.push(node_id.clone());
                p_params.push(p);
                q_params.push(q);
            }
        }

        RecordBatch::try_new(
            schema,
            vec![
                Arc::new(UInt32Array::from(walk_ids)),
                Arc::new(UInt32Array::from(steps)),
                Arc::new(StringArray::from(node_ids)),
                Arc::new(Float64Array::from(p_params)),
                Arc::new(Float64Array::from(q_params)),
            ],
        ).map_err(GraphError::from)
    }

    fn name(&self) -> &'static str {
        "node2vec"
    }

    fn description(&self) -> &'static str {
        "Generate Node2Vec-style biased random walks with return (p) and in-out (q) parameters"
    }
}

/// Graph sampling using various strategies
pub struct GraphSampling;

impl GraphSampling {
    /// Random node sampling
    pub fn random_node_sampling(
        &self,
        graph: &ArrowGraph,
        sample_size: usize,
        seed: Option<u64>,
    ) -> Result<Vec<String>> {
        let all_nodes: Vec<String> = graph.node_ids().cloned().collect();
        
        if sample_size >= all_nodes.len() {
            return Ok(all_nodes);
        }

        let mut rng = match seed {
            Some(s) => Pcg64::seed_from_u64(s),
            None => Pcg64::from_entropy(),
        };

        let sampled_nodes = all_nodes.choose_multiple(&mut rng, sample_size).cloned().collect();
        Ok(sampled_nodes)
    }

    /// Random edge sampling
    pub fn random_edge_sampling(
        &self,
        graph: &ArrowGraph,
        sample_ratio: f64,
        seed: Option<u64>,
    ) -> Result<RecordBatch> {
        if !(0.0..=1.0).contains(&sample_ratio) {
            return Err(GraphError::invalid_parameter(
                "sample_ratio must be between 0.0 and 1.0"
            ));
        }

        let mut rng = match seed {
            Some(s) => Pcg64::seed_from_u64(s),
            None => Pcg64::from_entropy(),
        };

        let mut sampled_sources = Vec::new();
        let mut sampled_targets = Vec::new();
        let mut sampled_weights = Vec::new();

        // Sample edges based on ratio
        for node_id in graph.node_ids() {
            if let Some(neighbors) = graph.neighbors(node_id) {
                for neighbor in neighbors {
                    if rng.gen::<f64>() < sample_ratio {
                        sampled_sources.push(node_id.clone());
                        sampled_targets.push(neighbor.clone());
                        sampled_weights.push(graph.edge_weight(node_id, neighbor).unwrap_or(1.0));
                    }
                }
            }
        }

        let schema = Arc::new(Schema::new(vec![
            Field::new("source", DataType::Utf8, false),
            Field::new("target", DataType::Utf8, false),
            Field::new("weight", DataType::Float64, false),
        ]));

        RecordBatch::try_new(
            schema,
            vec![
                Arc::new(StringArray::from(sampled_sources)),
                Arc::new(StringArray::from(sampled_targets)),
                Arc::new(Float64Array::from(sampled_weights)),
            ],
        ).map_err(GraphError::from)
    }

    /// Snowball sampling (BFS-based expansion)
    pub fn snowball_sampling(
        &self,
        graph: &ArrowGraph,
        seed_nodes: &[String],
        k_hops: usize,
        max_nodes: Option<usize>,
    ) -> Result<Vec<String>> {
        if k_hops == 0 {
            return Ok(seed_nodes.to_vec());
        }

        let mut sampled_nodes: std::collections::HashSet<String> = std::collections::HashSet::new();
        let mut current_frontier: std::collections::HashSet<String> = std::collections::HashSet::new();

        // Initialize with seed nodes
        for seed_node in seed_nodes {
            if !graph.has_node(seed_node) {
                return Err(GraphError::node_not_found(seed_node.clone()));
            }
            sampled_nodes.insert(seed_node.clone());
            current_frontier.insert(seed_node.clone());
        }

        // Expand k hops
        for _ in 0..k_hops {
            let mut next_frontier = std::collections::HashSet::new();

            for node in &current_frontier {
                if let Some(neighbors) = graph.neighbors(node) {
                    for neighbor in neighbors {
                        if !sampled_nodes.contains(neighbor) {
                            sampled_nodes.insert(neighbor.clone());
                            next_frontier.insert(neighbor.clone());

                            // Check if we've reached the maximum number of nodes
                            if let Some(max) = max_nodes {
                                if sampled_nodes.len() >= max {
                                    return Ok(sampled_nodes.into_iter().collect());
                                }
                            }
                        }
                    }
                }
            }

            current_frontier = next_frontier;
            if current_frontier.is_empty() {
                break; // No more nodes to expand
            }
        }

        Ok(sampled_nodes.into_iter().collect())
    }
}

impl GraphAlgorithm for GraphSampling {
    fn execute(&self, graph: &ArrowGraph, params: &AlgorithmParams) -> Result<RecordBatch> {
        let sampling_method: String = params.get("method").unwrap_or("random_node".to_string());

        match sampling_method.as_str() {
            "random_node" => {
                let sample_size: usize = params.get("sample_size").unwrap_or(graph.node_count() / 2);
                let seed: Option<u64> = params.get("seed");
                
                let sampled_nodes = self.random_node_sampling(graph, sample_size, seed)?;

                let schema = Arc::new(Schema::new(vec![
                    Field::new("node_id", DataType::Utf8, false),
                ]));

                RecordBatch::try_new(
                    schema,
                    vec![Arc::new(StringArray::from(sampled_nodes))],
                ).map_err(GraphError::from)
            }
            "random_edge" => {
                let sample_ratio: f64 = params.get("sample_ratio").unwrap_or(0.5);
                let seed: Option<u64> = params.get("seed");
                
                self.random_edge_sampling(graph, sample_ratio, seed)
            }
            "snowball" => {
                let seed_nodes: Vec<String> = params.get("seed_nodes")
                    .unwrap_or_else(|| vec![graph.node_ids().next().unwrap().clone()]);
                let k_hops: usize = params.get("k_hops").unwrap_or(2);
                let max_nodes: Option<usize> = params.get("max_nodes");
                
                let sampled_nodes = self.snowball_sampling(graph, &seed_nodes, k_hops, max_nodes)?;

                let schema = Arc::new(Schema::new(vec![
                    Field::new("node_id", DataType::Utf8, false),
                ]));

                RecordBatch::try_new(
                    schema,
                    vec![Arc::new(StringArray::from(sampled_nodes))],
                ).map_err(GraphError::from)
            }
            _ => Err(GraphError::invalid_parameter(format!(
                "Unknown sampling method: {}. Supported methods: random_node, random_edge, snowball",
                sampling_method
            )))
        }
    }

    fn name(&self) -> &'static str {
        "graph_sampling"
    }

    fn description(&self) -> &'static str {
        "Perform various graph sampling strategies including random node/edge sampling and snowball sampling"
    }
}