Algorithm for solving the traveling salesman problem