Sparse Matrix and its representations | Set 2 (Using List of Lists and Dictionary of keys)

In this post other two methods of sparse matrix representation are discussed.

1. List of Lists
2. Dictionary

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. Recommended: Please try your approach on {IDE} first, before moving on to the solution.

Output:

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

Recommended: Please try your approach on {IDE} first, before moving on to the solution.

 // C++ program for Sparse Matrix Representation // using Dictionary #include using namespace std; #define R 4 #define C 5    // Driver of the program int main() {     // Assume 4x5 sparse matrix     int Sparse_Matrix[R][C] =     {         {0 , 0 , 3 , 0 , 4 },         {0 , 0 , 5 , 7 , 0 },         {0 , 0 , 0 , 0 , 0 },         {0 , 2 , 6 , 0 , 0 }     };        /* Declaration of map where first field(pair of        row and column) represent key and second        field represent value */     map< pair, int > new_matrix;        for (int i = 0; i < R; i++)         for (int j = 0; j < C; j++)             if (Sparse_Matrix[i][j] != 0)                 new_matrix[make_pair(i+1,j+1)] =                                 Sparse_Matrix[i][j] ;        int c = 0;        // Iteration over map     for (auto i = new_matrix.begin(); i != new_matrix.end(); i++ )     {         if (c != i->first.first)         {             cout << "row = " << i->first.first << endl ;             c = i->first.first;         }         cout << "column = " << i->first.second <<" ";         cout << "value = " << i->second << endl;     }        return 0; }

Output:

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

References:
Wikipedia
This article is contributed by Akash Gupta. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

My Personal Notes arrow_drop_up

Article Tags :
Practice Tags :

Be the First to upvote.

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.