completed code for contraction of vertices