
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
C++ Program to Implement Sparse Array
What is Sparse Array?
Sparse array is used to determines the dimension of an array in which most of the elements are zero. It can be used for matrix calculation.
Characteristics of Sparse Array
Following are list of points that defines the characteristics of a sparse array:
- Most of the elements are zero or null values which means it is unused.
- Only non-zero elements and their indexes are stored.
- It is used to save the memory while comparing to normal array calculation.
Example
The Sparse array of 2*2 [2 0] [0 0] The sparse array of 3*3 [0 1 3] [0 0 9] [1 7 0]
There are three different ways to implement sparse array in C++ programs:
- Using for loop
- Using linked list
- Using map
Sparse Array Implementation Using for loop
Here, we declare array of two dimensional to store the zero and non-zero elements. Then we contruct the corresponding sparse matrix array, where each row represent the row index, column index and value of a non-zero element. All the row and columns are iterated using their respective iterators. Finally, display the sparse array by the given indexes.
Example
Following is the basic example of loop to demonstrates sparse array in C++.
#include<iostream> using namespace std; int main() { int matrix[2][2] = { {0, 5}, {3, 0} }; // maximum possible non-zero elements int sparse[4][3]; // counter of sparse array int k = 0; // // loop through matrix for (int i = 0; i < 2; i++) { for (int j = 0; j < 2; j++) { if (matrix[i][j] != 0) { sparse[k][0] = i; sparse[k][1] = j; sparse[k][2] = matrix[i][j]; k++; } } } // print sparse array cout << "Sparse Matrix (row, col, value):\n"; for (int i = 0; i < k; i++) { cout << sparse[i][0] << " " << sparse[i][1] << endl; } return 0; }
The above code produces the following result:
Sparse Matrix (row, col, value): 0 1 1 0
Sparse Array Implementation Using Linked List
To implement a sparse array in C++, we first structurize the node for each non-zero element. Then create one function to store each of these elements by allocating memory dynamically and assigning their row, column, and value. Next, traverse the matrix to create a triplet format (row, column, and value) that print the matrix result of 2*3. So, the main benefits of using linked list is to avoid memory leaks.
Example
In this example, we use linked list data structure to represent sparse array.
#include<iostream> using namespace std; // Node structure for each non-zero element struct Node { int row; int col; int value; Node* next; }; // Function to create a new node Node* createNode(int row, int col, int value) { Node* newNode = new Node; newNode->row = row; newNode->col = col; newNode->value = value; newNode->next = NULL; return newNode; } int main() { int matrix[2][2] = { {0, 5}, {3, 0} }; Node* head = NULL; Node* tail = NULL; // Traverse matrix and create linked list for (int i = 0; i < 2; i++) { for (int j = 0; j < 2; j++) { if (matrix[i][j] != 0) { Node* newNode = createNode(i, j, matrix[i][j]); if (head == NULL) { head = newNode; tail = newNode; } else { tail->next = newNode; tail = newNode; } } } } // Display sparse matrix cout << "Sparse Matrix using Linked List (row col value):\n"; Node* temp = head; while (temp != NULL) { cout << temp->row << " " << temp->col << " " << temp->value << endl; temp = temp->next; } // Free memory while (head != NULL) { Node* temp = head; head = head->next; delete temp; } return 0; }
The above code produces the following result:
Sparse Matrix using Linked List (row col value): 0 1 5 1 0 3
Sparse Array Implementation Using map Data Structure
The map of C++ is known for dictionaries that store the elements in the form of key-value pairs. It is used to retrieve data based on unique keys. Here, we create a map that stores the indexing position of row and column to return the array result in the form of a sparse matrix.
Example
Below the program demonstrated based on the above statement.
#include<iostream> #include<map> using namespace std; int main() { int matrix[3][3] = { {0, 1, 4}, {0, 0, 0}, {1, 2, 4} }; // Create a map to store non-zero elements map<pair<int, int>, int> sparse; // Traverse the matrix and insert non-zero values into map for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { if (matrix[i][j] != 0) { sparse[{i, j}] = matrix[i][j]; } } } // Print the sparse representation cout << "Sparse Matrix using std::map:\n"; cout << "(row, col) = value\n"; for (auto entry : sparse) { cout << "(" << entry.first.first << ", " << entry.first.second << ") = " << entry.second << endl; } return 0; }
The above code produces the following result:
Sparse Matrix using std::map: (row, col) = value (0, 1) = 1 (0, 2) = 4 (2, 0) = 1 (2, 1) = 2 (2, 2) = 4