0% found this document useful (0 votes)
62 views25 pages

Amity - Mod-1 - L - 1introduction To Algorithms

Here are the time complexities of the algorithms in the examples: Example1: The for loop iterates from 1 to n. So the statement inside the for loop executes n times. Therefore, the time complexity of this algorithm is O(n). Example2: The for loop condition is i<n but the updating expression is missing. So it becomes an infinite loop. The time complexity of an infinite loop is O(∞) which is simply written as O(1) according to conventions. So the time complexities are: Example1: O(n) Example2: O(1)

Uploaded by

Prajjwal Mehra
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
62 views25 pages

Amity - Mod-1 - L - 1introduction To Algorithms

Here are the time complexities of the algorithms in the examples: Example1: The for loop iterates from 1 to n. So the statement inside the for loop executes n times. Therefore, the time complexity of this algorithm is O(n). Example2: The for loop condition is i<n but the updating expression is missing. So it becomes an infinite loop. The time complexity of an infinite loop is O(∞) which is simply written as O(1) according to conventions. So the time complexities are: Example1: O(n) Example2: O(1)

Uploaded by

Prajjwal Mehra
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 25

Amity School of Engineering & Technology (CSE)

Analysis and Design of Algorithm(ADA)

Introduction

Dr. Garima Aggarwal


Associate Professor-ASET

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)

 What is an Algorithm in General?


It is a finite set of instruction which specifies a sequence of operation.

 Why we study an Algorithm?


Problem Solving, Challenging

 Why we need an Algorithm?

The Algorithm gives a clear description of requirements and goal of the


problem to the designer. A good design can produce a good solution.

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)

 How to write an Algorithm

Syntax 1: Syntax 2: Syntax 3: Syntax 4:


Algorithm swap(a,b) Algorithm swap(a,b) Algorithm swap(a,b) 1. Start ADD
{ begin begin 2. Get values of a
and b
temp = a; temp := a; temp  a;
3. c a + b
a = b; a := b; a  b;
4. Display c
b = temp; b := temp; b  temp;
5. Stop
} end end

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)

Difference between priori analysis and posteriori analysis

Efficiency of an algorithm can be analyzed at two different stages, before


implementation and after implementation.

A Priori Analysis − This is a theoretical analysis of an algorithm. Efficiency of an


algorithm is measured by assuming that all other factors, for example, processor 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 programming language. This is then executed on target
computer machine. In this analysis, actual statistics like running time and space
required, are collected.

13
Amity School of Engineering & Technology (CSE)

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).​

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)

 Find the time complexity for the following iterative algorithm:

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)

Algorithm Design Techniques


1. Divide and Conquer Approach: It is a top-down approach based on
dividing the problems into sub-problems.
2. Greedy Technique: Greedy method is used to solve the optimization
problem.
3. Dynamic Programming: Dynamic Programming is a bottom-up approach
we solve all possible small problems and then combine them to obtain
solutions for bigger problems.
4. Branch and Bound: In Branch & Bound algorithm a given sub-problem,
which cannot be bounded, has to be divided into at least two new restricted
sub-problems.
5. Randomized Algorithms: A randomized algorithm is defined as an
algorithm that is allowed to access a source of independent, unbiased
random bits, and it is then allowed to use these random bits to influence its
computation.
6. Backtracking Algorithm: Backtracking Algorithm tries each possibility until
they find the right one.
23
Amity School of Engineering & Technology (CSE)

Practice Questions

24
Amity School of Engineering & Technology (CSE)

25

You might also like