changed get_topo_dist to use bigmatrix and optimize ram