Saturday, June 15, 2013

the Space Odyssey

These are unique kind of problems trying to find the number of ways of travelling from one point to another in n-dimension space. Here n could be anything, from 1 dimension to 2 dimension or 3 dimension et al. I initially encountered these problems during my GATE exam preparation and till the exam I couldn’t find a way to deal with them. In short, I went to the exam without solving such problems, even though being aware of the fact that these questions had repeated themselves in disguise. Fortunately enough, paper setters didn’t mind to test me on these! But then I was destined to understand and solve them only after the exam. And little did I know that even though these problems sounded weird related to geometry or astronomy, but they were the easiest to crack once I had clear understanding of permutations and combinations. Thanks to Kenneth Rosen!

A typical problem sounds like –

“Given a 2 dimensional space how many paths exist from point (0,0) to point (x,y) with the condition that we are allowed to move either up or right and no other direction?”

Here usually (x,y) is specified, for example  (3,2) or (5,6) and both of them are completely random, no correlation whatsoever.

Indeed it sounds scary enough to give goose bumps. But let’s start with simple 1 dimension space. The number of paths from point 0 to x is 1. C’mon that’s lame. No matter what is x, the number of paths remain still 1. And the condition is irrelevant.

Ok, having cracked 1 dimension space, let’s move on to 2 dimensions. Here the condition matters. In fact this condition suggests us a cue to reach the solution. We are restricted in our movement, only two choices are available at any point. So let’s model the path from point (0,0) to point (x,y) as combinations of rights and/or ups. Let’s further encode right as 0 and up as 1. Thus any path between the above points is string containing 0s and 1s and its length exactly is x+y. Since any path to point (x,y) should exactly contain x rights and y ups, we find that our string denoting the path contains preciously x 0s and y 1s.

So we have morphed the original problem to familiar field of permutation and combinations. What only remains is finding the number of strings of length x+y which had preciously x 0s and y 1s. And that would reflect the required number of paths and the answer. Now, a simple solution for the new transformed problem is from permutation perspective which has few objects of each type. A typical problem of such type would be finding the number of distinct words possible from the word – “SUCCESS”.  And it’s quite simple (I’m refraining from technicalities here)-

= 7!/(2!2!)

So our problem reduces to finding the number of distinct strings that are possible from the string that contains x 0s and y 1s. So that’s just a cakewalk now –

= (x+y)!/(x!y!)

And this preciously is the solution for the above 2 dimension problem. For example, the number paths between the points (0,0) and (3,2) is

=(3+2)!/(3!2!)

=5!/(3!2!)  = 10

And you could verify this by enumerating all the paths. Another perspective to solve such problem is to think as number of ways of arranging x 0s (or y 1s) in a string of length x+y i.e.

= (x+y)Cx = (x+y)Cy

Using the same example, the solution now would be 

=5C3 = 5C2 = 10 

Wow! I liked the thing when permutations and combinations converge to give the same solution. Both in fact attack the same problem from their perspective to drive the solution out of the box!

Ok, take a long breath as we move on to 3 dimensions. But that should also be another cakewalk. Isn’t it? Any point (x,y,z) would exactly contain x rights, y ups and z inwards(if restriction is on going outwards). So modeling on similar lines we get a string of length x+y+z containing x 0s, y 1s and z 2s. So the solution on the similar lines is

=(x+y+z)!/(x!y!z!)

Going through the combinations way, we find the same answer too. However it’s bit tricky now. First we find the number of ways to arrange x 0s in the string of length x+y+z and then number of ways to arrange y 1s in the remaining string of length y+z i.e.

= (x+y+z)Cx * (y+z)Cy

Verifying that both the formulae have same destination is simple matter of substituting values of x,y and z.

And similarly, an n dimensions problem could be attacked and morphed into a simple permutation or combination problem of grade 10. But the initial problem could be tweaked to raise the difficulty level. For example, instead of having point(0,0) as the origin, any arbitrary point could be selected (does that really matter?) or putting a roadblock between two points on the way from source to destination like putting a constraint that there is only one path between point(3,2) to point (4,3) which lie on the midway between the source or destination. And similarly, restrictions and tweaks could go on to design more fascinating and intriguing problems!

The Dream Exam


Ever thought of an exam that has passing probability of 0.9 or greater? It indeed sounds cool to attend one!  But not so cool, when it boils down to design one. There was a similar problem of designing a question paper in my MSc(Engg.) SERC test at IISc.
 
“The probability of answering a question correctly is ½. A candidate is required to attempt 2 questions correctly in order to pass the exam. What are the minimum number of questions required in the exam so that the probability of passing is at least 0.9?”

Though the question looks very straight forward but when I started solving it using tree diagram, I started facing lot of issues and it took lot of time. Indeed, it is not a straight forward question but reverse version of a usually asked question. Suppose, had the question asked – “What is the probability of passing when there are, say 5 questions?” This one is pretty straight forward and lame. Since the total search space or the possible number of response combinations for a 5 question exam is 25=32. Now out of these we are interested in responses that have at least 2 correct answers. So total number of such responses would be –
 = Number of responses with 2 correct answers +
   Number of responses with 3 correct answers +
   Number of responses with 4 correct answers +
   Number of responses with 5 correct answers

= 5C2 + 5C3 + 5C4 + 5C5
= 10 + 10 + 5 +1
= 26
Thus Probability of passing is 26/32 = 0.81 (approx.)

Aah, so close to our required probability of 0.9! Now instead of calculating so many combinations, a direct option would be –
Probability of passing = Probability of at least 2 correct answers
         = 1 – Probability of at most 1 correct answer
         = 1 – (5C0 + 5C1)/32
                                 = 1 – (1+ 5)/32
                                 = 26/32 = 0.81

Here 5C0 indicates number of responses with 0 correct answers or all 5 wrong answers and there is only one possibility – all wrong answers. And 5C1 indicates the other part, where only one answer is correct and hence 5 possible responses i.e. any of the 5 questions is answered correctly.

But our task is exactly the reverse. We need to find out the minimum number of questions required such that the passing probability is at least 0.9. Though I couldn’t find the straight forward solution but I could attack it from reverse direction and keep on calculating the probabilities for each number until we find the required probability. Hence probability of passing when the number of questions, N = 6,
                                    = 1 – (6C0 + 6C1)/26 
                                    =57/64 = .89
Similarly for N = 7,
                                   = 1 – (7C0 + 7C1)/27


                                   = 120/128 = 0.93

That’s it. We have the required probability and hence the answer, equal to 7. The exam needs to have at least 7 questions so that the passing probability is at least 0.9. Wow, how good it would be if everyone designed such exam!

Though, this is not the way it should be solved. But I found only this! So let me delve more into it until I find one, if any, or I faint!

Friday, June 14, 2013

the sorted Array and the scanner problem


I came across this puzzle during my Msc(Engg) interview in IISc Bangalore. The puzzle was simple and stated as -

"You are given a sorted array of length n and a pointer that points to the first element in the array. This is a special pointer that can be reversed in direction only once. That means if it was moving to right and then it decides to move towards left then it keeps on moving to left. It cannot change its direction again."

The problem is to come up with an efficient algorithm to search a number in the array.

While it took me fews seconds to put all the conditions and situations together, my first response was to do a linear search. And that was accepted quite generously. Of course, the next question as anticipated was "Can you do better?"

This algorithm had a running time of n. So I decided to divide the array into two parts. I suggested directly compare the middle element with the search element and depending upon the outcome, we move into either of the half. This reduced the running time to n/2. That too was accepted cordially and followed by the same question!

Instead of targeting the middle element, we could target, say n/3th element. So running time is down to n/3 from my first response of n. Next question was a bouncer.

"So what's the appropriate step size that gives the best efficiency?"

He meant, we could go endlessly by choosing n/5 th, n/10 th, n/20 th and so on. Which one of them would be the best?

Suddenly I recalled a similar problem that I had read long back on 20bits.com, which tries to find similar solution. But I couldn’t recollect the way he had solved, probably because four IIsc professors were continuously gazing me. I tried recollecting and gave up. But I gave them a brief idea how could we find the solution. They looked satisfied with my broken answer and asked me to ponder upon it after the interview.

Soon, I was heading back home in a BMTC bus. My mind was completely occupied with calculations and trying to find the solution for that problem. But I did it! I found it in BMTC!!

It was not that difficult as it sounded in the interview room. We wanted to find the step size that would lead best efficiency. So If I choose n/3 step size, then once finding the appropriate partition I had to do a linear search for remaining 2 elements in the partition. The explanation being, since each partition had 3 elements and one of them gets compared at each step size, I had to check the remaining 2 using linear search. What if I take step size n/10? I needed to check another 9 elements using linear search in the partition.

So, mathematically put, the total number of comparisons required –

n/s + (s-1)

where

n -> array length and s -> step size.

This equation preciously defines the number of comparisons required for a generic step size s. The first part of the equation: n/s, indicates the number of partitions that are formed due to step size s and these were the number of comparisons required on the step size in worst case, as the search element could be in the last partition. The other part: (s-1), indicates the number of linear comparisons required once we found our partition. Since each partition had s elements and one of them was already compared during partition searching, we required only s-1 more comparisons.

Now we have a mathematical representation of the problem we are dealing with and we just needed to find the step size that resulted into minimum comparisons. Hence we simply needed to find the value of s where the above equation had a minima. And that’s typical differentiation stuff. We differentiate the above equation wrt s and equate it to zero to get the required value of s.

Thus on differentiating and equating the above equation to zero,

-n/s2 +1 =0

=> n = s2

Or s = √n

 i.e. s = sqrt(n)

And that’s it!

continued...

Rebooting ...


I'm back here. It's been really long long time, I penned here. But the news is, yes! I have decided to write again. System's rebooting! The look has been updated and the site has got new address http://my1bit.blogspot.in/  from previous one omsaiindia.blogspot.com. Plus, I have merged my another blog: bitsnthebytes.blogspot.com with this! Get ready, the fun begins now ...