Karachi   ->   Sweden   ->   Karachi, again   ->   Dubai   ->   Bahrain   ->   Karachi, once more   ->   London and Leeds

Saturday, May 26, 2007

The Aha! Moments

I am not sure who coined this term. I first encountered it while reading Programming Pearls by Jon Bentley. An Aha! moment is when you have a sudden insight into something (it's more enjoyable when you had thought that you already knew that "something"). Some books of Scott Meyers also talk about it; see, for example, his article titled My Most Important C++ Aha! Moments...Ever.

It's weird how something becomes clear all of a sudden. I was going through the following problem last night (taken from Programming Pearls): You have to find an efficient algorithm to find the maximum sum of consecutive numbers in a series (which can include both positive and negative numbers). For example, the maximum sum in "-1, 2, 3, -4, 1" is 5 which starts at 2 and ends at 3. Any other sequence will include negative numbers and thus, reduce the sum.

A brute force algo for the problem would be as follows:
maxSoFar = 0
for i = [0,n)
   sum = 0
   for j = [i, n)
      sum += x[j]
      maxSoFar = max (sum, maxSoFar)
Its runtime is O(n2) which isn't good. How can we optimize it?

  • One first observation is that you can scan the array and keep counting until your sum becomes negative. If that happens you have to start over from the next index in the array.

  • However, your maximum sum might be the one just before your sum became negative. That is, without any negative numbers in the array, you can safely sum up all the numbers and declare the maximum.
If you keep thinking about this problem, and try out various examples by hand, you might encounter an Aha! moment :)

I once encountered a similar explanation by Kraig Brockschmidt in his book Mystic Microsoft:
Then suddenly, as if magnetically drawn by my intense concentration upon it, the singular question that I so deeply yearned to answer returned: “How do all these pieces of OLE 2 fit together?”

BOOM! A bolt of lightning flashed into my consciousness with tremendous power and in one timeless moment I simply understood OLE 2. Not just bits and pieces—everything! It was magnificent.

A scientific explanation of the phenomenon states that
It may not appear in the shape of a light bulb above your head, but researchers say "Aha!" moments are marked by a surge of electrical activity in the brain.

That is, there is more to "creative problem solving" than "usual problem solving." The creative part requires an insight, the ability to get out of context and look at the problem from a higher level/ different perspective. It won't be surprising if such problems require a different kind of physical activity in the brain, as suggested by the article quoted above.

Wednesday, May 23, 2007

Another Instance of Parthenogenesis!

As if the Komodo Dragons' virgin birth was less surprising, this time sharks in captivity have reproduced asexually!!

A more detailed article on the phenomenon can be found here.

Saturday, May 19, 2007

Generating Graphs from Code

In memory representation of graphs using adjacency matrices (very poor for sparse graphs) or lists of nodes and associated edges is pretty easy to code. What's a show-stopper for me is generating a visualization of that graph. Even more complex is providing editing features on the nodes and edges. You might have guessed that I'm not good at UI programming.

I've been thinking of using MS Visio's Object Model to generate vsd diagrams. This approach has two drawbacks; the first and the important one is that the sheer size of the object model is daunting, specially when you don't know where to start from. Secondly, Visio is heavy! However, if you are interested in Visio based development, this might be a good start.

I recently stumbled upon WinGraphviz (a COM component that reads graphs written in DOT language and generates variety of outputs including PNG images).

The DOT language is extremely simple. The following example is taken straight out of the manual:
digraph G {
main -> parse -> execute;
main -> init;
main -> cleanup;
execute -> make_string;
execute -> printf
init -> make_string;
main -> printf;
execute -> compare;
}

which results in the following diagram:



You just pass the above mentioned textual description as a string and get back SVG, PS or an image file as output from the COM object. Easy, isn't it? I can't think of anything more simple than that. Besides, the DOT language is very rich and WinGraphviz can generate extremely complex diagrams. And as if that wasn't enough, the tool is open source!

Monday, May 14, 2007

Karachi!

Somebody has coined the term "City of Emergency Lights" for Karachi.

The situation in Karachi is bad, as you can see here as well.

Almost every institution of Pakistan is a "mini-Pakistan." Take any organization (be it a software house) and you'll identify that the problems at the micro level are same as those on the national level: tolerance, truth, accountability, etc. are missing.

We have had a false sense of security and prosperity---just like what's happening in Dubai cannot be described as "development and progress." The folks in Dubai are just making concrete structures for the rich by the richer. Till you address the core issues and help to improve your neighborhood as well, you are not safe.

Like the 2005 earthquake, we seem to be waiting for some more catastrophes. We discuss; we read; we write and then we forget.

Wednesday, April 11, 2007

Religion and Reasoning - Part III

First of all, lots of apologies for being away. I recently shifted my home and there is lots of work in office as well---I work in a company which makes software for ATMs and alternate delivery channels.

I'll like to continue with the example of a perfect statue in a wilderness: while you are wondering where it came from, suppose somebody (call him Mr. Truthful) tells you that the sculpturer was so and so. Mr. Truthful is well known in the area---people respect him for his truthfulness and honesty---and he further claims that he himself met the sculpturer.

After coming to know this, there are two logical conclusions: Either believe in what Mr. Truthful told you, or regard his meeting with and knowledge of the sculpturer as a hallucination. Disbelievers tend to conclude the later one. On the other hand, I find it absurd---if you trust a person in all the worldly affairs, and agree that all his faculties are superior to yours, why not trust him in things that can't be seen by the eye? After all, I do trust things like who my parents are; 1+1 equals 2; atom is made up of electrons, protons and neutrons; etc. I trusted my teachers and my parents for I didn't have the knowledge to prove or disprove the facts.

There are some similarities between the allegory and the coming of Prophets; there are further questions that one should ask himself: Why did the Prophets (Peace be upon them) one after the other validated their predecessors? Why did all of them prophesied the coming of the next one? Why each of these talked about the same things: monotheism, creation, day of judgment, and moral obligations? Why each of these was the best of his era (in terms of knowledge, wisdom, influence, etc.)? Why Gabriel is the arch-angel? Why the Old Testament (Torah), New Testament and Quran are so much similar in their teachings?

Besides, everything fits so nicely into this explanation of the creation and the purpose. Moreover, what all it asks you is to spend your life in a pious way---to refrain from what's harmful for you (both body and soul) and to practice what increases your level of awareness.