complete algorithm