You are on page 1of 2

Algorithm Design

We shall discuss algorithm design. An Algorithm is a formal procedure for solving a


problem.

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.

In designing an algorithm to solve a given problem, it is of course necessary to


understand the problem well. It is no use trying to solve a problem that you do not
understand well. Understanding a problem means understanding the scope of what is
required and what a solution should look like.
After understanding the problem, the computer scientist must then develop a procedure
for finding the solution. Most problems have several parts to them. It often helps to break
down a problem into smaller sub-problems, which can be solved individually. Designing
a procedure to solve a problem does not necessarily mean sitting at the computer and
beating your head. Most competent computer scientists first sit down with pen and paper
and first try to draw up a point by point solution to the problem. They do not try to
reinvent the wheel either: If one of the sub-problems to be solved has a well known
algorithm for solving it, then they use that algorithm or adopt it to their needs. After
understanding how to solve the problem, the computer scientist then goes about turning
their solution into an actual computer program to solve the problem.

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.

Illustrating the ideas


In class we shall discuss solutions to three classical problems as a way of illustrating the
ideas above. I am not giving you a reference on these as I do not believe it is necessary.
What you need to do is simply think about the problems along the guidelines given above
and come prepared to participate actively in discussing solutions to them.

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.

You might also like