TPS Sa

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 2

HEY!!!

Here it goes!!!
X=[];
Y=[];
format short e
for n=1:20
arcs=(n*(n-1))/2;
tours=(factorial(n-1))/2;
X(1,n)=arcs
Y(1,n)=tours
end
subplot(1,2,1)
plot(X,'r*-')
xlabel 'No. of nodes'
ylabel 'No. of arcs'
title 'Nodes vs. Arcs'
subplot(1,2,2)
semilogy(Y,'->')
xlabel 'No. of nodes'
ylabel 'No. of tours'
axis([0 20 0 10^20])
title 'Nodes vs. Tours'
A=[0 3 4 7; 3 0 5 1; 4 5 0 10; 7 1 10 0];
function bf4(A)
tic
tour_length(1)=A(1,2)+A(2,3)+A(3,4)+A(4,1)
tour1='1,2,3,4,1'
tour_length(2)=A(1,2)+A(2,4)+A(4,3)+A(3,1)
tour2='1,2,4,3,1'
tour_length(3)=A(1,3)+A(3,2)+A(2,4)+A(4,1)
tour3='1,3,2,4,1'
tour=[tour1;tour2;tour3]
tour=cellstr(tour)
Opt_Tour_Length=min(tour_length)
Opt_Tour=(find(tour_length == Opt_Tour_Length))
Shortest_Tour=tour(Opt_Tour)
elapsed_time = toc
%B=[0 7 36 11 9; 7 0 13 19 23; 36 13 0 8 42; 11 19 8 0 10; 9 23 42 10 0];
function bf4(B)
tic
tour_length(1)=B(1,2)+B(2,3)+B(3,4)+B(4,5)+B(5,1)
tour1='1,2,3,4,5,1'
tour_length(2)=B(1,2)+B(2,4)+B(4,3)+B(3,5)+B(5,1)
tour2='1,2,4,3,5,1'
tour_length(3)=B(1,2)+B(2,5)+B(5,4)+B(4,3)+B(3,1)
tour3='1,2,5,4,3,1'

tour_length(4)=B(1,3)+B(3,2)+B(2,5)+B(5,4)+B(4,1)
tour4='1,3,2,5,4,1'
tour_length(5)=B(1,3)+B(3,5)+B(5,4)+B(4,2)+B(2,1)
tour5='1,3,5,4,2,1'
tour_length(6)=B(1,3)+B(3,4)+B(4,2)+B(2,5)+B(5,1)
tour6='1,3,4,2,5,1'
tour_length(7)=B(1,2)+B(2,5)+B(5,3)+B(3,4)+B(4,1)
tour7='1,2,5,3,4,1'
tour_length(8)=B(1,5)+B(5,4)+B(4,2)+B(2,3)+B(3,1)
tour8='1,5,4,2,3,1'
tour_length(9)=B(1,4)+B(4,2)+B(2,5)+B(5,3)+B(3,1)
tour9='1,4,2,5,3,1'
tour_length(10)=B(1,4)+B(4,3)+B(3,2)+B(2,5)+B(5,1)
tour10='1,4,3,2,5,1'
tour_length(11)=B(1,5)+B(5,3)+B(3,2)+B(2,4)+B(4,1)
tour11='1,5,3,2,4,1'
tour_length(12)=B(1,4)+B(4,5)+B(5,3)+B(3,2)+B(2,1)
tour12='1,4,5,3,2,1'
tour=[tour1;tour2;tour3;tour4;tour5;tour6;tour7;tour8;tour9;tour10;tour11;tour12]
tour=cellstr(tour)
Opt_Tour_Length=min(tour_length)
Opt_Tour=(find(tour_length == Opt_Tour_Length))
Shortest_Tour=tour(Opt_Tour)
elapsed_time = toc

You might also like