0% found this document useful (0 votes)
5 views

Module 1 A

The document is a comprehensive guide on the analysis and design of algorithms, aimed at BTECH CSE students in their 5th semester. It covers fundamental concepts such as definitions, characteristics, and the importance of algorithms, as well as methods for analyzing time and space complexity. Additionally, it discusses various algorithm design techniques and provides examples to illustrate key points.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Module 1 A

The document is a comprehensive guide on the analysis and design of algorithms, aimed at BTECH CSE students in their 5th semester. It covers fundamental concepts such as definitions, characteristics, and the importance of algorithms, as well as methods for analyzing time and space complexity. Additionally, it discusses various algorithm design techniques and provides examples to illustrate key points.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 28

Amity School of Engineering and Technology

Analysis and Design of Algorithms

Introduction
Module 1 - Part 1

BTECH CSE - Semester 5TH

Compiled By:
Dr. Prabhishek Singh
Learning Objectives

• Understand all the basic concepts of


algorithm.
• Understand to calculate the time and
space complexity.

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?

1. It is a finite set of instruction.


2. It specifies a sequence of operation.
3. It is carried out in order to solve a specific
problem or class of problems is called an
Algorithm.

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

Algorithm to add two numbers entered by the


user
Step 1: Start
Step 2: Declare variables num1, num2 and sum.
Step 3: Read values num1 and num2.
Step 4: Add num1 and num2 and assign the result
to sum.
sum←num1+num2
Step 5: Display sum
Step 6: Stop 7
Example of Algorithm: 2
Algorithm to find the largest among three different numbers entered by
the user
Step 1: Start
Step 2: Declare variables a,b and c.
Step 3: Read variables a,b and c.
Step 4: If a > b
If a > c
Display a is the largest number.
Else
Display c is the largest number.
Else
If b > c
Display b is the largest number.
Else
Display c is the greatest number.
8
Step 5: Stop
Characteristics of Algorithm

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

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.

14
How to write an Algorithm

We can use any syntax to write an algorithm.


Syntax 1: Syntax 2:
Algorithm swap (a, b) Algorithm swap (a, b)
{ begin
temp = a; temp := a;
a = b; a := b;
b = temp; b := temp;
} end
15
How to write an Algorithm
Syntax 3:
Algorithm swap (a, b)
begin
temp ß a;
a ß b;
b ß temp;
end

16
How to analyze an Algorithm (Complexity)

Algorithm analysis is an important part of computational complexity


theory, which provides theoretical estimation for the required resources
of an algorithm to solve a specific computational problem.

1.Time Complexity: Running time of a program as a function of the


size of the input.
2.Space Complexity: Some forms of analysis could be done based on
how much space an algorithm needs to complete its task. This space
complexity analysis was critical in the early days of computing when
storage space on the computer was limited. When considering this
algorithm are divided into those that need extra space to do their work
and those that work in place.
3.Network Consumption
4.Power Consumption
5.CPU Registers 17
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.

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

for (int i = 1; i <=n; i += c) {


for (int j = 1; j <=n; j += c) {
// some O(1) expressions
}
}

for (int i = n; i > 0; i -= c) {


for (int j = i+1; j <=n; j += c) {
// some O(1) expressions
}
For example Selection sort and Insertion Sort
have O(n^2) time complexity. 22
• O(log n): Time Complexity of a loop is considered as
O(log n) if the loop variable is divided / multiplied by a
constant amount.
for (int i = 1; i <=n; i *= c) {
// some O(1) expressions
}
for (int i = n; i > 0; i /= c) {
// some O(1) expressions
}
• For example Binary Search(refer iterative
implementation) has O(log n) 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.

// Here c is a constant greater than 1


for (int i = 2; i <=n; i = pow(i, c)) {
// some O(1) expressions
}
//Here fun is sqrt or cuberoot or any other constant root
for (int i = n; i > 1; i = fun(i)) {
// some O(1) expressions
}
24
How to combine time complexities of
consecutive loops?

• When there are consecutive loops, we calculate time complexity as


sum of time complexities of individual loops.

for (int i = 1; i <=m; i += c) {


// some O(1) expressions
}
for (int i = 1; i <=n; i += c) {
// some O(1) expressions
}
• Time complexity of above code is O(m) + O(n) which is O(m+n)
• If m == n, the time complexity becomes O(2n) which is O(n).

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

You might also like