Amity - Mod-1 - L - 1introduction To Algorithms
Amity - Mod-1 - L - 1introduction To Algorithms
Introduction
1
Amity School of Engineering & Technology (CSE)
2
Amity School of Engineering & Technology (CSE)
3
Amity School of Engineering & Technology (CSE)
4
Amity School of Engineering & Technology (CSE)
5
Amity School of Engineering & Technology (CSE)
Learning Outcomes
Students will be able to
Design,
Analyze a
Apply Knowledge Implement and
problem and
of Mathematics, Understand and evaluate a
identify and
Science, Analyze the computer-based
define the
Engineering and recursive and system, process,
computing
computing non recursive component or
requirements
approaches to algorithms programmer to
appropriate to its
the problem. meet desired
solution.
results.
6
Amity School of Engineering & Technology (CSE)
Module Objectives
Understand to
Understand all Algorithm
How to Analyze calculate time
the concepts of Design
the Algorithm and space
algorithm. Techniques
complexity
7
Amity School of Engineering & Technology (CSE)
8
Amity School of Engineering & Technology (CSE)
Definition
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.
Algorithm vs Program?
9
Amity School of Engineering & Technology (CSE)
10
Amity School of Engineering & Technology (CSE)
Correctness
Finiteness
Characteristics of Algorithm An Absence of Ambiguity
Definition of Sequence
Input/output
Feasibility
How to analyze an
Flexibility
Algorithm
Efficient
Time Complexity
Independent
Space Complexity
Network Consumption
Power Consumption
CPU Registers
11
Amity School of Engineering & Technology (CSE)
12
Amity School of Engineering & Technology (CSE)
13
Amity School of Engineering & Technology (CSE)
#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).
14
Amity School of Engineering & Technology (CSE)
Space Complexity
Space Complexity of an algorithm/program is the amount of memory it needs to run
to completion. Space needed by an algorithm is sum of following components:
• Fixed Part
• Variable Part
15
Amity School of Engineering & Technology (CSE)
Space Complexity
Space Complexity of an algorithm/program is the amount of memory it needs to run
to completion. Space needed by an algorithm is sum of following components:
• Fixed Part
• Variable Part
16
Amity School of Engineering & Technology (CSE)
Q.1)
Q.2)
•
Q.3)
17
Amity School of Engineering & Technology (CSE)
Step Count:
The idea is to count the instructions that are used by the given algorithm to
perform the given task.
The idea is to find the step count (called steps per execution s/e) of each
instruction.
Frequency is the number of times the instruction is executed. The total
count can be obtained by multiplying the frequency and steps per execution.
Time complexity=
18
Amity School of Engineering & Technology (CSE)
Operation Count:
The idea is to count all the operations like add, sub, multiplication and
division. Some of the operations that are typically used are assignment
operations, Comparison operations, Arithmetic operations and logical
operations. The time complexity is then given as the number of repetitions of
the basic operation as a function of input size.
Time complexity=
Amity School of Engineering & Technology (CSE)
Check your progress1
Example1: Example2:
A() A()
{ {
int i,j;
for(i=1; i<=n, i++)
for(i=1;i<=n,i=i*2)
for(j=1; j<=n; j++) printf("Amity");
printf("Amity"); }
}
Example3: Example4:
Assume n>=2 A()
A() {
{
While(n>1)
for(i=1; i<=n; i++)
{ for(i=1;i<=n,i=i*2)
n=n/2; {
} printf("Amity");
} }
}
20
Amity School of Engineering & Technology (CSE)
Check your progress1
Example5: Example6:
A() A()
{
int i,j,k;
{
for(i=1; i<=n; i++) int i,j,k,n
{ for(i=1; i<=n;i++)
for(j=1; j<=i; j++) {
{ for(j=1; j<=i2;j++)
for(k=1;k<=500;k++) {
{ for(k=1;k<=n/2;k++)
printf("Amity"); {
}
} printf("Amity");
} }
}
}
}
21
Amity School of Engineering & Technology (CSE)
22
Amity School of Engineering & Technology (CSE)
Practice Questions
24
Amity School of Engineering & Technology (CSE)
25