Data structure — Algorithms basis.

Algorithm is a step by step procedure, which define a set of instructions to be executed in a certain order to get the desired output.

  • Algorithm are generally created independent of underlying language.
  • An algorithm can be implemented in more that one programming language.

From a data structure point of view the following are some important categories of algorithms:-

  • Search — algorithm to search an item in a data structure.
  • Sort — algorithm to sort an item in a certain order.
  • Insert — algorithm to insert item in a data structure.
  • Update — algorithm to update an existing item in a data structure.
  • Delete — algorithm to delete an existing item from a data structure.

Characteristics of an Algorithm.

  1. Unambiguous — Algorithms should be clear and unambiguous. Each step and their input/output should be clear and must lead to only one meaning.
  2. Input — An algorithm should have 0 or more well defined inputs.
  3. Outputs — An algorithm should have 1 or more well defined outputs, and should match the desired output.
  4. Finiteness — Algorithm must terminate after a finite number of steps.
  5. Feasibility — should be feasible with the available resources.
  6. Independent — An algorithm should have step-by-step directions, which should be independent of any programming code.

How to write an algorithm.

There is no well defined standard for writing algorithms. We write algorithms in a step by step manner, but it is not always the case.

Algorithm writing is a process and is executed after the problem domain is well-defined. That is, we should know the problem domain for which we are designing a solution.

Example:

Problem: — Design an algorithm to add two numbers and display the results.

Step 1 — start.

Step 2 — declare three integers a,b and c.

Step 3 — define values of a & b.

Step 4 — add values of a & b.

Step 5 — store the output of step 4 to c.

Step 6 — print c.

Step 7 — stop.

Alternatively:

Step 1 — START ADD

Step 2 — get values of a & b

Step 3 — c ← a + b

Step 4 — display c

Step 5 — STOP.

We design algorithm to get a solution of a given problem. A problem can be solved in a more that one way.

In the example above, usually the second method is used to describe an Algorithm. It makes it easy for the analyst to anslyze the Algorithm ingoring all unwanted definitions.

Algorithm Analysis.

Effeciency of an algorithm can be analyzed into two different stages, before implementation and after implementation. They are the following -

  • A priori Analysis — This is the theoretical analysis of an algorithm. The efficiency of an algorithm is measured by assuming that all other factors for example process speed, are constant and have no effect on the implementation.
  • A posterior Analysis — This is an empirical analysis of an algorithm. The selected algorithm is implemented using a programming language. This is then executed in a target computer machine. In this analysis, actual statistics like running time and space required, are collected.

Algorithm complexity.

Supposed X is an algorithm and n is the size of input data, the time and space used by the algorithm X are the two main factors, which decide the efficiency of X.

  • Time factor — Time is measured by counting the number of key operation such as comparisons in sorting algorithm.
  • Space Factor — Space is measured by counting the maximum memory space required by the algorithm

The complexity of an algorithm f(n) gives the running time and/or the storage space required by the algorithm in terms of n and the size of input data.

Space Complexity.

Space complexity of an algorithm represents the amount of memory space required by the algorithm in its life cycle. The space required by an algorithm is equal to the sum of the following two components —

  1. A fixed part that is a space required to store certain data and variables, that are independent of the size of the problem. For example, simple variables and constants used, program size, etc.
  2. A variable part is a space required by variables, whose size depends on the size of the problem. For example, dynamic memory allocation, recursion stack space, etc.

Space complexity S(P) of any algorithm P is S(P) = C + SP(I), where C is the fixed part and S(I) is the variable part of the algorithm, which depends on instance characteristics I. Following is a simple example that tries to explain the concept —

Algorithm: SUM(A, B)

Step 1 — START

Step 2 — C ← A+ B + 10

Step 3 — stop

Here we have three variables A, B and C and one constant. Hence S(P) = 1+3. Now space depends on data types of given variables and constant types and it will be multiplied accordingly.

Time Complexity

Time complexity of an algorithm represents the amount of time required by the algorithm to run to completion. Time requirements can be definitions as a numerical function T(n), where T(n) can be measured as the number of steps, provided each steps consumes constant time.

For example, addition of two n-bit integers takes n steps. Consequently, the total computational time is T(n) = c * n, where c is the time taken for the additional of two bits. Here we observe that T(n) grows linearly as the input size increase.

Thank you very much for reading my article. I will continue with these journey of data structure and algorithm to enhance my skills as a software engineer.

My next article will be on asymptotic Analysis.

Regards.

Software Engineer