Tools for spectral analysis of complex networks: Laplacian operators, eigen-spectra, heat kernels, diffusion distances, spectral dimension scaling, and network embeddings—plus metrics (BH, shortest paths), partition similarity, geographic distances, and plotting helpers.
Inspired by: Diffusion Geometry Unravels the Emergence of Functional Clusters in Collective Phenomena
M. De Domenico, Phys. Rev. Lett. 118, 168301 (2017). Open access: https://arxiv.org/abs/1704.07068
# install.packages("devtools")
devtools::install_github("yourname/SpectralGeometry")Requires R ≥ 4.2.0. Core imports: igraph, Matrix, RSpectra, expm, stats, utils.
Optional (used in examples/plots): ggplot2, ggdendro, RColorBrewer, plot3D, MBA, dendextend, fossil, reshape2, geosphere.
Random walks and diffusion processes reveal functional proximity beyond pure topology. The (normalized / symmetric) Laplacian’s spectrum shapes return probabilities, diffusion distances, and low-dimensional embeddings that highlight mesoscale structure and functional clusters.
library(SpectralGeometry)
library(igraph)
# A simple graph
g <- make_ring(50)
# 1) Laplacians
Ls <- get_laplacians(g)
L <- getLaplacianMatrix(g, type = "Laplacian") # combinatorial
Lrw <- getLaplacianMatrix(g, type = "Normalized Laplacian") # random-walk
Lsym<- getLaplacianMatrix(g, type = "Quantum Laplacian") # symmetric
# 2) Spectral dimension scaling (average return probability)
taus <- 10^seq(-2, 1, length.out = 10)
sd <- getSpectralDimensionScaling(g, taus) # uses Lrw by default
# 3) Diffusion distance at time tau
D <- getDistanceMatrix(g, tau = 1.0, type = "Normalized Laplacian")
# 4) Embedding (MDS on diffusion distances)
coords <- getEmbeddingCoordinates(g, tau = 1.0, dimension = 2, method = "MDS")
plot(coords[,1], coords[,2], pch = 19, xlab = "x1", ylab = "x2",
main = "Diffusion embedding (MDS)")
# 5) Partition similarity (VI/NMI/RAND/ARAND)
m1 <- rep(1:2, each = 25)
m2 <- sample(m1)
getPartitionSimilarity(m1, m2, method = "NMI")library(SpectralGeometry); library(igraph)
set.seed(1)
g <- sample_smallworld(1, size = 60, nei = 2, p = 0.05)
# Diffusion distance (functional)
D_diff <- getDistanceMatrix(g, tau = 0.8, type = "Normalized Laplacian")
# Shortest path (topological)
D_sp <- getDistanceMatrixSP(g)
# Compare 2D embeddings
emb_diff <- getEmbeddingCoordinatesFromDistanceMatrix(D_diff, 2, "MDS")
emb_sp <- getEmbeddingCoordinatesFromDistanceMatrix(D_sp, 2, "MDS")
par(mfrow = c(1,2))
plot(emb_sp[,1], emb_sp[,2], pch=19, main="MDS on shortest paths", xlab="x1", ylab="x2")
plot(emb_diff[,1], emb_diff[,2], pch=19, main="MDS on diffusion distance", xlab="x1", ylab="x2")
par(mfrow = c(1,1))# Two identical layers should match single-layer distance
g1 <- make_ring(30)
g2 <- graph_from_edgelist(as_edgelist(g1), directed = FALSE)
D1 <- getDistanceMatrix(g1, tau = 0.6)
D2 <- getDistanceMatrixEdgeColored(list(g1, g2), tau = 0.6)
max(abs(D1 - D2)) # ~ 0If you use this package in academic work, please cite the original paper:
@article{DeDomenico2017PRL,
title = {Diffusion Geometry Unravels the Emergence of Functional Clusters in Collective Phenomena},
author = {De Domenico, Manlio},
journal = {Physical Review Letters},
year = {2017},
volume = {118},
number = {16},
pages = {168301},
doi = {10.1103/PhysRevLett.118.168301},
url = {https://arxiv.org/abs/1704.07068}
}
MIT © 2025 CoMuNe Lab. See LICENSE and LICENSE.md.