示例#1
0
void initialization(int * const part, double ** const matrice, PriorityQueue * const Qpart, PriorityQueue * const Q, PriorityQueue * const Qinst, double ** const D, int n, int k, int * const deficit, int * const surplus)
{
  int i,j;

  /* ##### PriorityQueue initializations ##### */
  /* We initialize Qpart with a size of k because it contains the subsets's indexes. */
  PQ_init(Qpart, k);

  /* We initialize each Q[i] with a size of n because each vertex is in one of these queue at any time. */
  /* However we could set a size of (n/k)+1 as this is the maximum size of a subset when the partition is not balanced. */
  for(i=0; i<k; ++i)
    PQ_init(&Q[i], n);

  /* We initialize each Qinst[i] with a size of k because fo each vertex i, Qinst[i] contains the D(i,j) values for j = 0...(k-1) */
  for(i=0; i<n; ++i)
    PQ_init(&Qinst[i], k);

  /* ##### Computing the D(i,j) values ##### */
  for(i=0; i < n; ++i) /*for each vertex i*/
    {
      for(j=0; j < n; ++j) /*and for each vertex j*/
	{
	  D[i][part[j]] += matrice[i][j];
	}
    }

  /* ##### Filling up the queues ##### */
  /* ### Qinst ### */
  for(i=0; i < n; ++i) /*for each vertex i*/
    for(j=0; j < k; ++j) /*and for each subset j*/
      PQ_insert(&Qinst[i], j, D[i][j]); /*we insert the corresponding D(i,j) value in Qinst[i]*/

  /* ### Q ### */
  for(i=0; i<n; ++i) /*for each vertex i*/
    PQ_insert(&Q[part[i]], i, PQ_findMaxKey(&Qinst[i])-D[i][part[i]]); /*we insert in Q[part[i]] the vertex i with its highest possible gain*/

  /* ### Qpart ### */
  for(i=0; i < k; ++i) /*for each subset i*/
    PQ_insert(Qpart, i, PQ_findMaxKey(&Q[i])); /*we insert it in Qpart with the highest possible gain by one of its vertex as key*/


  /* ##### Initialization of deficit/surplus ##### */
  *surplus = *deficit = 0;
}
示例#2
0
void balancing(int n, int deficit, int surplus, double ** const D, int * const part)
{
  if(surplus != deficit) /*if the current partition is not balanced*/
    {
      int i;
      PriorityQueue moves; /*we use a queue to store the possible moves from surplus to deficit*/
      PQ_init(&moves, n);
      for(i=0; i<n; ++i) /*for each vertex*/
	{
	  if(part[i] == surplus) /*if i is from surplus*/
	    PQ_insert(&moves, i, D[i][deficit]-D[i][surplus]); /*we insert i in moves with the gain we get from moving i from surplus to deficit as key */
	}
      part[PQ_deleteMax(&moves)] = deficit; /*we put the i from moves with the highest gain in deficit*/
      PQ_exit(&moves);
    }
}
示例#3
0
int main(void)
{
    int n = 30000;
    int max = 1000000;
    int times = 10;
    PQ_t pq = PQ_init(n);
    int i;

    for (i = 0; i < n; i++) {
        PQ_insert(pq, rand() % max);
    }

    int j;

    for (j = 0; j < times; j++) {
        clock_t begin = clock();

        for (i = 0; i < n/2; i++) {
            PQ_delmax(pq);
        }

        clock_t end = clock();
        printf("delete needs %.2f seconds\n",
               (double)(end - begin)/CLOCKS_PER_SEC);
        begin = clock();

        for (i = 0; i < n/2; i++) {
            PQ_insert(pq, rand() % max);
        }

        end = clock();
        printf("insert needs %.2f seconds\n",
               (double)(end - begin)/CLOCKS_PER_SEC);
    }

    PQ_finalize(&pq);
    return 0;
}