FDS Unit I
FDS Unit I
• Algorithmic Strategies
• Recursive Algorithm
• Backtracking Algorithm
Fundamentals of Data Structures
Introduction to Algorithm and Data Structure
Algorithm
Program
Programming Language
Introduction to Algorithm and Data Structure
Algorithm is a sequence of clear and precise step-by-
step instructions for solving a problem in a finite
amount of time.
Algorithms are implemented by translating the step-
by-step instructions into a computer program that
can be executed by a computer. This translation
process is called computer programming or simply
programming.
Computer programs are constructed using a
programming language appropriate to the
problem.
Introduction to Algorithm and Data Structure
Data structure is a representation of data and the
operations allowed on that data.
Problem
Solution
Algorithm
Data Structure
Program
Introduction : From Problem to Program
Problem
Solution
Algorithm
Data Structure
Program
Algorithm
Algorithm is a sequence of clear and precise step-by-
step instructions for solving a problem in a finite
amount of time.
Data
Information
Knowledge
Data Structure
Data
Data Structure
Information
Knowledge
Knowledge
Abstract Data Type
Data Abstraction
What a data type can do
How it is done is hidden
Defines a particular data structure in terms of data
and operations
An ADT consists of
Definition
Declaration of data
Declaration/List of operations
Basic Data Structure
Data Structure Classification
Linear and Non Linear
Static and Dynamic
Persistent and Ephemeral
Sequential Access and Direct Access
Linear Data Structure
Non-Linear Data Structure
Data Structure Classification
Static and Dynamic
Sr. Static Data Structure Dynamic Data Structure
No.
Pseudo-code
Flowchart
Pseudocode
Pseudo code is a method of describing algorithms using a
combination of natural language and programming lang.
BEGIN
NUMBER s1, s2, sum
OUTPUT("Input number1:")
INPUT s1
OUTPUT("Input number2:")
INPUT s2
sum=s1+s2
OUTPUT sum
END
Python Program
Q.3 To read any 10 Numbers and Display its total and average
Algorithm
Flowchart
Psudo-code
Q.3 Menu driven program for area calculation of circle, rectangle and
triangle
Algorithm
Flowchart
Psudo-code
Fundamentals of Data Structures
Algorithm Analysis
The time and space implemented by the Algorithm X are the two main factors
which determine the efficiency of X.
The complexity of an algorithm f(N) provides the running time and / or storage
space needed by the algorithm with respect of N.
Time Complexity
Space Complexity
Time Complexity
The algorithm that performs the task in the smallest number of operations is
considered the most efficient one.
Input Size: Input size is defined as total number of elements present in the
input. For a given problem we characterize the input size n appropriately.
For example:
Sorting problem: Total number of item to be sorted
Graph Problem: Total number of vertices and edges
Numerical Problem: Total number of bits needed to represent a number
The algorithm that performs the task in the smallest number of operations is
considered the most efficient one.
Input Size: Input size is defined as total number of elements present in the
input. For a given problem we characterize the input size n approproately.
Fot example:
Sorting problem: Total number of item to be sorted
Graph Problem: Total number of vertices and edges
Numerical Problem: Total number of bits needed to represent a number
Run all the three algorithms on three different computers, provide same input
and find the time taken by all the three algorithms and choose the one that is
taking the least amount of time.
Run all the three algorithms on the same computer and try to find the time
taken by the algorithm and choose the best.
So, we have seen that we can't judge an algorithm by calculating the time taken
during its execution in a particular system.
Best
Average
Asymptotic Notation
Logarithmic O(log n)
Linear O(n)
Quadratic O(n2)
Cubic O(n3)
Exponential O(2n)
Analysis of programming constructs-
linear quadratic cubic logarithmic
Constant-Time Algorithms - O(1)
A constant-time algorithm is one that takes the same
amount of time, regardless of its input.
In most cases, we use 2 as the base of the log, but it doesn't matter
which base because we ignore constants.
●Binary search
●Searching a tree data structure
Analysis of programming constructs-
linear quadratic cubic logarithmic
● Compare each item of a list against all the other items in the list
● Fill in a n-by-n game board
Analysis of programming constructs-
linear quadratic cubic logarithmic
Cubic-Time Algorithms - O(n3)
A cubic-time algorithm is one that takes a number of steps proportional to n3.
In other words, if the size of the input increases by one, the number of steps
doubles.
Best
Average
Big-O Notation (O-notation)
Represents the upper bound of the running time of an algorithm.
Thus, it gives the worst case complexity of an algorithm.
Definition: f(n) = O(g(n)) (read as “f(n) is Big Oh of g(n)”) iff positive constants
c and n0 exist such that f(n) ≤ cg(n) for all n, n ≥ n0.
Asymptotic Notation
Big-O Notation (O-notation)
For any value of n, the running time of an algorithm does not cross time
provided by O(g(n)).
Since it gives the worst case running time of an algorithm, it is widely used to
analyze an algorithm as we are always interested in the worst case scenario.
Big-O Notation (O-notation)
f(n) = n2+2n+1
f(n) = O g(n)
f(n) <= c g(n)
f(n) belongs to the set Ω(g(n)) if there exists a positive constant c such that it
lies above cg(n), for sufficiently large n.
For any value of n, the minimum time required by the algorithm is given by
Omega Ω(g(n)).
Theta Notation, θ
Represents the upper and the lower bound of the running time of an algorithm
Used for analyzing the average case complexity of an algorithm
Definition: f(n) = Q(g(n)) (read as “f(n) is theta of g(n)”) if positive constants c1,
c2 and n0 exist such that c1g(n) ≤ f(n) ≤ c2g(n) for all n, n ≥ n0.
eeAsymptotic Notation
Theta Notation, θ
For a function g(n), Θ(g(n)) is given by the relation:
If a function f(n) lies anywhere in between c1g(n) and c2 > g(n) for all n ≥ n0,
then f(n) is said to be asymptotically tight bound.
Big-O Notation (O-notation)
7n+2≤_________
O(?)
Omega Notation (Ω-notation)
n2 n 0<=c.n<=n2 n2=Ω(n)
Total Cost=C1+C2+C3+C4
=O(1)
Finding Complexity using Step
count Method
Cost Frequency Total Cost
Total Cost=
Finding Complexity using Step
count Method
Cost Frequency Total Cost
C1 1 C1
C2 1 C2
C3 1 C3
Total Cost=C1+C2+C3
O(1)
Finding Complexity using Step
count Method
S(P)=C+Sp
Algorithm structure
–Iterative (execute action in loop)
–Recursive
Problem type
Satisfying
Optimization
Algorithmic Strategies
Algorithmic Strategies
•Recursive algorithms
•Backtracking algorithms
•Divide and conquer algorithms
•Dynamic programming algorithms
•Greedy algorithms
•Brute force algorithms
•Branch and bound algorithms
•Heuristic algorithms
Algorithmic Strategies -Recursive
algorithms
Algorithmic Strategies -Recursive algorithms
Algorithmic Strategies
Backtracking algorithms
Algorithmic Strategies
Backtracking algorithms
N - Queens problem is to place n - queens in such a manner on an n x n
chessboard that no queens attack each other by being in the same row, column
or diagonal.
Algorithmic Strategies
4-Queens Problem
Algorithmic Strategies
•Backtracking algorithms
Algorithmic Strategies
•Backtracking algorithms
8 Queens Problem
Algorithmic Strategies
Algorithmic Strategies
•Recursive algorithms
•Backtracking algorithms
•Divide and conquer algorithms
•Dynamic programming algorithms
•Greedy algorithms
•Brute force algorithms
•Branch and bound algorithms
•Heuristic algorithms
Algorithmic Strategies - Divide and
conquer
Algorithmic Strategies - Divide and
conquer
Algorithmic Strategies - Divide and
conquer
Algorithmic Strategies - Divide and
conquer
Merge Sort
Fundamentals of Data Structures
Algorithmic Strategies – Greedy
Algorithm
For most optimization problems you want
to find, not just a solution, but the best
solution.
A greedy algorithm sometimes works
well for optimization problems. It works in
phases. At each phase:
Take the best you can get right now, without
regard for future consequences.
Hope that by choosing a local optimum at each
step, you will end up at a global optimum.
Algorithmic Strategies – Greedy
Algorithm
Suppose you want to count out a certain
amount of money, using the fewest
possible coins
A greedy algorithm to do this would be:
At each step, take the largest possible
coin
Example: To make 12 RS, you can choose:
10 RS Coin, 5 RS Coin, 2 RS Coin, 1 Rs Coin
Algorithmic Strategies
•Recursive algorithms
•Backtracking algorithms
•Divide and conquer algorithms
•Dynamic programming algorithms
•Greedy algorithms
•Brute force algorithms
•Branch and bound algorithms
•Heuristic algorithms
Fundamentals of Data Structures
Case Study
Multiplication Technique by the
mathematician Carl Friedrich Gauss and
Karatsuba Algorithm for Fast
Multiplication
Case Study
Multiplication Technique
Case Study
Multiplication Technique by Divide and
Conquer
Case Study
Multiplication Technique by Divide and
Conquer
Case Study
Multiplication Technique by Divide and
Conquer
x0*y0, x0*y1, x0*y0, x0*y1,
Case Study
Multiplication Technique by Karatsuba
Algorithm for Fast Multiplication
Fundamentals of Data Structures
UNIT II
Thank You