Sparse Matrix and its representations | Set 2 (Using List of Lists and Dictionary of keys)
Prerequisite : Sparse Matrix and its representations Set 1 (Using Arrays and Linked Lists)
In this post other two methods of sparse matrix representation are discussed.
- List of Lists
List of Lists (LIL)
One of the possible representation of sparse matrix is List of Lists (LIL). Where one list is used to represent the rows and each row contains the list of triples: Column index, Value(non – zero element) and address field, for non – zero elements. For the best performance both lists should be stored in order of ascending keys.
row=1 column=3 value=3 column=5 value=4 row=2 column=3 value=5 column=4 value=7 row=4 column=2 value=2 column=3 value=6
Dictionary of Keys
An alternative representation of sparse matrix is Dictionary. For the key field of the dictionary, pair of row and column index is used that maps with the non – zero element of the matrix. This method saves space but sequential access of items is costly.
In C++, dictionary is defined as map class of STL(Standard Template Library). To know more about map click the link below:
Basics of map
row = 1 column = 3 value = 3 column = 5 value = 4 row = 2 column = 3 value = 5 column = 4 value = 7 row = 4 column = 2 value = 2 column = 3 value = 6
This article is contributed by Akash Gupta. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.