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

Data Structure and Algorithms

This document discusses various topics related to algorithms including: 1. Introduction to algorithms, their properties, categories and structure. 2. Algorithm design methods like greedy, divide and conquer. 3. Asymptotic analysis including Big-O, Omega and Theta notations to analyze time complexity. 4. Examples of algorithms to find largest of 3 numbers and quadratic equation roots.

Uploaded by

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

Data Structure and Algorithms

This document discusses various topics related to algorithms including: 1. Introduction to algorithms, their properties, categories and structure. 2. Algorithm design methods like greedy, divide and conquer. 3. Asymptotic analysis including Big-O, Omega and Theta notations to analyze time complexity. 4. Examples of algorithms to find largest of 3 numbers and quadratic equation roots.

Uploaded by

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

Topics to be discussed

Introduction to Algorithm
Algorithm Design
Complexity
Asymptotic notations
Introduction to Data Structures
Classification of Data structure
Abstract Data Type (ADT)
Introduction to Algorithm
An algorithm is a Step By Step process to solve a
problem, where each step indicates an intermediate task.
Algorithm contains finite number of steps that leads to the
solution of the problem.
Structure of an Algorithm
An algorithm has the following structure
Input Step
Assignment Step
Decision Step
Repetitive Step
Output Step
Properties /Characteristics of
an Algorithm
Algorithm has the following basic properties
 Input-Output:- Algorithm takes ‘0’ or more input and
produces the required output. This is the basic characteristic
of an algorithm.
 Finiteness:- An algorithm must terminate in countable
number of steps.
Properties /Characteristics of
an Algorithm
Definiteness: Each step of an algorithm must be stated
clearly and unambiguously.
Effectiveness: Each and every step in an algorithm can be
converted in to programming language statement.
Generality: Algorithm is generalized one. It works on all
set of inputs and provides the required output. In other
words it is not restricted to a single input value.
Categories of Algorithm
Based on the different types of steps in an Algorithm, it
can be divided into three categories, namely
Sequence
Selection and
Iteration
Categories of Algorithm
Sequence: The steps described in an algorithm are performed
successively one by one without skipping any step. The sequence
of steps defined in an algorithm should be simple and easy to
understand. Each instruction of such an algorithm is executed,
because no selection procedure or conditional branching exists in
a sequence algorithm.
Example:
// adding two numbers
Step 1: start
Step 2: read a,b
Step 3: Sum=a+b
Step 4: write Sum
Step 5: stop
Categories of Algorithm
Selection: The sequence type of algorithms are not
sufficient to solve the problems, which involves decision
and conditions. In order to solve the problem which involve
decision making or option selection, we go for Selection
type of algorithm. The general format of Selection type of
statement is as shown below:
if(condition)
Statement-1;
else
Statement-2;
Categories of Algorithm
Categories of Algorithm
Iteration: Iteration type algorithms are used in solving the
problems which involves repetition of statement. In this type
of algorithms, a particular number of statements are repeated
‘n’ no. of times.
Example1:
Step 1 : start
Step 2 : read n
Step 3 : repeat step 4 until n>0
Step 4 : (a) r=n mod 10
(b) s=s+r
(c) n=n/10
Step 5 : write s
Step 6 : stop
Algorithm Design
 Precisely define the problem. Precisely specify the input
and output. Consider all cases.
Come up with a simple plan to solve the problem at hand.

The plan is independent of a (programming) language.

The precise problem specification influences the plan.


Turn the plan into an implementation – The problem
representation (data structure) influences the
implementation
Classification by Design Method
 Greedy Method
 Divide and Conquer
 Dynamic Programming
 Backtracking
 Branch and Bound
Classification by Design Approaches

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.
In approach different, part of a complex program is
solved using a programming language and then this is
combined into a complete program.
1.Write an algorithm for roots of a Quadratic Equation?
// Roots of a quadratic Equation
Step 1 : start
Step 2 : read a,b,c
Step 3 : if (a= 0) then step 4 else step 5
Step 4 : Write “ Given equation is a linear equation “
Step 5 : d=(b * b) _ (4 *a *c)
Step 6 : if ( d>0) then step 7 else step8
Step 7 : Write “ Roots are real and Distinct”
Step 8: if(d=0) then step 9 else step 10
Step 9: Write “Roots are real and equal”
Step 10: Write “ Roots are Imaginary”
Step 11: stop
2. Write an algorithm to find the largest among three different numbers
entered by 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.
Step 5: Stop
Complexity of Algorithm
Algorithmic complexity is concerned about how fast or
slow particular algorithm performs. We define complexity
as a numerical function T(n) - time versus the input size n.
Performance Analysis an Algorithm:
The Efficiency of an Algorithm can be measured by the
following metrics.
Time Complexity:
The amount of time required for an algorithm to complete
its execution is its time complexity. An algorithm is said
to be efficient if it takes the minimum (reasonable)
amount of time to complete its execution.
Complexity of Algorithm
Space Complexity:
The amount of space occupied by an algorithm is known
as Space Complexity. An algorithm is said to be efficient if
it occupies less space and required the minimum amount of
time to complete its execution.
Example
Example

The total frequency counts of the program segments A, B and C given by 1,


(3n+1) and (3n2+3n+1) respectively are expressed as O(1), O(n) and O(n2)
Asymptotic Notations
Asymptotic notations are the mathematical notations used
to describe the running time of an algorithm when the
input tends towards a particular value or a limiting value.
It is often used to describe how the size of the input data
affects an algorithm’s usage of computational resources.
The time required by an algorithm falls under three types
Best Case − Minimum time required for program
execution.
Average Case − Average time required for program
execution.
Worst Case − Maximum time required for program
execution.
Asymptotic Notations
The time required by an algorithm falls under three types
Best Case − Minimum time required for program
execution.
Average Case − Average time required for program
execution.
Worst Case − Maximum time required for program
execution.
Types of Asymptotic Notations
There are mainly three asymptotic notations:
1.Big-O Notation (O-notation)
2.Omega Notation (Ω-notation)
3.Theta Notation (Θ-notation)

Big-O Notation (O-notation)


Big-O notation represents the upper bound of the running
time of an algorithm. It measures the worst case time
complexity or the longest amount of time an algorithm can
possibly take to complete.
Asymptotic Notations
If f(n) describes the running time of an algorithm, f(n) is
O(g(n)) if there exist a positive constant C and positive
integer n0 such that, 0 ≤ f(n) ≤ cg(n) for all n ≥ n0
Example: Find upper bound of running time of constant
function f(n) = 6993.
To find the upper bound of f(n), we have to find c and
n0 such that 0 ≤ f (n) ≤ c.g(n) for all n ≥ n0
0 ≤ f (n) ≤ c × g (n)
0 ≤ 6993 ≤ c × g (n)
0 ≤ 6993 ≤ 6993 x 1
So, c = 6993 and g(n) = 1
Any value of c which is greater than 6993, satisfies the
above inequalities, so all such values of c are possible.
0 ≤ 6993 ≤ 8000 x 1 → true
0 ≤ 6993 ≤ 10500 x 1 → true
Function f(n) is constant, so it does not depend on
problem size n. So n0= 1
f(n) = O(g(n)) = O(1) for c = 6993, n0 = 1
f(n) = O(g(n)) = O(1) for c = 8000, n0 = 1 and so on.
Example: Find upper bound of running time of a linear
function f(n) = 6n + 3.
To find upper bound of f(n), we have to find c and n 0 such
that 0 ≤ f (n) ≤ c × g (n) for all n ≥ n0
0 ≤ f (n) ≤ c × g (n)
0 ≤ 6n + 3 ≤ c × g (n)
0 ≤ 6n + 3 ≤ 6n + 3n, for all n ≥ 1 (There can be such
infinite possibilities)
0 ≤ 6n + 3 ≤ 9n
So, c = 9 and g (n) = n, n0 = 1
Tabular Approach
0 ≤ 6n + 3 ≤ c × g (n)
0 ≤ 6n + 3 ≤ 7 n
Now, manually find out the proper n0, such that f (n) ≤ c.g
(n)
n f(n) = 6n + 3 c.g(n) = 7n

1 9 7
2 15 14
3 21 21
4 27 28
5 33 35
From Table, for n ≥ 3, f (n) ≤ c × g (n) holds true. So, c =
7, g(n) = n and n0 = 3, There can be such multiple pair of
(c, n0)
f(n) = O(g(n)) = O(n) for c = 9, n0 = 1
f(n) = O(g(n)) = O(n) for c = 7, n0 = 3
and so on.
Omega Notation (Ω-Notation)
Omega notation represents the lower bound of the
running time of an algorithm. It measures the best case time
complexity or the best amount of time an algorithm can
possibly take to complete.
If f(n) describes the running time of an algorithm, The
function f is said to be Ω(g), if there is a constant c > 0 and a
natural number n0 such that c*g(n) ≤ f(n) for all n ≥ n0
Omega Notation (Ω-Notation)
Example
Find lower bound of running time of constant function
f(n) = 23.
To find lower bound of f(n), we have to find c and n0 such
that { 0 ≤ c × g(n) ≤ f(n) for all n ≥ n0 }
0 ≤ c × g(n) ≤ f(n)
0 ≤ c × g(n) ≤ 23
0 ≤ 23.1 ≤ 23 → true
0 ≤ 12.1 ≤ 23 → true
0 ≤ 5.1 ≤ 23 → true
Example: Find lower bound of running time of a linear
function f(n) = 6n + 3.
To find lower bound of f(n), we have to find c and n 0 such
that 0 ≤ c.g(n) ≤ f(n) for all n ≥ n 0
0 ≤ c × g(n) ≤ f(n)
0 ≤ c × g(n) ≤ 6n + 3
0 ≤ 6n ≤ 6n + 3 → true, for all n ≥ n 0
0 ≤ 5n ≤ 6n + 3 → true, for all n ≥ n 0
Above both inequalities are true and there exists such
infinite inequalities. So,
f(n) = Ω (g(n)) = Ω (n) for c = 6, n0 = 1
f(n) = Ω (g(n)) = Ω (n) for c = 5, n0 = 1

Theta Notation (Θ-Notation)
Theta notation represents the 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.
The function f is said to be Θ(g), if there are constants c1,
c2 > 0 and a natural number n0 such that c1* g(n) ≤ f(n) ≤
c2 * g(n) for all n ≥ n0
Theta Notation (Θ-Notation)
Find tight bound of running time of constant function f(n)
= 23.
To find tight bound of f(n), we have to find c1, c2 and
n0 such that, 0 ≤ c1× g(n) ≤ f(n) ≤ c2 × g(n) for all n ≥ n0
0 ≤ c1× g(n) ≤ 23 ≤ c2 × g(n)
0 ≤ 22 ×1 ≤ 23 ≤ 24 × 1, → true for all n ≥ 1
0 ≤ 10 ×1 ≤ 23 ≤ 50 × 1, → true for all n ≥ 1
Above both inequalities are true and there exists such
infinite inequalities.
So, (c1, c2) = (22, 24) and g(n) = 1, for all n ≥ 1
(c1, c2) = (10, 50) and g(n) = 1, for all n ≥ 1
f(n) = Θ (g (n)) = Θ (1) for c1 = 22, c2 = 24, n0 = 1
f(n) = Θ (g (n)) = Θ (1) for c1 = 10, c2 = 50, n0 = 1
and so on.
Example: Find tight bound of running time of a linear
function f(n) = 6n + 3.
 To find tight bound of f(n), we have to find c1, c2 and
n0 such that, 0 ≤ c1× g(n) ≤ f(n) ≤ c2 × g(n) for all n ≥ n0
0 ≤ c1× g(n) ≤ 6n + 3 ≤ c2 × g(n)
0 ≤ 5n ≤ 6n + 3 ≤ 9n, for all n ≥ 1
Above inequality is true and there exists such infinite
inequalities.
So, f(n) = Θ(g(n)) = Θ(n) for c1 = 5, c2 = 9, n0 = 1
Data Structure
Data structure is a representation of data and the
operations allowed on that data.

Data structure is a way to store and organize data in order


to facilitate the access and modifications.

Data Structure are the method of representing of logical


relationships between individual data elements related to
the solution of a given problem.
Operations on the Data
Structures
Following operations can be performed on the data
structures:
Traversing
Searching
Inserting
Deleting
Sorting
Merging
Operations on the Data Structures
Traversing- It is used to access each data item exactly once so
that it can be processed.
Searching- It is used to find out the location of the data item if
it exists in the given collection of data items.
Inserting- It is used to add a new data item in the given
collection of data items.
Deleting- It is used to delete an existing data item from the
given collection of data items.
Sorting- It is used to arrange the data items in some order i.e.
in ascending or descending order in case of numerical data and
in dictionary order in case of alphanumeric data.
Merging- It is used to combine the data items of two sorted
files into single file in the sorted form.
Application of Data Structures
The following are some of the most important areas
where data structures are used:
Artificial intelligence
Compiler design
Machine learning
 Database design and management
Blockchain
Numerical and Statistical analysis
Operating system development
 Image & Speech Processing
Cryptography
Basic Data Structure
array

Linked list

queue
tree stack
Selection of Data Structure
The choice of particular data model depends on two
consideration:
It must be rich enough in structure to represent the
relationship between data elements
The structure should be simple enough that one can
effectively process the data when necessary
Types of Data Structure
Linear: In Linear data structure, values are arrange in
linear fashion.
Array: Fixed-size
Linked-list: Variable-size
Stack: Add to top and remove from top
Queue: Add to back and remove from front
Priority queue: Add anywhere, remove the highest
priority
Types of Data Structure
Non-Linear: The data values in this structure are not
arranged in order.
Hash tables: Unordered lists which use a ‘hash
function’ to insert and search
Tree: Data is organized in branches.
Graph: A more general branching structure, with less
strict connection conditions than for a tree
Type of Data Structures
Homogenous: In this type of data structures, values of the
same types of data are stored.
Array

Non-Homogenous: In this type of data structures, data


values of different types are grouped and stored.
Structures
Classes
Abstract Data Type and Data
Structure
Definition:-
Abstract Data Types (ADTs) stores data and allow various
operations on the data to access and change it.
A mathematical model, together with various operations
defined on the model
An ADT is a collection of data and associated operations for
manipulating that data
Data Structures
Physical implementation of an ADT
data structures used in implementations are provided in a
language (primitive or built-in) or are built from the
language constructs (user-defined)
Each operation associated with the ADT is implemented by
one or more subroutines in the implementation
Abstract Data Type(ADT)
ADTs support abstraction, encapsulation, and information
hiding.

Abstraction is the structuring of a problem into well-


defined entities by defining their data and operations.

The principle of hiding the used data structure and to only


provide a well-defined interface is known as
encapsulation.
The Core Operations of ADT
Every Collection ADT should provide a way to:
add an item
remove an item
find, retrieve, or access an item

Many, many more possibilities


is the collection empty
make the collection empty
give me a sub set of the collection
The Core Operations of ADT
• No single data structure works well for all purposes, and
so it is important to know the strengths and limitations
of several of them
Stacks
Collection with access only to the last element inserted
Last in first out
Data4 Top
insert/push
remove/pop Data3

top Data2
make empty
Data1
Queues
Collection with access only to the item that has been
present the longest
Last in last out or first in first out
enqueue, dequeue, front
priority queues and dequeue
Front Back

Data1 Data2 Data3 Data4


List
A Flexible structure, because can grow and shrink on
demand.
Elements can be:
 Inserted
 Accessed
 Deleted
At any position
last
first
Tree
A Tree is a collection of elements called nodes.
One of the node is distinguished as a root, along with a
relation (“parenthood”) that places a hierarchical structure
Root
on the nodes.

You might also like