Jagged Array?
Can we use a jagged array to store a sparse matrix?
1 Answer
Know the answer? Post it. Somebody with the same question will find it here.
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend4days leftRegister
VulpesRank #55 · 29.2K reputationPosted Jul 29, 2012, 7:40 AM EDT
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