sqlitegraph 2.2.2

Embedded graph database with full ACID transactions, HNSW vector search, dual backend support, and comprehensive graph algorithms library
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
//! Weakly Connected Components (WCC) algorithm for undirected connectivity analysis.
//!
//! This module provides algorithms for finding weakly connected components in directed graphs.
//! A weakly connected component is a maximal subgraph where all nodes are connected
//! when treating all edges as undirected.
//!
//! # When to Use WCC
//!
//! - **Dependency Clustering**: Find groups of interconnected dependencies
//! - **Graph Fragmentation**: Identify isolated subgraphs
//! - **Reachability Analysis**: Understand undirected connectivity structure
//! - **Preprocessing**: Step before algorithms that require connected graphs
//!
//! # WCC vs SCC vs Connected Components
//!
//! | Algorithm | Edge Treatment | Use Case |
//! |-----------|---------------|----------|
//! | **Weakly Connected Components** | Treats edges as bidirectional | Undirected connectivity analysis |
//! | **Strongly Connected Components** | Respects edge direction | Finds cycles and mutual reachability |
//! | **Connected Components** | Treats edges as bidirectional | Undirected graphs |
//!
//! # Algorithm
//!
//! Uses bidirectional breadth-first search (BFS):
//!
//! 1. Start with an unvisited node
//! 2. Explore all reachable nodes using both outgoing AND incoming edges
//! 3. All visited nodes form one weakly connected component
//! 4. Repeat until all nodes are visited
//!
//! # Complexity
//!
//! - **Time**: O(|V| + |E|) - visits each node and edge once
//! - **Space**: O(|V|) for visited set and BFS queue
//!
//! # Example
//!
//! ```rust,ignore
//! use sqlitegraph::{SqliteGraph, algo::weakly_connected_components};
//!
//! let graph = SqliteGraph::open_in_memory()?;
//! // ... add nodes and edges ...
//!
//! let components = weakly_connected_components(&graph)?;
//!
//! for (i, component) in components.iter().enumerate() {
//!     println!("Component {}: {} nodes", i, component.len());
//! }
//! ```

use std::collections::VecDeque;

use crate::progress::ProgressCallback;
use crate::{errors::SqliteGraphError, graph::SqliteGraph};

/// Finds all weakly connected components in the graph using bidirectional BFS.
///
/// A weakly connected component is a maximal subgraph where any two nodes are connected
/// by a path when ignoring edge direction. This function treats all edges as bidirectional.
///
/// # Arguments
/// * `graph` - The graph to analyze
///
/// # Returns
/// Vector of components, where each component is a sorted vector of node IDs.
/// Components are sorted by their smallest node ID for deterministic output.
///
/// # Complexity
/// Time: O(|V| + |E|) - visits each node and edge once
/// Space: O(|V|) for visited set and BFS queue
///
/// # Edge Cases
/// - Empty graph: Returns empty vector
/// - Single node: Returns [[node_id]]
/// - Disconnected graph: Returns multiple components
///
/// # Example
/// ```rust,ignore
/// use sqlitegraph::{SqliteGraph, algo::weakly_connected_components};
///
/// let graph = SqliteGraph::open_in_memory()?;
/// // ... add nodes and edges ...
///
/// let components = weakly_connected_components(&graph)?;
///
/// // Each component is a sorted vector of node IDs
/// for (i, component) in components.iter().enumerate() {
///     println!("Component {}: {:?}", i, component);
/// }
/// ```
pub fn weakly_connected_components(graph: &SqliteGraph) -> Result<Vec<Vec<i64>>, SqliteGraphError> {
    let mut components = Vec::new();
    let mut visited = ahash::AHashSet::new();

    // Get all node IDs
    let all_ids = graph.all_entity_ids()?;

    // Process each unvisited node
    for id in all_ids {
        if !visited.insert(id) {
            continue; // Already visited
        }

        // Start BFS from this node
        let mut queue = VecDeque::new();
        queue.push_back(id);
        let mut component = Vec::new();

        // Bidirectional BFS - explore both outgoing and incoming edges
        while let Some(node) = queue.pop_front() {
            component.push(node);

            // Explore outgoing edges
            for next in graph.fetch_outgoing(node)? {
                if visited.insert(next) {
                    queue.push_back(next);
                }
            }

            // Explore incoming edges
            for prev in graph.fetch_incoming(node)? {
                if visited.insert(prev) {
                    queue.push_back(prev);
                }
            }
        }

        // Sort component for deterministic output
        component.sort();
        components.push(component);
    }

    // Sort components by their first element for deterministic output
    components.sort_by(|a, b| a.first().cmp(&b.first()));

    Ok(components)
}

/// Finds all weakly connected components with progress tracking.
///
/// Same algorithm as [`weakly_connected_components`] but reports progress during execution.
/// Useful for long-running operations on large graphs.
///
/// # Arguments
/// * `graph` - The graph to analyze
/// * `progress` - Progress callback for reporting execution status
///
/// # Returns
/// Vector of components, where each component is a sorted vector of node IDs.
///
/// # Progress Reporting
///
/// The callback receives:
/// - `current`: Current node being processed (1-indexed)
/// - `total`: Total number of nodes in the graph
/// - `message`: "Finding weakly connected components"
///
/// # Example
/// ```rust,ignore
/// use sqlitegraph::{
///     algo::weakly_connected_components_with_progress,
///     progress::ConsoleProgress
/// };
///
/// let progress = ConsoleProgress::new();
/// let components = weakly_connected_components_with_progress(&graph, &progress)?;
/// // Output: Finding weakly connected components [10/100]...
/// ```
pub fn weakly_connected_components_with_progress<F>(
    graph: &SqliteGraph,
    progress: &F,
) -> Result<Vec<Vec<i64>>, SqliteGraphError>
where
    F: ProgressCallback,
{
    let mut components = Vec::new();
    let mut visited = ahash::AHashSet::new();

    // Get all node IDs
    let all_ids = graph.all_entity_ids()?;
    let total = all_ids.len();

    // Process each unvisited node
    for (idx, id) in all_ids.iter().enumerate() {
        if !visited.insert(*id) {
            continue; // Already visited
        }

        // Report progress
        progress.on_progress(idx + 1, Some(total), "Finding weakly connected components");

        // Start BFS from this node
        let mut queue = VecDeque::new();
        queue.push_back(*id);
        let mut component = Vec::new();

        // Bidirectional BFS - explore both outgoing and incoming edges
        while let Some(node) = queue.pop_front() {
            component.push(node);

            // Explore outgoing edges
            for next in graph.fetch_outgoing(node)? {
                if visited.insert(next) {
                    queue.push_back(next);
                }
            }

            // Explore incoming edges
            for prev in graph.fetch_incoming(node)? {
                if visited.insert(prev) {
                    queue.push_back(prev);
                }
            }
        }

        // Sort component for deterministic output
        component.sort();
        components.push(component);
    }

    // Sort components by their first element for deterministic output
    components.sort_by(|a, b| a.first().cmp(&b.first()));

    // Report completion
    progress.on_complete();

    Ok(components)
}

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

    /// Helper to create a test graph with a linear chain: 0 -> 1 -> 2 -> 3
    fn create_linear_chain_graph() -> SqliteGraph {
        let graph = SqliteGraph::open_in_memory().expect("Failed to create graph");

        // Create nodes 0-3
        for i in 0..4 {
            let entity = GraphEntity {
                id: 0,
                kind: "node".to_string(),
                name: format!("node_{}", i),
                file_path: Some(format!("node_{}.rs", i)),
                data: serde_json::json!({"index": i}),
            };
            graph
                .insert_entity(&entity)
                .expect("Failed to insert entity");
        }

        // Get entity IDs and create edges: 0 -> 1 -> 2 -> 3
        let entity_ids = graph.list_entity_ids().expect("Failed to get IDs");
        for i in 0..entity_ids.len().saturating_sub(1) {
            let edge = crate::GraphEdge {
                id: 0,
                from_id: entity_ids[i],
                to_id: entity_ids[i + 1],
                edge_type: "next".to_string(),
                data: serde_json::json!({}),
            };
            graph.insert_edge(&edge).ok();
        }

        graph
    }

    /// Helper to create a disconnected graph: 0 -> 1 and 2 -> 3 (two components)
    fn create_disconnected_graph() -> SqliteGraph {
        let graph = SqliteGraph::open_in_memory().expect("Failed to create graph");

        // Create nodes 0-3
        for i in 0..4 {
            let entity = GraphEntity {
                id: 0,
                kind: "node".to_string(),
                name: format!("node_{}", i),
                file_path: Some(format!("node_{}.rs", i)),
                data: serde_json::json!({"index": i}),
            };
            graph
                .insert_entity(&entity)
                .expect("Failed to insert entity");
        }

        // Get entity IDs and create two disconnected chains: 0 -> 1 and 2 -> 3
        let entity_ids = graph.list_entity_ids().expect("Failed to get IDs");

        // First chain: 0 -> 1
        let edge1 = crate::GraphEdge {
            id: 0,
            from_id: entity_ids[0],
            to_id: entity_ids[1],
            edge_type: "next".to_string(),
            data: serde_json::json!({}),
        };
        graph.insert_edge(&edge1).ok();

        // Second chain: 2 -> 3
        let edge2 = crate::GraphEdge {
            id: 0,
            from_id: entity_ids[2],
            to_id: entity_ids[3],
            edge_type: "next".to_string(),
            data: serde_json::json!({}),
        };
        graph.insert_edge(&edge2).ok();

        graph
    }

    #[test]
    fn test_wcc_empty_graph() {
        // Scenario: WCC on empty graph
        // Expected: Returns empty vector
        let graph = SqliteGraph::open_in_memory().expect("Failed to create graph");

        let result = weakly_connected_components(&graph);
        assert!(result.is_ok(), "WCC failed on empty graph");

        let components = result.unwrap();
        assert_eq!(components.len(), 0, "Expected 0 components in empty graph");
    }

    #[test]
    fn test_wcc_single_node() {
        // Scenario: WCC on graph with single node
        // Expected: Returns [[node_id]]
        let graph = SqliteGraph::open_in_memory().expect("Failed to create graph");

        let entity = GraphEntity {
            id: 0,
            kind: "node".to_string(),
            name: "single_node".to_string(),
            file_path: Some("single_node.rs".to_string()),
            data: serde_json::json!({}),
        };
        graph
            .insert_entity(&entity)
            .expect("Failed to insert entity");

        let result = weakly_connected_components(&graph);
        assert!(result.is_ok(), "WCC failed on single node");

        let components = result.unwrap();
        assert_eq!(components.len(), 1, "Expected 1 component");
        assert_eq!(components[0].len(), 1, "Expected 1 node in component");
    }

    #[test]
    fn test_wcc_linear_chain() {
        // Scenario: WCC on linear chain 0 -> 1 -> 2 -> 3
        // Expected: All nodes in one component (edges are bidirectional)
        let graph = create_linear_chain_graph();

        let result = weakly_connected_components(&graph);
        assert!(result.is_ok(), "WCC failed on linear chain");

        let components = result.unwrap();
        assert_eq!(components.len(), 1, "Expected 1 component in linear chain");
        assert_eq!(
            components[0].len(),
            4,
            "Expected all 4 nodes in single component"
        );

        // Verify all nodes appear exactly once
        let all_nodes: Vec<i64> = graph.list_entity_ids().expect("Failed to get IDs");
        let component_nodes: Vec<i64> = components[0].clone();
        assert_eq!(
            all_nodes.len(),
            component_nodes.len(),
            "Mismatch in node count"
        );
    }

    #[test]
    fn test_wcc_disconnected() {
        // Scenario: WCC on disconnected graph: 0 -> 1 and 2 -> 3
        // Expected: Two components, each with 2 nodes
        let graph = create_disconnected_graph();

        let result = weakly_connected_components(&graph);
        assert!(result.is_ok(), "WCC failed on disconnected graph");

        let components = result.unwrap();
        assert_eq!(
            components.len(),
            2,
            "Expected 2 components in disconnected graph"
        );

        // Each component should have 2 nodes
        assert_eq!(
            components[0].len(),
            2,
            "First component should have 2 nodes"
        );
        assert_eq!(
            components[1].len(),
            2,
            "Second component should have 2 nodes"
        );

        // Verify all nodes appear exactly once across all components
        let all_nodes: i64 = graph.list_entity_ids().expect("Failed to get IDs").len() as i64;
        let component_nodes: i64 = components.iter().map(|c| c.len() as i64).sum();
        assert_eq!(all_nodes, component_nodes, "Not all nodes accounted for");
    }

    #[test]
    fn test_wcc_with_progress() {
        // Scenario: WCC with progress callback
        // Expected: Progress callback is called, results match non-progress version
        use crate::progress::NoProgress;

        let graph = create_linear_chain_graph();

        let progress = NoProgress;
        let result =
            weakly_connected_components_with_progress(&graph, &progress).expect("WCC failed");

        let result_no_progress =
            weakly_connected_components(&graph).expect("WCC without progress failed");

        // Results should be identical
        assert_eq!(
            result.len(),
            result_no_progress.len(),
            "Component count mismatch"
        );
        for (comp_with, comp_without) in result.iter().zip(result_no_progress.iter()) {
            assert_eq!(comp_with, comp_without, "Component mismatch");
        }
    }

    #[test]
    fn test_wcc_deterministic_ordering() {
        // Scenario: WCC produces deterministic output
        // Expected: Multiple calls produce same component ordering
        let graph = create_disconnected_graph();

        let result1 = weakly_connected_components(&graph).expect("First WCC failed");
        let result2 = weakly_connected_components(&graph).expect("Second WCC failed");

        // Results should be identical (same ordering)
        assert_eq!(result1, result2, "WCC results are non-deterministic");
    }
}