UNIT 1 FDS Updated
UNIT 1 FDS Updated
.
Syllabus:
• Introduction: From Problem to Program (Problem, Solution, Algorithm,
Data Structure and Program). Data Structures: Data, Information,
Knowledge, and Data structure, Abstract Data Types (ADT), Data
• Structure Classification (Linear and Non-linear, Static and Dynamic,
Persistent and Ephemeral data
• structures).
• Algorithms: Problem Solving, Introduction to algorithm, Characteristics
of algorithm, Algorithm design tools: Pseudo-code and flowchart.
Complexity of algorithm: Space complexity, Time complexity,
Asymptotic notation- Big-O, Theta and Omega, finding complexity
using step count method, Analysis of programming constructs-Linear,
Quadratic, Cubic, Logarithmic. Algorithmic Strategies: Introduction to
algorithm design strategies- Divide and Conquer, and Greedy strategy.
WHY?
• They enable efficient data manipulation,
making it easier to preprocess and prepare
data for modeling.
• The choice of data structure often depends on
the nature of the data and the ML model
used.
• Understanding data dimensions helps you
grasp how your data evolves, improving
feature engineering and model design.
ALGORITHMS
ALGORITHM – PROBLEM SOLVING
COMPUTER :
“Computer is multi purpose Electronic Machine which is
used for storing , organizing and processing data by set of
program
Problem :
“Problem is defined as situation or condition which needs
to solve to achive goal”
3. Merging solution
6. Solve by analogy.
Solve by analogy.
Step 6 − print c
Step 7 − STOP
ALGORITHM DESIGN TOOL
• There can be two tools :
1. Flowchart
2. Pseudo Code
Flowchart :
“ Flowchart is graphical representation of the algorithms”
Pseudo Code :
“It is simply an implementation of an algorithm in the form
of annotations and informative text written in plain English.
FLOWCHART
Symbols used in Flowchart :
EXAMPLE OF FLOWCHART
EXAMPLE FOR ALGORITH & FLOWCHART
Step1: Start
Step2: Initialize the count variable to zero
Step3: Initialize the sum variable to zero
Step4: Read a number say x
Step 5: Add 1 to the number in the count
variable Step6: Add the number x to the sum
variable.
Step7:yes,
50?If Is the countthe
display variable in the
sum: go memory
to step 8.
greater than
If No, Repeat from step 4
Step8: Stop
Design an algorithm and flowchart to input
fifty numbers and calculate their sum.
WRITE A PROGRAM FOR ADDING 10 NUMBERS
WRITE A PROGRAM TO FIND FACTORIAL OF NUMBER
DIFFERENT APPROCHES TO DESIGN
ALGORITHMS
Types of approach :
1. Top down approach
2. Bottom up approach
TOP DOWN APPROACH BOTTOM UP APPROACH
1. Larger problem divided into smaller Smaller pieces are combined together
2. Execution Start from top to down Execution start from bottom to top
Time complexity :
“The time which is required for analysis of given problem
of particular size is known as time complexity”
Space complexity :
“The amount of computer memory required to solve the
given problem of particular size is called as space
complexity”
1.The operations and values in user defined data types is not specified
by language itself but specified by the user.
Examples:
Structure, Union,Enum
2. In structure we are defining our own data type including other data
type.
struct point
{
int x;
int y;
}
ABSTRACT DATA TYPES
f(n) = 5n2 + 6n + 12
where n is the number of instructions executed, and it depends on the size of the input.
When n=1
% of running time due to 5n2 = * 100 = 21.74%
n 5n2 6n 12
Data :
“Data is nothing but collection of information i.e.
facts or figures.”
Data Object :
“Data object is a of storage that
region
contains a value or group of value”
NEED OF DATA STRUCTURE
1. Stores huge data
6. Better algorithms
ABSTRACT DATA TYPE
ADT :
“Abstract data types are mathematical models of a set of data
values or information that share similar behavior or qualities and that can
be specified and identified independent of specific implementations.
Abstract data types, or ADTs, are typically used in algorithms.”
Another definition
of ADT is ADT is set of D,
F and A.
D – domain = Data
object
F – function = set of operations which can carried out on data
TYPES OF DATA STRUCTURE
There are two types :
1. Primitives data structure
2. Non-primitive data structure
TYPES OF DATA STRUCTURE
1. Primitives data structure :
2. Merge sort
3. Recursive algorithm
4. Backtracking algorithms
5. Heuristic algorithms
disks. The objective of the puzzle is to move the entire stack to another rod,
2)Each move consists of taking the upper disk from one of the stacks and
placing it on top of another stack i.e. a disk can only be moved if it is the
Algorithm Steps :
.
GREEDY STRATEGIES
2. Prims algorithm: Prim’s Algorithm also use Greedy approach to find the
minimum spanning tree. In Prim’s Algorithm we grow the spanning tree
from a starting position. Unlike an edge in Kruskal's, we add
vertex to the growing spanning tree in Prim's.
Algorithm Steps:
2.Find all the edges that connect the tree to new vertices, find the minimum
and add it to the tree.
• In nested if and if else ladder statements also the initial if statement is executed at
least once but inner statements will be executed based on the previous statements’
execution.
3. Loop statements:
• Loop statements are iterative statements. They are executed one or more times
based on a given condition.
• A typical for(i = 0; i ≤ n; i++) statement will be executed “n+1” times for the first
n times the condition is satisfied and the inner loop will be executed and for the
(n+1)th time the condition is failed and the loop terminates.
• While: The statement is executed until the given
condition is satisfied.
• Do while: The statement will repeat until the given
condition is satisfied. The do-while statement will
execute at least once because for the first time it will
not check the condition.
4. Functions:
• Functions are executed based on the number of times
they get called. If they get called n times they will be
executed n times. If they are not called at least once
then they will not be executed. Other statements
like BEGIN, END and goto statements will be
executed one time.
switch(expression) if (condition1)
• { {
// Executes when condition1 is true
• case value1: statement_1; if (condition2)
• break; {
• case value2: statement_2; // Executes when condition2 is true
}
• break; else
• . {
• . // Executes when condition2 is false
• .
}
• case value_n: statement_n;
• break;
• default: default_statement;
• }
Analysis of Linear Search algorithm
Let us consider a Linear Search Algorithm.
Linearsearch(arr, n, key)
{
i = 0;
for(i = 0; i < n; i++)
{
if(arr[i] == key)
{
printf(“Found”);
}
}
Where,
i = 0, is an initialization statement and takes O(1) times.
for(i = 0;i < n ; i++), is a loop and it takes O(n+1) times .
if(arr[i] == key), is a conditional statement and takes O(1)
times.
printf(“Found”), is a function and that takes O(0)/O(1)
times.
Therefore Total Number of times it is executed is n + 4 times.
As we ignore lower exponents in time complexity total time
became O(n).
Time complexity: O(n).
Simple Examples
#include <iostream>
using namespace std;
int main()
{
int i, n = 8;
for (i = 1; i <= n; i++) {
cout << "Hello World !!!\n";
}
return 0;
} # time complexity O(n)
• #include <iostream>
• using namespace std;
• int main()
• {
• int i, n = 8;
• for (i = 1; i <= n; i=i*2)
• {
• cout << "Hello World !!!\n";
• }
• return 0;
• } #time complexity O(log2(n))
• #include <iostream>
• #include <cmath>
• using namespace std;
• int main()
• {
• int i, n = 8;
• for (i = 2; i <= n; i=pow(i,2))
• {
• cout << "Hello World !!!\n";
• }
• return 0; } # time complexity O(log(log n))
Recurrence Relation
Recurrence relation :
“A recurrence relation is an equation that recursively defines a
.
Recurrence Relation
Types Recurrence relation :
1. Linear recurrence relations –
Following are some of the examples of recurrence relations based on linear
recurrence relation.
T(n) = T(n-1) + n for n>0 and T(0) = 1
These types of recurrence relations can be easily soled using substitution
method (Put link to substitution method).
For example,
T(n) = T(n-1) + n
= T(n-2) + (n-1) + n
= T(n-k) + (n-(k-1))….. (n-1) + n
Substituting k = n, we get
T(n) = T(0) + 1 + 2+….. +n = n(n+1)/2 = O(n^2)
Recurrence Relation
Types Recurrence relation :
1. Homogeneous linear recurrence relation –
Homogeneous refers to the fact that the total degree of each term is the
same (thus there is no constant term) Constant Coefficients refers to the
fact that c1,c2,...,ck are fixed real numbers that do not depend on n. ...
The recurrence relation An = (1.04)An−1 is a linear homogeneous
recurrence relation of degree one.
.
Type of Recurrence Relation
Generating Functions
Generating Functions represents sequences where each term of a sequence
is expressed as a coefficient of a variable x in a formal power series.
Mathematically, for an infinite sequence, say a0,a1,a2,…,ak,…,a0,a1,a2,…,ak,…, the
generating function will be −
Gx=a0+a1x+a2x2+⋯+akxk+⋯=∑akxk
Some Areas of Application
Generating functions can be used for the following purposes −
-For solving a variety of counting problems. For example, the number of ways to
make change for a Rs. 100 note with the notes of denominations Rs.1, Rs.2, Rs.5,
Rs.10, Rs.20 and Rs.50
- For solving recurrence relations
- For proving some of the combinatorial identities
- For finding asymptotic formulae for terms of sequences