Saturday, January 24, 2015

Selection Sort

The main idea of the algorithm is quite simple as follows. The input data array is divided to two imaginary parts, sorted and unsorted. Initially the sorted part is empty. The algorithm searches for the smallest element of the unsorted part and adds it to the end of the sorted part. When the unsorted part becomes empty algorithm stops. However Selection sort is inefficient in sorting large sets.

Worst case performance - O(n^2)

selection sort
Source - Quazoo
C++
void selection_sort(int array[], int length)
{
    for (int j = 0; j < length - 1; j++)
    {
        int min = j;
        for (int i = j + 1; i < length; i++)
        {
            if (array[i] < array[min])
            {
                min = i;
            }
        }
        if (min != j)
        {
            int temp = array[j];
            array[j] = array[min];
            array[min] = temp;
        }
    }
}

 Java
public int[] selection_sort(int[] array) {
    for (int j = 0; j < array.length - 1; j++) {
        int min = j;
        for (int i = j + 1; i < array.length; i++) {
            if (array[i] < array[min]) {
                min = i;
            }
        }
        if (min != j) {
            int temp = array[j];
            array[j] = array[min];
            array[min] = temp;
        }
    }
    return array;
}
Matlab
function array = selection_sort(array)
    length = numel(array);
    for i = (1:length-1)
        min = i;
        for j = (i+1:length)
            if array(j) <= array(min)
                min = j;
            end
        end
        if i ~= min
            array([min i]) = array([i min]);
        end
    end
end
Python
def selection_sort(array):
  for j in range(len(array)-1):
 
    min = j
    
    for i in range(j+1,len(array)):
      if array[i] < array[min]:
        min = i
        
    if min != j:
      temp = array[j]
      array[j] = array[min]
      array[min] = temp

Thursday, January 22, 2015

Insertion Sort

When we need to sort small number of elements, Insertion sort would be an efficient algorithm. Insertion sort removes one element from the input data, finds the location it belongs within the sorted list, and inserts it there. It repeats until no input elements remain.(Wikipedia)

Worst case performance - O(n^2)

insertion sort
Source - Introduction to Algorithms 3e
C++
void insertion_sort(int array[], int length)
{
    for(int j=1; j<length; j++)
    {
        int key = array[j];
        int i = j-1;
        while(i>=0 && array[i]>key)
        {
            array[i+1] = array[i];
            i = i-1;
        }
        array[i+1] = key;
    }
}
Java
public int[] insertion_sort(int[] array) {
    for (int j = 1; j < array.length; j++) {
        int key = array[j];
        int i = j - 1;
        while (i >= 0 && array[i] > key) {
            array[i + 1] = array[i];
            i = i - 1;
        }
        array[i + 1] = key;
    }
    return array;
}
Matlab
function array = insertion_sort(array)
    for i = 2:numel(array)
        key = array(i);
        j = i - 1;
        while (j > 0) && (array(j) > key)
            array(j+1) = array(j);
            j = j-1;
        end
        array(j+1) = key;
    end
end
Python
def insertion_sort(array):
   for j in range(1,len(array)):

     key = array[j]
     i = j

     while i>0 and array[i-1]>key:
         array[i] = array[i-1]
         i = i-1

     array[i] = key

If we closely look at the Insertion sorting algorithm, we can see that still it is possible to increase the efficiency of the algorithm by doing some changes. Since the correct position of the inserting element is found from the already sorted part of the array, we can perform binary search algorithm to that part of the array. Here is the Python implementation of the modified algorithm.
def insertion_sort(array):
    for i in range(1, len(array)):
 
        key = array[i]
        low, high = 0, i
     
        while high > low:
            mid = (low + high) // 2
            if array[mid] < key:
                low = mid + 1           
            else:
                high = mid
             
     array[:] = array[:low] + [key] + array[low:i] + array[i+1:]

Tuesday, January 20, 2015

How to create a Heat map using Processing.

In this post I explain how to create a heat map using Processing language. Processing is mostly used to generate creative visual designs through programming. Underlying language of Processing is Java. But in Processing it uses simplified syntax for graphical programming unlike Java. For more information you can refer home page of Processing

To design our heat map I am going to use Bi-linear Interpolation algorithm. Bi-linear interpolation is an extension of linear interpolation for interpolating functions of two variables (e.g., x and y) on a regular 2D grid (Wikipedia).

Algorithm 
Source - Wikipedia
Goal - Find the unknown value f(p) at a point p =(x,y)using the,
Points with Known values -
  •  Q11 = (x1,y1)
  •  Q12 = (x1,y2)
  •  Q21 = (x2,y1)
  •  Q22 = (x2,y2)
Linear interpolation in x direction,
Source - Wikipedia
 Linear interpolation in y direction,
Source - Wikipedia
 Combining both results, f(p) = f(x,y)
Source - Wikipedia

Here is the Processing sketch of the above algorithm for an 2X2 array. The interpolated values are represented by colors, which will eventually generate the desired heat map.

int r = 2;  // number of rows in input array
int c = 2;  // number of columns in input array
int t = 300;  // parameter (array resize factor)
int rows = (r-1)*t;  // height of the heat map
int cols = (c-1)*t;  // width of the heat map

float[][] array = {{3,1},{2,5}};  // input array
float[][] interp_array = new float[rows][cols]; // interpolated array

void setup() {
  size(cols, rows);
  noStroke();
}

void draw() {
  bilinearInterpolation();
  applyColor();
}

void bilinearInterpolation() {  // Bi-linear Interpolation algorithm

  for (int i=0; i<r; i++) {
    for (int j=0; j<c; j++) {
      int x = j*t - 1;
      int y = i*t - 1;
      if (x<0)
        x=0;
      if (y<0)
        y=0;
      interp_array[y][x] = array[i][j];
    }
  }

  for (int y=0; y<rows; y++) {
    int dy1 = floor(y/(t*1.0));
    int dy2 = ceil(y/(t*1.0));
    int y1 = dy1*t - 1;
    int y2 = dy2*t - 1;
    if (y1<0)
      y1 = 0;
    if (y2<0)
      y2 = 0;
    for (int x=0; x<cols; x++) {
      int dx1 = floor(x/(t*1.0));
      int dx2 = ceil(x/(t*1.0));
      int x1 = dx1*t - 1;
      int x2 = dx2*t - 1;
      if (x1<0)
        x1 = 0;
      if (x2<0)
        x2 = 0;
      float q11 = array[dy1][dx1];
      float q12 = array[dy2][dx1];
      float q21 = array[dy1][dx2];
      float q22 = array[dy2][dx2];

      int count = 0;
      if (q11>0)
        count++;
      if (q12>0)
        count++;
      if (q21>0)
        count++;
      if (q22>0)
        count++;

      if (count>2) {
        if (!(y1==y2 && x1==x2)) {

          float t1 = (x-x1);
          float t2 = (x2-x);
          float t3 = (y-y1);
          float t4 = (y2-y);
          float t5 = (x2-x1);
          float t6 = (y2-y1);

          if (y1==y2) {
            interp_array[y][x] = q11*t2/t5 + q21*t1/t5;
          } else if (x1==x2) {
            interp_array[y][x] = q11*t4/t6 + q12*t3/t6;
          } else {
            float diff = t5*t6;
            interp_array[y][x] = (q11*t2*t4 + q21*t1*t4 + q12*t2*t3 + q22*t1*t3)/diff;
          }
        } else {
          interp_array[y][x] = q11;
        }
      } else {
        interp_array[y][x] = 0;
      }
    }
  }
}

void applyColor() {  // Generate the heat map
 
  color c1 = color(0, 0, 255);  // Blue color
  color c2 = color(0, 255, 0);  // Green color
  color c3 = color(255, 0, 0);  // Red color
  color c4 = color(255,255,0);  // Yellow color
 
  for (int i = 0; i < rows; i++) {
    for (int j = 0; j < cols; j++) {
      float value = interp_array[i][j];
      color c;
      float fraction;
     
      if (value>=1 && value<2) {
        fraction = (value-1)/1.0;
        c = lerpColor(c1, c2, fraction);
      } else if (value>=2 && value<3) {
        fraction = (value-2)/1.0;
        c = lerpColor(c2, c3, fraction);
      } else if (value>=3 && value<5) {
        fraction = (value-3)/2.0;
        c = lerpColor(c3, c4, fraction);
      } else
        c = c4;
      stroke(c);
      point(j, i);
    }
  }
}


If you are not familiar with Processing syntax, you can refer the Processing reference page.  
Here is the generated heat map.

generated heat map

Wednesday, January 7, 2015

Designing a BCD adder & subtractor with HDL

In computer based electronic items binary number system is used. But the human readable format is decimal number system. Binary coded decimal is a digital encoding mechanism to convert binary data between machine and human readable formats. To convert decimal data to binary, binary coded decimal adder subtractors are used in those electronic items. In this post I show you how to design a BCD adder subtractor using HDL (Hardware Descriptive Language) and here I use ‘Verilog’ language. Gate level design is the approach used to develop the BCD adder.

Here is the flow diagram of the circuit.

flow diagram of the circuit

Because the gate level design is used in this example the whole implementation is done using logic gates.

1-bit full adder

Here I start from the basic unit which is 1 -bit full adder. As shown in figure it has 3 inputs (A, B, Ci) and 2 outputs (S, Co). This circuit has designed using 2 XORs, 2 ANDs and 1 OR gate.


1-bit full adder
//define a 1bit fulladder
module fulladd(a, b, c_in,sum, c_out);
  input a, b, c_in;
  output sum, c_out;
  wire s1, c1, c2;
  xor (s1,a, b);
  and ( c1,a, b);
  xor (sum,s1, c_in);
  and (c2,s1, c_in);
  or (c_out,c2, c1);
endmodule
4-bit full adder

In the next step I combine 4 1 -bit full adders to create the 4-bit full adder as shown in figure. In this configuration it has 4bits per each input with a carry in has 4bit output with a carry out.


4-bit full adder
//define a 4-bit full adder
module fulladd4(a, b, c_in,sum, c_out);
  //i/o port declaration
  input [3:0] a, b;
  input c_in;
  output [3:0] sum;
  output c_out;
  //internal net
  wire c1, c2, c3;
  fulladd fa0(a[0], b[0], c_in, sum[0], c1);
  fulladd fa1(a[1], b[1], c1, sum[1], c2);
  fulladd fa2(a[2], b[2], c2, sum[2], c3);
  fulladd fa3(a[3], b[3], c3, sum[3], c_out);
endmodule

1-digit BCD adder


Here arithmetic operations are done using BCD number format. Hence the result of normal binary addition should convert to BCD format using error correction methods as shown in figure. For this configuration I use two 4-bit binary adders, 2 AND gates and 1 OR gate. In the error correction, 0110 binary 6 should add to the result of normal binary addition to convert the result in BCD format.
1-digit BCD adder
module bcd_adder( A, B, CIN, F, COUT);
  input [3:0] A, B;
  input CIN;
  output [3:0] F;
  output COUT;
  wire [3:0] Z,S;
  wire k,w1,w2,w3;
  fulladd4 add0(A, B, CIN, Z, k);
  and (w1,Z[3],Z[2]);
  and (w2,Z[3],Z[1]);
  or (COUT,k,w1,w2);
  assign S = {1'b0,COUT,COUT,1'b0};
  fulladd4 add1(Z, S, 0,F,w3);
endmodule

9’s compliment generator


Since the final circuit is made for both addition and subtraction operations, I'm going to make a special circuit with mode selection. 2’s compliment method is used for binary subtraction. But in BCD it must be 9’s compliment method. As shown in figure I have designed 9’s compliment generator in the following manner. 

9 - B = X 

truth table of 9's compliment generator
Here is the logic used in 9’s complement generator.
X3 = B3M’ + B3’B2’B1’M
X2 = B2M’ + (B2’B1 + B2B1’) M
X1 = B1
X0 = B0M’ + B0’M

9's compliment generator
module complement_generator(B, M, x);
  input [3:0]B;
  input M;
  output [3:0]x;
  wire w1,w2,w3,w4,w5,w6,w7,w8,w9;
  xor (x[0],B[0],M);
  assign x[1]=B[1];
  xor (w5,B[1],B[2]);
  and(w9,w5,M);
  not (w1,M);
  and (w6,B[2],w1);
  or (x[2],w9,w6);
  not (w2,B[1]);
  not (w3,B[2]);
  not (w4,B[3]);
  and (w8,M,w2,w3,w4);
  and (w7,B[3],w1);
  or (x[3],w8,w7);
endmodule

1-digit BCD adder subtractor


Combining the above 9’s compliment generator and the BCD adder it is able to design the 1 -digit BCD adder subtractor as follows.
 
1-digit BCD adder subtractor
module add_sub(A, B, CIN, M, F, COUT);
  input [3:0]A,B;
  input CIN,M;
  output [3:0]F;
  output COUT;
  wire [3:0]W;
  complement_generator comgen0(B,M,W);
  bcd_adder add0(A, W, CIN, F, COUT);
endmodule