Codes For North West Corner Cell Method, Least Corner Cell Method and Vogel'S Approximation Method
Codes For North West Corner Cell Method, Least Corner Cell Method and Vogel'S Approximation Method
Codes For North West Corner Cell Method, Least Corner Cell Method and Vogel'S Approximation Method
CODE:
%% Transportation Problem using least cost cell method
close all
clear all
clc
A = input('Enter the cost matrix:')
supply = input('Enter vector of supplies')
demand = input('Enter vector of demands')
%Initialization of cost
y = 0;
while (~isempty(supply) && ~isempty(demand))
M = min(A);
N = min(M);
i = 0;
j = 0;
[i, j] =find(A==N);
X = min(supply(i(1)),demand(j(1)));
y = y+X*N;
if(X==supply(i(1)))
A(i(1),:) = [];
supply(i(1))=[];
demand(j(1))= demand(j(1))-X;
end
if(X==demand(j(1)))
A(:,j(1)) = [];
demand(j(1))=[];
supply(i(1))=supply(i(1))-X;
end
end
disp('THe least cost of given input matrix is:')
disp(y)
OUTPUT:
Enter the cost matrix:
[4 5 1;3 4 3;6 2 8]
A =
4 5 1
3 4 3
6 2 8
supply =
40 60 70
demand =
70 40 60
for i=1:m
min1=inf;
for j=1:n
if c1(i,j)<min1
min1=c1(i,j);
jmin(i)=j;% position of min1 on each row
end
end
minrow1(i)=min1;
end;
for i=1:m
min2=inf;
for j=1:n
if j~=jmin(i)
if c1(i,j)<=min2
min2=c1(i,j);
end
end
end
minrow2(i)=min2;
end
%% Column Difference
mincol1=zeros(1,n);
mincol2=zeros(1,n);
imin=zeros(n,1);
for j=1:n
minR1=inf;
for i=1:m
if c1(i,j)<minR1
minR1=c1(i,j);
imin(j)=i;
end
end
mincol1(j)=minR1;
end
for j=1:n
minR2=inf;
for i=1:m
if i~=imin(j)
if c1(i,j)<=minR2
minR2=c1(i,j);
end
end
end
mincol2(j)=minR2;
end
%% Difference
diffrow=zeros(m,1);
diffcol=zeros(1,n);
for i=1:m
diffrow(i)=minrow2(i)-minrow1(i);
end
for j=1:n
diffcol(j)=mincol2(j)-mincol1(j);
end
%% The greatest difference
R=0;
Row=zeros(m,1);
for i=1:m
if diffrow(i)>=R
R=diffrow(i);
iminrow=i;
end
end
Row(iminrow)=R;
S=0;
Col=zeros(1,n);
for j=1:n
if diffcol(j)>=S
S=diffcol(j);
jmincol=j;
end
end
Col(jmincol)=S;
great=zeros(1,n);
for j=1:n
if S>=R
great(jmincol)=Col(jmincol);
Colline=1;
else
great(iminrow)=Row(iminrow);
Colline=0;
end
end
%% Search the entry cell
if Colline==1
j=jmincol;
R1=inf;
for i=1:m
if c1(i,jmincol)<=R1
R1=c1(i,jmincol);
igreat=i;
end
end
if s1(igreat)>d1(jmincol)
x(igreat,jmincol)=d1(jmincol);
s1(igreat)=s1(igreat)-d1(jmincol);
d1(jmincol)=0;
eliminaterow=0;
elseif s1(igreat)<d1(jmincol)
x(igreat,jmincol)=s1(igreat);
d1(jmincol)=d1(jmincol)-s1(igreat);
s1(igreat)=0;
eliminaterow=1;
elseif s1(igreat)==d1(jmincol)
x(igreat,jmincol)=s1(igreat);
d1(jmincol)=0;
s1(igreat)=0;
eliminaterow=2;
end
% Eliminate a column or a row
if eliminaterow==0
for i=1:m
c1(i,jmincol)=inf;
end
elseif eliminaterow==1
for j=1:n
c1(igreat,j)=inf;
end
elseif eliminaterow==2
for i=1:m
c1(i,jmincol)=inf;
end
for j=1:n
c1(igreat,j)=inf;
end
end
else % Colline=0;
i=iminrow;
R2=inf;
for j=1:n
if c1(iminrow,j)<R2
R2=c1(iminrow,j);
jgreat=j;
end
end
if s1(iminrow)>d1(jgreat)
x(iminrow,jgreat)=d1(jgreat);
s1(iminrow)=s1(iminrow)-d1(jgreat);
d1(jgreat)=0;
eliminaterow=0;
elseif s1(iminrow)<d1(jgreat)
x(iminrow,jgreat)=s1(iminrow);
d1(jgreat)=d1(jgreat)-s1(iminrow);
s1(iminrow)=0;
eliminaterow=1;
elseif s1(iminrow)==d1(jgreat)
x(iminrow,jgreat)=s1(iminrow);
d1(jgreat)=0;
s1(iminrow)=0;
eliminaterow=2;
end
% Eliminate a column or a row
if eliminaterow==0
for i=1:m
c1(i,jgreat)=inf;
end
elseif eliminaterow==1
for j=1:n
c1(iminrow,j)=inf ;
end
elseif eliminaterow==2
for i=1:m
c1(i,jgreat)=inf
end
for j=1:n
c1(iminrow,j)=inf;
end
end
end
%% Calculate the objective function
z=0;
for j=1:n
for i=1:m
if x(i,j)>0
z=z+c(i,j)*x(i,j);
end
end
end
sums=0;
for i=1:m
sums=sums+s1(i);
end
sumd=0;
for j=1:n
sumd=sumd+d1(j);
end
if (sums & sumd) ==0
return
end
end
%% The degeneracy
countx=0;
for i=1:m
for j=1:n
if x(i,j)>0
countx=countx+1;
x1(i,j)=x(i,j);
x2(i,j)=x(i,j);
end
end
end
if countx>=numbasic
disp('Total cost of non-degeneracy VAM')
disp(z)
disp('Occupied Matrix of VAM')
disp(x)
else
disp('Total cost of degeneracy VAM')
disp(z)
disp('Occupied Matrix of VAM')
disp(x)
end
end
OUTPUT: