Example #1
0
// increase key for the object 'val' and reorder the heap accordingly
void heap::increaseKey(graph_object* val)
{
	if(minheap)
		heapifyDown(val->key);
	else
		heapifyUp(val->key);
}
Example #2
0
// reorders the subheap with elts_[index] as its root
void heap::heapifyDown(int index)
{
  int child1 = index*2+1;
  int child2 = index*2+2;
  int which;
	
  // find smallest (or largest, depending on heap type) child
  if (child1 >= count)
    return;
  else if (child2 >= count)
    which = child1;
  else if (rotate(_elts[child1], _elts[child2]))
    which = child2;
  else
    which = child1;

  if (rotate(_elts[index], _elts[which]))
	{
    graph_object *tmp = _elts[which];
    _elts[which] = _elts[index];
    _elts[index] = tmp;
    _elts[which]->key = which;
    _elts[index]->key = index;
    heapifyDown(which);
  }
}
Example #3
0
/*This will set the top value to zero and then heapify it down to the bottom*/
void deleteMax(heapRef h){
	if(h->array[0] == 0){
		printf("Trying to delete when no value.\n");
	} else {
		h->length--;
		heapifyDown(h);
	}
}
Example #4
0
void BinaryHeap<T>::deleteRootElem() {
	swap(0, heap.size() - 1);
	BinaryHeapNode * prevRoot = heap[heap.size() - 1];
	heap.pop_back();
	idToIndexMap.erase(prevRoot->id);
	free(prevRoot);
	heapifyDown(0);
}
Example #5
0
PartialColoring* heapExtractMin(PartialColoringHeap *heap) {
	if (comparer == &partialColoringBfs) { 
		int time=0;
		while (heap->size[time]==0 && time<heap->alloc_time_count) time++;
		if (time == heap->alloc_time_count) return 0;
		PartialColoring **array = heap->arrays[time];
		int size = heap->size[time];
		PartialColoring *rc = array[0];
		array[0] = array[--size];
		array[size]=0;
		heapifyDown(array, size);
		heap->size[time] = size;

		heap->item_count--;
		assert(heap->item_count>=0);

		return rc;
	} else {
		while (heap->time_count >0) {
			const int last_time = heap->time_count-1;
			for (int t=last_time+1;t<heap->alloc_time_count;t++) {
				assert(heap->size[t]==0);
			}
			if (heap->size[last_time] > 0) {
				PartialColoring **array = heap->arrays[last_time];
				int size = heap->size[last_time];
				PartialColoring *rc = array[0];
				array[0] = array[--size];
				array[size]=0;
				heapifyDown(array, size);
				heap->size[last_time] = size;

				heap->item_count--;
				assert(heap->item_count>=0);

				return rc;
			}
			heap->time_count--;
		}
	}
	//heap->item_count--;
	//assert(heap->item_count>=0);

	return 0;
}
Example #6
0
void heap<T, Compare>::heapifyDown( size_t currentIdx ) {
    /// @todo Implement the heapifyDown algorithm.
	 if(!hasAChild(currentIdx))
        return;
    size_t pirorChildIdx =maxPriorityChild(currentIdx );
    if( !higherPriority( _elems[ currentIdx ], _elems[ pirorChildIdx ] ) ) {
        std::swap( _elems[ currentIdx ], _elems[ pirorChildIdx ] );
        heapifyDown(pirorChildIdx);
    }
}
Example #7
0
T heap<T, Compare>::pop() {
    /// @todo Remove, and return, the element with highest priority
	if(_elems.size() != 1)
	{
		T highest_elem = *(_elems.begin()+1);
		_elems[1] = _elems[_elems.size()-1];
		_elems.pop_back();
		heapifyDown(1);
		return highest_elem;
	}
    return T();
}
Example #8
0
void heap<T, Compare>::heapifyDown( size_t currentIdx ) {
    /// @todo Implement the heapifyDown algorithm.
	if(hasAChild(currentIdx))
	{
	size_t minChildIndex=maxPriorityChild(currentIdx);
		if(!higherPriority(_elems[currentIdx],_elems[minChildIndex]))
		{
			std::swap(_elems[currentIdx],_elems[minChildIndex]);
			heapifyDown(minChildIndex);
		}
	}
}
Example #9
0
/**
 * Remove the item with the highest priority from the heap & re-heapify.
 */
graph_object *heap::remove()
{
  if (empty())
		return 0;
  count--;
  graph_object *ans = _elts[0];
  _elts[0] = _elts[count];
  _elts[0]->key = 0;
  _elts.pop_back();
  heapifyDown(0);

  return ans;
}
Example #10
0
heap<T, Compare>::heap( const std::vector<T> & elems ) {
    /// @todo Construct a heap using the buildHeap algorithm
	_elems.push_back(T());
	for(size_t i = 0; i < elems.size(); i++)
	{
		_elems.push_back(elems[i]);
		//heapifyUp(i+1);	
	}
	for(size_t i = parent(_elems.size()-1); i >0 ; i--)
	{
		//_elems.push_back(elems[i]);
		heapifyDown(i);	
	}
}
Example #11
0
void BinaryHeap<T>::deleteElemWithId(int id) {
	map<int,int>::iterator it;
	it = idToIndexMap.find(id);
	if(it == idToIndexMap.end()) return;

	int idx = idToIndexMap[id];
	swap(idx, heap.size() - 1);
	BinaryHeapNode * prevHeapNode = heap[heap.size() - 1];
	heap.pop_back();
	idToIndexMap.erase(prevHeapNode->id);
	free(prevHeapNode);
	if(idx == heap.size()) return;
	heapifyDown(idx);
	heapifyUp(idx);
}
Example #12
0
T heap<T, Compare>::pop() {
    /// @todo Remove, and return, the element with highest priority
	/*T minval=_elems[1];  
	_elems[1]=_elems[_elems.size()-1];//size-1?
	_elems.pop_back();
	heapifyDown(1);
	return minval;
 // return T();*/
	T returnValue = _elems[1];
    std::swap( _elems[1], _elems[_elems.size()-1] );
        _elems.pop_back();
	      
	 heapifyDown(1);
    return returnValue;
}
Example #13
0
heap<T, Compare>::heap( const std::vector<T> & elems ) {
    /// @todo Construct a heap using the buildHeap algorithm
	//_elems=elems;	
	/*_elems.push_back(T());
	for(int i=parent(_elems.size()-1);i>=1;i--)
		heapifyDown(i);*/
	//_elems.clear();
        _elems.push_back(T());
        for (int i=0;i<elems.size();i++) // copy elements
         _elems.push_back(elems[i]);
        
 
        for (int j=_elems.size()-1; j>=1; j--) // start and end and heapifyDown
         heapifyDown(j);
       
	

}
Example #14
0
int
Heap::heapifyDown(Heap* currPos)
{
	if(currPos->left==NULL && currPos->right==NULL)
	{
		return 0;
	}
	Heap* minChild=NULL;
	if(currPos->left!=NULL && currPos->right!=NULL)
	{
		minChild=currPos->right;
		if(currPos->left->p.dist<currPos->right->p.dist)
		{
			minChild=currPos->left;
		}
	}
	else if(currPos->left!=NULL)
	{
		minChild=currPos->left;
	}
	else if(currPos->right!=NULL)
	{
		minChild=currPos->right;
	}
	if(minChild->p.dist<currPos->p.dist)
	{
		//Need to shift
		if(currPos->parent!=NULL)
		{
			if(currPos->parent->left==currPos)
			{
				currPos->parent->left=minChild;
			}
			else if(currPos->parent->right==currPos)
			{
				currPos->parent->right=minChild;
			}
		}	
		minChild->parent=currPos->parent;
		currPos->parent=minChild;
		if(minChild->parent==NULL)
		{
			root=minChild;
		}
		if(currPos->left==minChild)
		{
			Heap* oldleft=minChild->left;
			minChild->left=currPos;
			currPos->left=oldleft;
			if(oldleft!=NULL)
			{
				oldleft->parent=currPos;
			}
		}
		else if(currPos->right==minChild)
		{
			Heap* oldright=minChild->right;
			minChild->right=currPos;
			currPos->right=oldright;
			if(oldright!=NULL)
			{
				oldright->parent=currPos;
			}
		}
		heapifyDown(currPos);				
	}
	return 0;
}
Example #15
0
//-----------------------------------------------------------------------------
// This function swaps the partition of a vertex
//-----------------------------------------------------------------------------
void fmSwap(EdgeCutProblem *graph, const EdgeCut_Options *options, Int vertex, double gain,
            bool oldPartition)
{
    Int *Gp             = graph->p;
    Int *Gi             = graph->i;
    double *Gx          = graph->x;
    bool *partition     = graph->partition;
    double *gains       = graph->vertexGains;
    Int *externalDegree = graph->externalDegree;
    Int **bhHeap        = graph->bhHeap;
    Int *bhSize         = graph->bhSize;

    /* Swap partitions */
    bool newPartition = !oldPartition;
    partition[vertex] = newPartition;
    gains[vertex]     = -gain;

    /* Update neighbors. */
    Int exD = 0;
    for (Int p = Gp[vertex]; p < Gp[vertex + 1]; p++)
    {
        Int neighbor           = Gi[p];
        bool neighborPartition = partition[neighbor];
        bool sameSide          = (newPartition == neighborPartition);

        /* Update the bestCandidate vertex's external degree. */
        if (!sameSide)
            exD++;

        /* Update the neighbor's gain. */
        double edgeWeight   = (Gx) ? Gx[p] : 1;
        double neighborGain = gains[neighbor];
        neighborGain += 2 * (sameSide ? -edgeWeight : edgeWeight);
        gains[neighbor] = neighborGain;

        /* Update the neighbor's external degree. */
        Int neighborExD = externalDegree[neighbor];
        neighborExD += (sameSide ? -1 : 1);
        externalDegree[neighbor] = neighborExD;
        Int position             = graph->BH_getIndex(neighbor);

        /* If the neighbor was in a heap: */
        if (position != -1)
        {
            /* If it had its externalDegree reduced to 0, remove it from the
             * heap. */
            if (neighborExD == 0)
            {
                bhRemove(graph, options, neighbor, neighborGain,
                         neighborPartition, position);
            }
            /* If the neighbor is in the heap, we touched its gain
             * so make sure the heap property is satisfied. */
            else
            {
                Int v = neighbor;
                heapifyUp(graph, bhHeap[neighborPartition], gains, v, position,
                          neighborGain);
                v = bhHeap[neighborPartition][position];
                heapifyDown(graph, bhHeap[neighborPartition],
                            bhSize[neighborPartition], gains, v, position,
                            gains[v]);
            }
        }
        /* Else the neighbor wasn't in the heap so add it. */
        else
        {
            if (!graph->isMarked(neighbor))
            {
                ASSERT(!graph->BH_inBoundary(neighbor));
                bhInsert(graph, neighbor);
            }
        }
    }

    externalDegree[vertex] = exD;
}
Example #16
0
void BinaryHeap<T>::fixTreeAtElemWithId(int id) {
	int idx = idToIndexMap[id];
	heapifyUp(idx);
	heapifyDown(idx);
}
Example #17
0
int 
Heap::deleteFromHeap_getLeaf(Heap* removeMe)
{
	Heap* leaf=getLeaf(removeMe);
	//Otherwise replace the removeMe by the leaf node
	if((strcmp(removeMe->p.node1.c_str(),"ADH2-BAR1-SPO11-FUS1-SIN3-RME1-SWI1")==0) && (strcmp(removeMe->p.node2.c_str(),"ALPHA1-HXT7-MFALPHA2-SNF2_SWI1-ARG5-HXT6-MCM1-STA2-STA1-SAG1-STE6-PHO11-PHO5")==0))
	{
		cout << "Stop here " << endl;
	}
	if(root->parent!=NULL)
	{
		cout <<"Some shit happened at " << root->p.node1 << " " << root->p.node2 << " root's parent is garbage"  << endl;
	}
	//cout <<"Deleting " << removeMe->p.node1 <<" " << removeMe->p.node2 << endl;
	if(leaf==removeMe)
	{
		if(leaf->parent!=NULL)
		{
			if(leaf->parent->left==leaf)
			{
				leaf->parent->left=NULL;
			}
			else if(leaf->parent->right==leaf)
			{
				leaf->parent->right=NULL;
			}
			removeMe->parent=NULL;
		}
		if(removeMe==root)
		{
			root=NULL;
		}
		delete removeMe;
		return 0;
	}
	//Now connect the current children of removeMe to leaf
	if(leaf!=removeMe->left)
	{
		leaf->left=removeMe->left;
		if(removeMe->left!=NULL)
		{
			removeMe->left->parent=leaf;
		}
	}
	if(leaf!=removeMe->right)
	{
		leaf->right=removeMe->right;
		if(removeMe->right!=NULL)
		{
			removeMe->right->parent=leaf;
		}
	}
	//next disconnect the leaf from it's.
	if(leaf->parent->left==leaf)
	{
		leaf->parent->left=NULL;	
		leaf->parent=NULL;
	}
	else if(leaf->parent->right==leaf)
	{
		leaf->parent->right=NULL;
		leaf->parent=NULL;
	}
	//Finally if removeMe had a parent then leaf's parent should be updated
	if(removeMe->parent!=NULL)
	{
		leaf->parent=removeMe->parent;
		if(removeMe->parent->left==removeMe)
		{
			removeMe->parent->left=leaf;
		}
		else if(removeMe->parent->right==removeMe)
		{
			removeMe->parent->right=leaf;
		}
		else
		{
			cerr <<"Dangling pointer for " << removeMe->p.node1<<"-" << removeMe->p.node2 << endl;	
			exit(-1);
		}
	}
	else
	{
		root=leaf;
		root->parent=NULL;
	}
	removeMe->parent=NULL;
	delete removeMe;
	heapifyDown(leaf);
	if(root->parent!=NULL)
	{
		cout <<"Some shit happened at " << root->p.node1 << " " << root->p.node2 << " root's parent is garbage"  << endl;
	}
	return 0;
}