Skip to main content

Module csgraph

Module csgraph 

Source
Expand description

Compressed sparse graph algorithms module

This module provides graph algorithms optimized for sparse matrices, similar to SciPy’s sparse.csgraph module.

§Features

  • Shortest path algorithms (Dijkstra, Bellman-Ford)
  • Connected components analysis
  • Graph traversal utilities (BFS, DFS)
  • Laplacian matrix computation
  • Minimum spanning tree algorithms
  • Graph connectivity testing

§Examples

§Shortest Path

use scirs2_sparse::csgraph::shortest_path;
use scirs2_sparse::csr_array::CsrArray;

// Create a graph as a sparse matrix
let rows = vec![0, 0, 1, 2];
let cols = vec![1, 2, 2, 0];
let data = vec![1.0, 4.0, 2.0, 3.0];
let graph = CsrArray::from_triplets(&rows, &cols, &data, (3, 3), false).expect("Operation failed");

// Find shortest paths from vertex 0
let distances = shortest_path(&graph, Some(0), None, "dijkstra", true, false).expect("Operation failed");

§Connected Components

use scirs2_sparse::csgraph::connected_components;
use scirs2_sparse::csr_array::CsrArray;

// Create a graph
let rows = vec![0, 1, 2, 3];
let cols = vec![1, 0, 3, 2];
let data = vec![1.0, 1.0, 1.0, 1.0];
let graph = CsrArray::from_triplets(&rows, &cols, &data, (4, 4), false).expect("Operation failed");

// Find connected components
let (n_components, labels) = connected_components(&graph, false, "weak", true).expect("Operation failed");

Re-exports§

pub use connected_components::*;
pub use laplacian::*;
pub use minimum_spanning_tree::*;
pub use shortest_path::*;
pub use traversal::*;

Modules§

connected_components
Connected components analysis for sparse graphs
laplacian
Laplacian matrix computation for sparse graphs
minimum_spanning_tree
Minimum spanning tree algorithms for sparse graphs
shortest_path
Shortest path algorithms for sparse graphs
traversal
Graph traversal algorithms for sparse graphs

Enums§

GraphMode
Graph representation modes

Functions§

num_edges
Get the number of edges in a graph matrix
num_vertices
Get the number of vertices in a graph matrix
to_adjacency_list
Convert a sparse matrix to adjacency list representation
validate_graph
Check if a sparse matrix represents a valid graph