Understanding the beam search algorithm in NLP