implemented connected components algorithm