function sparseplot(name,flag)
%sparseplot(name,flag)
%  sparseplot("mytest.1")  plots the matrix structure in the files mytest.1.* 
%       using the result of the Cuthill-McKee algorithm
%  sparseplot("mytest.1",1)  plots the matrix structure in the files mytest.1.*
%       using the the original node numbering
%
%  the file mytest.1.sparse can be generated by CuthillMcKee with the option -g



[fid,msg]=fopen(strcat(name,".sparse"),"r");
Nnodes=fscanf(fid,"%d",1); % number of nodes
Nedges=fscanf(fid,"%d",1); % number of edges

map=zeros(Nnodes,1);
if (nargin>1)
   map=linspace(1,Nnodes,Nnodes);
else
   map=zeros(1,Nnodes);
   [fmap,msg]=fopen(strcat(name,".map"),"r");
   tmp=fscanf(fmap,"%d",1);
   for i=1:Nnodes
      tmp=fscanf(fmap,"%d",1); 
      map(i)=fscanf(fmap,"%d",1);
   endfor;
   fclose(fmap);
endif


   n=Nnodes+2*Nedges;
x=zeros(n,1);
y=x;
for i=1:Nnodes
    x(i)=i;y(i)=i;
endfor	

edge=zeros(1,2);
for k=1:Nedges
  edge=fscanf(fid,"%i",[1,2]);
  x(k+Nnodes) = map(edge(1));
  y(k+Nnodes) = map(edge(2));
endfor
fclose(fid);

x(Nnodes+Nedges+1:Nnodes+2*Nedges)=y(Nnodes+1:Nnodes+Nedges);
y(Nnodes+Nedges+1:Nnodes+2*Nedges)=x(Nnodes+1:Nnodes+Nedges);

gset nokey
eval(sprintf('gset yrange [1:%d] reverse',Nnodes))
eval(sprintf('gset xrange [1:%d] noreverse',Nnodes))
if (Nnodes<1000)
   plot(x,y,"+");
else
   plot(x,y,".");
endif



endfunction




