I have a pairwise relatedness 39x39 matrix containing the relatedness values for all pairwise combinations of 39 individuals. I would like to find the largest group of individuals that are completely unrelated, i.e. where all pairwise relatedness values i the group equal 0.
Is there an easy way to do this in R?
A simpler example:
set.seed(420)
#Create the matrix
relatedness.matrix <- matrix(data = sample(x = c(0.5, 1, 0,0), size = 25, replace = TRUE), nrow = 5, ncol = 5)
# Matrix has the same upper and lower triangles
relatedness.matrix[upper.tri(relatedness.matrix)] <- relatedness.matrix[lower.tri(relatedness.matrix)]
# Add names for simplicity of reference
colnames(relatedness.matrix) <- letters[1:5]
rownames(relatedness.matrix) <- letters[1:5]
# Relatedness between the same individual does not count
diag(relatedness.matrix) <- NA
In this case there are three possible solutions: a 2x2 matrix with only e and b, a 2x2 matrix with only c and d, and a 2x2 matrix with only a and e. Adding any other individuals to any of those matrices would be adding a related individual.
EDIT: added that upper and lower triangles are the same, and that there's actually multiple 2x2 solutions in this example.
The set of unrelated individuals in a symmetric adjacency matrix describes an independent set. We can use
igraph::largest_ivsto find the largest such sets.I'll use a larger matrix that is actually symmetric.
Check that the matrix is symmetric
Get the largest independent sets:
Verify that no edges exist among the independent sets.
As a side note, the independent sets in a graph formed from an adjacency matrix,
m, will be the same as the cliques in the graph formed by!m. Interestingly, for my small example,largest_cliquesis more performant thanlargest_ivsfor finding the desired answer:And the performance difference grows as the matrix gets larger:
Check that the answers are the same.