I have a binary square sparse matrix A that can be seen as an adjacency matrix of a Graph.
Now consider a second binary square sparse matrix B that could have a different dimension.
I would like to build an algorithm that reorder the matrix A, or a set of elements of matrix A, in order to maximize the overlap with matrix B.
Below is an example:
I create a random sparse matrix B in python:
import scipy.spatial as spatial
import matplotlib.pyplot as plt
import numpy
coords_B = numpy.random.uniform(size=(20,3))*20
pdistmat = spatial.distance.squareform(spatial.distance.pdist(coords_B))
B = (pdistmat <= 8)
matshow(B)
colorbar()
Now I build the matrix A by adding noise to B and adding some rows and columns:
coords_A = numpy.r_[numpy.random.uniform(size=(5,3))*20,
coords_B + numpy.random.uniform(size=(20,3))*4,
numpy.random.uniform(size=(6,3))*20]
pdistmat = spatial.distance.squareform(spatial.distance.pdist(coords_A))
A = (pdistmat <= 8)
matshow(A)
colorbar()
Now I randomized the matrix A:
random_order = numpy.random.choice(A.shape[0], replace=False,
size=A.shape[0])
A_randomized = A[random_order][:, random_order]
matshow(A_randomized)
colorbar()
I would like to reorder A_randomized
to maximize the overlap with B as below:
x, y = numpy.where(A==1)
scatter(y,30-x, label='A')
x, y = numpy.where(B==1)
x+=5
y+=5
scatter(y,30-x, marker='.', label='B')
legend()
I would like to implement the algorithm in python using numpy and scipy.sparse libraries.