02 Algorithm Design
02 Algorithm Design
Introduction
Right from the beginning you have been writing programs to solve assigned problems.
What you have actually been doing is developing algorithms and implementing them. We
shall look at ways of formalizing this process. By formalizing I mean discussing more
systematic ways of developing algorithms and implementing them.
When the program is written and compiled, it is tested time and again to make sure it
works correctly. A common mistake is to assume that because one’s method looks
correct, there is no need to test the actual implementation of it. Another common mistake
is to forget to check that the implementation works reasonably for extreme cases of the
problem specified. For instance, a prime number generator program may be correct in all
other respects except that it fails to report 2 as a prime number! This is of course not good
enough.
After testing the algorithm implementation, the computer scientist may not deem it
necessary to go to sleep just yet. An algorithm and its implementation can almost always
be improved on. The computer scientist should examine the algorithm for possible
improvements, and should also examine their implementation to see how it can be
improved on. This is an iterative process. One must be careful not to overdo this all the
same. Clarity should not be sacrificed for efficiency. One should always aim for clarity
and simplicity without being inefficient.
The first problem we shall discuss if that of efficient polynomial root finding. Actually,
this problem is your next assignment but we shall discuss the algorithm together. Given a
polynomial f(x), we want to find the value of x that makes f(x) = 0. We shall look at
efficient ways of solving this particular problem. I have some ideas on it but I would like
to hear yours too.
The second problem is the problem of finding the greatest common divisor (GCD) of two
positive numbers. An efficient algorithm for this one exists (attributed to the
mathematician Euclid). This problem is quite old and the solution has application in
many fields of computer science.
The third problem we shall look at is the ‘Josephus Problem’: You have a group of N
people who have decided to commit mass suicide. They will achieve this by killing each
other one by one. They stand in a circle, and starting with any particular person, the Mth
person in the circle is killed. They continue doing this until all but one is dead. The
problem is to find out which person remains alive (and hopefully does not kill
themselves). For example, if N = 9 and M = 5, then the order in which people are killed is
5, 1, 7, 4, 3, 6, 9, 2, 8. We shall aim at writing a program that solves this problem for
given N and M.