Exemple #1
0
void heap_heapify_down(heap* h) {
    unsigned int pos = 0, 
                 newpos,
                 temp;
	node** graph_nodes;

	graph_nodes = &h->g->nodes;
	
    while(1) {
        if(HEAP_LEFT(pos) <= h->count) { 
            if(HEAP_RIGHT(pos) <= h->count) {
                newpos = (*h->compare)(h->g, h->nodes[HEAP_RIGHT(pos)], h->nodes[HEAP_LEFT(pos)]) < 0 ? HEAP_RIGHT(pos) : HEAP_LEFT(pos);
            } else {
                newpos = HEAP_LEFT(pos);
            }
        } else {
            break; 
        }
        
        if((*h->compare)(h->g, h->nodes[pos], h->nodes[newpos]) > 0) {
            temp = h->nodes[pos];
			(*graph_nodes)[temp].heap_index = newpos;
			(*graph_nodes)[h->nodes[newpos]].heap_index = pos;
            h->nodes[pos] = h->nodes[newpos];
            h->nodes[newpos] = temp;
        } else {
            break; /* A posição está correta*/
        }
    }
}
Exemple #2
0
void
heap_heapify(heap_t* h, int i)
{
    int l, r;
    int largest;
    double tmp;
    double* tmp_data; // FIXME: void*

    if (i < h->len/2) {
        l = HEAP_LEFT(i);
        r = HEAP_RIGHT(i);

        if((l < h->len) && (h->A[l] > h->A[i]))
            largest = l;
        else
            largest = i;

        if((r < h->len) && (h->A[r] > h->A[largest]))
            largest = r;

        if(largest != i)
        {
            tmp = h->A[i];
            tmp_data = h->data[i];
            h->A[i] = h->A[largest];
            h->data[i] = h->data[largest];
            h->A[largest] = tmp;
            h->data[largest] = tmp_data;
            heap_heapify(h,largest);
        }
    }
}
Exemple #3
0
/**
 * @brief CSLR MAX-HEAPIFY
 *
 * This routine creates a max-heap for the subtree rooted at index i. The
 * index is 0 based.
 *
 * @param[in] h The heap to operate on
 * @param[in] i The (0-based) subtree index
 * @return HEAP_ERR_E
 */
HEAP_ERR_E heap_max_heapify (HEAP_T* h, unsigned long i)
{
   unsigned long l = HEAP_LEFT(i);
   unsigned long r = HEAP_RIGHT(i);
   unsigned long largest;

   //fprintf (stdout, "i=%lu; l=%lu; r=%lu\n", i, l, r);
   if (h->type == DS_HEAP_MIN)
      return HEAP_ERR_WRONG_TYPE;
   
   if (l < h->heap_size && (HEAP_KEY(h, l) > HEAP_KEY(h, i)))
      largest = l;
   else
      largest = i;
   
   if (r < h->heap_size && (HEAP_KEY(h, r) > HEAP_KEY(h, largest)))
      largest = r;

   if (largest != i)
   {
      HEAP_SWAP_NODES(i,largest);
      heap_max_heapify (h, largest);
   }
   /* todo: set was_heapified to true */
   return HEAP_ERR_OK;
}
Exemple #4
0
/**
 * @brief graphviz description plugin for heap ds.
 *
 * @param[in] h The heap to operate in
 * @param[in] filename The file to write the graphviz description to.
 * @return HEAP_ERR_E
 */
HEAP_ERR_E heap_graphviz_description
(
HEAP_T* h,
char* filename
)
{
   FILE* fp;
   unsigned long idx = 0;
   unsigned long weight;
   if (NULL == filename)
   {
      fp = stderr;
   }
   else
   {
      fp = fopen (filename, "w");
      if (NULL == fp)
         return HEAP_ERR_ERR;
   }
   
   fprintf (fp, "graph G {\n");
   fprintf (fp, "node [shape = circle, style=filled, color=\"sienna2\"];\n");
   fprintf (fp, "size=\"12,8\"\n");

   weight = h->heap_size;
   if (HEAP_LEFT(idx) < h->heap_size)
      fprintf (fp, "\t%lu -- %lu [headlabel=\"%lu\", weight=%lu];\n ",
               HEAP_KEY(h, idx),
               HEAP_KEY(h, HEAP_LEFT(idx)),
               HEAP_LEFT(idx),
               weight);
   if (HEAP_RIGHT(idx) < h->heap_size)
      fprintf (fp, "\t%lu -- %lu [headlabel=\"%lu\", weight=%lu];\n",
               HEAP_KEY(h, idx),
               HEAP_KEY(h, HEAP_RIGHT(idx)),
               HEAP_RIGHT(idx),
               weight);
   
   for (idx = 1; idx < h->heap_size; idx++)
   {
      if (HEAP_LEFT(idx) < h->heap_size)
         fprintf (fp, "\t%lu -- %lu [headlabel = \"%lu\"]; \n ",
                  HEAP_KEY(h, idx),
                  HEAP_KEY(h, HEAP_LEFT(idx)),
                  HEAP_LEFT(idx));
      if (HEAP_RIGHT(idx) < h->heap_size)
         fprintf (fp, "\t%lu -- %lu [headlabel = \"%lu\"];\n",
                  HEAP_KEY(h, idx),
                  HEAP_KEY(h, HEAP_RIGHT(idx)),
                  HEAP_RIGHT(idx));
   }
   
   fprintf (fp, "}\n");

   if (NULL != filename)
      fclose (fp);
   
   return HEAP_ERR_OK;
}
Exemple #5
0
void heap_dump(const heap* h, const unsigned int from) {
    if(from == 0) {
        printf("digraph g {\nnode [shape = record,height=.1];\n");
    }
    if(from < h->count) {
        printf("node%d[ label = \"<f0> | <f1> %s | <f2>\"];\n", from, h->g->nodes[h->nodes[from]].value);

        if(HEAP_LEFT(from) < h->count) {
            printf("\"node%d\":f0 -> \"node%d\":f1\n", from, HEAP_LEFT(from));
            heap_dump(h, HEAP_LEFT(from));
        }
    
        if(HEAP_RIGHT(from) < h->count) {
            printf("\"node%d\":f2 -> \"node%d\":f1\n", from, HEAP_RIGHT(from));
            heap_dump(h, HEAP_RIGHT(from));
        }
    }
    
    if(from == 0) { 
        printf("}\n");
    }
}
Exemple #6
0
static void max_heap_fix_down(int *t, int n, int p)
{
	while (1)
	{
		int i = p;
		int left = HEAP_LEFT(i);
		int right = HEAP_RIGHT(i);
		if (left < n && t[i] < t[left])
			i = left;
		if (right < n && t[i] < t[right])
			i = right;
		if (i == p)
			break;
		swap_places(t, p, i);
		p = i;
	}
}
Exemple #7
0
static void BLI_heap_down(Heap *heap, int i)
{
	while (1) {
		int size = heap->size, smallest;
		int l = HEAP_LEFT(i);
		int r = HEAP_RIGHT(i);

		smallest = ((l < size) && HEAP_COMPARE(heap->tree[l], heap->tree[i]))? l: i;

		if ((r < size) && HEAP_COMPARE(heap->tree[r], heap->tree[smallest]))
			smallest = r;
		
		if (smallest == i)
			break;

		HEAP_SWAP(heap, i, smallest);
		i = smallest;
	}
}
Exemple #8
0
void heap_max_heapify(int *v, int size, int i)
{
	int l, r;
	int largest;

	l = HEAP_LEFT(i);
	r = HEAP_RIGHT(i);

	if (l < size && v[l] > v[i])
		largest = l;
	else
		largest = i;
	if (r < size && v[r] > v[largest])
		largest = r;
	if (largest != i) {
		swap(v, i, largest);
		heap_max_heapify(v, size, largest);
	}
}
Exemple #9
0
static void
heap_heapify(struct heap_list *h, int i)
{
	int		l, r, largest;
	void	**p;

restart:
	l = HEAP_LEFT(i);
	r = HEAP_RIGHT(i);
	p = h->heap_data;

	if (l < h->heap_keys && (h->heap_comp_fun(p[l], p[i]) > 0))
		largest = l;
	else
		largest = i;
	if (r < h->heap_keys && h->heap_comp_fun(p[r], p[largest]) > 0)
		largest = r;
	if (largest != i) {
		heap_exchange(h, i, largest);
		i = largest;
		goto restart;
	}
}
Exemple #10
0
/**
 * @brief CSLR MIN-HEAPIFY
 *
 * This routine creates a min-heap for the subtree rooted at index i. The
 * index is 0 based.
 *
 * @param[in] h The heap to operate on
 * @param[in] i The (0-based) subtree index
 * @return HEAP_ERR_E
 */
HEAP_ERR_E heap_min_heapify (HEAP_T* h, unsigned long i)
{
   unsigned long l = HEAP_LEFT(i);
   unsigned long r = HEAP_RIGHT(i);
   unsigned long smallest;

   if (h->type == DS_HEAP_MAX)
      return HEAP_ERR_WRONG_TYPE;
   
   if (l < h->heap_size && (HEAP_KEY(h, l) < HEAP_KEY(h, i)))
      smallest = l;
       else
      smallest = i;
   
   if (r < h->heap_size && (HEAP_KEY(h, r) < HEAP_KEY(h, smallest)))
      smallest = r;

   if (smallest != i)
   {
      HEAP_SWAP_NODES(i,smallest);
      heap_min_heapify (h, smallest);
   }
   return HEAP_ERR_OK;
}