Skip to content

Instantly share code, notes, and snippets.

@purmac
Last active December 16, 2015 08:49
Show Gist options
  • Select an option

  • Save purmac/5408503 to your computer and use it in GitHub Desktop.

Select an option

Save purmac/5408503 to your computer and use it in GitHub Desktop.
Brute Force TSP for 9 nodes
l = 'abcdefgho';
P = perms([1 2 3 4 5 6 7 8 9]);
P = P(P(:,1)==9,:);
index_x = 1:9;
index_y = mod(index_x,9)+1;
cost_table = zeros(size(P,1),1);
sym_cost = TSP;
for i=1:size(P,1)
cost = 0;
for j=1:9
cost = cost + sym_cost(P(i,index_x(j)),P(i,index_y(j)));
end
cost_table(i) = cost;
end
optimal = min(cost_table);
optimal_route = find(cost_table == optimal);
l(P(optimal_route,:))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment