two phase algorithm