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

Data Structure and Algorithm

Uploaded by

danielbarbe756
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

Data Structure and Algorithm

Uploaded by

danielbarbe756
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 19

DATA STRUCTURES AND ALGORITHMS

Chapter 1: Introduction to Algorithm

Topic : Introduction to Algorithm

Mr. Umesh Kumar,


Lecturer,
CopyrightFICT
2024 ©, ISBAT University, All rights reserved.
Objective
2

 To understand various algorithm design techniques.


 To understand time and space complexity.
 To understand asymptotic notation to analyze the algorithm.

© ISBAT UNIVERSITY – 2024 05/03/24


Algorithm
3

 An algorithm is a sequence of computational steps that transform the


input into the output.
 We can also view an algorithm as a tool for solving a well-specified
computational problem.
 In combination we can say that algorithm is the step by step
procedure to solve any problem.
 An algorithm is said to be correct if, for every input instance, it halts
with the correct output.

© ISBAT UNIVERSITY – 2024. 05/03/24


Algorithms Design Techniques
4

 We can classify algorithms in various ways. They are:

- Classification by Implementation Method


- Classification by Design Method
- Classification by Design Approach

© ISBAT UNIVERSITY – 2024. 05/03/24


Classification by Implementation Method
5

 Recursion or Iteration: A recursive algorithm is an algorithm which


calls itself again and again until a base condition is achieved.
- Iterative algorithms use loops and/or data structures like stacks,
queues to solve any problem.
 Exact or Approximate: Algorithms that are capable of finding an
optimal solution for any problem are known as the exact algorithm.
- For all those problems, where optimized solution is not found, an
approximation algorithm is used. Approximate algorithms are the
type of algorithms that find the result as an average outcome of sub
outcomes to a problem.
© ISBAT UNIVERSITY – 2024. 05/03/24
6

 Serial or Parallel or Distributed Algorithms: In serial algorithms,


one instruction is executed at a time.
- Parallel algorithms are those in which we divide the problem into sub
problems and execute them on different processors.
- If parallel algorithms are distributed on different machines, then they
are known as distributed algorithms.

© ISBAT UNIVERSITY – 2024. 05/03/24


Classification by Design Method
7

 Greedy Method: In the greedy method, at each step, a decision is


made to choose the local optimum, without thinking about the future
consequences.
 Divide and Conquer: The Divide and Conquer strategy involves
dividing the problem into sub-problem, recursively solving them, and
then recombining them for the final answer.
 Backtracking: This technique is very useful in solving combinatorial
problems that have a single unique solution.

© ISBAT UNIVERSITY – 2024. 05/03/24


Classification by Design Approach
8

 Top-Down Approach: In the top-down approach, a large problem is


divided into small sub-problem and keep repeating the
process of decomposing problems until the complex problem is
solved.
 Bottom-up approach: The bottom-up approach is also known as the
reverse of top-down approaches. Building a system by starting with
the individual components and gradually integrating them to form a
larger system.

© ISBAT UNIVERSITY – 2024. 05/03/24


Time and Space Complexity of Algorithms
9

 There are different ways to solve a problem in computer science with


different algorithms.

 Therefore it is required to use a method to compare the solutions in


order to find which algorithm is optimal.

 There are two such methods are:


 Time Complexity
 Space Complexity

© ISBAT UNIVERSITY – 2024. 05/03/24


Time Complexity
10

 The time required by the algorithm to solve given problem is called


time complexity of the algorithm.
 Time complexity is very useful measure in algorithm analysis.
 To estimate the time complexity, we need to consider the cost of each
fundamental instruction and the number of times the instruction is
executed.

© ISBAT UNIVERSITY – 2024. 05/03/24


Example: Addition of two scalar variables
11

Algorithm ADD SCALAR(A, B)


//Description: Perform arithmetic addition of two numbers
//Input: Two scalar variables A and B
//Output: variable C, which holds the addition of A and B
C <- A + B
return C

 The addition of two scalar numbers requires one addition operation.


the time complexity of this algorithm is constant, so T(n) = O(1) .
© ISBAT UNIVERSITY – 2024. 05/03/24
Space Complexity
12

 The amount of memory required by the algorithm to solve given


problem is called space complexity of the algorithm.
 To estimate the memory requirement we need to focus on two parts:

 (1) A fixed part: It is independent of the input size. It includes


memory for instructions (code), constants, variables, etc.

 (2) A variable part: It is dependent on the input size. It includes


memory for recursion stack, referenced variables, etc.

© ISBAT UNIVERSITY – 2024. 05/03/24


Example: Addition of two scalar variables
13

Algorithm ADD SCALAR(A, B)


//Description: Perform arithmetic addition of two numbers
//Input: Two scalar variables A and B
//Output: variable C, which holds the addition of A and B
C <- A + B
return C

The addition of two scalar numbers requires one extra memory location
to hold the result. Thus the space complexity of this algorithm is constant,
hence S(n) = O(1).
© ISBAT UNIVERSITY – 2024. 05/03/24
Execution Time Cases
14

 Worst Case − This is the scenario depicting the maximum execution


time of an operation of a data structure.

 Average Case − This is the scenario depicting the average execution


time of an operation of a data structure.

 Best Case − This is the scenario depicting the least possible


execution time of an operation of a data structure.

© ISBAT UNIVERSITY – 2024. 05/03/24


Asymptotic Notation
15

 Asymptotic Notations are mathematical tools that allow you to


analyze an algorithm’s running time by identifying its behavior as its
input size grows.
 This is also referred to as an algorithm’s growth rate.
 We can’t compare two algorithm’s head to head.
 We can compare space and time complexity using asymptotic
analysis.
 It compares two algorithms based on changes in their performance as
the input size is increased or decreased.

© ISBAT UNIVERSITY – 2024. 05/03/24


Types of Asymptotic notations
16

 Theta Notation (Θ-notation)


 It represents the upper and the lower bound of the running time of an
algorithm, it is used for analyzing the average-case complexity of an
algorithm.
 Big-O Notation (O-notation)
 It represents the upper bound of the running time of an algorithm.
Therefore, it gives the worst-case complexity of an algorithm. It is the most
widely used notation for Asymptotic analysis.
 Omega Notation (Ω-notation)
 It represents the lower bound of the running time of an algorithm. Thus, it
provides the best case complexity of an algorithm.
© ISBAT UNIVERSITY – 2024. 05/03/24
Exercise – To Do
17

 Write an Algorithm to calculate the simple interest.


 Write an Algorithm to calculate the area of circle.
 Write an Algorithm to find the largest between three numbers.

© ISBAT UNIVERSITY – 2024. 05/03/24


Learning Outcome
18

 What is data structure and algorithm.


 The difference between linear and non-linear data structures
 The most common linear data structures, including arrays, linked lists, and stacks
and queues
 The most common non-linear data structures, including trees and graphs
 How to design algorithms.
 Time complexity and space complexity to analyze the performance of algorithm.

© ISBAT UNIVERSITY – 2024. 05/03/24


19

Thank you

© ISBAT UNIVERSITY – 2024. 05/03/24

You might also like