/*
Parameter – It take 1 MinFibnode and do cascading.
If y is a root list no more cascading required, for y to be in root list it parent would
be NULL.
If child cut happens first time no more cut and cascading required
Just mark the node to indicate child cut is true
If y is marked y is just lost the second child, y is cut from here and added to the
root list and cascading cut is called to the parent of the node



*/
void FibHeap::cascadeCut(FibHeapNode *node)
{
	FibHeapNode *parent=node->parent;
	/* If parent  is a root list  no more cascading required */


	if(parent !=NULL)
	{
		/* If childcut happens first time no more cut and cascading required
		           just mark the node to indicate childcut is true */
		if(!node->ChildCut)
		{
			node->ChildCut=true;
		}

		/* if parent  is marked parent  is just lost the second child , parent  is cut from here 
		           and added to the root list and cascading cut is called to the parent 
				              of the node */
		else{
			cutNode(parent,node);
			cascadeCut(parent);
		}
	}


}
/* This function decrease the value of fibonacci node x to value k */
void FibHeap::decreaseKey(FibHeapNode *node,int key)
{
	if(!node||!minNode||node->weight<key)//if node weight whose key to changed is already less than key then we wont perform decrease key operation 
	{
		cout<<"Decrease key operation could not be performed successfully "<<endl;
		return ;
	}
	else
	{
		node->weight=key;/* Assign new weight to node */
		FibHeapNode *parent=node->parent;
		/*If Heap order is violated call funtion cut and cascade_cut */
		if(parent!=NULL&&(parent->weight>node->weight))
		{
			cutNode(parent,node);
			cascadeCut(parent);
		}
		if(node->weight<minNode->weight)
		{
			minNode=node;

		}

	}

}
Beispiel #3
0
Boolean Btree<T>::pushDown(T& x,
                           unsigned ptr,
                           T& item,
                           unsigned &itemRST)
//
// Purpose:  recursive function that inserts item x in the B-tree.
// The function returns TRUE if a the height of the B-tree needs
// to be increased.  Otherwise, the function yields FALSE.
//
// Parameters:
//
//    input: x - the inserted data
//           ptr - the index to the manipulated node
//
//    output:
//           item - the median element
//           itemRST - the index to the right subtree of the node
//             which contains item
//
{
  unsigned i;
  Bstruct<T> *buf;

  if (ptr == BTREE_NIL) {
    // cannot insert into an empty tree
    item = x;
    itemRST = BTREE_NIL;
    return TRUE;
  }
  else {
    buf = new Bstruct<T>;
    readNode(buf, ptr);
    // attempt to isnert a duplicate key in the current node?
    if (searchNode(x, buf, i)) {
      strcpy(errMsg, "Cannot insert duplicates");
      return FALSE;
    }
    // reinsert the median element
    if (pushDown(x, buf->nodeLink[i], item, itemRST)) {
      if (buf->count < BTREE_MAX) {
        pushIn(item, itemRST, buf, i);
        writeNode(buf, ptr);
        delete buf;
        return FALSE;
      }
      else {
        cutNode(item, itemRST, ptr, i, item, itemRST);
        delete buf;
        return TRUE;
      }
    }
    else {
      delete buf;
      return FALSE;
   }
  }
}
Beispiel #4
0
/****************************************************************
 * Name    : partition                                          *
 * Function: partition the graph recursively.                   *
 * Input   : void                                               *
 * Output  :  int                                               *
 ****************************************************************/
static int partition(struct node * source, struct node * left, struct node * right){
  struct node * l_left;
  struct node * l_right;
  struct node * r_left;
  struct node * r_right;
  int i;

  /* cut the source node into left node and right node */
  if(cutNode(source, left, right, BC)<0){
    printf("cutNode error! \n");
    return -1;
  }
  /* link the two sub-trees to the root */
  source->left = left;
  source->right = right;

  /* if left sub-tree is still bigger than threshold */
  if(left->numberPoints > threshold) {
    l_left = (struct node *)calloc(1, sizeof(struct node));
    if(l_left == NULL)
      {
	printf("cannot allocate memory for left! \n");
	return -1;
      }
    l_right = (struct node *)calloc(1, sizeof(struct node));
    if(l_right == NULL)
      {
	printf("cannot allocate memory for right! \n");
	return -1;
      }

    if(partition(left, l_left, l_right)<0)
      return -1;

  }else{
    groupLength[groupIndex] = left->numberPoints;
    groups[groupIndex] =  (int *)calloc(left->numberPoints, sizeof(int));
    if(groups[groupIndex] == NULL)
      {
	printf("cannot allocate memory for groups! \n");
	return -1;
      }
    for(i=0;  i<left->numberPoints; i++){
      groups[groupIndex][i] = left->points[i];
    }/* end for */
    groupIndex++;
    printf("groupIndex = %d \n", groupIndex);
    if(groupIndex >= MAXGROUPS)
      {
	printf("groupIndex is now bigger than MAXGROUPS \n");
	return -1;
      }

    /*following part is for output matlab file after first phase */
    /*
      printf("%Points in this group include: \n c%d = [", index_matlab);
      index_matlab++;
      for(i=0; i<left->numberPoints; i++)
      {
      printf("%f %f \n ", points[left->points[i]].x, points[left->points[i]].y);
      }
      printf("];\n\n");
    */
  }/* end if...else... */
  
  if(right->numberPoints > threshold){
    r_left = (struct node *)calloc(1, sizeof(struct node));
    if(r_left == NULL)
      {
	printf("cannot allocate memory for left! \n");
	return -1;
      }
    r_right = (struct node *)calloc(1, sizeof(struct node));
    if(r_right == NULL)
      {
	printf("cannot allocate memory for right! \n");
	return -1;
      }

    if(partition(right, r_left, r_right)<0)
      return -1;
  }else{
    groupLength[groupIndex] = right->numberPoints;
    groups[groupIndex] =  (int *)calloc(right->numberPoints, sizeof(int));
    if(groups[groupIndex] == NULL)
      {
	printf("cannot allocate memory for groups! \n");
	return -1;
      }
    for(i=0;  i<right->numberPoints; i++){
      groups[groupIndex][i] = right->points[i];
    }/* end for */
    groupIndex++;
    printf("groupIndex = %d \n", groupIndex);
    if(groupIndex >= MAXGROUPS)
      {
	printf("groupIndex is now bigger than MAXGROUPS \n");
	return -1;
      }


    /*following part is for output matlab file after first phase */
    /*
      printf("%Points in this group include: \n c%d =[", index_matlab);
      index_matlab++;
      for(i=0; i<right->numberPoints; i++)
      {
      printf("%f %f \n ", points[right->points[i]].x, points[right->points[i]].y);
      }
      printf("];\n\n");
    */
  }/* end if...else... */
  
  return 1;
}/* end partition */
Beispiel #5
0
/****************************************************************
 * Name    : computeGoodness                                    *
 * Function: compute goodness for merging two clusters.         *
 * Input   : int group0 -- group0's index.                      *
 *           int group1 -- group1's index.                      *
 * Output  :  float                                             *
 ****************************************************************/
static float computeGoodness(int group0, int group1){
  float goodness;
  float ri, rc;
  int ubFactor;

  /* form two nodes according to two groups */
  struct node * node0;
  struct node * node1;
  /* left and right is for cutting the node0 and node 1 to
   * compute internal connectivity and internal closeness.
   */
  struct node * left0;
  struct node * right0;
  struct node * left1;
  struct node * right1;

  int i;
  /* absolute inter-connectivity and absolute closeness */
  float aic, ac, aic0, ac0, aic1, ac1;

  /*!!!!!!!*/
  ubFactor = 6;

  /* First create node0 and node1 according to group0 and group1 */
  node0 = (struct node *)calloc(1, sizeof(struct node));
  node1 = (struct node *)calloc(1, sizeof(struct node));
  if(node0==NULL || node1==NULL){
    printf("cannot allocate memory for node0 and node1. \n");
    return -1;
  }/* end if */

  node0->left=NULL;
  node0->right=NULL;
  node0->numberPoints = groupLength[group0];
  node0->points = (int *)calloc(node0->numberPoints, sizeof(int));
  if(node0->points == NULL){
    printf("cannot allocate memory for node0->points. \n");
    return -1;
  }
  for(i=0; i<groupLength[group0]; i++){
    node0->points[i]=groups[group0][i];
  }/* end for */


  node1->left=NULL;
  node1->right=NULL;
  node1->numberPoints = groupLength[group1];
  node1->points = (int *)calloc(node1->numberPoints, sizeof(int));
  if(node1->points == NULL){
    printf("cannot allocate memory for node0->points. \n");
    return -1;
  }
  for(i=0; i<groupLength[group1]; i++){
    node1->points[i]=groups[group1][i];
  }/* end for */

  /* Now compute absolute inter-connectivity and 
   * absolute closeness.
   */
  computeAIC_AC(node0, node1, &aic, &ac);


  if(ac > 0.0 && aic > 0.0){
    /* now need to compute the internal connectivity of 
     * node0 and node1.
     */
    left0 = (struct node *)calloc(1, sizeof(struct node));
    right0 = (struct node *)calloc(1, sizeof(struct node));
    if(left0==NULL || right0==NULL){
      printf("cannot allocate memory for node0 and node1. \n");
      return -1;
    }/* end if */
    
    if(cutNode(node0, left0, right0, ubFactor)<0)
      {
	printf("cut node0 error! \n");
	return -1;
      }
    
    /* Now compute internal inter-connectivity and 
     * absolute closeness of node0.
     */
    computeAIC_AC(left0, right0, &aic0, &ac0);
    
    left1 = (struct node *)calloc(1, sizeof(struct node));
    right1 = (struct node *)calloc(1, sizeof(struct node));
    if(left1==NULL || right1==NULL){
      printf("cannot allocate memory for node0 and node1. \n");
      return -1;
    }/* end if */
    
    if(cutNode(node1, left1, right1, ubFactor)<0)
      {
	printf("cut node0 error! \n");
	return -1;
      }
    
    /* Now compute internal inter-connectivity and 
     * absolute closeness of node0.
     */
    computeAIC_AC(left1, right1, &aic1, &ac1);
    
    
    /* Now compute Relative Inter-Connectivity */
    ri = (aic*2)/(aic0+aic1);

    /* Now compute Relative Closeness */
    rc = ac*(node0->numberPoints+node1->numberPoints)/((node0->numberPoints)*ac0 + 
						       (node1->numberPoints)*ac1);
  
    /* 
       printf("aic is %f, aic0 is %f, aic1 is %f \n", aic, aic0, aic1);
       printf("ac is %f, ac0 is %f, ac1 is %f \n", ac, ac0, ac1);
       printf("node0->numberPoints is %d, node1->numberPoints is %d \n", 
       node0->numberPoints, node1->numberPoints);
    */

    if(ri>10000 || rc>10000){
      printf("ri or ic is wrong! ri= %f, rc= %f \n", ri, rc);
      return -1;
    }
    /* The final goodness according to p11 of the CHAMELEON paper */
    /* printf("ri is %f, rc is %f. \n", ri, rc);*/
    goodness = ri*pow((double)rc, ALPHA);


    free(node0->points);
    free(node1->points);
    free(left0->points);
    free(right0->points);
    free(left1->points);
    free(right1->points);
    free(left0);
    free(right0);
    free(left1);
    free(right1);
    free(node0);
    free(node1);
  }
  else{
    ri=0.0;
    rc=0.0;
    /* printf("ri is %f, rc is %f. \n", ri, rc);*/
    goodness = 0.0;

    free(node0->points);
    free(node1->points);
    free(node0);
    free(node1);
  }/* end if...else...*/

  return goodness;
}/* end computeGoodness */