1
#' @title Spectral gap of a graph
2
#' @description  The spectral (or eigen) gap of a graph is the absolute 
3
#'  difference between the biggest and second biggest eigenvalue
4
#' of the adjacency matrix. To compare spectral gaps across networks, the fraction can be used.
5
#' 
6
#' @param g igraph object
7
#' @param method A string, either "frac" or "abs"
8
#' @return Numeric value
9
#' @details The spectral gap is bounded between 0 and 1 if `method="frac"`. The closer
10
#' the value to one, the bigger the gap. 
11
#' @author David Schoch
12
#' @examples
13
#' #The fractional spectral gap of a threshold graph is usually close to 1
14
#' g <- threshold_graph(50,0.3)
15
#' spectral_gap(g,method = "frac")
16
#' 
17
#' @export
18
#'
19
spectral_gap <- function(g, method = "frac") {
20 2
    spec_decomp <- eigen(igraph::get.adjacency(g, "both"))$values[c(1, 2)]
21 2
    if (method == "frac") {
22 2
        return(1 - spec_decomp[2]/spec_decomp[1])
23 2
    } else if (method == "abs") {
24 2
        return(spec_decomp[1] - spec_decomp[2])
25
    } else {
26 2
        stop("method must be one of 'frac' or 'abs'")
27
    }
28
}

Read our documentation on viewing source code .

Loading