Beispiel #1
0
static void simple_heap_test() {
  printf("Simple Heap\n");

  heap hp;
  heap_node mem[64];
  init_heap(&hp, mem, 64);

  char a = 'a', b = 'b', c = 'c', d = 'd';
  heap_insert(&hp, 1, &b);
  heap_insert(&hp, 100, &d);
  heap_insert(&hp, 0, &a);
  heap_insert(&hp, 5, &c);

  char ret = *(char*) heap_min_value(&hp);
  assert_equals(ret, 'a', "Simple Heap Min 1");

  ret = *(char*) heap_delete_min(&hp);
  assert_equals(ret, 'a', "Simple Heap Delete 1");

  ret = *(char*) heap_min_value(&hp);
  assert_equals(ret, 'b', "Simple Heap Min 2");

  ret = *(char*) heap_delete_min(&hp);
  assert_equals(ret, 'b', "Simple Heap Delete 2");

  ret = *(char*) heap_delete_min(&hp);
  assert_equals(ret, 'c', "Simple Heap Delete 3");

  ret = *(char*) heap_delete_min(&hp);
  assert_equals(ret, 'd', "Simple Heap Delete 4");
}
Beispiel #2
0
void heap_sort_max(Heap heap) {
  guint size = heap_size(heap);
  for (guint i = size; i > 1; i--) {
    heap[i] = heap_delete_min(heap);
  }
  heap_size(heap) = size;
}
Beispiel #3
0
int main() {
    heap_node* n[5];

    heap *ph = heap_new();
    n[4] = heap_insert(ph, 4);
    n[1] = heap_insert(ph, 1);
    n[0] = heap_insert(ph, 0);
    n[3] = heap_insert(ph, 3);
    n[2] = heap_insert(ph, 2);

    int i;
    for(i=0; i<5; i++) {
        n[i]->value.v_in_mst = -i;
        n[i]->value.v_not_in_mst = i;
    }

    heap_decrease_key(ph, n[3], -1);

    for(i=0; i<5; i++) {
        int v_not_in_mst = heap_min(ph);
        heap_value* val = &n[v_not_in_mst]->value;
        printf("%d %d %f\n", val->v_in_mst, v_not_in_mst, val->weight);
        heap_delete_min(ph);
    }

    return 0;
}
Beispiel #4
0
void dijkstra(graph* g, unsigned int source) {
	unsigned int u, v, edge_count;
	node *n, *d;
	edge *e;
	heap *Q;

	g->nodes[source].distance = 0;
	Q = heap_make(compare, g);

	while(!heap_is_empty(Q)) {
		u = heap_delete_min(Q);
		n = &g->nodes[u];
		edge_count = n->edge_count;
		for(v = 0; v < edge_count; v++) {	
			e = &n->edges[v];	
			d = &g->nodes[e->destination];
			if(d->distance > n->distance + e->weight) {
				/* 
					Relajo los vertices 
				*/			
				d->distance = n->distance + e->weight;
				/* 
					Actualizo el nodo con la distancia optima a este 
				*/
				d->previous = u;
				/* 
					Actualizo la cola de prioridad (el vertice solo puede 
				   haber subido en prioridad, entonces solo hago heapify-up 
				*/
				heap_heapify_up(Q, d->heap_index);				
			}
		}
	}
	heap_destroy(Q);
}
Beispiel #5
0
static void emptying_heap_test() {
  printf("Emptying Heap\n");

  heap hp;
  heap_node mem[64];
  init_heap(&hp, mem, 64);

  char a = 'a', b = 'b';
  heap_insert(&hp, 1, &a);

  char ret = *(char*) heap_delete_min(&hp);
  assert_equals(ret, 'a', "Emptying Heap Delete 1");

  heap_insert(&hp, 1, &b);
  ret = *(char*) heap_delete_min(&hp);
  assert_equals(ret, 'b', "Emptying Heap Delete 2");
}
Beispiel #6
0
static uint32 do_partition_core(graph_t *graph, 
				heap_t *heap, uint32 edge_cut,
				uint32 row_start, uint32 row_end,
				uint32 col_start, uint32 col_end) {

	uint32 i, j;
	uint32 min_edge_cut;
	uint32 num_history;
	uint32 history[MAX_HISTORY];

	for (i = 0; i < 2; i++) {
		uint32 vertex_lower = (i == 0) ? row_start : col_start;
		uint32 vertex_upper = (i == 0) ? row_end : col_end;

		for (j = vertex_lower; j <= vertex_upper; j++) {
			vertex_t *v = graph->vertex + j;
			if (v->visited) {
				v->visited = 0;
				heap_insert_vertex(graph, heap, j);
			}
		}
	}

	min_edge_cut = edge_cut;
	num_history = 0;

	while (1) {
		uint32 v_offset = heap_delete_min(graph, heap);
		if (v_offset == INVALID_INDEX)
			break;

		edge_cut = repartition_vertex(graph, heap, v_offset, edge_cut,
						row_start, row_end,
						col_start, col_end);

		if (edge_cut <= min_edge_cut) {
			num_history = 0;
			min_edge_cut = edge_cut;
		}
		else {
			history[num_history] = v_offset;
			if (++num_history == MAX_HISTORY)
				break;
		}
	}

	for (i = num_history - 1; (int32)i >= 0; i--) {
		edge_cut = repartition_vertex(graph, heap, 
					history[i], edge_cut,
					row_start, row_end,
					col_start, col_end);
	}

	return min_edge_cut;
}
Beispiel #7
0
static void same_priority_test() {
  printf("Same Priority\n");

  char a = 'a', b = 'b', c = 'c';

  heap hp;
  heap_node mem[64];
  init_heap(&hp, mem, 64);

  heap_insert(&hp, 1, &a);
  heap_insert(&hp, 1, &b);
  heap_insert(&hp, 3, &c);

  char ret1 = *(char*) heap_delete_min(&hp);
  char ret2 = *(char*) heap_delete_min(&hp);

  assert_true((ret1 == 'a' && ret2 == 'b') || (ret1 == 'b' && ret2 == 'a'), "Same Priority Delete 1");

  char ret = *(char*) heap_delete_min(&hp);
  assert_equals(ret, 'c', "Same Priority Delete 2");
}
Beispiel #8
0
int main()
{
  heap_t heap = heap_init(10, is_smaller2);

  printf("Testing empty heap...\n");
  assert_verbose(heap_is_empty(heap) == true);
  
  printf("Adding an item...\n");
  heap_insert(heap, "charlie");
  
  printf("Adding an item...\n");
  heap_insert(heap, "alpha");
  
  printf("Adding an item...\n");
  heap_insert(heap, "bravo");
  
  printf("Testing non-empty heap...\n");
  assert_verbose(heap_is_empty(heap) == false);
  
  printf("Testing output...\n");
  assert_verbose(strcmp(heap_delete_min(heap), "alpha") == 0);
  assert_verbose(strcmp(heap_delete_min(heap), "bravo") == 0);
  assert_verbose(strcmp(heap_delete_min(heap), "charlie") == 0);
  
  printf("Testing empty heap...\n");
  assert_verbose(heap_is_empty(heap) == true);
  
  heap_free(heap);
  
  heap = heap_init(10, is_smaller);
  for(int i = 30; i > 10; i--)
  {
    heap_insert(heap, &i);
  }
  assert_verbose(*((int *)heap_delete_min(heap)) == 10);
  heap_free(heap);
  printf("Passed all tests\n");
}
Beispiel #9
0
static void easy_heap_test() {
  printf("Easy Heap\n");

  heap hp;
  heap_node mem[64];
  init_heap(&hp, mem, 64);

  char f = 'f', g = 'g', h = 'h', i = 'i';
  heap_insert(&hp, 12, &i);
  heap_insert(&hp, 7, &h);

  char ret = *(char*) heap_delete_min(&hp);
  assert_equals(ret, 'h', "Easy Heap Delete 1");

  heap_insert(&hp, 1, &f);
  heap_insert(&hp, 2, &g);

  ret = *(char*) heap_delete_min(&hp);
  assert_equals(ret, 'f', "Easy Heap Delete 2");
  ret = *(char*) heap_delete_min(&hp);
  assert_equals(ret, 'g', "Easy Heap Delete 3");
  ret = *(char*) heap_delete_min(&hp);
  assert_equals(ret, 'i', "Easy Heap Delete 4");
}