Hi, I wrote a simple algorithm that minimize the space required to represent Sparse Matrix, I think you may find it useful :) .
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace sparse_matrix
{
class Program
{
static void Main(string[] args)
{
/*
* In this algorithm I store all the Sparse Matrix into single diminsion matrix of type byte, each row
* in this matrix has a number that represent the position of the "1" in the original Sparse matrix.
* By this way, I save a lot of space... and store the same matrix with the minimum space(I could reach)
* Since I treat the digits as bits, and stored them into the byte, since the biggest number I will reach
* will not exceed 255 bit.
* I read about the byte data type from here:(http://msdn.microsoft.com/en-us/library/5bdb6693.aspx)
* In this program, I run it on a (3 * 3) matrix, however it can work the same for (255 * 10^6)
*/
int matrixRows = 3;
int matrixColumns = 3;
int[,] matrix;
matrix = new int[matrixRows, matrixColumns];
matrix[0, 0] = 0;
matrix[1, 0] = 0;
matrix[2, 0] = 1;
matrix[0, 1] = 1;
matrix[1, 1] = 0;
matrix[2, 1] = 0;
matrix[0, 2] = 0;
matrix[1, 2] = 1;
matrix[2, 2] = 0;
byte []x = new byte [matrixColumns];
Console.WriteLine("The Original Sparse Matrix:\n");
for (int i = 0; i != matrixColumns; i++)
{
for (int j = 0; j != matrixRows; j++)
{
if (matrix[j, i] == 1)
{
x[i] = (byte)j;
}
Console.Write(matrix[i, j] + " ");
}
Console.WriteLine();
Console.WriteLine();
}
Console.WriteLine();
Console.WriteLine();
Console.WriteLine("The new representation for the Sparse matrix:\n");
for (int k = 0; k != matrixColumns; k++)
{
Console.WriteLine(x[k] + "\n");
}
}
}
}
I will be happy to receive any feedback from you.