improve with weighting and path compression