def basic_greedy():
start = 1
mindist = d_dict[(1,2)]
m = 0
while len(tour)!=size:
tour.append(start)
for k in range(len(cities)):
if cities[k] not in tour:
if d_dict[(start,cities[k])]<mindist:
mindist = d_dict[(start,cities[k])]
m = k
start = cities[m]
return tour
This is my code for basic greedy search in Python. start is the start city, tour is a list that shall contain cities in order they are visited, cities is a list containing all cities from 1 to size (1,2,3,4.....12..size) where size is the number of cities. d_dict is a dictionary containing distances between every possible pair of cities. mindist and m are just temporary variables to keep track of nearest neighbours etc.
I expect the code to start from city 1, move to the nearest neighbour, then to the nearest till every city is covered once. I expect the output of this code to be something along the lines of [1,5,3,8,4,6,2,7] for cities 1 to 8 (some combination of visiting all the cities exactly once) but I get [1,7,7,7,7,7,7,7]. What's wrong?