code to check if vertices are connected.