How to store a Sparse Vector efficiently?
Last Updated :
22 Nov, 2022
A sparse vector is a vector that has a large number of zeros so it takes unwanted space to store these zeroes.
The task is to store a given sparse vector efficiently without storing the zeros.
Examples:
Input: vector = { 2, 0, 0, 0, 0,
3, 0, 4, 0, 0,
0, 1, 5, 0, 0,
0, 0, 0, 0, 0,
0, 0, 4, 0, 0,
0, 2, 0, 0, 0,
0, 0, 0, 3, 0,
0, 0, 1, 0, 0,
0, 0, 5 }
Output: {2, 3, 4, 1, 5,
4, 2, 3, 1, 5}
Approach:
To store the sparse vector efficiently, a vector of pairs can be used. The First element of pair will be the index of sparse vector element(which is non-zero) and the second element will be the actual element.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
vector<pair< int , int > >
convertSparseVector(vector< int > v)
{
vector<pair< int , int > > res;
for ( int i = 0; i < v.size(); i++) {
if (v[i] != 0) {
res.push_back(make_pair(i, v[i]));
}
}
return res;
}
void print(vector<pair< int , int > > res)
{
for ( auto x : res) {
cout << "index: " << x.first
<< " -> value: "
<< x.second << endl;
}
}
int main()
{
vector< int > v{ 2, 0, 0, 0, 0,
3, 0, 4, 0, 0,
0, 1, 5, 0, 0,
0, 0, 0, 0, 0,
0, 0, 4, 0, 0,
0, 2, 0, 0, 0,
0, 0, 0, 3, 0,
0, 0, 1, 0, 0,
0, 0, 5 };
vector<pair< int , int > > res
= convertSparseVector(v);
print(res);
return 0;
}
|
Java
import java.util.ArrayList;
class GFG{
static class Pair
{
int first, second;
public Pair( int first, int second)
{
this .first = first;
this .second = second;
}
}
static ArrayList<Pair> convertSparseVector( int [] v)
{
ArrayList<Pair> res = new ArrayList<>();
for ( int i = 0 ; i < v.length; i++)
{
if (v[i] != 0 )
{
res.add( new Pair(i, v[i]));
}
}
return res;
}
static void print(ArrayList<Pair> res)
{
for (Pair x : res)
{
System.out.printf( "index: %d -> value: %d\n" ,
x.first, x.second);
}
}
public static void main(String[] args)
{
int [] v = { 2 , 0 , 0 , 0 , 0 ,
3 , 0 , 4 , 0 , 0 ,
0 , 1 , 5 , 0 , 0 ,
0 , 0 , 0 , 0 , 0 ,
0 , 0 , 4 , 0 , 0 ,
0 , 2 , 0 , 0 , 0 ,
0 , 0 , 0 , 3 , 0 ,
0 , 0 , 1 , 0 , 0 ,
0 , 0 , 5 };
ArrayList<Pair> res = convertSparseVector(v);
print(res);
}
}
|
Python3
def convertSparseVector(v):
res = []
for i in range ( len (v)):
if (v[i] ! = 0 ):
res.append([i, v[i]])
return res
def printf(res):
for x in res:
print ( "index:" , x[ 0 ],
" -> value:" , x[ 1 ])
if __name__ = = '__main__' :
v = [ 2 , 0 , 0 , 0 , 0 ,
3 , 0 , 4 , 0 , 0 ,
0 , 1 , 5 , 0 , 0 ,
0 , 0 , 0 , 0 , 0 ,
0 , 0 , 4 , 0 , 0 ,
0 , 2 , 0 , 0 , 0 ,
0 , 0 , 0 , 3 , 0 ,
0 , 0 , 1 , 0 , 0 ,
0 , 0 , 5 ]
res = convertSparseVector(v)
printf(res)
|
C#
using System;
using System.Collections.Generic;
public class GFG
{
public class Pair
{
public int first;
public int second;
public Pair( int first, int second)
{
this .first = first;
this .second = second;
}
}
public static List<Pair> convertSparseVector( int [] v)
{
List<Pair> res = new List<Pair>();
for ( int i = 0; i < v.Length; i++)
{
if (v[i] != 0)
{
res.Add( new Pair(i, v[i]));
}
}
return res;
}
public static void print(List<Pair> res)
{
foreach (Pair x in res)
{
Console.Write( "index: {0} -> value: {1}\n" ,x.first,x.second);
}
}
public static void Main(String[] args)
{
int [] v = {2, 0, 0, 0, 0, 3, 0, 4, 0, 0, 0, 1, 5, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 3, 0, 0, 0, 1, 0, 0, 0, 0, 5};
List<Pair> res = GFG.convertSparseVector(v);
GFG.print(res);
}
}
|
Javascript
<script>
function convertSparseVector(v) {
let res = [];
for (let i = 0; i < v.length; i++) {
if (v[i] != 0) {
res.push([i, v[i]]);
}
}
return res;
}
function print(res) {
for (let x of res) {
document.write( "index: " + x[0] + " -> value: "
+ x[1] + "<br>" );
}
}
let v = [2, 0, 0, 0, 0,
3, 0, 4, 0, 0,
0, 1, 5, 0, 0,
0, 0, 0, 0, 0,
0, 0, 4, 0, 0,
0, 2, 0, 0, 0,
0, 0, 0, 3, 0,
0, 0, 1, 0, 0,
0, 0, 5];
let res = convertSparseVector(v);
print(res);
</script>
|
Output:
index: 0 -> value: 2
index: 5 -> value: 3
index: 7 -> value: 4
index: 11 -> value: 1
index: 12 -> value: 5
index: 22 -> value: 4
index: 26 -> value: 2
index: 33 -> value: 3
index: 37 -> value: 1
index: 42 -> value: 5
Share your thoughts in the comments
Please Login to comment...