Retrieves the stochastic matrix of a graph of class igraph
.
Usage
stochastic_matrix(
graph,
column.wise = FALSE,
sparse = igraph_opt("sparsematrices")
)
Details
Let \(M\) be an \(n \times n\) adjacency matrix with real non-negative entries. Let us define \(D = \textrm{diag}(\sum_{i}M_{1i}, \dots, \sum_{i}M_{ni})\)
The (row) stochastic matrix is defined as $$W = D^{-1}M,$$ where it is assumed that \(D\) is non-singular. Column stochastic matrices are defined in a symmetric way.
Author
Gabor Csardi csardi.gabor@gmail.com
Examples
library(Matrix)
## g is a large sparse graph
g <- sample_pa(n = 10^5, power = 2, directed = FALSE)
W <- stochastic_matrix(g, sparse = TRUE)
## a dense matrix here would probably not fit in the memory
class(W)
#> [1] "dgCMatrix"
#> attr(,"package")
#> [1] "Matrix"
## may not be exactly 1, due to numerical errors
max(abs(rowSums(W)) - 1)
#> [1] 5.651035e-13