Data Structure and Algorithms
Data Structure and Algorithms
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.
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.
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
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