function to find path to parent and return as string after beam search if over