1 function [bestRoute, bestLength] = ltspsEnumerativeSolver (distanceMatrix)
2 matrixDims = size(distanceMatrix);
3 numCities = matrixDims(1,1);
6 % Shortest route until now and its length
7 bestRoute = zeros(1,numCities+1);
10 permutations = perms(1:numCities);
13 % * n! rows (the permutations, minus the one for the first vertex)
14 % * n cols (the number of cities, minus the one for the first vertex)
16 rowLimit = factorial(numCities);
20 % Get the permutation (= the route minus the first and last vertex)
21 thisRoute = permutations(i,1:colLimit);
23 currentCity = thisRoute(1);
26 % While there are still cities to travel to...
28 % Find the new distance and travel it.
29 newDistance = distanceMatrix(currentCity,thisRoute(1,j));
30 currentDistance = currentDistance + newDistance;
32 % Place yourself in the next city.
33 currentCity = thisRoute(1,j);
36 if currentDistance < bestLength
37 bestLength = currentDistance;
38 bestRoute = [thisRoute thisRoute(1)];