Distributed Median problem

Alice has an array , and Bob has an array . All elements in A and B are distinct. Alice and Bob are interested in finding the median element of their combined arrays. That is, they want to determine which element m satisfies the following property:

This equation says that there are a total of n elements in both A and B that are less than or equal to m. Note that m might be drawn from either A or B.

Because Alice and Bob live in different cities, they have limited communication bandwidth. They can send each other one integer at a time, where the value either falls within {0,…,n} or is drawn from the original A or B arrays. Each numeric transmission counts as a “communication” between Alice and Bob. One goal of this problem is to minimize the number of communications needed to compute the median.

              Compute the combined median of A and B. Your algorithm should run in time and use  communications. Write your algorithm in pseudocode first. Then write the code to accomplish the computing. (Hint: you will do sorting first, it is required to use heapsort).

               

Input:  is {61, 17, 30, 50, 83, 9}

 is {2, 27, 198, 25, 4, 18}

Output: You can scan the computing procedure such as: Alice sends Bob ***;

              Finally give combined median m.

[code]

#include "iostream""

using namespace std;

void swap(int &i,int &j)
{
 int tmp = i;
     i = j;
        j = tmp;
}

void MaxHeapify(int *A,int i,int heapsize)
{
 int l = 2*i;
 int r = 2*i+1;
 int largest;
 if(l <= heapsize && A[l] > A[i])
  largest = l;
 else largest = i;
 if(r <= heapsize && A[r] > A[largest])
  largest = r;
 if(largest != i)
 {
  swap(A[i],A[largest]);
        MaxHeapify(A,largest,heapsize);
 }
}

void BuildMaxHeap(int *A,int heaplength)
{
  for(int i = heaplength/2; i >= 0; i--)
   MaxHeapify(A,i,heaplength);
}


void HeapSort(int * A,int heaplength)
{
 BuildMaxHeap(A,heaplength);
 for(int i = heaplength; i >= 1; i--)
 {
  swap(A[0],A[i]);
  heaplength = heaplength-1;
  MaxHeapify(A,0,heaplength);
 }
}

int FindNumber(int * B,int number,int heaplength)
{
 int findnum = 0;
 for(int i = 0; i <= heaplength; i++)
 {
  if(B[i] <= number)
   findnum++;
 }
 return findnum;
}

int DistributedMedian(int * A,int * B,int heaplength,int sender)
{
 int lower = 0;
 int upper = heaplength;
 int median = 0;
 int count = 0;
 while(lower <= upper && median == 0)
 {
  int i=lower + (upper-lower)/2;
  if(sender == 0)
  {
   printf("Alice send %2d to Bob./n",A[i]);
  } 
        else
  {
   printf("Bob send %2d to Alice/n",A[i]);
  }
  if(median == 0)
  {   
   count = FindNumber(B,A[i],heaplength);
   if(sender == 0)
   {
    printf("Bob send %2d to Alice./n/n",count);
   }
   else
   {
    printf("Alice send %2d to Bob./n/n",count);
   }
      if(i + count == heaplength)
       median = A[i];
      else if (i + count < heaplength)
           lower = i + 1;
   else upper = i - 1;
  }
 }
 return median;
}

void main()
{
 int A[6]={61,17,30,50,83,9};
 int B[6]={2,27,198,25,4,18};
 HeapSort(A,5);
 HeapSort(B,5);
 int send;
 int median = 0;
 send = 0;
 median = DistributedMedian(A,B,5,0);
 if(median == 0)
  median = DistributedMedian(B,A,5,1);
 printf("The median is: %2d/n/n",median);
}

 

 
[code]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值