R/graph_to_complex.R

Defines functions graph_to_complex

Documented in graph_to_complex

#' Generates a simplicial complex from a graph.
#'
#' Takes the adjacency matrix of a graph and returns a clique complex that can be used
#' by other functions in the \code{RayleighSelection} package.
#'
#' @param adjacency a weighted adjacency matrix.
#' @param clique if set to FALSE the computation of 2-simplices is skipped.
#' @return An object of the class \code{simplicial}. The class \code{simplicial} inherits from
#' the class \code{igraph}.
#' @examples
#' library(RayleighSelection)
#' # Load MNIST example dataset
#' data("mnist")
#'
#' # Compute a correlation matrix for a subset of the LFW dataset using only pixels with high variance
#' mnist_test <- mnist[,1:800]
#' mnist_test_top <- mnist_test[apply(mnist_test, 1, var) > 0.9,]
#' mnist_test_distances <- cor(mnist_test_top)
#'
#' # Compute a simplifical complex
#' gg <- graph_to_complex(mnist_test_distances)
#'
#' # Plot the skeleton of the simplicial complex colored by the intensity of the 500th pixel
#' plot_skeleton(gg, k=as.numeric(mnist_test[500,]))
#'
#' @export
#'
graph_to_complex <- function(adjacency, clique=TRUE)
{
  # Builds igraph object
  diag(adjacency) <- 0
  g2 <- graph.adjacency(adjacency, mode='undirected', weighted=TRUE)

  # Enriches the class with information about the open cover and adjacency matrix
  g2$points_in_vertex <- seq(1, sqrt(length(adjacency)), 1)
  g2$order <- seq(1, sqrt(length(adjacency)), 1)
  g2$adjacency <- get.adjacency(g2, sparse = TRUE)
  g2$adjacency[lower.tri(g2$adjacency)] <- 0
  g2$one_simplices <- g2$adjacency
  g2$two_simplices <- list()
  siz <- sqrt(length(g2$adjacency))
  for (i in 1:siz){
    g2$two_simplices[[i]] <- Matrix(0, siz, siz, sparse=TRUE)
  }
  if (clique) {
    for (m in suppressWarnings(cliques(g2,3,3))) {
      g2$two_simplices[[as.numeric(m[1])]][as.numeric(m[2]), as.numeric(m[3])] <- 1
    }
  }

  # Decorates the graph to include node sizes, color, etc.
  V(g2)$size <- 4.0
  V(g2)$label <- ""
  V(g2)$frame.color <- "black"

  class(g2) <-  c('simplicial', 'igraph')
  return(g2)
}
CamaraLab/RayleighSelection documentation built on Aug. 16, 2021, 12:01 p.m.