INTRODUCTION TO ALGORITHMS
INTRODUCTION TO ALGORITHMS
Algorithms
Introduction to Algorithms
Dr G.Kalyani
Department of Information Technology
Velagapudi Ramakrishna Siddhartha Engineering College
Topics
• Algorithm Specification
• Pseudocode Convention
• Performance Analysis
• Asymptotic Notations
What is an Algorithm
• Definition
– An Algorithm is a finite set of instructions that,
if followed, accomplishes a particular task.
Characteristics of an Algorithm
All algorithms must satisfy the following criteria:
(1)Input: There are zero or more quantities that are
externally supplied.
(2)Output: At least one quantity is produced.
(3)Definiteness: Each instruction is clear and
unambiguous.
(4)Finiteness: If we trace out the instructions of an
algorithm, then for all cases, the algorithm terminates
after a finite number of steps.
(5)Effectiveness: Every instruction must be basic enough
to be carried out, in principle, by a person using only
pencil and paper. It is not enough that each operation be
definite and also must be feasible.
Study of Algorithms
• The study of algorithms includes many important and active
areas of research.
• Validate an Algorithm:
• Once an algorithm is devised, it is necessary to show that it
computes the correct answer for all possible legal inputs.
• Once the validity of the method has been shown, a program can
be written and a second phase begins.
Analyze and Test the Algorithms
• 3. Analyze the Algorithm
– Analysis of algorithms or performance analysis refers to the task
of determining how much computing time and storage an
algorithm requires.
• 4. Test a Program
– Testing a program consists of two phases: debugging and
profiling (or performance measurement).
– Debugging is the process of executing programs on sample data
sets to determine whether faulty results occur and, if so, to
correct them.
– A proof of correctness is much more valuable than a thousand
tests(if that proof is correct),since it guarantees that the program
will work correctly for all possible inputs.
– Profiling or performance measurement is the process of executing
a correct program on datasets and measuring the time and space
it takes to compute the result.
Topics
• Algorithm Specification
• Pseudocode Convention
• Performance Analysis
• Asymptotic Notations
Describing Algorithms
• Natural language
– English
• Instructions must be definite and effectiveness
• Graphic representation
– Flowchart
• work well only if the algorithm is small and simple
• Pseudocode
– Readable
– Instructions must be definite and effectiveness
Pseudocode
• Pseudocode:
– Implementation of an algorithm in the form of annotations and
informative text written in plain English.
– It has no syntax like any of the programming language and thus
can’t be compiled or interpreted by the computer.
• Advantages of Pseudocode
– Improves the readability of any approach.
– Acts as a bridge between the program and the process. Also works
as a rough documentation, so the logic of one developer can be
understood easily when a pseudo code is written out.
– Explains what exactly each line of a program should do, hence
making the code construction phase easier for the programmer.
• Disadvantages of Pseudocode
– Pseudocode does not provide a visual representation of the logic
of programming.
– There are no proper format for writing the for pseudocode.
Pseudocode Conventions
• 1. Comments begin with // and continue until the end of line.
node = record
{
data type_1 data;
.
.
.
data type_n data;
node *link;
}
Pseudocode Conventions
• 5. Assignment of values to variables is done using the assignment statement
variable:=expression or value;
• 6. There are two Boolean values true and false. In order to produce these
values, the logical operators and, or, and not and the relational operators <,
and > are provided.
• 7. Elements of multi dimensional arrays are accessed using ‘[‘ and ‘]’.
– For example, if A is a two dimensional array, the (i,j)th element of the array is denoted as -
A[i,j].
• 8. The while, repeat-until and for loops takes the following form:
while (condition)do Repeat for variable:= value l to value 2 step value do
{ { {
statement 1 … Statement 1 …. (statement 1) ….
statement n Statement n (statement n)
} } until(condition) }
Pseudocode Conventions
• 9. A conditional statement has the following forms:
– if (condition) then statement;
– if (condition) then statement 1 else statement 2;
– case
{
: condition 1: statement 1 …..
: condition n: statement n
: else: statement n + 1
}
• 10. Input and output are done using the instructions read and write.
• Algorithm Specification
• Pseudocode Convention
• Performance Analysis
• Asymptotic Notations
Performance Evaluation
• Evaluate a program
– MWGWRERE
Meet specifications, Work correctly,
Good user-interface, Well-documentation,
Readable, Effectively use functions,
Running time acceptable,
Efficiently use space
• Fixed space
– Includes the instructions, variables, and constants
– Independent of the number and size of Input and
Output
• Variable space
– Depends on an instance ‘I’ of the problem
– Includes dynamic allocation, functions' recursion
• Total space of any program
– S(P)= c+ Sp(Instance)
Examples of Evaluating Space Complexity
float abc(float a, float b, float c)
{
return a+b+b*c+(a+b-c)/(a+b)+4.00;
}
Ssum(n)= 0
Time Complexity
• Definition
The time complexity, T(p), taken by a program P is the sum of
the compile time and the run time
• How to evaluate?
– Use the system clock (machine dependent)
– Number of steps performed (machine-independent)
2n+ 3
Examples of Determining Steps(Cont.)
void add_matrices(int a[ ], int b[ ], int c[ ], int R, int C)
{
int i, j;
for i=0 to R
{
count++; /* for the for i loop */
for j=0 to C
{ = (R* x)+1
count++; /* for the for j loop */
count++; /* for the addition */ = (R*(2+y))+1
x
y c[i, j]:= a[i, j] + b[i,j]; = (R*(2+(2*C)))+1
}
= (2*R*C+2*R)+1
count++; /* last execution of for j */
}
count++; /* last execution of for i */
}
Examples of Determining Steps(Cont.)
float rsum(float list[], int n)
{
count ++; /* for if condition */
if (n!=1) then
{
count++; /* for return and rsum invocation */
return rsum(list, n-1)+ list[n-1];
}
count++; /* return */ Trsum(1) = 2
return list[0]; Trsum(n) = 2 + Trsum(n-1)
} = 2 +(2 + Trsum(n-2))
= 2*2 + Trsum(n-2)
= 2*2+(2+ Trsum(n-3))
= 3*2+ Trsum(n-3)
= ……………
= (n-1)*2 + Trsum(n-(n-1))
= 2*n-2+2
= 2*n
Examples of Determining Steps(Cont.)
• The second method: build a table to count the number of steps
s/e: steps per execution
frequency: total numbers of times each statements is executed
Statement s/e Frequency Total Steps
Total 2*n +3
Examples of Determining Steps(Cont.)
• The second method: build a table to count
s/e: steps per execution
frequency: total numbers of times each statements is executed
Statement s/e Frequency Total Steps
Total
Examples of Determining Steps(Cont.)
• The second method: build a table to count
s/e: steps per execution
frequency: total numbers of times each statements is executed
Statement s/e Frequency Total Steps
Total 2*R*C+2*R+1
Examples of Determining Steps(Cont.)
• The second method: build a table to count
s/e: steps per execution
frequency: total numbers of times each statements is executed
Statement s/e Frequency Total Steps
Total
Examples of Determining Steps(Cont.)
• The second method: build a table to count
s/e: steps per execution
frequency: total numbers of times each statements is executed
Statement s/e Frequency Total Steps
Total 2*n + 2
Tasks on Performance Analysis
• Write an algorithm for matrix multiplication and calculate its time
complexity.
• Pseudocode Convention
• Performance Analysis
• Asymptotic Notations
Algorithm Analysis
• To analyze the given algorithm, we need to know with which inputs the
algorithm takes less time and with which inputs the algorithm takes a long time.
– Ω (Omega) Notation
– θ (Theta) Notation
– o (Little-oh) Notation
– ⴍ (Little-omega) Notation
O (Big-Oh) Notation-Upper Bounding function
• 2) Nested loops: Analyze from the inside out. Total running time is
the product of the sizes of all the loops.
Guidelines for Asymptotic Analysis
• 3. Consecutive statements: Add the time complexities of each
statement.
Guidelines for Asymptotic Analysis
• 4) If-then-else statements: Worst-case running time: the test,
plus either the then part or the else part (whichever is the
larger).
Guidelines for Asymptotic Analysis
• 5) Logarithmic complexity: An algorithm is O(logn) if it takes a
constant time to cut the problem size by a fraction (usually by
½).
Examples on Performance Analysis
• https://www.geeksforgeeks.org/miscellaneous-
problems-of-time-complexity/
• https://www.geeksforgeeks.org/analysis-
algorithms-set-5-practice-problems/?ref=rp