calculate connected components