Introduction to Algorithms
Introduction to Algorithms
DEFINITION OF ALGORITHM
An algorithm is a well-organized, pre-arranged and well-defined step by step
procedure solve a given problem by receiving input and providing output.
Algorithms are used to solve complex problems. An algorithm is said to be
accurate only when it provides the exact output. Every problem can be solved using
various different methods.
The best method or the best algorithm depends on the following factors.
1. An algorithm should be easy to understand, code and debug (design of algorithms).
2. An algorithm should make use of the computer resources efficiently (analysis of
Algorithms).
CHARACTERISTICS OF AN ALGORITHM
1. Input: Zero or more input values are externally supplied initially before the
algorithm begins or dynamically as algorithm is executed.
2. Output: At least one output value is produced. One or more values are produced.
3. Definiteness: Each instruction should be clear and unambiguous.
4. Finiteness: For all cases, the algorithm terminates after a finite number of steps.
5. Effectiveness: Every instruction must be very basic so that it can be carried out
easily in a finite amount of time.
NOTION OF ALGORITHM
The sequence of steps that are followed in the design and analysis of algorithms are
listed below.
It is important to ascertain the capabilities of the computational device for which the
algorithm is intended. This is based on the following factors.
3. Memory space
In applications that involve large volumes of data and complex problems, it is
important to choose a computer that has large memory space.
There are two types of algorithms based on whether we want the output to be exact
solution or an approximate solution: 1. EXACT 2.APPROXIMATE
1.Exact algorithm: Exact algorithms are the algorithms that solve the problem
exactly.
Examples : algorithms for sorting, searching, pattern matching, traveling salesman
problem, etc, that give the exact result.
Proper data structure is to be chosen so that with well defined efficient algorithm and
suitable data structure, efficient computer programs can be developed.
V. Designing an algorithm
Natural Language
Algorithms that are written in English like statements with few mathematical
expressions are not preferred because of the inherent ambiguity in natural language
representation. So it is rarely used.
Flowchart
It is a pictorial representation of an algorithm. All the steps of an algorithm are drawn
using geometric shapes such as boxes, circles, connecting arrows etc. that describe
the steps of algorithm. Flowcharts are used to help the programmer to understand
the logic of the program.
Pseudocode
It is a method of representing an algorithm using natural language and
programming language constructs. It is more precise than a natural language.
There is no single form of pseudocode.
STEP 1: Start
STEP 2: Read n
STEP 3: Initialize sum=0
STEP 4: for i 1 to n do
STEP 5: sum sum + i
STEP 6: Print ” sum of ”, n, ”natural numbers is”, sum
STEP 7: Stop
The correctness of the algorithm can be proved by finding whether the algorithm
gives a required result for every valid input in a finite amount of time. This process
is called Algorithm Validation.
The purpose of validation is to assure that the algorithm will work correctly
independent of the programming language that is to be used to implement it.
The time and space the algorithm uses are two major measures of the efficiency of
an algorithm. They are
a. Time efficiency
b. Space efficiency
Simplicity
Simpler algorithms are easier to understand and easier to program.
Simpler algorithms are sometimes more efficient than more complicated algorithms.
Generality
The algorithm developed for the problem that is general and the range of inputs it
accepts are to be considered.
Testing is the process of identifying errors in a program (debugging) and finding how
well the program works (performance measurement).
Analysis Profiling
Analysis is the process of determining Profiling is the process of executing the
how much computing time and storage correct program on data sets and
an algorithm will require. measuring the time and space it takes to
compute the results.
This is independent of machine, This is dependent on machine,
programming language and will not programming language and the compiler
involve the execution of program. used.
This allows making quantitative It helps in finding the logical places to
judgements about the value of one perform optimization and also confirms
algorithm over the other. the previously done analysis.
The other criteria for judging algorithms that have a more direct relationship to
performance are
• Running time (computing time)
• Memory space (storage requirements)
Efficiency can be studied in precise quantitative terms. There are two kinds of
efficiency.
1. Time efficiency (Time complexity)
2. Space efficiency (Space complexity)
ANALYSIS FRAMEWORK
1. Space efficiency
The space efficiency of an algorithm is the amount of memory required to run the
program completely and efficiently.
2. Time efficiency
The time efficiency of an algorithm is the amount of time taken to execute an
algorithm.
All algorithms run longer on larger inputs. It takes longer to sort larger arrays,
multiply larger matrices, and so on. So it is important to investigate an algorithm's
efficiency as a function of some parameter 'n' indicating the algorithm's input size.
For the analysis of time efficiency of algorithm, it is measured by counting the number
of times the algorithm's basic operation is executed on inputs of size 'n'.
Order of Growth
5) n² : Running time of a algorithm is quadratic. The algorithms will have 2 loops like
bubble sort, selection sort, addition and subtraction of two matrices etc.
6) n³ : Running time of algorithm is cubic. The algorithms will have 3 loops, like
matrix multiplication, Gauss Elimination method of solving simultaneous equations
Note: All these functions can be ordered according to their order of Growth (from
lowest to highest) 1 < log n < n < n log n < n² < n³ < 2n < n!
1.Worst-Case Efficiency:
The efficiency of an algorithm for the input of size n for which the algorithm takes
longest time to execute among all possible inputs is called Worst Case efficiency.
2.Best-Case Efficiency:
The efficiency of an algorithm for the input of size n for which the algorithm takes
least time to execute among all possible inputs of that size is called Best Case
Efficiency.
3.Average-Case Efficiency:
The efficiency of an algorithm for the input of size n for which the algorithm takes
average time to execute among all possible inputs of that size is called Average
Case Efficiency.