Example of Huffman Algorithm by using Heap

Example of Huffman algorithm by using heap.

NOTIFICATION: These examples are provided for educational purposes. Using this code is under your own responsibility and risk. The code is given ‘as is’. I do not take responsibilities of how they are used.

huffman.c:

/* 
 * Program: Huffman using heap
 * Author: Alejandro G. Carlstein
 */

#include <stdio.h>
#include <stdlib.h>
#include <limits.h>

#define MAX_CODE 1000000
#define MAX_LETTERS 4
#define MAX_HEAP 10
#define LEFT(i) ((i << 1) + 1)
#define RIGHT(i) ((i << 1) + 2)
#define PARENT(i) (((i + 1) >> 1) - 1)
#define DIV_BY_2(i) (i >> 1)
#define DBG_LV0 1
#define DBG_LV1 1
#define DBG_LV2 1
#define DBG_LV3 0
#define LETTER_A 65
#define DIR_LEFT '0'
#define DIR_RIGHT '1'

#define ROOT_INDEX 0

struct Data{
  int letter;
  int frequency;
  int left;
  int right;
  int parent;
}Data;

struct Data data[MAX_LETTERS];
int code[MAX_CODE][MAX_LETTERS];
int heap[MAX_HEAP];

void readInputLetters(void);
void readInputCode(void);
void printCodes(int *codes, int numCodes);
void printStructArray(struct Data array[], int length);
int huffman(void);
void buildMinHeap(int array[], int heapSize);
void minHeapify(int array[], int index, int heapSize);
int heapMin(int array[]);
int heapExtractMin(int array[], int *length);
void insertMinHeap(int array[], int value, int *length);
void exchange(int *a, int *b);
void printArray(int array[], int length);
int isLeft(struct Data array[], int indexLeft, int indexRight);
void test1();
void test2();

int num_letters;
int numCodes = 0;

int main(int argc, char* argv[]){

	readInputLetters();		

	readInputCode();

	if (DBG_LV3) printStructArray(data, num_letters);

	int i;
	for (i = 0; i < num_letters; ++i)
		heap[i] = i;

  printf('ROOT(huffman): %d \n', huffman());

	if (DBG_LV1) printStructArray(data, num_letters);

  //decodeMessage(data, num_letters, &code[0][0], numCodes);

	if (DBG_LV2) printf('\n');

	return 0;

}

void readInputLetters(void){
	if (DBG_LV0) printf('\nreadInputLetters()\n');

  for (num_letters = 0; num_letters < MAX_LETTERS; ++num_letters){       
    scanf('%d', &data[num_letters].frequency);
    data[num_letters].letter = LETTER_A + num_letters;
    data[num_letters].left = -1;
    data[num_letters].right = -1;
		data[num_letters].parent = -1;

    heap[num_letters] = -1;
  }

}

void readInputCode(void){
	if (DBG_LV0) printf('\nreadInputCode()\n');

	int c;
  int indexCode = 0;

  numCodes = 0;

  while ((c = getchar()) != EOF){

    if (c != ' ' && c != '\n'){
      if (DBG_LV3) printf('[%d][%d]%c{%d}\n', numCodes, indexCode, c, c);
      code[numCodes][indexCode] = (int)c;
      ++indexCode;
    }

    if (c == '\n'){
      code[numCodes][indexCode] = -1;
      if (DBG_LV3) printf('{{%d}}\n', code[numCodes][indexCode]);
      ++numCodes;
      indexCode = 0;

    }
  }

	if (DBG_LV3){
	  printf('CODES: \n');
	  printCodes(&code[0][0], numCodes);

  }

}

void printCodes(int *codes, int numCodes){
  if (DBG_LV2) printf('printCodes(numCodes: %d)\n', numCodes);

  int indexCode, index;
	int indexArray;

  for (indexCode = 0; indexCode < numCodes; ++indexCode){

		indexArray = indexCode * sizeof(int);

    for (index = 0; codes[indexArray + index] > -1 ; ++index){

      printf('[%d][%d]: %c(%d) \n', 
             indexArray, index, 
             codes[indexArray + index], codes[indexArray + index]);    

    }

    printf('\n');

  }

}

void printStructArray(struct Data array[], int length){
  if (DBG_LV0) printf('printStructArray()\n');

  int i;
  for (i = 0; i < length; ++i)
	  printf('[%d]%c - %d (L:%d, R:%d, P:%d) \n',
	         i, data[i].letter, data[i].frequency, data[i].left, data[i].right, data[i].parent);
}

int huffman(void){
	if (DBG_LV0) printf('\nHUFFMAN()\n');

	int length = num_letters;
	int i;
	int left;
	int right;
	int parent;
	int n = length;

	length++;

	printf('length: %d\n\n', length);

	if (DBG_LV1) printArray(heap, num_letters);

	for (i = 0; i < n - 1; ++i){

		left = heapExtractMin(heap, &length);

		printf('length: %d\n\n', length);

		right = heapExtractMin(heap, &length);

		parent = left + right;

		data[left].parent = parent;
		data[right].parent = parent;

		printf('length: %d\n\n', length);

		if (DBG_LV2){
			printf('left: %d, ', left);				
			printf('right: %d, ', right);
			printf('parent: %d\n', parent);
			printArray(heap, length);		
		}

		insertMinHeap(heap, parent, &length);

		printf('length: %d\n\n', length);
		//--*length;

	}

	if (DBG_LV1) printArray(heap, num_letters);

	return heapExtractMin(heap, &length);
}

void buildMinHeap(int array[], int heapSize){

	int index;

	for (index = DIV_BY_2(heapSize); index >= ROOT_INDEX; --index){

		minHeapify(array, index, heapSize);

	}

}

void minHeapify(int array[], int index, int heapSize){

	int left, right, smallest;

	smallest = index;
	left = LEFT(index);
	right = RIGHT(index);	

	// Find smallest value
	if (left < heapSize && array[left] < array[index])
		smallest = left;	

	if (right < heapSize && array[right] < array[smallest]) 
		smallest = right;

	if (smallest != index){

		// Exchange 
		exchange(&array[index], &array[smallest]);

		// Rebuild heap region		
		minHeapify(array, smallest, heapSize);	

	}

}

int heapMin(int array[]){
	return array[ROOT_INDEX];
}

int heapExtractMin(int array[], int *length){
	if (DBG_LV0) printf('heapExtractMin()\n');

	if (length < 0){
		printf('[X] Error: heap overflow!\n');
		return -1;
	}

	--*length;

	int heapSize = *length;

	int min = array[ROOT_INDEX];

	--heapSize;

	printf('exchange: array[%d]: %d, array[%d]:%d \n',
				 ROOT_INDEX, array[ROOT_INDEX],
				 heapSize, array[heapSize]);

	exchange(&array[ROOT_INDEX], &array[heapSize]);

	--heapSize;

	minHeapify(array, ROOT_INDEX, heapSize);

	return min;
}

void insertMinHeap(int array[], int value, int *length){

	if (DBG_LV0) printf('insertMinHeap(value: %d, length: %d)\n',
											value, *length);

	int heapSize = *length;

	++*length;

	array[heapSize] = INT_MAX;

	if (value > array[heapSize]){
		printf('[X] Error: new value is bigger than biggest element!\n');
	}else{

		array[heapSize] = value;

		if (DBG_LV2) printArray(array, *length);

		while (heapSize > ROOT_INDEX && 
					 array[PARENT(heapSize)] > array[heapSize]){

			exchange(&array[heapSize], &array[PARENT(heapSize)]);

			heapSize = PARENT(heapSize);

		}

	}

}

void exchange(int *a, int *b){
		if (DBG_LV3) printf('exchange()\n');

		int temp;
		temp = *a;
		*a = *b;
		*b = temp;
}

void printArray(int array[], int length){
	if (DBG_LV0) printf('printArray()\n');

	int i;

	for (i = 0; i < length; ++i)
		printf('[%d]', i);

	printf('\n');

	for (i = 0; i < length; ++i)
		printf(' %d ', array[i]);

	if (DBG_LV1) printf('\n\n');
}

int isLeft(struct Data array[], int indexLeft, int indexRight){
	if (DBG_LV0) printf('isLeft()\n');

	if (array[indexLeft].frequency == array[indexRight].frequency){

		 	if (array[indexLeft].letter < array[indexRight].letter){
		 		return DIR_LEFT;
		 	}else{
		 		return DIR_RIGHT;
		 	}

	}else if (array[indexLeft].frequency < array[indexRight].frequency){
		return DIR_LEFT;	 	
	}else{
		return DIR_RIGHT;
	} 

}

void test1(){
	if (DBG_LV1) printf('test1()\n');

	int i, length = 5;

	if(DBG_LV1)	printf('BUILD HEAP ARRAY:\n');
	for(i = 0; i < length; ++i){
		heap[i] = i * 2;
		printf('heap[%d]: %d \n', i, heap[i]);
	}
	heap[4] = 1;

	buildMinHeap(heap, length);

	printArray(heap, length);

	insertMinHeap(heap, 3, &length);

	printArray(heap, length);
}

void test2(){

	int heapLen = num_letters;

	printf('heapLen: %d\n', heapLen);

	printArray(heap, heapLen);

	int result1, result2;

	printf('TEST HEAPEXTRACTMIN:\n');
	result1 = heapExtractMin(heap, &heapLen);
  printf('result1: %d\n', result1);
	printArray(heap, heapLen);

	printf('heapLen: %d\n', heapLen);

	result2 = heapExtractMin(heap, &heapLen);
  printf('result2: %d\n', result2);
	printArray(heap, heapLen);

	printf('heapLen: %d\n', heapLen);

}

input.txt:

Share

Multi-player 2D Game Introduction

Today, I am writing the introduction of the multi-player 2D game. This is the version 06

Long time ago…
Our civilization kept growing…
Greedy ruled the world…
Resources started lacking…
And a war for them began…
At the end, the world was destroyed…
A wasteland was instead…
Life was at the border of extinction…
However, some how…
Life managed to survive…
Now, in a new world….
Were previous damage still remain …
This world shall be explored again…
A new cycle has just began…
Sound file:[
.mp3 ]
Share

Example of Ferry Loading Algorithm

Example of Ferry Loading algorithm.

NOTIFICATION: These examples are provided for educational purposes. Using this code is under your own responsibility and risk. The code is given ‘as is’. I do not take responsibilities of how they are used.

ferry_loading.c:

/**
 * Ferry Loading Algorithm
 * @author: Alejandro G. Carlstein R. M. 
 */
#include <stdio.h>

/* Macros Functions */
#define max(a,b) \
           ({ typeof (a) _a = (a); \
              typeof (b) _b = (b); \
            _a > _b ? _a : _b; })

/* Debugging flags */
#define DBG_LV1 0
#define DBG_LV2 1
#define DBG_LV3 0

/* Constants */
#define MAX_NUM_CAR 100
#define MAX_FERRY_LEN 100

enum{ UNKNOWN = -1, LEFT = 0, RIGHT = 1,
};

/* Our dynamic programming table element.  We have the value
 * that we can store, and also a way to remember what we selected.
 */
typedef struct{
  int length;
  int prev_row;
  int prev_col;
} Table;

int cars[MAX_NUM_CAR];
int side[MAX_NUM_CAR];
int i, j;
int ferry_len;
int total_cars;

// [ROW] [COLUMN]
Table ferry[MAX_FERRY_LEN][MAX_FERRY_LEN];

/*
 *
 */
int main(int argc, char *argv[]){

  /* Read the length of the ferry */
  scanf('%d', &ferry_len);

  // Read length of each car and set side to UNKNOWN since
  // we haven't decided yet where they will go
  cars[0] = 0;
  side[0] = UNKNOWN;
  for (i = 1; scanf('%d', &cars[i]) == 1; ++i){
    side[i] = UNKNOWN;
  };
  total_cars = i;

  // Initialize the first row and the first column
  for (i = 0; i <= ferry_len; ++i)
    ferry[0][i].length = ferry[i][0].length = 0;
    ferry[0][i].prev_row = ferry[i][0].prev_col = 0;

  // Print info
  if (DBG_LV2){
    printf('\nLength of Ferry: %d \n', ferry_len);
    printf('Number of Cars: %d \n\n', total_cars);

    for (i = 0; i < total_cars; ++i)
      printf('Car[%d]: %d length\n',
             i, cars[i]);
    printf('************************************\n');
  }//end if

  // Fill table
  int result = recursive_ferry(0,0,0, ferry_len);

  if (DBG_LV2){
    printf('Result: %d length value\n',  result);
  }

  if (DBG_LV2)
    print_tables();

  // Track back foot steps
  if (DBG_LV2)  printf('tracking(%d, %d, %d)\n\n', ferry_len, ferry_len, total_cars);
  tracking(ferry_len, ferry_len, total_cars);

  // Print results
  if (DBG_LV2) printf('\n SIDE: \n');

  for (i = 1; i < total_cars; ++i)
    switch(side[i]){
      case UNKNOWN:
        if (DBG_LV2) printf('UNKNOWN side[%d]: %d\n', i, side[i]);
        break;

      case RIGHT:
        if (DBG_LV2) printf('RIGHT side[%d]: %d\n', i, side[i]);
        printf('right\n');
        break;

      case LEFT:
        if (DBG_LV2) printf('LEFT side[%d]: %d\n', i, side[i]);
        printf('left\n');
        break;

    };

  return 0;
}

/*
 *
 */
int tracking(int left, int right, int idx_car){

  if (1) printf('\ntracking(%d,%d,%d)\n', left, right, idx_car);

  if (idx_car < 0)
    return;

  if (DBG_LV2) printf('.1 ');

  if (ferry[left][right].length == 0)
    return;

  if (DBG_LV2) printf('.2 ');

  --idx_car;

  int prev_len = (left + right) - cars[idx_car];
  int prev_left = left - cars[idx_car];
  int prev_right = right - cars[idx_car];

  if (DBG_LV2) printf('prev_len: %d = (%d + %d) - %d (car[%d])\n', prev_len, left, right, cars[idx_car], idx_car);

  if (ferry[prev_left][right].length == prev_len && prev_left >= 0){

    if (DBG_LV2) printf('ferry[prev_LEFT: %d][right: %d] MATCH [from RIGHT]\n', prev_left, right);

    side[idx_car] = RIGHT;

    tracking (prev_left, right, idx_car);

  }else  if (ferry[left][prev_right].length == prev_len && prev_right >= 0){

    if (DBG_LV2) printf('ferry[left: %d][prev_RIGHT: %d] MATCH [from LEFT]\n', left, prev_right);

    side[idx_car] = LEFT;    

    tracking (left, prev_right, idx_car);

  }else{

    if (DBG_LV2) printf('ferry[prev_left][right] and ferry[left][prev_right] DO NOT MATCH\n');
    tracking (left, right, idx_car);  

  }//end if

  return;
}

/*
 *
 */
int recursive_ferry(int left, int right, int idx_car, int ferry_len){

  if (DBG_LV1) printf('r_f(left: %d, right: %d, idx_car: %d (car_len: %d) \n',
                      left, right, idx_car, cars[idx_car]);

  if (left > ferry_len || right > ferry_len || idx_car > total_cars)
    return 0;

  if (ferry[left][right].length > 0){
    if (DBG_LV3) printf('++Return length: %d \n', ferry[left][right].length);
    return ferry[left][right].length;
  }// end if

  // Store value in ferry
  ferry[left][right].length += (left + right);

  // Record foot steps
  int nxt_row = ferry[left][right].prev_row = left;
  int nxt_col = ferry[left][right].prev_col = right;

  //
  int rtn_left = recursive_ferry(left + cars[idx_car], right, idx_car + 1, ferry_len);
  int rtn_right = recursive_ferry(left, right + cars[idx_car], idx_car + 1, ferry_len);

  if (DBG_LV1 && (rtn_left == 0))
    printf('L>');

  if (DBG_LV1)
    printf('[%d,%d](%d | %d)', nxt_row, nxt_col, rtn_left, rtn_right);

  if (DBG_LV1 && (rtn_right == 0))
    printf('<R');

  if (DBG_LV2) printf('\n');
  // Obtain the result of the best route
  return max(rtn_left, rtn_right);
}                    

/*
 *
 */
int print_tables(void){

    printf('\n\nTABLE\n ');
    for (i = 0; i <= ferry_len; ++i)
      printf(' %d ',i);
    printf('\n');

    for (i = 0; i <= ferry_len; ++i){
      for (j = 0; j <= ferry_len; ++j){
        if (j == 0) printf('%d', i);
        printf (' %d ', ferry[i][j].length);
      }// end for
      printf('\n');
    }//end for
    printf('\n');

    printf('\n TABLE XY\n ');
    for (i = 0; i <= ferry_len; ++i)
      printf(' [ %d ]',i);
    printf('\n');

    for (i = 0; i <= ferry_len; ++i){
      for (j = 0; j <= ferry_len; ++j){
        if (j == 0) printf('%d ', i);
        printf('[%d,%d] ', ferry[i][j].prev_row, ferry[i][j].prev_col);
      }//end for
      printf('\n');
   }//end for
   printf('\n');

  return 0;
}

If you encounter any problems or errors, please let me know by providing an example of the code, input, output, and an explanation. Thanks.

Share

3D Printer – Using a desktop case as a Base

My friend Gary Schoonmaker got his desktop broken. After trying many things, I found out that the Microprocessor was busted. Then decided to give me the desktop so I could used it.

I think is going to be a good base for the 3D printer, plus this is going to be considerate as recycled.

Here are some examples:

Share

Ask me questions!

Up to know I though that posting things in a blog was to share my stuff in hopes to helps others that may be interested.

However, I found out how wrong I was. I think that a blog can be made more interesting by having interaction with people.

You are welcome to ask me questions. I will try my best to answer them.

Sometimes I would not be able to answer a question right away. I might be very busy or I will not know the answer, but I will try my best to make some time or find the answer.

Share