The following gives you an edge/vertex adjacency matrix, but for all edges, not just those included in triangles.
library(igraph)
set.seed(1)
g <- erdos.renyi.game(6, .6)
plot(g)
ij <- get.edgelist(g)
library(Matrix)
m <- sparseMatrix(
i = rep(seq(nrow(ij)), each=2),
j = as.vector(t(ij)),
x = 1
)
As you suggest, you can use maximal.cliques
to identify the edges that are part of triangle
(equivalently, that are part of a maximal
clique of size at least 3).
# Maximal cliques of size at least 3
cl <- maximal.cliques(g)
cl <- cl[ sapply(cl, length) > 2 ]
# Function to test if an edge is part of a triangle
triangle <- function(e) {
any( sapply( cl, function(u) all( e %in% u ) ) )
}
# Only keep those edges
kl <- ij[ apply(ij, 1, triangle), ]
# Same code as before
m <- sparseMatrix(
i = rep(seq(nrow(kl)), each=2),
j = as.vector(t(kl)),
x = 1
)
m
# 5 x 5 sparse Matrix of class "dgCMatrix"
# [1,] 1 1 . . .
# [2,] . 1 1 . .
# [3,] 1 . . . 1
# [4,] . 1 . . 1
# [5,] . . 1 . 1