Good Algorithm

Distribute Education like Computer Geek

Characteristics of a Good Algorithm

Multiple algorithm of a problem, Multiple programs of an algorithm
Multiple algorithm of a problem, Multiple programs of an algorithm

A problem can have many algorithms, and an algorithm can have many programs in one language as well as different languages.

A good algorithm:
  1. It must be time-efficient.
  2. It uses less space.
  3. solves problems in the minimum possible results.
  4. uses a minimum resource to reach the solution.
  5. It must be easily understood.
  6. can be maintained at a minimum cost.
  7. It must be flexible.
  8. manages all types of exceptions and errors.
  9. is self-sufficient.
  10. can be applied to a wide variety of similar problems.
  11. should be balanced.

According to Coreman’s book, algorithms are just like technology. We all use the latest and fastest processors, but we need to run implementations of the good algorithms on that computer to properly take benefit of the money that we spent to have the latest processor.

ExampleComputer A runs a sorting algorithm whose running time grows like n2 (It means time complexity is C*n2) against a slower computer B, running a sorting algorithm whose running time grows like nlogn (it means time complexity = C*nlogn). C is a constant.

They each must sort an array of 10 lakh numbers. (Input n is 10 lakh numbers = 106 numbers)

Computer A
Computer B
Executes 100 crore instruction per sec = 109Executes 1 crore instructions per sec = 107
Algorithm for Computer A requires n2 instructionsAlgorithm for Computer B requires 25nlogn instructions
Time taken = n2/ (execute instructions in a sec) = (106)2/109Time taken = n log n / (execute instructions in a sec) = 25(106 log106)/107
It takes 16.66 minutesIt will take 15 seconds

By using a faster algorithm, a slower computer can win the race.

Test Yourself

Q1- Can an algorithm have two or more than two problems?
Q2- Can a program have two or more than two algorithms?

The answer is yes. Why? Because if you have a program in an old language and decide to make a program in a new and efficient language, you will need the program’s algorithm. If you don’t have the algorithm of the same program, then you can write one.

Q3- Can an algorithm have two or more than two programs?

Yes. Programs can be made in many languages if you have the algorithm’s logic and many programs can be made in the same language also.

Q4- According to Coreman’s book, algorithms are just like __________.

Technology

Q5- Can a fast algorithm makes the slow computer fast?

Yes

Q6- What a good algorithm means in time context?

A good algorithm must be time-efficient.

Q7- “A good algorithm should be balanced”. Explain?

There are two complexities in an algorithm in which we can find its efficiency. One is Time Complexity and Second is Space Complexity.
So, a good algorithm should be balanced between the two complexities. It may not be the case that the time complexity of the algorithm is very low, but it is taking so much space that there is no point in doing it, and vice-versa also.

BOOKS

Algorithm T H CoremanAlgorithm by Udit AgarwalAlgorithm Ellis HorowitzData Structure & Algorithm Made Easy Narasimha