コード例 #1
0
ファイル: test.c プロジェクト: steeb/studium-ppr
int main (void)
{
    int i;
    char outOld;
    char out;
    BOOL allesJut;
    srand (time (NULL));
    heap_init ();

    for (i = 0; i < 10; i++)
    {
        heap_insert ( (26 * (rand () / (RAND_MAX + 1.0))) + 97);
    }

    heap_print ();

    heap_extract_min (&outOld);
    allesJut = heap_extract_min (&out);
    while (allesJut) 
    {
        printf ("\n  %c <= %c\n", outOld, out);
        if (outOld <= out)
        {
            outOld = out;
            allesJut = heap_extract_min (&out);
        }
        else
        {
            printf ("\n\tFUCK\n");
            allesJut = FALSE;
        }
    }

}
コード例 #2
0
int main()
{

    heap_t *heap;
    node_t node;
    int key;
    
    heap = make_heap();

    freopen("input.txt","r",stdin);
     
    while((scanf("%d",&key))!= EOF){
        heap_insert(heap,key);
    }
    
    printf("after insert :\n");
    print_heap(heap);
 
    heap_extract_min(heap,&node);
    printf("min:%d\n",node.key);
    printf("after one extract min\n");
    print_heap(heap);

    return 0;
}
コード例 #3
0
ファイル: graph_ops.c プロジェクト: childhood/dsLib
/**
 * @brief Dijkstra's shortest path algorithm 
 *
 * @param[in] g The graph to operate on
 * @param[in] s The starting vertex
 * @param[in] cb The function to call when a shortest spath vertex is determined.  
 */
void sp_dijkstra (GRAPH_T* g, unsigned long s, SP_DJ_FP_T cb)
{
   HEAP_T* h;
   VTX_D_T* u = NULL;
   VTX_D_T* v;
   unsigned long key, no;
   char* ctx = NULL;
   EDGE_T* e;
   void* p;
   
   initialize_single_source (g, s);
   h = heap_create (DS_HEAP_MIN, GRAPH_NO_VERTICES(g));

   while (NULL != (u = graph_vertex_next_get (g, u)))
   {
      heap_min_insert (h, D_SP_AUX_SPEST(u), u, &D_SP_AUX_I(u));
   }

   while (HEAP_SIZE(h))
   {
      heap_extract_min (h, &p, &key);
      u = (VTX_D_T*)p;

      ctx = NULL;
      no = ((VTX_D_T*)u)->no;

      while (no)
      {
         e = graph_vertex_next_edge_get (g, u, &ctx);
         if (e->v1 == u)
            v = e->v2;
         else
            v = e->v1;
         if (v->id.iid == s)
         {
            no--;
            continue;
         }
         DEBUG_PRINT ("Relaxing v=%lu (OLD weight: %lu; NEW weight: u=%lu w=%lu)\n",
                 v->id.iid, D_SP_AUX_SPEST(v), u->id.iid, e->weight);
         relax (g, u, v, e->weight);
         heap_decrease_key (h, D_SP_AUX_I(v), D_SP_AUX_SPEST(v));
         no--;
      }
   }

   if (cb)
   {
      v = NULL;
      while (NULL != (v = graph_vertex_next_get (g, v)))
      {
         cb (v);
         //fprintf (stderr, "vid = %lu sp=%lu\n", v->id.iid, D_SP_AUX_SPEST(v));
      }
   }
}
コード例 #4
0
ファイル: huffman.c プロジェクト: joamaki/hcpak
struct hcnode * huffman(struct hcnode **nodes, size_t count)
{
	struct heap *heap;
	struct hcnode *z, *x, *y;
	struct hcnode *root;
	int i;

	heap = heap_build(nodes, count);
	for(i=0; i<count-1; i++) {
		z = xmalloc(sizeof(struct hcnode));
		x = z->left = heap_extract_min(heap);
		y = z->right = heap_extract_min(heap);
		z->frequency = x->frequency + y->frequency;
		x->parent = y->parent = z;
		z->parent = NULL;
		heap_insert(heap, z);
	}

	root = heap_extract_min(heap);
	heap_free(heap);
	return root;
}
コード例 #5
0
ファイル: unittest.c プロジェクト: joamaki/hcpak
void test_heap(void)
{
	struct heap *h;
	struct hcnode a, b, c, d, e;
	struct hcnode *nodes[3];
	d.frequency = 4;
	e.frequency = 5;
	b.frequency = 2;
	c.frequency = 3;
	a.frequency = 1;
	nodes[0] = &a; nodes[1] = &b; nodes[2] = &c;
	nodes[3] = &d; nodes[4] = &e;
	h = heap_build(nodes, 5);

	assert (1 == heap_extract_min(h)->frequency);
	assert (2 == heap_extract_min(h)->frequency);
	assert (3 == heap_extract_min(h)->frequency);
	assert (4 == heap_extract_min(h)->frequency);
	assert (5 == heap_extract_min(h)->frequency);

	heap_free(h);
}
コード例 #6
0
ファイル: main.c プロジェクト: steeb/studium-ppr
int main (void)
{
    char a;
    heap_init ();
    heap_insert ('0');
    heap_insert ('3');
    heap_insert ('2');
    heap_insert ('1');
    heap_insert ('4');
    heap_insert ('5');
    heap_insert ('6');
    heap_insert ('7');
    heap_insert ('8');
    heap_insert ('9');
    heap_print ();
    heap_extract_min (&a);
    do
    {
        printf ("\n--------------------\nremove: %c\n", a);
        heap_print ();
    } while (heap_extract_min (&a));
    heap_destroy ();
}
コード例 #7
0
ファイル: graph.c プロジェクト: evg-kazartseff/DSA
void ShortestPath_Dijekstra(struct graph *g, int src, int *d, int *prev)
{
    struct heap *prio_q;
    prio_q = heap_create(g->nvertices);
    
    int v;
    
    for (v = 1; v <= g->nvertices; v++) {
	if (v != src) {
	    g->visited[v] = 0;
	    d[v] = INT_MAX;
	    prev[v] = -1;
	    heap_insert(prio_q, d[v], v);
	}
    }
    
    d[src] = 0;
    prev[src] = -1;
    g->visited[src] = 0;
    heap_insert(prio_q, d[src], src);
    
    struct heapnode node;
    int tmp;
    for (v = 1; v <= g->nvertices; v++) {
	node = heap_extract_min(prio_q);
	tmp = node.value;
	g->visited[tmp] = 1;
	/*printf("vert %d\n\n", v);
	printf("min prio %d to vert %d\n", node.key, node.value);*/

	for (int u  = 1; u <= g->nvertices; u++) {
	    
	    int way = graph_get_edge(g, tmp, u);
	    if ((way != 0) && (g->visited[u] != 1)) {
		//printf("sm ne pos vert %d\nway %d\n", u, way);
		if (d[tmp] + way < d[u]) {
		    d[u] = d[tmp] + way;
		    //printf("path do %d = %d\n", u, d[u]);
		    heap_decrease_key(prio_q, u, d[u]);
		    prev[u] = tmp;
		}
	    }
	}
    }
}
コード例 #8
0
void main_loop(void)
{
        int n;
        struct timer *timer;
        int delay;

        while (num_pollfds > 1 || heap_len(timers) > 1
               || pollfds[0].events & POLLOUT) {
                if (heap_empty(timers)) {
                        timer = NULL;
                        delay = -1;
                } else {
                        timer = heap_peek(timers);
                        delay = (timer->time - get_now()) * 1000;
                }
                
                if (timer && delay <= 0)
                        n = 0;
                else
                        n = poll(pollfds, num_pollfds, delay);

                if (!n) {
                        timer = heap_extract_min(timers);
                        timer->func(timer->data);
                        free(timer);
                        goto cont;
                }

                for (int i = 0; i < num_pollfds && n; i++) {
                        if (pollfds[i].revents) {
                                event_handlers[i]->func(event_handlers[i]->data);
                                n--;

                                /* We may have just deleted this id.
                                 * Try it again in case it's a new
                                 * one. */
                                i--;
                        }
                }

        cont:
                maybe_dequeue();                
        }
}
コード例 #9
0
ファイル: graph.c プロジェクト: nphuc/alg
void dijkstra(long s){
  heap* pq=heap_init();
  elem* entries[n+1];
  edge result;
  memset(result.i,0,n+1);
  int i,ml[n+1];
  for(i=1;i<=n;++i){
    ml[i]=i;
    entries[i]=heap_insert(&pq,MAX,ml+i);
    //printf("%d %lld\n",*(int*)entries[i]->value,entries[i]->key);
  }
  heap_decrease_key(&pq,entries[s],0);
  //printf("%d %lld\n",*(int*)entries[s]->value,entries[s]->key);
  //printf("decrease ok\n");
  while(!is_empty(pq)){
    data curr=heap_extract_min(&pq);
    //print_data(curr);
    int cv=*(int*) curr.value;
    result.i[cv]=1;
    result.v[cv]=curr.key;
    for(i=1;i<=n;++i){
      if(has(result,i)) continue;
      if(!has(g.v[cv],i)){
        continue;
      }
      printf("path %d ->%d\n",cv,i);
      ll pathCost=index(result,i)+ g.v[cv].v[i];
      elem* dest=entries[i];
      printf("pC=%lld key=%lld\n",pathCost,dest->key);
      if( dest->key> pathCost){
        printf("desc key\n");
        heap_decrease_key(&pq,dest,pathCost);
      }
    }
  }
  for(i=1;i<=n;++i){
    res[s][i]=result.v[i];
  }
}
コード例 #10
0
ファイル: fiboHeap.c プロジェクト: greptruth/algo_lab
void  heap_delete(heap** H, elem* x){
    heap_decrease_key(H, x, INT_MIN);
    heap_extract_min(H);
}
コード例 #11
0
int main()
{
	freopen("input.txt","r",stdin);

	char str[500];
	scanf("%s",str);
	char visit[26];
	node* ptr[26];
	int i,l = strlen(str),j=1,k;

	for(i=0; i<26; i++)
		visit[i] = 0;

	/*for(i=0; i<l; i++)
		freq[str[i]-'A']++;*/

	heap min_queue;
	min_queue.length = 0;
	node *n1, *n2, *n;

	for(i=0; i<l; i++)
	{
		k = str[i]-'A';
		if(visit[k] == 0)
		{
			visit[k] = 1;
			ptr[k] = min_queue.arr[j++] = get_node(str[i], 1);//made changes here inplace of i use j for min_queue arr
			min_queue.length++;
		}
		else
		{
			ptr[k]->freq++;
		}
	}

	/*for(i=0; i<min_queue.length; i++)
		print_node(min_queue.arr[i+1]);
*/
	build_min_heap(&min_queue);

	/*for(i=0; i<min_queue.length; i++)
		print_node(min_queue.arr[i+1]);*/

	/*n1 = heap_extract_min(&min_queue);
		print_node(n1);
		n2 = heap_extract_min(&min_queue);
		print_node(n2);

		printf("ABCD\n");

	for(i=0; i<min_queue.heap_size; i++)
		print_node(min_queue.arr[i+1]);*/

	while(min_queue.heap_size >= 2)
	{
		n1 = heap_extract_min(&min_queue);
		print_node(n1);
		n2 = heap_extract_min(&min_queue);
		print_node(n2);
		n = get_node('0',0);
		n->freq = n1->freq + n2->freq;
		n->lchild = n1;
		n->rchild = n2;
		n1->isleft = 1;
		n2->isleft = 0;
		n1->parent = n2->parent = n;
		min_heap_insert(&min_queue,n);
	}

	printf("%d\n", count);
	n = heap_extract_min(&min_queue);
	postorder(n);
	printf("\n");
	inorder(n);
	printf("\n");
	for(i=0; i<l; i++)
	{
		huff_code(ptr[str[i]-'A']);
	}

		/*ENDDDDDDDDDDDDDDDDDD
	/*print_node(n);
	printf("\n");
	print_node(n->lchild);
	printf("\n");
	print_node(n->rchild);
	printf("\n");
	print_node(n->rchild->lchild);
	printf("\n");
	print_node(n->rchild->rchild);
	printf("\n");
	print_node(n->rchild->rchild->lchild);
	printf("\n");
	print_node(n->rchild->rchild->rchild);*/

	//Used for checking if Prority Queue is working fine
	/*while(min_queue.heap_size >= 1)
	{
		node* tmp = heap_extract_min(&min_queue);
		printf("%c-%d\n",tmp->ch, tmp->freq);
	}*/

	return 0;
}