Generalized algorithm to handle any type of vertex.