0
我們需要找到幾個整數排序數組的交集。 這裏是例子:解決System.OutOfMemoryException
例子:
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
我寫的算法和它的作品快。
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);
但我的代碼是摔倒與OutOfMemoryException異常時,元素數是高於約10000我怎樣才能提高我的算法或什麼我可以做的來解決這個問題?
可能重複的[查找排序整數陣列的交叉點組(http://stackoverflow.com/questions/10889479/find-intersection-group-of-sorted-integer-arrays) –
@米奇小麥沒有。這個關於解決算法中具體問題的問題。鏈接問題即將找到快速算法。 – Neir0
看起來像我一樣的問題... –