optimized connected components