1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
| A %une matrice carrée d'ordre n(aij>0)
V %vecteur(1,n)
r=0;
r1=2;
while r<=r1
for j=1:n
rows = V(1,1:n-1);
cols = V(1,2:n);
B=[rows' cols'];
B(B(:,1)==j|B(:,2)==j,:)=[];
B1=[B(:,1) ones(size(B,1),1)*j B(:,end) ];
rows = B1(:,1:2);
cols = B1(:,2:3);
C= sum( A( sub2ind(size(A),rows,cols) ) ,2);
rows = B(:,1);
cols = B(:,2);
C1=A( sub2ind(size(A),rows,cols) );
S=C-C1;
[b,~,m]=unique(S);
find=V==j;
V0=1:n;
idx=V0(find);
if (idx~=1) && (idx~=n)
N0=A(V(1,idx-1),j)+A(j,V(1,idx+1))-A(V(1,idx-1), V(1,idx+1));
else N0=A(V(1,idx),j);
end
N1=A(V(1,1),j);
N2=A(V(1,end),j);
N=max([N0 N1 N2]);
MP1=V(1,:);
MP1(MP1==j)=[];
if b( length(b))>N
x=1;
idx=m>=length(b);
b1=1:length(S);
B1=B(b1(idx),:);
for d=1:size(B1,1)
find=MP1==B1(d,1);
idx=V0(find);
MPP(end+1,:)=[MP1(1,1:idx) j MP1(1,idx+1:end)];
end
else
if N1>N0
x=1;
MPP(end+1,:)=[ j MP1(1,1:end)];
else
if N2>N0
x=1;
MPP(end+1,:)=[ MP1(1,1:end) j];
end
end
end
end
MPP=unique(MPP,'rows');
rows = MPP(:,1:n-1);
cols = MPP(:,2:n);
C= sum( A( sub2ind(size(A),rows,cols) ) ,2) ;
[~,IX]=sort(C,'descend');
V=MPP(IX(1),:);
r=r+1;
r1=r1+1;
end |
Partager