Rudy’s OBTF Rudolf Adamkovič

Home / Computer science / Traveling salesman problem (TSP) / Computation in Python


Use traveling_salesman.tour_nearest_neighbor

import traveling_salesman as ts

instance = ts.read_instance('pr76.tsp')
distances = ts.distances(instance)
tour, distance = ts.tour_nearest_neighbor(instance, distances)
solution = ts.solution(tour, instance)

return f"Solution: \n {solution}" "\n\n" f"Distance: {distance:.2E}"
Solution:
 [65 66 51 52 53 54 42 43 28 29 30 31 19 20  5  6  7  8  9 10 11 12 13 14
 15 16 17 18 37 36 35 34 40 41 60 59 58 57 63 64 62 61 55 56 50 49 48 44
 45 46 24 25 21 22 23  1  2  3  4 26 27 33 32 38 39 72 73 71 67 68 69 47
 70 76 75 74]

Distance: 1.57E+05

© 2025 Rudolf Adamkovič under GNU General Public License version 3.
Made with Emacs and secret alien technologies of yesteryear.