Frage

How can I write an R/Python program which creates a node-edge adjacency matrix in which rows denote nodes and columns denote the edges and an entry is one in this adjacency matrix if the edge is part of a triangle and the node is part of the same triangle. I am actually more interested to make use of igraph or linkcomm for this purpose but wouldn't mind seeing a different package/program for this purpose.

I know I can use maximal.clique(g) for locating the triangle but I am not sure of how to make use of this data to create the node-edge triangle adjacency matrix.

> g <- erdos.renyi.game(15, 45, type="gnm", dir=TRUE)
> triad.census(g)
 [1] 113 168 38 16 13 49 23 17 7 2
[11] 2 1 2 2 2 0
> str(g)
IGRAPH D--- 15 45 -- Erdos renyi (gnm) graph
+ attr: name (g/c), type (g/c), loops
 (g/x), m (g/n)
+ edges:
 1 -> 3 4 6 12 13 2 -> 1 3 7 
 3 -> 2 5 10 15 4 -> 5 12 14 
 5 -> 6 7 9 6 -> 4 8 12 
 7 -> 5 9 12 8 -> 2 7 15 
 9 -> 1 4 11 13 10 -> 4 5 8 
11 -> 1 2 9 12 -> 1 4 14 15 
13 -> 15 14 -> 11 12 
15 -> 3 
> maximal.cliques(g)
[[1]]
[1] 13 15


[[2]]
[1] 13 1 9


[[3]]
[1] 2 8 7


[[4]]
[1] 2 1 3


[[5]]
[1] 2 1 11


[[6]]
[1] 3 5 10


[[7]]
[1] 3 15


[[8]]
[1] 4 14 12


[[9]]
[1] 4 10 5


[[10]]
[1] 4 5 6


[[11]]
[1] 4 5 9


[[12]]
[1] 4 1 9


[[13]]
[1] 4 1 12 6


[[14]]
[1] 5 7 9


[[15]]
[1] 6 8


[[16]]
[1] 7 12


[[17]]
[1] 8 15


[[18]]
[1] 8 10


[[19]]
[1] 9 1 11


[[20]]
[1] 11 14


[[21]]
[1] 12 15


Warning message:
In maximal.cliques(g) :
 At maximal_cliques_template.h:203 :Edge directions are ignored for maximal clique calculation

According to the Vincent's answer when I use the following I am doubtful if it finds the clique of exactly size 3 or it finds cliques of size 3 and greater? (I just need the triangles). One problem is that this code is super slow. Any idea on how to speed up this?

library(igraph)
set.seed(1)
g <- erdos.renyi.game(100, .6)
#print(g)
plot(g)
ij <- get.edgelist(g)
print(ij)
library(Matrix)
m <- sparseMatrix(
  i = rep(seq(nrow(ij)), each=2),
  j = as.vector(t(ij)),
  x = 1
)
print(m)
# Maximal cliques of size at least 3
cl <- maximal.cliques(g)
print(cl)
cl <- cl[ sapply(cl, length) > 2 ]
print(cl)
# Function to test if an edge is part of a triangle
triangle <- function(e) {
  any( sapply( cl, function(u) all( e %in% u ) ) )
}
print(triangle)
# Only keep those edges
kl <- ij[ apply(ij, 1, triangle), ]
print(kl)
# Same code as before
m <- sparseMatrix(
  i = rep(seq(nrow(kl)), each=2),
  j = as.vector(t(kl)),
  x = 1
)
print(m)

Also for some reasons the function cocluster tells me that the output m is not a matrix. Any idea on what I should do to make use of m sparse matrix in the cocluster function?

>library("blockcluster")
> out<-cocluster(m,datatype="binary",nbcocluster=c(2,3))
Error in cocluster(m, datatype = "binary", nbcocluster = c(2, 3)) : 
  Data should be matrix.
War es hilfreich?

Lösung

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
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top