sparse matrix algorithm