Reimplementation of vertex contraction