I graph can count isomorphic subgraphs.
Example 1. Checking for the number of 3 cliques in a 5 clique. There is obviously 5 choose 3 = 10.
library(igraph)
##
## Attaching package: 'igraph'
## The following objects are masked from 'package:stats':
##
## decompose, spectrum
## The following object is masked from 'package:base':
##
## union
adj_G = matrix(c(0,1,1,1,1,
1,0,1,1,1,
1,1,0,1,1,
1,1,1,0,1,
1,1,1,1,0),5,5)
adj_H = matrix(c(0,1,1,
1,0,1,
1,1,0), 3,3)
G <- graph_from_adjacency_matrix(adj_G)
H <- graph_from_adjacency_matrix(adj_H)
subgraph_isomorphisms = count_subgraph_isomorphisms(H, G, method = c("lad"))
# we mod out by the order of the automorphism group of H.
size_of_automorphism_group_H = count_isomorphisms(H,H)
count = subgraph_isomorphisms/size_of_automorphism_group_H
count
## [1] 10
It worked
Does it support directed graphs?
# does it support directed graphs?
adj_G = matrix(c(0,1,0,0,
0,0,0,1,
0,1,0,0,
1,0,1,0),4,4)
adj_H = matrix(c(0,1,0,
0,0,1,
1,0,0), 3,3)
G <- graph_from_adjacency_matrix(adj_G)
H <- graph_from_adjacency_matrix(adj_H)
subgraph_isomorphisms = count_subgraph_isomorphisms(H, G, method = c("lad"))
size_of_automorphism_group_H = count_isomorphisms(H,H)
count = subgraph_isomorphisms/size_of_automorphism_group_H
count
## [1] 2
Yes!
Now, how fast is it for a random larger (n = 100) graph?
make_random_asymmetric <- function(n, threshold){
number_of_entries = n**2
x = matrix(runif(number_of_entries),nrow=n)
x = x - diag(diag(x),n,n)
mat = round(x,2)
mat[mat >= threshold] <- 1
mat[mat < threshold] <- 0
return(mat)
}
make_random_asymmetric(5,0.6)
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0 0 1 0 0
## [2,] 1 0 1 1 1
## [3,] 0 0 0 1 1
## [4,] 1 0 1 0 1
## [5,] 0 0 0 1 0
adj_G = make_random_asymmetric(100,0.6)
adj_H = matrix(c(0,1,0,
0,0,1,
1,0,0), 3,3)
G <- graph_from_adjacency_matrix(adj_G)
H <- graph_from_adjacency_matrix(adj_H)
subgraph_isomorphisms = count_subgraph_isomorphisms(H, G, method = c("lad"))
size_of_automorphism_group_H = count_isomorphisms(H,H)
count = subgraph_isomorphisms/size_of_automorphism_group_H
count
## [1] 22204
Not that slow! Maybe 20 seconds.