Module 1 A
Module 1 A
Introduction
Module 1 - Part 1
Compiled By:
Dr. Prabhishek Singh
Learning Objectives
2
Table of content
1. What is an Algorithm in General?
2. Why study Algorithm?
3. Algorithm (Definition)
4. Characteristics of Algorithm
5. Need of Algorithm
6. Difference between Algorithm & Program
7. Difference between priori analysis and posteriori analysis
8. How to write an Algorithm
9. How to analyze an Algorithm (Complexity)
10. Algorithm Design Techniques
11. Frequency Count Method for Time and Space Complexity
3
What is an Algorithm in General?
4
Why study Algorithm?
1. Important for all other
branches of computer science.
Theoretical Reasons
2. Plays a key role in modern
technologies innovation.
3. Problem solving: Design new
Algorithm à Analyze efficiency
à Implement à Experiment
Practical Reasons
and Test Results.
4. Challenging (good for brain!!).
5. Fun.
5
Algorithm (Definition)
1. Step by step procedure for solving the computational
problem.
2. An algorithm can be defined as a well-defined
computational procedure that takes some values, or
the set of values, as an input and produces some
value, or the set of values, as an output.
3. An algorithm is thus a sequence of computational
steps that transform the input into output.
4. It describes specific computational procedures for
achieving the input-output relationship.
6
Example of Algorithm: 1
1. Correctness
2. Finiteness
3. An Absence of Ambiguity
4. Definition of Sequence
5. Input/output
6. Feasibility
7. Flexibility
8. Efficient
9. Independent
9
Need of Algorithm
1. To understand the basic idea of the problem.
2. To find an approach to solve the problem.
3. To improve the efficiency of existing techniques.
4. To understand the basic principles of designing the
algorithms.
5. To compare the performance of the algorithm with respect to
other techniques.
6. It is the best method of description without describing the
implementation detail.
7. The Algorithm gives a clear description of requirements and
goal of the problem to the designer.
8. A good design can produce a good solution.
10
Need of Algorithm
9. To understand the flow of the problem.
10. To measure the behavior (or performance) of the methods in
all cases (best cases, worst cases, average cases)
11. With the help of an algorithm, we can also identify the
resources (memory, input-output) cycles required by the
algorithm.
12. With the help of algorithm, we convert art into a science.
13. To understand the principle of designing.
14. We can measure and analyze the complexity (time and
space) of the problems concerning input size without
implementing and running it; it will reduce the cost of design.
11
Difference between Algorithm & Program
12
Difference between Algorithm & Program
13
A Priori vs A Posteriori Analysis
14
How to write an Algorithm
16
How to analyze an Algorithm (Complexity)
18
Frequency Count Method for Time Complexity
• Time Complexity of any algorithm/program is not the measure of actual time taken for the program
to be executed, rather it is the number of times each statement of the logic gets executed to
produce the required output.
• In simpler terms, here is what we mean. Let us consider the below code.
#include <stdio.h>
void main()
{ int i, n = 5;
for (i = 1; i <= n; i++) {
printf("FACE Prep n");
}}
• So, the above code when executed using a compiler has given the below output. If you can see,
the compiler shows that the code was executed in 1.166 secs and a lot of us assume this to be
time complexity, but it isn't.
• Rather, the time complexity of this code is dependent on the number of time the statements get
executed. Here, the for loop gets executed 'n' number of times and hence complexity is
O(n).
19
• O(1): Time complexity of a function (or set of
statements) is considered as O(1) if it doesn’t contain
loop, recursion and call to any other non-constant time
function.
// set of non-recursive and non-loop statements
• For example swap() function has O(1) time complexity.
A loop or recursion that runs a constant number of times
is also considered as O(1). For example the following
loop is O(1).
// Here c is a constant
for (int i = 1; i <= c; i++)
{
// some O(1) expressions
20
}
• O(n): Time Complexity of a loop is considered as
O(n) if the loop variables is incremented /
decremented by a constant amount. For example
following functions have O(n) time complexity.
• // Here c is a positive integer constant
for (int i = 1; i <= n; i += c) {
// some O(1) expressions
}
for (int i = n; i > 0; i -= c) {
// some O(1) expressions
}
21
• O(n2): Time complexity of nested loops is equal to the number of
times the innermost statement is executed. For example the
following sample loops have O(n2) time complexity
23
• O(LogLogn): Time Complexity of a loop is considered
as O(LogLogn) if the loop variables is reduced /
increased exponentially by a constant amount.
25
Space Complexity
• Compute the space complexity of an algorithm by
analyzing the storage requirements (as a function on the
input size) in the same way.
26
Space Complexity
27
THANK YOU
28