Ds Chapter 1
Ds Chapter 1
18881a0161 X 18 M
18881a0162 y 19 F
18881a0163 Z 18 M
18881a0164 ABC 19 F
In the above example there are four records.First record contents are
18881a0161 X 18 M
Each row of the above table is called a Record.
6. File:
Group of records is called a File.
Ex: Student file
Student Student name age gender
Roll number
18881a0161 X 18 M
18881a0162 y 19 F
18881a0163 Z 18 M
18881a0164 ABC 19 F
7. Key: The name of the data is called Key.It is also called attribute.
Ex: In the table below,Student Roll number,Student name,age and gender are called
Keys.
Student Student name age gender
Roll number
18881a0161 X 18 M
18881a0162 y 19 F
18881a0163 Z 18 M
18881a0164 ABC 19 F
8.Primary Key:
This key uniquely identifies the record of a file.
Ex:In the table below,Student Roll number is called Primary Key. Student name,age
and gender are not primary keys because more than one student can have same
name,age or gender.
18881a0161 X 18 M
18881a0162 y 19 F
18881a0163 Z 18 M
18881a0164 ABC 19 F
Data Structure Definition:
It is a particular way of storing and organizing data in a computer memory.So that the
data can be updated and retrieved effectively and efficiently.
(or)
It is a Logical way of storing and organizing data in a computer.
1 9 0
A[0] A[1] a[2]
2. Stack:
It is a Linear Data Structure in Which the data is inserted and the data is deleted
from only one side called top.This data Structure follows a principle called
LIFO(Last In First Out) Means the data which is inserted last in the stack will be
removed or deleted first.
Example1:
Here Push is nothing Insert operation ,Pop is nothing but Delete Operation.
Example2:
3. Queue:
It is a Linear Data Structure in Which the data is inserted at one side called rear
and the data is deleted from other side called front.This data Structure follows a
principle called FIFO(First In First Out) Means the data which is inserted first in
the queue will be removed or deleted first.
Example1:
Example2:
×
4.Linked List:
Collection or group of elements or data connected one another is called a Linked
List.
Every element of a Linked List is called a Node.
Node
Data Field Link /Next Field
Each Node contains two fields -first field for data or information ,second field for
link.
×Link field is a pointer used to store the address of immediate next node.The
Last node link field value is NULL indicated by
Example1:
Date:21-6-2019
Non Linear Data Structures:
If the data not arranged in a sequence or data arranged in level by level(or hierarchy) in a data
structure,then such a data structure is called Non Linear Data Structure.
Examples:
1. Trees: It is a Non Linear Data Structure which organizes the data in a hierarchy fashion
or level by level. In trees,every element is called a node.The first node of a tree is called
root and it never contains any loop.
Ex:
2.
3. Graphs:
It is a Non Linear Data Structure which organizes the data in a hierarchy fashion or
level by level.Graphs contains vertices and edges.It may or may not contain a loop.
Example:
4. Hash Tables:
Hash Table is a data structure which stores data in an associative manner. In a hash table, data
is stored in an array format, where each data value has its own unique index value.
Example:
Hash table
key value
0 Reyan
1 Vihan
● Time Complexity: The amount of time required by an algorithm for its execution is called
Time complexity.
We can’t measure the time complexity in terms of Physical clock units like hours,minutes and
seconds.To measure the time complexity,we use a concept or method called frequency count.
Frequency count:It denotes how many times a particular statement gets executed.
The following table gives information about a statement and its frequency count value.
statement Frequency
count
Assignment statement 1
return statement 1
Problem1: Find the frequency count value for the given algorithm
Algorithm print()
{
for i=1 to n
Print “ds”}
Solution:
Frequency count
Algorithm print()-------------------------------- 0
{ ----------------------------- 0
for i=1 to n ----------------------------- n+1
} ------------------------------------------------------------- 0
____________
Total= 2n+1
1. Best case Time Complexity: If any algorithm takes the smallest amount of time for its
execution,then its the best case time complexity of an algorithm.
2. Worst case Time Complexity: If any algorithm takes the largest amount of time for its
execution,then it's the Worst case time complexity of an algorithm.
3. Average case Time Complexity If any algorithm takes the Average amount of time for its
execution,then its the Average case time complexity of an algorithm.
Asymptotic Notations:
These are the Mathematical Symbols that are used to represent Time complexity of an
algorithm. These are used to represent Time complexity of an algorithm in shorthand way.
Types of Asymptotic Notations:
These are three types
1. Big Oh Notation
2. Omega Notation
3. Theta Notation
1. Big Oh Notation: It is denoted by O (read it as Big oh) symbol. It is used to represent the
maximum amount of time or longest amount of time taken by the algorithm for its execution. It
denotes the upper bound of the algorithm's execution time.
● Mathematical Definition:
f(n)=O(g(n)) if and only if f(n)≤c*g(n) , ∀n≥n0.Here f(n),g(n) are positive functions,c,n,n0 are
positive constants.
● Graphical Representation:
Example: Let f(n)=2n+1 ,g(n)=n2 .Prove that f(n) =O(g(n)) i.e. f(n)=O(n2 ).
Solution: To prove f(n) =O(g(n)) i.e f(n)=O(n2 )
We have to show f(n)≤c*g(n) , ∀n≥n0 here n,n0, c are positive constants.
2n+1 ≤c* n2
Assume c=1
Then 2n+1 ≤ 1* n2
If n=1 2×1 +1 ≤ 1*12 i.e 3 ≤ 1 false
If n=2 2*2+1 ≤ 1*22 i.e 5≤4 false
If n=3 2*3+1≤1*32 i.e 7≤9 true
If n=4 2*4+1≤ 1*42 i.e 9≤16 true
: : :
Therefore f(n)=O(g(n) i.e f(n)=O(n2 ) ∀n ≥3
Variants of Big oh Notation:
Notaion Meaning
O(1) Constant Time
Example: Let f(n)= n2 ,g(n)= 2n+1 .Prove that f(n) = Ω(g(n)) i.e. f(n)= Ω(2n+1 )
Solution: To prove f(n) = Ω (g(n)) i.e f(n)= Ω (n2 )
We have to show f(n) ≥c*g(n) , ∀n≥n0 here n,n0, c are positive constants.
2n+1 ≤c* n2
Assume c=1
Then n2 ≥ 1* 2n+1
If n=1 12 ≥1*2×1 +1 i.e 1≥3 false
If n=2 22 ≥ 1*2*2+1 i.e 4 ≥5 false
If n=3 32 ≥1* 2*3+1 i.e 9≥7 true
If n=4 42 ≥ 1* 2*4+1 i.e 16≥9 true
: : :
Therefore f(n)= Ω (g(n) i.e f(n)= Ω (2n+1 ) ∀n ≥3
4. Theta Notation:
It is denoted by Ө symbol. It is used to represent the average amount of time taken by the
algorithm for its execution. It lies in between upper bound and the lower bound of the
algorithm's execution time.
● Mathematical Definition:
f(n)= Ө (g(n)) if and only if c1×g(n)≤ f(n)≤c2*g(n) , ∀n≥n0.Here f(n),g(n) are positive functions,
c1, c2,n,n0 are positive constants.
● Graphical Representation:
Example: Let f(n)= 2n ,g(n)= n.Prove that f(n) = Ө (g(n)) i.e. f(n)= Ө (n )
Solution: To prove f(n) = Ө (g(n)) i.e. f(n)= Ө (n )
We have to show that
c1×g(n)≤ f(n)≤c2*g(n) , ∀n≥n0
c1×n≤2n≤c2×n, ∀n≥n0
Assume c1 =1, c2 =3
1×n≤2n≤3×n
If n=1 then 1×1≤2×1≤3×1 i.e. 1≤2≤3 true
If n=2 then 1×2≤2×2≤3×2 i.e. 2≤4≤6 true
If n=3 then 1×3≤2×3≤3×3 i.e. 3≤6≤9 true
: : :
Therefore f(n)= Ө (g(n) i.e f(n)= Ө (n ) ∀n ≥1
Note: Concepts Highlighted in Yellow colour should not be skipped in the Exams preparation .