is_connected algorithm