Exemple #1
0
// A standard function to heapify at given idx
// This function also updates position of nodes when they are swapped.
// Position is needed for decreaseKey()
void minHeapify(struct MinHeap* minHeap, int idx)
{
    int smallest, left, right;
    smallest = idx;
    left = 2 * idx + 1;
    right = 2 * idx + 2;
 
    if (left < minHeap->size &&
        minHeap->array[left]->dist < minHeap->array[smallest]->dist )
      smallest = left;
 
    if (right < minHeap->size &&
        minHeap->array[right]->dist < minHeap->array[smallest]->dist )
      smallest = right;
 
    if (smallest != idx)
    {
        // The nodes to be swapped in min heap
        MinHeapNode *smallestNode = minHeap->array[smallest];
        MinHeapNode *idxNode = minHeap->array[idx];
 
        // Swap positions
        minHeap->pos[smallestNode->v] = idx;
        minHeap->pos[idxNode->v] = smallest;
 
        // Swap nodes
        swapMinHeapNode(&minHeap->array[smallest], &minHeap->array[idx]);
 
        minHeapify(minHeap, smallest);
    }
}
void minHeapify (MinHeap* minHeap, int idx) //for the first call, idx is always 0
{
	int smallest, left, right;
	smallest = idx; //reset
	left = 2 * idx + 1; //left node index has this relation
	right = 2 * idx + 2; //right node index has this relation
	if (left < minHeap->size && minHeap->array[left]->freq < minHeap->array[smallest]->freq)
		smallest=left; //this is done, as it was not following the rule otherwise
	if (right < minHeap->size && minHeap->array[right]->freq < minHeap->array[smallest]->freq)
		smallest=right; //this is done, as it was not following the rule otherwise
	//the rule: children nodes have values larger than parent node
	if (smallest != idx) //meaning nothing needs to be changed!
	{
		swapMinHeapNode ( &minHeap->array[smallest], &minHeap->array[idx] ); //sending addresses of nodes
		minHeapify (minHeap, smallest); //recursive call of the function, till smallest = idx
	}
}
Exemple #3
0
// Function to decreasy dist value of a given vertex v. This function
// uses pos[] of min heap to get the current index of node in min heap
void decreaseKey(struct MinHeap* minHeap, int v, int dist)
{
  // Get the index of v in heap array
  int i = minHeap->pos[v];
  // Get the node and update its dist value
  minHeap->array[i]->dist = dist;
  // Travel up while the complete tree is not hepified.
  // This is a O(Logn) loop
  while (i && minHeap->array[i]->dist < minHeap->array[(i - 1) / 2]->dist)
  {
    // Swap this node with its parent
    minHeap->pos[minHeap->array[i]->v] = (i-1)/2;
    minHeap->pos[minHeap->array[(i-1)/2]->v] = i;
    swapMinHeapNode(&minHeap->array[i], &minHeap->array[(i - 1) / 2]);
    // move to parent index
    i = (i - 1) / 2;
  }
}
Exemple #4
0
void minHeapify(struct MinHeap* minHeap, int idx)
{
    int smallest = idx;
    int left = 2 * idx + 1;
    int right = 2 * idx + 2;

    if (left < minHeap->size && minHeap->array[left]->freq < minHeap->array[smallest]->freq)
        smallest = left;

    if (right < minHeap->size && minHeap->array[right]->freq < minHeap->array[smallest]->freq)
        smallest = right;

    if (smallest != idx)
    {
        swapMinHeapNode(&minHeap->array[smallest], &minHeap->array[idx]);
        minHeapify(minHeap, smallest);
    }
}