0% found this document useful (0 votes)
5 views38 pages

Introduction and Analysis of an Algorithm

The document provides an overview of data structures, their types, features, and operations, emphasizing their importance in organizing and manipulating data efficiently. It distinguishes between linear and non-linear data structures, discusses abstract data types, and outlines the analysis of algorithms, including time and space complexity. Various examples and practical applications of data structures in everyday scenarios are also presented.

Uploaded by

rohangupta102000
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 views38 pages

Introduction and Analysis of an Algorithm

The document provides an overview of data structures, their types, features, and operations, emphasizing their importance in organizing and manipulating data efficiently. It distinguishes between linear and non-linear data structures, discusses abstract data types, and outlines the analysis of algorithms, including time and space complexity. Various examples and practical applications of data structures in everyday scenarios are also presented.

Uploaded by

rohangupta102000
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/ 38

Introduction

• A data structure is a way to organize and store data in a computer so


that it can be efficiently accessed, modified, and manipulated.
• A data structure provides a way to manage large amounts of data,
making it possible to perform operations such as:-
• Storing and retrieving data
• Sorting and searching data
• Inserting, deleting, and modifying data
• Manipulating data to extract useful information
Need of data structure

Examples
• Phone contact list
• Dictionary
• Library bookshelf
Simple real-life examples where data structures are used:
1. Phonebook or Contact List-
Data Structure: Array or Linked List
How it works: Stores names and phone numbers of contacts, allowing for
efficient insertion, deletion, search, and sorting.

2. Music Playlist-
Data Structure: Linked List or Stack-
How it works: Stores songs in a specific order, allowing for efficient addition and
removal of songs, as well as playback in a specific order.

3. Online Shopping Cart-


Data Structure: Array or Linked List-
How it works: Stores items added to the cart, allowing for efficient addition and
removal of items, as well as calculation of total cost.
4. Traffic Light Control System-
Data Structure: Queue-
How it works: Stores traffic signals in a specific order, allowing for
efficient management of traffic flow and ensuring that signals are
displayed in a specific order.

5. Library Bookshelf-
Data Structure: Array or Linked List-
How it works: Stores books in a specific order, allowing for efficient
addition and removal of books, as well as searching for specific books.

And many more example see in your daily life


feature of data structures
1. Efficient Data Storage: Data structures provide a way to store data in a compact
and organized manner, reducing memory usage.

2. Fast Data Retrieval: Data structures enable fast retrieval of data, making it
possible to perform operations quickly.

3. Data Manipulation: Data structures provide a way to manipulate data, such as


inserting, deleting, and modifying elements.

4. Scalability: Data structures can handle large amounts of data, making them
scalable.
5. Improved Performance: Data structures can improve the performance
of algorithms by reducing the time complexity.

6. Simplified Code: Data structures can simplify code by providing a


standard way of organizing and manipulating data.

7. Reusability: Data structures can be reused in different programs,


reducing code duplication.

8. Abstraction: Data structures provide a level of abstraction, allowing


programmers to focus on the logic of the program without worrying
about the details of data storage and manipulation.
Types of data structure

Linear data structure Non - Linear data structure


Linear data structure
• A linear data structure is a type of data structure in which the elements are
arranged in a linear sequence, one after the other.
• Each element is connected to its previous and next element, and the elements
are accessed in a sequential manner.
• At most 2 neighbours
• Examples
• Arrays
• Linked List
• Stack and Queues
Non - Linear data structure
• A non-linear data structure is a type of data structure in which the elements are
not arranged in a linear sequence.
• Instead, the elements are connected to each other in a more complex way, and
the elements are accessed in a non-sequential manner.
• Can have more than 2 neighbours
• Examples
• Tree
• Graph
• Hashing (linear or non linear both)
Data Structure Operations:
Data structure operations are the actions that can be performed on a data
structure to manipulate its elements or modify its structure.

Here are some common data structure operations:


Traversal Operations
1. Access: Retrieve a specific element from the data structure.
2. Search: Find the location of a specific element within the data structure.
3. Traversal: Visit each element in the data structure in a specific order.

Insertion Operations
1. Insert: Add a new element to the data structure.
2. Push: Add a new element to the top of a stack or queue.
3. Enqueue: Add a new element to the end of a queue.
Deletion Operations
1. Delete: Remove a specific element from the data structure.
2. Pop: Remove an element from the top of a stack or queue.
3. Dequeue: Remove an element from the front of a queue.

Modification Operations
1. Update: Modify the value of an existing element in the data structure.
2. Replace: Replace an existing element with a new one.

Sorting and Ordering Operations


1. Sort: Arrange the elements of the data structure in a specific order.
2. Reverse: Reverse the order of the elements in the data structure.

Miscellaneous Operations
1. Size: Return the number of elements in the data structure.
2. IsEmpty: Check if the data structure is empty.
3. Clear: Remove all elements from the data structure.
Abstract data type
• An abstract data type (ADT) is a high-level description of a data structure that
defines its behaviour and operations, without specifying how it is implemented.
• An ADT is a mathematical model that defines the interface and behaviour of a
data structure, without worrying about the details of how it is stored or
manipulated.
An ADT typically includes:
• A set of values: The ADT defines the type of values it can hold.
• A set of operations: The ADT defines the operations that can be performed
on the values, such as insertion, deletion, search, etc.
• A set of axioms: The ADT defines the rules and constraints that govern the
behaviour of the operations.
Examples
Stack: push and pop operations
Queue: enqueue and dequeue operations
Concreate Data structure Vs Abstract data type
S. No. Abstract Data Types or structure (ADT) Concrete Data Types or structure (CDT)

Abstract Data Types or structures describe the data and Concrete data types or structures provide how these
1
the operations to manipulate and change it. operations are actually implemented.

Most of the program becomes independent of the


Which is not possible in Concrete Data Types or
2 abstract data types representation, so it can be improved
structure (CDT)
without breaking the program.

It’s easier for each part of a program to use an


3 implementation of its data types and that will be more It is not so efficient compared to ADT.
efficient.

4 Implementation of a high level concept Implementation of a simple concept

5 It is usable beyond its original use. It is rarely reusable beyond its original use.

6 It hides the internal details. It doesn’t hide anything.

7 It uses class. It uses structure.

8 Examples- lists, sets, stacks. Examples-Arrays, linked lists, trees, graphs.


Analysis of an Algorithm
Algorithm
• An Algorithm consists finite number of steps to solve any problem.
• Every step involves some operations and each operation must be definite and effective.
• Every algorithm may take zero / more inputs
• Every algorithm must produce at least one output
• Every algorithm should terminate in finite amount time.
Algorithm life cycle
Testing and
debugging

Step - 8
Life Cycle Steps

Step - 4
Develop the
algorithm
Analysis of algorithm
• WHY
• WHAT
• HOW
Needs of Analysis (WHY?)
• Performance comparison to find out efficient solution (algorithm)
• To determine resource consumption
(WHAT?) Time, space, register, cost

• Time complexity of an algorithm quantifies the amount of time taken


by an algorithm to run as a function of the input size.
• Space complexity of an algorithm quantifies the amount of space or
memory taken by an algorithm to run as a function of input size.
Methodology of Analysis (HOW)
[xy+z]
• language
• O.S
• H/w
• CPU
• Memory + IO
Aposteriori analysis
{ platform dependent}
Advantage Drawback/ limitation
• Always gives exact value of time • It is difficult to carry out (manual work)
complexity and space • Cannot consider for all case of inputs
complexity
• Non uniformity : performance
comparison becomes difficult
• System to system result varies
2. Apriori analysis
{ platform independent}
• Take into account all possible inputs
• Allows us to calculates the relative efficiency( platform) of two
algorithm in a way that is independent of platform
• Can be carried out by studying the high level description of algorithm
without actual implementation
• It is easy to carry out

✓Drawback
• Will not give real/actual values in unit
• Estimates/ approximates
Components of analytic frame work
• A language( pseudo) for describing algorithm steps
• A computation modal(RAM model) that the algorithm
executes within it
• A matric for measuring algorithm running tie
• An approach/ notation to characterize running time.
algorithm test Step count Methode
{
x  y + z;
for i  1 to n
a  b + c; Each fundamental operation/
basic operation takes one unit of
for i  1 to n time { RAM model of
{ computation}
for i  1 to n
a  b + c;
}
}
Order of magnitude
To determine the time of algorithm
under aproiri analysis
• Order of magnitude of a statement/ step of the algorithm refers to
the frequency/ count of the fundamental operation in the
statement/ step

• The basic objective of apriori analysis is to represent (obtain) the


time complexity (running time) of the algorithm by means of a
mathematical function with respect to input size (say n)
T(n) = 1 + n + n2
T(n) = n + 1
T(n) = 4 (c)
Time ∝ funtion

Polynomial Exponential

Logarithmic

• Exponential function have higher rate of growth (takes more time)


• Polynomial function have lesser rate of growth (takes less time)
• The objective is always to develop algorithm for problems having polynomial
time complexity (efficient

Constants < logarithms < polynomial < exponential


𝒏
c < log n < 𝒏 < n < n log n < 𝒏𝟐 < 𝒏𝟑 <...< 𝟐𝒏 < 𝟑𝒏 < 𝐧𝒏 < 𝒏 𝒏
Apriori analysis (objective)

• To determine the running time with respect to increasing input


size(n)
• To observe the behaviour of the algorithm for a fixed input of size n (
behaviour / type of analysis)
Algorithm LS (A, n, x)
{
integer n, A[n]
int i;
for i 1 to n
{
if(x==A[i])
{
Print(i)
Exit;
}
}
Print(“ele not found”);
}
Types of Analysis
• Worst Case
The input class for which the algorithm does maximum time is worst case input
& corresponding time is worst case time.
• Best Case
The input class for which the algorithm does minimum time is best case input
& corresponding time is best case time.
• Average Case
Average case can be calculated form best case to worst case.
It is derived in 3 step
Enumerate all input classes (i1, i2, i3,…….in) 𝑘
Determine the time for each input class(t1, t2, t3,…..tn) 𝐴 𝑛 = ෍ 𝑝𝑖 ∗ 𝑡𝑖
Associated with each input class the probability function (pi) 𝑖=1
Asymptotic Notations
• Asymptotic notation is a way to describe the performance or complexity of
an algorithm, which is the amount of time or space it requires as the input
size increases.

• Mathematical tool to obtain/ represent bounds of function


• Upper bound
• Lower bound
• Tight bound
Types of Asymptotic Notations
Asymptotic Notations

Big Notations Small / little Notations

1. Big oh : O 1. Little oh : o
2. Big omega : Ω 2. Little omega : 𝜔
3. Theta : 𝜃
Big O – Notation
• This notation gives an upper bound on the Time
complexity of an algorithm. It is used to describe c g(n)
the worst-case scenario, i.e., the maximum
amount of time or space an algorithm requires.
• It represent upper bound f(n)

• F(n) in O(g(n)) if and only if there exits some


constant c > 0 and n0 > 0 such that f(n) ≤ c. g(n) ,
whenever n ≥ n0
• F(n) = O(g(n))
n0 n
• F(n) ∈ O(g(n))
f(n) = O(g(n))
Big omega (Ω) - Notation
Time
• This notation gives a lower bound on the f(n)
complexity of an algorithm. It is used to
describe the best-case scenario, i.e., the
minimum amount of time or space an algorithm c g(n)
requires.
• It represent lower bound
• F(n) in Ω(g(n)) if and only if there exits some
constant c > 0 and n0 > 0 such that f(n) ≥ c. g(n)
, whenever n ≥ n0 n0 n
• F(n) = Ω(g(n)) f(n) = 𝛺(g(n))
Theta (𝜃) - Notation
c2 g(n)

• This notation gives a tight bound on the Time f(n)


complexity of an algorithm. It is used to
describe the exact complexity of an
algorithm, i.e., the amount of time or c1 g(n)
space it requires in all cases.
• It represent tight bound
• F(n) in 𝜃(g(n)) if and only if f(n) is O(g(n))
& f(n) is Ω(g(n))
n0 n
• c1. g(n) ≤ f(n)≤ c2. g(n)
f(n) = 𝜃(g(n))
Properties with respect to asymptotic
notations
Reflexive Symmetric Transitive Transpose symmetric
Big oh (O)
Big omega (𝜴)
Theta (𝜽)
Small oh (o)
Small omega (𝝎)
Space Complexities
• Space complexity is a measure of how much memory an algorithm
needs to allocate in order to solve a problem

Space complexity S(P)

C
Sp
Input space + Work space

Additional space
for computation
Input space steps like stack,
linked list etc…
S(P) = C + Sp
Thank You

You might also like