%
%  When direct method are used for Ax=b, it is crucial to find 
%  a proper permutation of the unknowns to minimize the fill-in
%  in Cholesky or LU factorizations.
%
function sparse_perm()
%
% Load symmetric pos. def. sparse test matrix from file:
%
load('sparse1.mat','A');
fprintf('Sparse %d x %d matrix loaded...\n',size(A));
%
% Reverse Cuthill-McKee permutation:
%
pc = symrcm(A);
%
% Approximate minimum degree permutation:
%
pa = amd(A);
%
% Compute cholesky factor using different permutations:
%
tic; L  = chol(A,'lower'); toc   % the original numbering!
tic; Lc = chol(A(pc,pc),'lower'); toc
tic; La = chol(A(pa,pa),'lower'); toc
%
% Now, visualize the amount of fill-in using different permutations:
%
subplot(3,2,1);  spy(tril(A)); title('Original');
subplot(3,2,2); % spy(L);  % slow to visualize L
clear L
subplot(3,2,3); spy(tril(A(pc,pc))); title('RCM');
subplot(3,2,4); spy(Lc);
clear Lc
subplot(3,2,5); spy(tril(A(pa,pa))); title('AMD');
subplot(3,2,6); spy(La);
clear La
clear A

