implemented weighted algorithm without path compression