Yuce's Blog

Cognitive dissonance is yoga for the brain.*

Shameful Past

2005-10-24

A few hours ago, when I was dealing with my thesis, I've come up with a Python function that constructs a tour for a given TSP instance using the nearest neighbor heuristic:

def nearestneighbor(size, dists):
   tour = [0]*size
   candy = [True]*size
   candy[0] = False

   for i in range(1, size):
       prev = tour[i - 1]
       tour[i] = min([(dists[prev][c], c)
           for c in range(size) if candy[c] and  c != prev])[1]
   
continue.

python, shame on you, nearest neighbor