function [RM1]=bellman(W) 
%W is weight NXN matrix of constraint graph without N+1 node
%For example, Fig4.10 on page 106 has the following W:
%W=[0 1  0  0
%   0 0 -1 -1
%   0 1  0  1
%   1 2  2  0]
%Note: it does not node 5 as it is added in the program by:
%W=[ W      temp2
%    temp1  inf];
%solution is: RM1=[-1 0 -1 -1 0]

N=size(W,1);

U=N+1;
R1(U)=0;

temp1=zeros(1,N);
temp2=ones(N,1)*inf;
W=[ W      temp2
    temp1  inf];
N=N+1;

for kk=1:1:N
    if kk~=U, R1(kk)=W(U,kk);
    end;
end;
RM=R1;

for kk=1:1:N-1
    for V=1:1:N
        RM1(V)=RM(V);
        for jj=1:1:N
            if (RM1(V)>RM(jj)+W(jj,V)) && (RM(jj)<inf) && (W(jj,V)<inf)
                RM1(V)=RM(jj)+W(jj,V);
            end;
        end;
    end;
    RM=RM1;
end;

for V=1:1:N
    for jj=1:1:N
        if (RM1(V)>RM1(jj)+W(jj,V)) && (RM1(jj)<inf) && (W(jj,V)<inf)
            disp(' ***** No solution exists from Bellman *****')
        return;
        end;
    end;
end;

return



     