Skip to content

Commit

Permalink
Implementation of csr_to_sparse.m
Browse files Browse the repository at this point in the history
  • Loading branch information
David Gleich committed May 16, 2009
1 parent 9bccc10 commit f181379
Show file tree
Hide file tree
Showing 5 changed files with 102 additions and 3 deletions.
5 changes: 3 additions & 2 deletions Contents.m
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,10 @@
%
% Helper functions
% sparse_to_csr - Compressed sparse row arrays from a matrix
% csr_to_sparse - Convert back to Matlab sparse matrices
% load_gaimc_graph - Loads a sample graph from the library

% David Gleich
% David F. Gleich
% Copyright, Stanford University, 2008-2009

% History
Expand All @@ -47,4 +48,4 @@
% Future todos
% Implement weighted core nums
% More testing
% Implement all pairs shortest paths with Floyd Warshall
% Implement all pairs shortest paths with Floyd Warshall
54 changes: 54 additions & 0 deletions csr_to_sparse.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
function [nzi,nzj,nzv] = csr_to_sparse(rp,ci,ai,ncols)
% CSR_TO_SPARSE Convert from compressed row arrays to a sparse matrix
%
% A = csr_to_sparse(rp,ci,ai) returns the sparse matrix represented by the
% compressed sparse row representation rp, ci, and ai. The number of
% columns of the output sparse matrix is max(max(ci),nrows). See the call
% below.
%
% A = csr_to_sparse(rp,ci,ai,ncol) While we can infer the number of rows
% in the matrix from this expression, you may want a
% different number of
%
% [nzi,nzj,nzv] = csr_to_sparse(...) returns the arrays that feed the
% sparse call in matlab. You can use this to avoid the sparse call and
% customize the behavior.
%
% This command "inverts" the behavior of sparse_to_csr.
% Repeated entries in the matrix are summed, just like sparse does.
%
% See also SPARSE SPARSE_TO_CSR
%
% Example:
% A=sparse(6,6); A(1,1)=5; A(1,5)=2; A(2,3)=-1; A(4,1)=1; A(5,6)=1;
% [rp ci ai]=sparse_to_csr(A);
% A2 = csr_to_sparse(rp,ci,ai)

% David F. Gleich
% Copyright, Stanford University, 2008-2009

% History
% 2009-05-01: Initial version
% 2009-05-16: Documentation and example

nrows = length(rp)-1;
nzi = zeros(length(ci),1);
for i=1:nrows
for j=rp(i):rp(i+1)-1
nzi(j) = i;
end
end

if nargout<2,
if nargin>3,
nzi = sparse(nzi,ci,ai,nrows,ncols);
else
% we make the matrix square unless there are more columns
ncols = max(max(ci),nrows);
if isempty(ncols), ncols=0; end
nzi = sparse(nzi,ci,ai,nrows,ncols);
end
else
nzj = ci;
nzv = ai;
end
33 changes: 33 additions & 0 deletions test/test_csr_to_sparse.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
function test_csr_to_sparse
%% empty arguments
A = csr_to_sparse(1,[],[]);
if ~isequal(A,sparse([]))
error('gaimc:csr_to_sparse','failed to convert empty matrix');
end

A = csr_to_sparse(1,[],[],50);
if size(A,2)~=50
error('gaimc:csr_to_sparse','incorrect empty size output');
end

%% exact test
% clique to sparse
rp = 1:5:26;
ci = reshape(repmat(1:5,5,1)',25,1);
ai = ones(25,1);
A=csr_to_sparse(rp,ci,ai);
if ~isequal(A,sparse(full(ones(5))))
error('gaimc:csr_to_sparse','failed to convert clique');
end

%% 100 random trials of round trips between sparse_to_csr and csr_to_sparse
for t=1:100
A = sprand(100,80,0.01);
[rp ci ai]=sparse_to_csr(A);
A2 = csr_to_sparse(rp,ci,ai,80);
if ~isequal(A,A2)
error('gaimc:csr_to_sparse','random sparse test failed');
end
end


8 changes: 8 additions & 0 deletions test/test_examples.m
Original file line number Diff line number Diff line change
Expand Up @@ -73,3 +73,11 @@
load_gaimc_graph('cores_example'); % the graph A has three components
corenums(A)

%% sparse_to_csr
A=sparse(6,6); A(1,1)=5; A(1,5)=2; A(2,3)=-1; A(4,1)=1; A(5,6)=1;
[rp ci ai]=sparse_to_csr(A);

%% csr_to_sparse
A=sparse(6,6); A(1,1)=5; A(1,5)=2; A(2,3)=-1; A(4,1)=1; A(5,6)=1;
[rp ci ai]=sparse_to_csr(A);
A2 = csr_to_sparse(rp,ci,ai);
5 changes: 4 additions & 1 deletion test/test_main.m
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
function test_main
% TODO Check the directory
test_examples
test_sparse_to_csr
test_csr_to_sparse % should always come after sparse_to_csr
test_dfs
test_load_gaimc_graph
test_largest_component
test_examples

end
end

0 comments on commit f181379

Please sign in to comment.