使用PRAM算法合并包含重复元素的数组

时间:2013-12-12 02:37:10

标签: algorithm parallel-processing

我正在学习PRAM算法。我坚持一个问题。 “存在一种算法,给定任意两个整数的m元素数组,其中每个整数属于集合{1,2,3 ... m},并且允许重复元素,将两个数组合并在O中( 1)使用带有m个通用CRCW处理器的PRAM的时间“

例如,当m = 4时,它可以合并阵列< 1,2,3,3>和< 1,3,3,4>。在O(1)时间内使用4个常见的CRCW处理器

请回复, 感谢

2 个答案:

答案 0 :(得分:-1)

只要你有M个处理器和M长度数组,我就没有看到任何提及应该对最终数组进行排序:

每个处理器可以从第一个数组获取1个值,从第二个数组获取1个值并将它们放入最终数组,此操作将为O(1)

尝试查看这个伪代码:

int array1[M] = {1, 2, 3, 4};
int array2[M] = {1, 3, 3, 4};
int output[M * 2] = {};

parallel for (i=0; i<M; i++) // each iteration of this loop runs on its own processor, so all iterations run at the same time and will finish in O(1) time simultaneously
{
    output[i * 2] = array1[i];
    output[i * 2 + 1] = array2[i];
}

所以,循环中的操作显然是O(1),并且在一般编程中我们可以说最终复杂度将是O(M),因为循环,但对于M个处理器它将只是O(1)< / p>

答案 1 :(得分:-1)

所以我们有M个处理器并发读,并发写和2个数组:a,b。 我们可以这样做(通过计算每个数字的出现来进行排序):

//index     0  1  2  3  4  5
int a[M] = {1, 1, 1, 1, 2, 6};      
int b[M] = {1, 3, 3, 4, 4, 8};             
int o[M * 2] = {}; 
int tem1[M * 2] = {};  
int tem2[M * 2] = {};                     

parallel for (i=0; i<M; i++) 
// each iteration of this loop runs on its own processor, so all iterations run at the same 
// time and will finish in O(1) time simultaneously
// at reading/writing from/in the same location, processor i has higher priority than processor i+1, operations are queued
{
 // step 1 (depending on how much the values repeat, there will be processors that wait for others with higher priority,  // before performing their operations)
 tem1[a[i]]++;    
 tem2[b[i]]++;
 //   index: 0 1 2 3 4 5 6 7 8 9 10 11 12
 // -> tem1: 0 4 1 0 0 0 1 0 0 0 0  0  0
 // -> tem2: 0 1 0 2 2 0 0 0 1 0 0  0  0


 // step 2
 // again, some processors might wait until they perform their operations, because they access the same memory location
 o[tem1[a[i]+tem2[a[i]]-1] = a[i];
 tem1[a[i]]--;
 o[tem1[b[i]]+tem2[b[i]]-1] = b[i];
 tem2[b[i]]--;
 //   index: 0 1 2 3 4 5 6 7 8 9 10 11 12
 // ->    o: 1 1 1 1 1 2 3 3 4 4 6 8

}

- &GT;没有循环,每个处理器的操作数量恒定 - &gt; O(1)