FDS Unit 1
FDS Unit 1
1. Fundamentals:
A data structure is a way of organizing, processing, retrieving and storing data. There
are several basic and advanced types of data structures, all designed to arrange data to
suit a specific purpose. Data structures make it easy for users to access and work with
the data they need in appropriate ways. Most importantly, data structures frame the
organization of information so that machines and humans can better understand it.
Abstract Data type (ADT) is a type (or class) for objects whose behavior is defined
by a set of values and a set of operations. The definition of ADT only mentions what
operations are to be performed but not how these operations will be implemented. It
does not specify how data will be organized in memory and what algorithms will be
used for implementing the operations. It is called “abstract” because it gives an
implementation-independent view.
The process of providing only the essentials and hiding the details is known as
abstraction.
Fig. ADT
The user of data type does not need to know how that data type is implemented, for example,
we have been using Primitive values like int, float, char data types only with the knowledge
that these data type can operate and be performed on without any idea of how they are
implemented.
2.1.2 Non-primitive data structures store data of more than one type. Non-primitive
data structures can contain NULL values. Size not fixed. Ex- Array, linked list, stacks,
queues.
2.3.2 In dynamic data structures, the size of the data structure is not fixed and can be
modified (increased or decreased) during the operations performed on it. Dynamic
data structures are designed to facilitate change of data structures in the runtime.
Example: linked list
2.4.2 Ephemeral data structure is a data structure that does not preserve the previous
version of itself. Only the current state of data structure is maintained. Since no
maintenance of previous versions makes the overall structure simplier.
Example: Linked list, arrays
o Input: An algorithm has some input values. We can pass 0 or some input value to an
algorithm.
o Output: We will get 1 or more output at the end of an algorithm.
o Unambiguity: An algorithm should be unambiguous which means that the instructions
in an algorithm should be clear and simple.
o Finite-ness: The algorithm must be finite, i.e. it should terminate after a finite time.
o Effectiveness: An algorithm should be effective as each instruction in an algorithm
affects the overall process.
o Language independent: An algorithm must be language-independent so that the
instructions in an algorithm can be implemented in any of the languages with the same
output.
3.3.1 Time complexity: The time complexity of an algorithm is the amount of computer time
it need to run to complete.
In simple language, every algorithm requires some amount of computer time to execute its
instructions to perform specific task. This time is called as time complexity.
Step count:
i. For algorithm heading = 0 time
ii. For braces = 0
iii. For expressions = 1 unit of time
iv. For any looping statement = number of times the loop is repeating
Example:
i. Algorithm abc(d, e, f)…………………………………………0 time
{………………………………………………………………0 time
Return d + e * f + (d + e + f)/(d + e) + 4.0………….1 unit of time
} …………………………………………………………….0 time
Total time = 1 unit
Assume n = 0
Then T (0) = 2 // 1unit time for if statement and 1 for return statement
Assume n > 0
Then T(n) = 2+ T(n-1)
T(n) = 2+(2+T(n-2))
= 2*2+T(n-2)
= 2* 2+(2+T(n-3))
= 2*3+T(n-3)
.
.
.
= 2 * n + T(n-n)
= 2n+T(0) //T (0)=2; from above
T(n) = 2n+2
iv. Write a C/C++ code to find maximum between N numbers, where N varies from
10, 100, 1000, 10000. In linux operating system, if we run the program with the
following commands:
gcc program.c -o program
time ./program
result:
for N = 10……………………..0.5 ms time
for N = 10,000 ………………0.2 ms time may require
Consider the following two scenarios to more understand the time complexity:
Suppose you are having one problem and you have 3 algorithms for the same problem. Now
among these 3 algorithms you want to choose the best one. How to choose it?
Solution 1: run all the 3 algorithms on 3 different computers. Provide same input and find
time taken by all 3 algorithms and choose the one who took less time. But in this solution
there is possibility that these systems might be using different processors, so processing speed
might vary. So this solution is not efficient.
Solution 2: run all 3 algorithms on same computer and find which algorithm is taking least
time. But here also we might get wrong results because at the time of execution of a program,
other things are also running simultaneously. So this solution is also not efficient.
So there should be some standard notation to analyze the algorithm. These notations are
called as “Asymptotic Notations”.
In asymptotic notation, system configuration is not considered. Rather order of growth of
input will be considered.
The study of change in performance of the algorithm with the change in the order of the input
size is defined as asymptotic analysis.
Will try to find out how time or space taken by the algorithm will increase/decrease after
increasing/decreasing input size.
There are 3 asymptotic notations that are used to represent time complexity of an
algorithm:
a. Big O Notation
b. Omega Ω Notation
c. Theta Ɵ Notation
Usually, time required by an algorithm falls under 3 types:
a. Best case: minimum time required for program execution.
b. Average case: average time required for program execution.
c. Worst case: maximum time required for program execution.
Example:
Consider an array of size 5. If we want to find out one particular element from say [1, 2, 3, 4,
5] i.e. consider we want to find 1, then we found it at the very first position. So this is best
case. Now suppose array elements are [2, 3, 4, 5, 1] and we want to find 1 again. It is present,
but this time at the last position. So this can be considered as average case. Again if the array
is [2, 4 ,5, 3, 1] and if we are trying to find element 6 then this can be considered as worst
case scenario as 6 is not present in the array.
1. Big O Notation:
Example:
1. F(n) = 2n + 2, g(n) = n2
Note: f (n) should always be less than or equal to g(n)
Let n = 1
F(1) = (2*1) + 2 = 4 g(1) = 12 = 1 //f(1)>g(1)
Let n = 2
F(2) = (2*2) + 2 = 6 g(2) = 22 = 4 //f(2)>g(2)
Let n = 3
F(3) = (2*3) + 2 = 8 g(3) = 32 = 9 //f(3)<g(3)
Hence, f(n) = O(g(n)), where n>=3 and g(n) is bounding above when n>=3
2. Omega Ω Notation:
It denotes the lower bound of an algorithm i.e. the time taken by the algorithm cannot
be lower than this.
In other words, this is the fastest time taken algorithm when provided with best case
input.
A function f(n) is said to be in Ω(g(n)) denoted by f(n) = Ωg(n)), if f(n) is bounded
below by some constant multiple of g(n) for all large ‘n’.
i.e. if there exists some positive constant ‘c’ and some non-negative integer n0 such
that
f(n) > = c*g(n) for all n>=n0
Example:
F(n) = 2n2 + 3 g(n) = 7n
Note: f(n) should always be greater than or equal to g(n)
Let n = 1
F(1) = 2 * (12) + 3 = 5 g(1) = 7 * 1 = 7 //f(1)<g(1)
Let n = 2
F(2) = 2 * (22) + 3 = 11 g(2) = 7 * 2 = 14 //f(2)<g(2)
Let n = 3
F(3) = 2 * (23) + 3 = 19 g(3) = 7 * 3 = 21 //f(3)<g(3)
3. Theta Ɵ Notation:
A function f(n) is said to be Ɵ(g(n)), denoted as f(n) = Ɵ(g(n)), if f(n) is bounded both
above and below by some positive constant multiples of g(n) for all large ‘n’.
Ɵ is used to find out the average bound of an algorithm i.e. it defines upper bound and
lower bound and your algorithm will lie between these levels.
Fixed part: is independent of instance characteristic. i.e. space for simple variables,
constants, etc (int a; int b)
Variable part: is space for variables whose size is dependent on particular problem
instance.
Example: array
Example:
i. Algorithm max(A, n)
{
Result = A[1];
For i = 2 to n do
If A[i]>result then
Result = A[i];
Return = A[i];
}
Answer:
Variables i, n, result = 1 unit each // 4 bytes each, which is fixed, can’t be changed
Variable A = n units // n * 4 bytes, completely depends on ‘n’
Total = n + 4
Removing constant value:
Space complexity: O(n)