Monday, August 8, 2011

Algorithms : Divide and Conquer

A divide and conquer algorithm repeatedly reduces an instance of a problem to one or more smaller instances of the same problem until the instances are small enough to solve easily.





Equal number of nuts and bolts of various sizes are given. There is a match for every bolt in the set of the nuts. Comparisom of a bolt with a bolt and a nut with a nut is prohibited. How to make ut bolt pairs in shortest possible time?

We can use divide and conquer here. We cannot sort only nuts ( only bolts ) according to size because nut-nut (bolt-bolt ) comparison is prohibited.

So we try to match a nut with bolts. Three conditions arise :
a. Its a match.
b. Bolt is smaller.
c. Bolt is bigger.

Now we can divide bolts in two parts: smaller ones and bigger ones. The matching nut-bolt (pivot elements) are the first match.


Now try to match the pivot bolt with remaining nuts. Two conditions arise :
a. Nut is smaller.
b. Nut is bigger.

Now we can divide nuts in two parts: smaller ones and the bigger ones.


The set of smaller nuts have a match in the set of smaller bolts and vice versa.


So after ~2n (assuming n nuts and n bolts) comparisons the bigger problem is divided into 2 smaller problems of half size (~ n/2).


Hence the time complexity is:

T(n)= T(n/2)+ O(n) ~ O(n lg n).

Tuesday, August 2, 2011

Simple Algorithms - Find duplicates in a list

Let's take an array of n integers. No integer repeats itself except one.

Given:
a. If n is even then the repeating
integer is repeated n/2 times. ( That is count of repeating integer is equal to the count of non repeating integers)

b.
If n is odd the repeating integer is repeated (n-1)/2 times or (n+1)/2.( That is count of repeating integer is one greater than or one less than the count of non repeating integers)

c. The repeating integer is never placed side by side in the array.

How to find out the repeating
integer?


General method of finding the repeating
integer is : creating a binary tree from the given integers. Whenever we encounter a duplicate integer, we can stop building the binary tree and return the repeating integer.

For the given special case, we can use the same algorithm. There are many possible permutations for the input array. If first and third integer same then it will take only three integers to be inspected to find out the repeating element.

So, what is the worst case for above problem. Lets create a permutation where we encounter the repeating integer 2 times as far as possible from the starting point.


If the count of repeating integer is one more then the non-repeating integers, that would have permutations like the one given below.

i. start here->1,2,1,3,1,4,1,5,1,6,1,7,1 ---- We can identify 1 as repeating integer after inspecting 3 integers.

If the count of repeating integer is equal to the non-repeating integers, that would have permutations like the two examples given below.

ii. start here->1,2,1,3,1,4,1,5,1,6,1,7 ---- We can identify 1 as repeating integer after inspecting 3 integers.

iii. start here->2,1,3,1,4,1,5,1,6,1,7,1 ---- We can identify 1 as repeating integer after inspecting 4 integers.


If the count of repeating integer is one less then the non-repeating integers, that would have permutations like the examples given below.

iv. start here->1,2,3,4,1 ---- We can identify 1 as repeating integer after inspecting 5 integers.

v. start here-> 2,3,1,4,1 ---- We can identify 1 as repeating integer after inspecting 5 integers.

Now we can generalize. If the count of repeating integer is two less then the non-repeating integers, that would have permutations like the examples given below (not limited to).

vi. start here-> 1,2,3,4,5,1 ---- We can identify 1 as repeating integer after inspecting 6 integers.

vii. start here-> 2,3,4,1,5,1 ---- We can identify 1 as repeating integer after inspecting 6 integers.

So for the given problem we have to inspect at most 5 integers from the array.



A general formula:

If the count of repeating integer is x less then the non-repeating integers then we have to inspect at most x+4 integers from the array for n greater than equal to 5.