Graph connectedness algorithm.