Efficient algorithm to study interconnected networks