Menu

Travelling Sales Person [TSP] solver

Computer Science for Computer Scientists·Humble_Homework_3894·7 months ago
#3o1P18Ye
#reddit#quot#city#loop#shortest
Long article (529 words) — no AI summary yet.
Reading 0:00
15s threshold

Start from any city and go to the next city with the shortest distance, from there go to the next city with the shortest distance, repeat; until a loop is formed intersecting all cities—this is the base loop.

​Now, starting again from the initial city, go to the next city using the second shortest distance [if, before traversing a distance, you realize it will make the path longer than the base loop, then go to the next city using the next shortest distance; if you run out of paths, go back to the previous city and repeat; and if it is shorter, treat it as the new base loop and proceed], from there go to the next city using the second shortest distance, repeat; until another loop is formed intersecting all cities.

​Repeat this process until you find the shortest base loop, and then output that as the result.

------------------------------------------------------------------------------------------

Code:

import sys

def solve_tsp_custom_logic(distance_matrix):

n = len(distance_matrix)

if n <=…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.