clear all
clc
c=[2 7 4; 3 3 1; 5 5 4; 1 6 2] %cost matrix
d=[7 9 18] %demand mtrx
s=[5 8 7 14] %supply mtrx
if sum(s)==sum(d)
    fprintf('given transportation prob is balanced \\n')
else
    fprintf('transportation prob is unbalanced \\n')
    if sum(s)<sum(d)
        c(end+1,:)=zeros(1,length(d))%dummy row
        s(end+1)=sum(d)-sum(s)
    elseif sum(s)>sum(d)
            c(:,end+1)=zeros(length(s),1)%dummy col
            d(end+1)=sum(s)-sum(d)
    end
end
org_c=c
x=zeros(size(c))
for i=1:size(c,1) %loop on rows in c
    for j=1:size(c,2) %loop on cols in c
mc=min(c(:))
[mcr, mcc]=find(mc==c)

x11=min(s(mcr),d(mcc))
[allocval ind]=max(x11)
p=mcr(ind)
q=mcc(ind)
x(p,q)=allocval

%now updating the values in s nd d
s(p)=s(p)-allocval
d(q)=d(q)-allocval

if s(p)==0
    c(p,:)=inf
end

if d(q)==0
    c(:,q)=inf
end    

    end
end

tc=sum(sum(org_c.*x))
fprintf('total cost of transportation is %f’,tc)