Beispiel #1
0
void testcorrectnessveb(){
	int itr = 100000;
	int MAX = pow(2, 24);
	vebtree * vebt = veb_initialize(24, 64);
	binary_heap * bheap = bh_init_heap(MAX);
	FibHeap * fheap = fib_make_heap();
	
	uint8_t * arr = calloc(MAX, sizeof(uint8_t));
	int i;
	if (arr == NULL){	
		printf("dang... could not allocate enough memory\n");
		exit(1);
	}
	bh_element * e;
	for (i = 0; i < itr; i++){
		uint32_t s = random() % MAX;
		while(arr[s])
			s = random() % MAX;
		arr[s] = 1;
		veb_insert(s, NULL, vebt);
		bh_insert(s, NULL, bheap);
		fib_insert(s, NULL, fheap);
	}
	uint32_t v, b, f;
	FibNode * fn;
	linked_list * llveb = veb_prio_walk(vebt);
	linked_list_node * nveb = llveb->first;
	for (i = 0; i < itr; i++){
		v = vebt->min->value;
		veb_delete_min(vebt);
		e = bh_delete_min(bheap);
		b = e->key;
		free(e);
		fn = fib_find_min(fheap);
		f = fn->key;
		fib_delete_min(fheap);
		free(fn);
		if (b != v || b != f || v !=f || nveb->data != b){
			printf("one of the datastructures was not correct\n");
			printf("vEB: %d, bin: %d, fib: %d, veb walk: %d\n", v, b, f, nveb->data);
			exit(-1);
		}
		nveb = nveb->next;
	}
	printf("all data structures agree, so they can be assumed correct - %d -\n", bheap->size);
	free(arr);
	veb_destruct(vebt);
	bh_destruct(bheap);
	free(fheap);
}
Beispiel #2
0
/*
 * bh_try_insert () - insert an element into the heap if heap hasn't reached
 *		      the full capacity or if new element is smaller than the
 *		      top element
 * return : old root element if it was replaced
 * heap (in) : heap
 * elem (in) : new element
 */
BH_ELEM
bh_try_insert (BINARY_HEAP * heap, BH_ELEM elem)
{
  if (heap->element_count < heap->max_capacity)
    {
      bh_insert (heap, elem);
      return NULL;
    }
  /* if root is larger than new element, replace root */
  if (heap->cmp_func (heap->members[0], elem, heap->cmp_arg) == DB_GT)
    {
      return bh_replace_max (heap, elem);
    }
  return elem;
}
Beispiel #3
0
void plot_rand_sort_bin(int num_vertices, int thres, FILE *gnuplot_ins, FILE *gnuplot_dm, FILE *gnuplot_total){
	srandom(97643);
	printf("BinHeap: %d elements\n",num_vertices);
	int MAX = pow(2, 24);
	
	struct timespec delmin, ins, start, end;
	delmin.tv_nsec = 0;
	delmin.tv_sec = 0;
	ins.tv_nsec = 0;
	ins.tv_sec = 0;
	
	binary_heap * bheap = bh_init_heap(MAX);
	
	int i;
	uint8_t * arr = calloc(MAX, sizeof(uint8_t));
	if (arr == NULL){
		printf("dang...could not allocate enough memory\n");
		exit(1);
	}
	bh_element *e;
	for (i = 0; i < num_vertices; i++){
		uint32_t s = random() % MAX;
		while(arr[s])
			s = random() % MAX;
		arr[s] = 1;
		
		clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &start);
		bh_insert(s, NULL, bheap);
		clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &end);
		increment(&ins, &start, &end);
	}
	for (i = 0; i < num_vertices; i++){
		clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &start);
		e= bh_delete_min(bheap);
		clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &end);
		increment(&delmin, &start, &end);
		free(e);
	}
	free(arr);
	bh_destruct(bheap);
	if(gnuplot_ins)
		fprintf(gnuplot_ins, "%d %ld\n", num_vertices, ((ins.tv_sec*  1000000000)+(ins.tv_nsec))/num_vertices);
	if(gnuplot_dm)
		fprintf(gnuplot_dm, "%d %ld\n", num_vertices, ((delmin.tv_sec*1000000000)+(delmin.tv_nsec))/num_vertices);
	if(gnuplot_total)
		fprintf(gnuplot_total, "%d %ld\n", num_vertices, (((ins.tv_sec + delmin.tv_sec) *  1000)+(ins.tv_nsec + delmin.tv_nsec)/1000000));	
}
Beispiel #4
0
void testcorrectnessvebpq(){
	int itr = 100000;
	int MAX = pow(2, 24);
	vebtree * vebt = veb_pq_init(24);
	binary_heap * bheap = bh_init_heap(MAX);
	FibHeap * fheap = fib_make_heap();
	
	int i;
	veb_pq_node * n;
	bh_element * e;
	for (i = 0; i < itr; i++){
		uint32_t s = random() % MAX;
		veb_pq_node * n = malloc(sizeof(veb_pq_node));
		n->node_prio = s;
		veb_pq_insert(n, vebt);
		bh_insert(s, NULL, bheap);
		fib_insert(s, NULL, fheap);
	}
	uint32_t v, b, f;
	FibNode * fn;
	for (i = 0; i < itr; i++){
		v = vebt->min->value;
		n = veb_pq_deletemin(vebt);;
		free(n);
		e = bh_delete_min(bheap);
		b = e->key;
		free(e);
		fn = fib_find_min(fheap);
		f = fn->key;
		fib_delete_min(fheap);
		free(fn);
		if (b != v || b != f || v !=f){
			printf("one of the datastructures was not correct\n");
			printf("vEB: %d, bin: %d\n", v, b);
			exit(-1);
		}
	}
	printf("all data structures agree, so they can be assumed correct\n");
	veb_destruct(vebt);
	bh_destruct(bheap);
	free(fheap);
}
Beispiel #5
0
/*
 * NAME:	fork->binh()
 * DESCRIPTION:	copy a single fork for BinHex
 */
static
int fork_binh(hfsfile *ifile, unsigned long size)
{
  char buf[HFS_BLOCKSZ * 4];
  long bytes;
  unsigned long total = 0;

  while (1)
    {
      bytes = hfs_read(ifile, buf, sizeof(buf));
      if (bytes == -1)
	{
	  ERROR(errno, hfs_error);
	  return -1;
	}
      else if (bytes == 0)
	break;

      if (bh_insert(buf, bytes) == -1)
	{
	  ERROR(errno, bh_error);
	  return -1;
	}

      total += bytes;
    }

  if (total != size)
    {
      ERROR(EIO, "inconsistent fork length");
      return -1;
    }

  if (bh_insertcrc() == -1)
    {
      ERROR(errno, bh_error);
      return -1;
    }

  return 0;
}
Beispiel #6
0
/*
 * NAME:	binhx()
 * DESCRIPTION:	auxiliary BinHex routine
 */
static
int binhx(hfsfile *ifile)
{
  hfsdirent ent;
  unsigned char byte, word[2], lword[4];

  if (hfs_fstat(ifile, &ent) == -1)
    {
      ERROR(errno, hfs_error);
      return -1;
    }

  byte = strlen(ent.name);
  if (bh_insert(&byte, 1) == -1 ||
      bh_insert(ent.name, byte + 1) == -1 ||
      bh_insert(ent.u.file.type, 4) == -1 ||
      bh_insert(ent.u.file.creator, 4) == -1)
    {
      ERROR(errno, bh_error);
      return -1;
    }

  d_putsw(word, ent.fdflags);
  if (bh_insert(word, 2) == -1)
    {
      ERROR(errno, bh_error);
      return -1;
    }

  d_putul(lword, ent.u.file.dsize);
  if (bh_insert(lword, 4) == -1)
    {
      ERROR(errno, bh_error);
      return -1;
    }

  d_putul(lword, ent.u.file.rsize);
  if (bh_insert(lword, 4) == -1 ||
      bh_insertcrc() == -1)
    {
      ERROR(errno, bh_error);
      return -1;
    }

  if (hfs_setfork(ifile, 0) == -1)
    {
      ERROR(errno, hfs_error);
      return -1;
    }

  if (fork_binh(ifile, ent.u.file.dsize) == -1)
    return -1;

  if (hfs_setfork(ifile, 1) == -1)
    {
      ERROR(errno, hfs_error);
      return -1;
    }

  if (fork_binh(ifile, ent.u.file.rsize) == -1)
    return -1;

  return 0;
}
Beispiel #7
0
void plot_dkmax2_bin(uint32_t num_vertices, uint32_t source, FILE *gnuplot_ins, FILE *gnuplot_dm, FILE *gnuplot_total, FILE *gnuplot_dk){
	printf("BinHeap: %d vertices\n",num_vertices);
	uint32_t **w2 = malloc(sizeof(uint32_t *));
	uint32_t *weights;
	uint32_t **edges = generate_instance(num_vertices, w2);
	weights = *w2;
	struct timespec delmin, deck, ins, start, end;
	delmin.tv_nsec = 0;
	delmin.tv_sec = 0;
	deck.tv_nsec = 0;
	deck.tv_sec = 0;
	ins.tv_nsec = 0;
	ins.tv_sec = 0;
	
	binary_heap * heap = bh_init_heap(num_vertices);
	
	uint32_t *distances = malloc(num_vertices * sizeof(uint32_t));
	bh_element ** vertices = malloc(num_vertices * sizeof(bh_element *));
	
	uint32_t distance;
	uint32_t *data;
	uint32_t i;
	for (i = 0; i < num_vertices; i++) {
		if(i == source)
			distance = 0;
		else
			distance = UINT_MAX;
		distances[i] = distance;
		data = malloc(sizeof(uint32_t));
		*data = i;
		clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &start);
		vertices[i] = bh_insert(distance, data, heap);
		clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &end);
		increment(&ins, &start, &end);
		
	}
	bh_element *node;
	uint32_t decrease_key_calls = 0;
	
	clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &start);
	node = bh_delete_min(heap);
	clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &end);
	increment(&delmin, &start, &end);
	while (node) {
		uint32_t u = *((uint32_t *)node->data);
		for (i = 1; i <= edges[u][0]; i++) {
			uint32_t v = edges[u][i];
			uint32_t alt = distances[u] + weights[u * num_vertices + v];
			if (alt < distances[v]) {
				clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &start);
				bh_decrease_key(distances[v] - alt, vertices[v], heap);
				clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &end);
				increment(&deck, &start, &end);
				distances[v] = alt;
				decrease_key_calls++;
			}
		}
		free(node->data);
		free(node);
		clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &start);
		node = bh_delete_min(heap);
		clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &end);
		increment(&delmin, &start, &end);
	}
	for (i = 0; i < num_vertices; i++)
		free(edges[i]);
	free(edges);
	bh_destruct(heap);
	free(vertices);
	free(distances);
	free(w2);
	free(weights);
	if(gnuplot_ins)
		fprintf(gnuplot_ins, "%d %ld\n", num_vertices, ((ins.tv_sec*  1000000000)+(ins.tv_nsec))/num_vertices);
	if(gnuplot_dm)
		fprintf(gnuplot_dm, "%d %ld\n", num_vertices, ((delmin.tv_sec*1000000000)+(delmin.tv_nsec))/num_vertices);
	if(gnuplot_dk)
		fprintf(gnuplot_dk, "%d %ld\n", num_vertices, ((deck.tv_sec*  1000000000)+(deck.tv_nsec))/decrease_key_calls);
	if(gnuplot_total)
		fprintf(gnuplot_total, "%d %ld\n", num_vertices, (((ins.tv_sec + delmin.tv_sec + deck.tv_sec) *  1000)+(ins.tv_nsec + delmin.tv_nsec + deck.tv_nsec)/1000000));	
}
Beispiel #8
0
void time_bin_dijkstra(uint32_t num_vertices, uint32_t source, uint32_t * weights, uint32_t ** edges){
	clock_t start, end;
	double binit = 0;
	double bdm = 0;
	double bdk = 0;
	double bins = 0;
	
	start = clock();
	binary_heap * heap = bh_init_heap(num_vertices);
	end = clock();
	binit = ((double) (end-start) / CLOCKS_PER_SEC) * 1000;

	
	uint32_t *distances = malloc(num_vertices * sizeof(uint32_t));
	bh_element ** vertices = malloc(num_vertices * sizeof(bh_element *));
	
	uint32_t distance;
	uint32_t *data;
	uint32_t i;
	for (i = 0; i < num_vertices; i++) {
		if(i == source)
			distance = 0;
		else
			distance = UINT_MAX;
		distances[i] = distance;
		data = malloc(sizeof(uint32_t));
		*data = i;
		start = clock();
		vertices[i] = bh_insert(distance, data, heap);
		end = clock();
		bins += ((double) (end-start) / CLOCKS_PER_SEC) * 1000;
		
	}
	bh_element *node;
	uint32_t decrease_key_calls = 0;
	
	start = clock();
	node = bh_delete_min(heap);
	end = clock();
	bdm += ((double) (end-start) / CLOCKS_PER_SEC) * 1000;
	while (node) {
		uint32_t u = *((uint32_t *)node->data);
		for (i = 1; i <= edges[u][0]; i++) {
			uint32_t v = edges[u][i];
			uint32_t alt = distances[u] + weights[u * num_vertices + v];
			if (alt < distances[v]) {
				start = clock();
				bh_decrease_key(distances[v] - alt, vertices[v], heap);
				end = clock();
				bdk += ((double) (end-start) / CLOCKS_PER_SEC) * 1000;
				distances[v] = alt;
				decrease_key_calls++;
			}
		}
		free(node->data);
		free(node);
		start = clock();
		node = bh_delete_min(heap);
		end = clock();
		bdm += ((double) (end-start) / CLOCKS_PER_SEC) * 1000;
	}
	bh_destruct(heap);
	free(vertices);
	free(distances);
	printf("bin: init: %f - total time: %f\n", binit, binit+bdm+bins+bdk);
	printf("     insert: %f (avg: %f)\n", bins, bins/num_vertices);
	printf("     delmin: %f (avg: %f)\n", bdm, bdm/num_vertices);
	printf("     dec.ke: %f (avg: %f)\n\n", bdk, bdk/decrease_key_calls);
}
Beispiel #9
0
void testVEBperformance_random_sort(int itr, int thres){
	int MAX = pow(2, 24);
	double vinit, binit, finit, vins, bins, fins, vdm, bdm, fdm;
	finit = 0;
	fdm = 0;
	
	clock_t start = clock();
	vebtree * vebt = veb_initialize(24, thres);
	clock_t end = clock();
	vinit = ((double) (end-start) / CLOCKS_PER_SEC) * 1000;
	
	start = clock();
	binary_heap * bheap = bh_init_heap(MAX);
	end = clock();
	binit = ((double) (end-start) / CLOCKS_PER_SEC) * 1000;
	
	FibHeap * fheap = NULL;	
	if (itr < 1500000){
		start = clock();
		fheap = fib_make_heap();
		end = clock();
		finit = ((double) (end-start) / CLOCKS_PER_SEC) * 1000;
	}
	
	printf("vEB init: %f ms - bhinit: %f ms - fibinit: %f ms\n", vinit, binit, finit);
	int i;
	uint8_t * arr = calloc(MAX, sizeof(uint8_t));
	if (arr == NULL){
		printf("dang...could not allocate enough memory\n");
		exit(1);
	}
	vins = 0;
	bins = 0;
	fins = 0;
	vdm = 0;
	bdm = 0;
	fdm = 0;
	bh_element *e;
	FibNode * fn;
	for (i = 0; i < itr; i++){
		uint32_t s = random() % MAX;
		while(arr[s])
			s = random() % MAX;
		arr[s] = 1;
		
		start = clock();
		veb_insert(s, NULL, vebt);
		end = clock();
		vins += ((double) (end-start) / CLOCKS_PER_SEC) * 1000;
		
		start = clock();
		bh_insert(s, NULL, bheap);
		end = clock();
		bins += ((double) (end-start) / CLOCKS_PER_SEC) * 1000;
		if (itr < 1500000){
			start = clock();
			fib_insert(s, NULL, fheap);
			end = clock();
			fins += ((double) (end-start) / CLOCKS_PER_SEC) * 1000;
		}
	}
	printf("spend time inserting: vEB: %f ms (avg %f) - BH: %f ms (avg %f) - fib: %f ms (avg %f)\n", vins, vins/itr, bins, bins/itr, fins, fins/itr);
	//printf("avg: vEB %f ms - BH: %f ms\n", vins/itr, bins/itr);
	uint32_t v, b, f;
	for (i = 0; i < itr; i++){
		
		start = clock();
		v = vebt->min->value;
		veb_delete_min(vebt);
		end = clock();
		vdm += ((double) (end-start) / CLOCKS_PER_SEC) * 1000;
		
		start = clock();
		e= bh_delete_min(bheap);
		end = clock();
		b = e->key;
		free(e);
		bdm += ((double) (end-start) / CLOCKS_PER_SEC) * 1000;
		fn = fib_find_min(fheap);
		f = fn->key;
		
		if (itr < 1500000){
			start = clock();
			fib_delete_min(fheap);
			end = clock();
			fdm += ((double) (end-start) / CLOCKS_PER_SEC) * 1000;
			free(fn);
		}
	}
	printf("spend time deletemin: vEB: %f ms (avg %f) - BH: %f ms (avg %f)", vdm, vdm/itr, bdm, bdm/itr);
	if (itr < 1500000)
		printf(" - fib: %f ms (avg %f)\n", fdm, fdm/itr);
	else
		printf("\n");
	free(arr);
	veb_destruct(vebt);
	bh_destruct(bheap);
	if (itr < 1500000)
		free(fheap);
}
Beispiel #10
0
void testPQperformance_random(int itr){
	int MAX = pow(2, 24);
	double vinit, binit, finit, vins, bins, fins, vdm, bdm, fdm;
	
	clock_t start = clock();
	vebtree * vebt = veb_pq_init(24);
	clock_t end = clock();
	vinit = ((double) (end-start) / CLOCKS_PER_SEC) * 1000;
	
	start = clock();
	binary_heap * bheap = bh_init_heap(itr);
	end = clock();
	binit = ((double) (end-start) / CLOCKS_PER_SEC) * 1000;
	
	start = clock();
	FibHeap * fheap = fib_make_heap();
	end = clock();
	finit = ((double) (end-start) / CLOCKS_PER_SEC) * 1000;
	
	printf("vEB init: %f ms - bhinit: %f ms - fibinit: %f ms\n", vinit, binit, finit);
	int i;
	vins = 0;
	bins = 0;
	fins = 0;
	vdm = 0;
	bdm = 0;
	fdm = 0;
	veb_pq_node * n;
	bh_element * e;
	FibNode * fn;
	for (i = 0; i < itr; i++){
		uint32_t s = random() % MAX;
		veb_pq_node * n = malloc(sizeof(veb_pq_node));
		n->node_prio = s;
		n->node_nr = i;
		
		start = clock();
		veb_pq_insert(n, vebt);
		end = clock();
		vins += ((double) (end-start) / CLOCKS_PER_SEC) * 1000;
		
		start = clock();
		bh_insert(s, NULL, bheap);
		end = clock();
		bins += ((double) (end-start) / CLOCKS_PER_SEC) * 1000;
		
		start = clock();
		fib_insert(s, NULL, fheap);
		end = clock();
		fins += ((double) (end-start) / CLOCKS_PER_SEC) * 1000;
	}
	printf("spend time inserting: vEB: %f ms (avg %f) - BH: %f ms (avg %f) - fib: %f ms (avg %f)\n", vins, vins/itr, bins, bins/itr, fins, fins/itr);
	//printf("avg: vEB %f ms - BH: %f ms\n", vins/itr, bins/itr);
	uint32_t v, b, f;
	for (i = 0; i < itr; i++){
		v = vebt->min->value;
		
		start = clock();
		n = veb_pq_deletemin(vebt);;
		end = clock();
		free(n);
		vdm += ((double) (end-start) / CLOCKS_PER_SEC) * 1000;
		
		start = clock();
		e = bh_delete_min(bheap);
		end = clock();
		b = e->key;
		free(e);
		bdm += ((double) (end-start) / CLOCKS_PER_SEC) * 1000;
		fn = fib_find_min(fheap);
		f = fn->key;
		
		start = clock();
		fib_delete_min(fheap);
		end = clock();
		fdm += ((double) (end-start) / CLOCKS_PER_SEC) * 1000;
		free(fn);
		if (b != v || b != f || v != f){
			printf("vEB: %d, bin: %d\n", v, b);
			exit(-1);
		}
	}
	printf("spend time deletemin: vEB: %f ms (avg %f) - BH: %f ms (avg %f) - fib: %f ms (avg %f)\n", vdm, vdm/itr, bdm, bdm/itr, fdm, fdm/itr);
	//printf("avg: vEB %f ms - BH: %f ms\n", vdm/itr, bdm/itr);
	veb_destruct(vebt);
	bh_destruct(bheap);
	free(fheap);
}