Sunday, July 17, 2011

Algorithms and heuristics


Given an array of 9 numbers that contains numbers between 1 to 10, obviously no repetition, one number missing. How to find out the missing number?


Let the numbers in the array be 1,2,4,6,3,7,8,10,9 (total 9 numbers without repetition). I will try to solve this by using following algorithm that randomly came to my mind.

get_missing_number_1(array){

array=Sort(array);


for(i=0 to i
<9){

temp=array[i+1] - array[i] ;

if(temp>1) return array[i]+1;

}
}


To make things easy, I will give a dry run of this algorithm. Let the array after sorting is 1,2,3,4,6,7,8,9,10. Only number five is missing. For consecutive numbers in array i.e 2-1 is 1, 3-2 is 1 but 6-4 is 2, so 5 is missing.

But there exists a very simple method to find the missing number. Just do this

get_missing_number_2(array){

sum_array= sum of all numbers in array;

sum_1_to_10= sum of all numbers from 1 to 10;


missing_number= sum_1_to_10 - sum_array;


return missing_number

}

So, Did we wasted our time in the analysis of first algorithm?

The second method uses a heuristic and exploits particular properties of the problem. But there is lesser chance to solve the problem if it is generalized using a heuristic. Think, if array size is 8; that is, two numbers are missing , which of the above algorithms can be modified to find both the numbers?

The complexity of algorithm get_missing_number_1 is O(n log n) and get_missing_number_2 is O(n), but the first one is more generic.

Now question arises if there exists an algorithm that finds the missing number for generalized problem in linear time. The algorithm discussed get_missing_number_1 has time complexity O(n log n).

So, let's do divide and conquer. The image below is self-explanatory. The method used is to divide the problem by partitioning.



The time complexity comes out to be n + n/2 + n/4 ......... ~ O(n). {assuming partition divides the problem approximately into 2 equal parts}

On slight modification this algorithm can be used to find out multiple missing numbers. For that we have to follow multiple branches with missing numbers.

Thursday, July 7, 2011

Simple Algorithms - Generate a Series

Lets see how to generate a series i.e 0,1,2,...100,99.....1,0,1,2...... with an additional condition, that is, using only one variable.

First of all we must check the possibility of such an algorithm. Lets make an analogy (refer to figure below). A person runs from a pillar marked 0 towards another pillar marked 100, the distance between them is 100 meters. We take a variable called currentPosition to denote its position at any time. The value of currentPosition will change like 0,1,2,3,4......99,100. Now the person returns back to pillar marked 0. We can use the same variable to denote current position.
Here the direction of movement is different. But, we need not add another variable to denote direction. If the value of currentPosition is positive the person is moving from 0 to 100, if its negative the person is moving from 100 to 0. By analogy we can assume that it is possible to generate a series i.e 0,1,2,...100,99.....1,0,1,2...... using a single variable.

You can try this algorithm yourself.


Also, try to generate a series i.e 0,1,2,...100,99.....1,0,1,2......199,200,199,198............2,1,0 using only one variable? Is that possible?

Simple Algorithms - Swapping Values

We have three integer variables a,b,c. What are the steps to be followed to move value of a to b, b to c and c to a without using any extra variable?

To solve this we will try to find out a sub-problem first. This will make the task easier. A very simple problem is to swap values of two integer variables without using a third variable. Let a and b are two variables. The solution is:

swap(a,b) {
a=a+b;
b=a-b;
a=a-b;
}

The proof:

a =a+b; ----(1)
b=a-b=A(a+b) using (1) -b= a;---(2)
a=a-b= A(a+b) using (1)-B(a) using (2)=b;

Note: A(a+b) represents current value in variable a and so on.


Now if we have three variables a,b,c to move value of a to b, b to c and c to a we can follow following steps:

swap(a,b);
swap(a ,c);


So, what could be the algorithm if we have four integer variables i.e a, b, c, d ?