Spectral#

2026-03-13

4 min read time

Applies to Linux

This page provides C++ class references for the publicly-exposed elements of the raft/spectral package. The spectral methods in RAFT provide graph-based clustering and partitioning algorithms that operate on sparse matrix representations.

Graph Partitioning#

#include <raft/spectral/partition.cuh>

namespace raft::spectral

template<typename vertex_t, typename weight_t, typename nnz_t, typename EigenSolver, typename ClusterSolver> std::tuple< vertex_t, weight_t, vertex_t > partition (raft::resources const &handle, matrix::sparse_matrix_t< vertex_t, weight_t, nnz_t > const &csr_m, EigenSolver const &eigen_solver, ClusterSolver const &cluster_solver, vertex_t *__restrict__ clusters, weight_t *eigVals, weight_t *eigVecs)

Compute spectral graph partition.

Compute partition for a weighted undirected graph. This partition attempts to minimize the cost function: Cost = \(sum_i\) (Edges cut by ith partition)/(Vertices in ith partition)

Parameters:
  • handle – raft handle for managing expensive resources

  • csr_m – Weighted graph in CSR format

  • eigen_solver – Eigensolver implementation

  • cluster_solver – Cluster solver implementation

  • clusters – (Output, device memory, n entries) Partition assignments.

  • eigVals – Output eigenvalue array pointer on device

  • eigVecs – Output eigenvector array pointer on device

Returns:

statistics: number of eigensolver iterations, .

template<typename vertex_t, typename weight_t, typename nnz_t> void analyzePartition (raft::resources const &handle, matrix::sparse_matrix_t< vertex_t, weight_t, nnz_t > const &csr_m, vertex_t nClusters, const vertex_t *__restrict__ clusters, weight_t &edgeCut, weight_t &cost)

Compute cost function for partition.

This function determines the edges cut by a partition and a cost function: Cost = \(sum_i\) (Edges cut by ith partition)/(Vertices in ith partition) Graph is assumed to be weighted and undirected.

Parameters:
  • handle – raft handle for managing expensive resources

  • csr_m – Weighted graph in CSR format

  • nClusters – Number of partitions.

  • clusters – (Input, device memory, n entries) Partition assignments.

  • edgeCut – On exit, weight of edges cut by partition.

  • cost – On exit, partition cost function.

Modularity Maximization#

#include <raft/spectral/modularity_maximization.cuh>

namespace raft::spectral

template<typename vertex_t, typename weight_t, typename nnz_t, typename EigenSolver, typename ClusterSolver> std::tuple< vertex_t, weight_t, vertex_t > modularity_maximization (raft::resources const &handle, matrix::sparse_matrix_t< vertex_t, weight_t, nnz_t > const &csr_m, EigenSolver const &eigen_solver, ClusterSolver const &cluster_solver, vertex_t *__restrict__ clusters, weight_t *eigVals, weight_t *eigVecs)

Compute partition for a weighted undirected graph. This partition attempts to minimize the cost function: Cost = \(sum_i\) (Edges cut by ith partition)/(Vertices in ith partition)

Parameters:
  • handle – raft handle for managing expensive resources

  • csr_m – Weighted graph in CSR format

  • eigen_solver – Eigensolver implementation

  • cluster_solver – Cluster solver implementation

  • clusters – (Output, device memory, n entries) Partition assignments.

  • eigVals – Output eigenvalue array pointer on device

  • eigVecs – Output eigenvector array pointer on device

Returns:

statistics: number of eigensolver iterations, .

template<typename vertex_t, typename weight_t, typename nnz_t> void analyzeModularity (raft::resources const &handle, matrix::sparse_matrix_t< vertex_t, weight_t, nnz_t > const &csr_m, vertex_t nClusters, vertex_t const *__restrict__ clusters, weight_t &modularity)

Compute modularity.

This function determines the modularity based on a graph and cluster assignments

Parameters:
  • handle – raft handle for managing expensive resources

  • csr_m – Weighted graph in CSR format

  • nClusters – Number of clusters.

  • clusters – (Input, device memory, n entries) Cluster assignments.

  • modularity – On exit, modularity