Sunday, October 23, 2011

There is a simpler solution to any problem than the simplest ever found

This is about a programming problem that kept me bugging for few days. In a popular site hosting coding challenges, I faced this problem where I was to handle connections between nodes, and decide whether a specific pair of nodes are connected or not.

The first approach I tried using 2D arrays - a pretty easy one, but it grossly failed when number of nodes crossed some 20000. I laughed at myself, why in the first place I have thought of allocating some 20000 x 20000 bytes in memory! It would neatly bring a heap space error in no time.

I got another trump - hashing. Using HashMap and HashSets (I am a java person, by the way) to represent a graph structure. And a crisp search logic (over BFS or DFS) - that's it! My code was ready in no time.

And it failed in no time!

Recursion in the search logic soon went out of boundary bringing up a Stack Overflow.

I scratched my head over the problem, changing this and that to alter the flow, but I couldn't make it run for more than some 30000 nodes. Next I was thinking of Threading in recursion, but anyhow left it there to think fresh the day after.

Ideas come to me at night. It strikes. The next day I used a 1D array, positions denoting the nodes and values denoting a cluster that node belongs to. In absence of hashing, iterations were a bit more time consuming, still the code scaled well. Searches were fast, and it handled some 600000 nodes in 12.6 minutes.

I couldn't submit the solution, though, because my code was developed on linux simulated on a windows machine, and it was failing either on build or compilation on the server where I was to upload the solution.

I always believe that there is a simpler solution to any problem than the simplest ever found. There has to be. Adding recursion to this view, there is no end of finding a better solution, only you need to have the patience and dedication to find it.

Note 1: I will decide on a better title of this post later on.
Note 2: Does the view work in real life? I am currently thinking of instances.

1 comment: