fixed problem of spanning trees by applying additional sort