Challenge

Project - Traveling salesman problem

The travelling of salesman from one city to another with in the shortest distance is as follows with the graph mentioned below,

initially total city is taken as 20 as random in numbers and the shortest to longest is calculated randomly, so initial point is takenfrom orgin and the random points are calculated the shortest will map accordingly and the number of city visited will be calculated from that the maximum iteration is 20, which will be calculated in loop untill 20 city randomly choosed, once the 20 city is mapped on the graph it will close the loop.

city = 20;
x=randi(100,city,1);
y=randi(100,city,1);
plot(x,y,'bo','markersize',10);
title('travelling sales man problem');
grid on;
distances=sqrt(x.^2+y.^2);
[mindistance,indexofmin]=min(distances);
nearestx=x(indexofmin);
nearesty=y(indexofmin);
hold on;
plot(nearestx,nearesty,'r*','markersize',8,'linewidth',2);
line([0,nearestx],[0,nearesty],'linewidth',2,'color','b');
cityvisited = false(1,city);
counter = 2;
maxiterations = city;
cityvisited(indexofmin)=true
fprintf('(%d%d)-->',nearestx,nearesty); while sum(cityvisited) <=(city -1)&&(counter<=maxiterations)
previousx = x(indexofmin(counter -1));
previousy = y(indexofmin(counter -1));
x(cityvisited) = inf;
distances = sqrt((x - previousx).^2+(y - previousy).^2);
[mindistance,indexofmin(counter)]=min(distances);
nearestx = x(indexofmin(counter));
nearesty = y(indexofmin(counter));
line([previousx,nearestx],[previousy,nearesty],'linewidth',2,'color','b');
cityvisited(indexofmin(counter)) = true;
counter = counter+1;
fprintf('(%d%d)-->',nearestx,nearesty);
end

Your portfolio, next

Ship work like this
in your own course.

Talk to a career counsellor

Free 20-minute call. We'll map the right course to where you want to work.

By submitting you agree to our Terms and Privacy Policy.

Talk to a career counsellor

Free 20-minute call. We'll map the right course to where you want to work.

By submitting you agree to our Terms and Privacy Policy.