Jagged Array?
Can we use a jagged array to store a sparse matrix?
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
VulpesPosted Jul 29, 2012, 11:40 AM
For example:
0 0 1 0 0
0 0 0 1 0
1 0 0 0 0
1 0 1 0 1
0 1 0 0 0
could be stored in jagged form as:
0 0 1
0 0 0 1
1
1 0 1 0 1
0 1
However, a better idea might be to store the non-zero elements in a Dictionary which maps a (row, column) pair to a value. That way no zero elements would be stored at all.
In the above example (.NET 4.0 or later), we would have:
var sparse = new Dictionary
dict.Add(Tuple.Create(0,2), 1);
dictAdd(Tuple.Create(1,3), 1);
// and so on