We need to find intersection of several integer sorted arrays. Here is example:
Example:
Input:
1,3,7,8
2,3,8,10
3,10,11,12,13,14
minSupport = 1
Output:
1 and 2: 2, 8
1 and 3: 3
2 and 3: 3, 10
I wrote algorithm and it works fast.
var minSupport = 2;
var elementsCount = 10000;
var random = new Random(123);
// Numbers of each array are unique
var sortedArrays = Enumerable.Range(0,elementsCount)
.Select(x => Enumerable.Range(0,30).Select(t => random.Next(1000)).Distinct()
.ToList()).ToList();
var result = new List<int[]>();
var resultIntersection = new List<List<int>>();
foreach (var array in sortedArrays)
{
array.Sort();
}
var sw = Stopwatch.StartNew();
//****MAIN PART*****//
// This number(max value which array can contains) is known.
// Ofcourse we can use dictionary if donnt know maxValue
var maxValue = 1000;
var reverseIndexDict = new List<int>[maxValue];
for (int i = 0; i < maxValue; i++)
{
reverseIndexDict[i] = new List<int>();
}
for (int i = 0; i < sortedArrays.Count; i++)
{
for (int j = 0; j < sortedArrays[i].Count; j++)
{
reverseIndexDict[sortedArrays[i][j]].Add(i);
}
}
var resultMatrix = new List<int>[sortedArrays.Count,sortedArrays.Count];
for (int i = 0; i < sortedArrays.Count; i++)
{
for (int j = 0; j < sortedArrays[i].Count; j++)
{
var sortedArraysij = sortedArrays[i][j];
for (int k = 0; k < reverseIndexDict[sortedArraysij].Count; k++)
{
if(resultMatrix[i,reverseIndexDict[sortedArraysij][k]]==null) resultMatrix[i,reverseIndexDict[sortedArraysij][k]] = new List<int>();
resultMatrix[i,reverseIndexDict[sortedArraysij][k]].Add(sortedArraysij);
}
}
}
//*****************//
sw.Stop();
Console.WriteLine(sw.Elapsed);
But my code is fall down with outofmemoryException when elements count is more then about 10000. How can i improve my algorithm or what i can to do to resolve this issue?