Exemple #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);
}
Exemple #2
0
/**
 * Add object into heap.
 */
void heap::add(graph_object *val)
{
  val->key = count;
  _elts.push_back(val);
  count++;
  heapifyUp(val->key);
}
Exemple #3
0
void heap<T, Compare>::heapifyUp( size_t currentIdx ) {
    if( currentIdx == root() )
        return;
    size_t parentIdx = parent( currentIdx );
    if( higherPriority( _elems[ currentIdx ], _elems[ parentIdx ] ) ) {
        std::swap( _elems[ currentIdx ], _elems[ parentIdx ] );
        heapifyUp( parentIdx );
    }
}
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);
}
Exemple #5
0
void heap::heapifyUp(int index)
{
  if (index == 0) return;
  int parent = (index-1)/2;

  if (rotate(_elts[parent], _elts[index]))
	{
    graph_object *tmp = _elts[parent];
    _elts[parent] = _elts[index];
    _elts[index] = tmp;
    _elts[parent]->key = parent;
    _elts[index]->key = index;
    heapifyUp(parent);
  }
}
int heapInsert(PartialColoringHeap *heap, PartialColoring *pc) {
	const int timestep = pc->timestep;

	// create new heaps for new time step
	while (timestep >= heap->alloc_time_count) {
		int newsize = heap->alloc_time_count + heap->time_increment;

		heap->size = ReAlloc(heap->size, newsize*sizeof(int));
		heap->alloc_size = ReAlloc(heap->alloc_size, newsize*sizeof(int));
		heap->arrays = ReAlloc(heap->arrays, newsize*sizeof(PartialColoring**));

		memset(heap->size + heap->alloc_time_count, 0, heap->time_increment*sizeof(int));
		memset(heap->alloc_size + heap->alloc_time_count, 0, heap->time_increment*sizeof(int));
		memset(heap->arrays + heap->alloc_time_count, 0, heap->time_increment*sizeof(PartialColoring **));

		heap->alloc_time_count = newsize;

	}

    //printf("time: item %d heap %d\n", pc->timestep, heap->time_count);

	if (timestep+1>heap->time_count) {
		heap->time_count = timestep+1;
	}

	// increase heap size for new item
	if (heap->size[timestep] >= heap->alloc_size[timestep]) {
		int newsize = heap->alloc_size[timestep] + heap->array_increment;

		heap->arrays[timestep] = ReAlloc(heap->arrays[timestep], newsize * sizeof(PartialColoring*));
		memset(heap->arrays[timestep] + heap->alloc_size[timestep], 0, heap->array_increment*sizeof(PartialColoring **));
		heap->alloc_size[timestep] = newsize;
	}

	heap->arrays[timestep][heap->size[timestep]++] = pc;
	heapifyUp(heap->arrays[timestep], heap->size[timestep]);

	int found=0;
	for (int i=0;i<heap->size[timestep];i++) {
		if (heap->arrays[timestep][i]==pc) { found=1; break; }
	}
	assert(found);

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

	return 1;
}
Exemple #7
0
void heap<T, Compare>::push( const T & elem ) {
    /// @todo Add elem to the heap
		_elems.push_back(elem);
		heapifyUp(_elems.size()-1);	
}
Exemple #8
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;
}
void BinaryHeap<T>::insert(T elem, int id) {
	BinaryHeapNode * newNode = new BinaryHeapNode(elem, id);
	heap.push_back(newNode);
	idToIndexMap[id] = heap.size() - 1;
	heapifyUp(heap.size() - 1);
}
Exemple #10
0
void BinaryHeap<T>::fixTreeAtElemWithId(int id) {
	int idx = idToIndexMap[id];
	heapifyUp(idx);
	heapifyDown(idx);
}