Advertisement
MOOOSE21

lcm

May 7th, 2025
175
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MatLab 1.14 KB | None | 0 0
  1. C= [11, 13, 17, 14; 16, 18, 14, 10; 21, 24, 13, 10];
  2. S= [8 5 9];
  3. D= [8 7 5 4];
  4. [m ,n]= size(C);
  5.  
  6. if(sum(S)==sum(D))
  7.     disp('Balanced');
  8. else if(sum(S)<sum(D))
  9.         C(end+1,:)=zeros(1,n);
  10.         S(end+1)= sum(D)- sum(S);
  11.         s=sum(D);
  12.  
  13. else
  14.      C(:,end+1)=zeros(m,1);
  15.      D(end+1)= sum(S)- sum(D);
  16.      s=sum(C);
  17. end
  18. end
  19. table =C;
  20. table= [table , S(:)];
  21. mD = [D, s];  
  22. table= [table ; mD];
  23. var ={'D1','D2','D3', 'D4', 'Supply' };
  24. disp(array2table(table, 'VariableNames', var));
  25. X=C;
  26. Ci=C;
  27. while any(S~=0)|| any(D~=0)
  28.     mini = min(C(:));
  29.     [r, c]= find(C==mini);
  30.     y = min(S(r), D(c));
  31.     [alloc, ind]= max(y);
  32.     rr= r(ind);
  33.     cc= c(ind);
  34.     X(rr, cc)= alloc;
  35.     S(rr)= S(rr)- alloc;
  36.     D(cc)= D(cc)-alloc;
  37.     C(rr, cc)= inf;
  38.  
  39.  
  40. end
  41. table =X;
  42. table= [table , S(:)];
  43. mD = [D, s];  
  44. table= [table ; mD];
  45. var ={'D1','D2','D3', 'D4', 'Supply' };
  46. disp(array2table(table, 'VariableNames', var));
  47.  
  48. Cost = Ci.*X;
  49. [m, n]= size(X);
  50. if nnz(X)==m+n-1
  51.     disp('Non-degenerate initial BFS');
  52. else
  53.     disp('degenerate initial BFS');
  54. end
  55. final = sum(Cost(:));
  56. disp("Total Cost is ");
  57. disp(final);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement