0% found this document useful (0 votes)
30 views12 pages

Ds Chapter 1

Ds Chapter 1

Uploaded by

PIYUSH KUMAR
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
30 views12 pages

Ds Chapter 1

Ds Chapter 1

Uploaded by

PIYUSH KUMAR
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 12

Date: 20/6/2019

INTRODUCTION TO DATA STRUCTURES


Basic Terminology:
1. Data:
● It can be a text(letters(Ex:A,b,c,d,....),quantity (Ex:1,2,3.8,..)or special
Symbol(Ex:@,$,#,etc),images,sound,video,documents..etc.

It can be a value or set of values.
2. Group Data:If the data contains sub data,then it is called group data.
Ex: a person's name contains first name,middle name and last name.So, person’s name
is a group data.For example person’s name is Sachin Ramesh Tendulkar.Here first
name is Sachin,middle name is Ramesh and last name is Tendulkar.
3. Elementary Data or Primary Data:
4. If the data does not contain any sub data then it is called Elementary data or Primary
Data.
Ex: student roll number(18881a0199) ,age of a person are Elementary Datas or Primary
Datas.
5. Record:
Group of or set of data is called a Record or Row of data is also called Record.
Ex:
Student Student name age gender
Roll number

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.

Student Student name age gender


Roll number

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.

Application areas of a Data Structures:


The data structure concepts widely used in the following areas
1. Operating system
2. Compiler design
3. Computer Networks
4. Database Management Systems
5. Artificial Intelligence
6. Numerical Analysis
7. Graph Theory
8. Simulation and Modeling Softwares
9. Machine Learning
10. Big data
Classification of Data Structures or Types of Data Structures:
Data Structures are classified into two types
Data Structures

Primitive Data Structures Non Primitive Data Structures


Primitive Data Structures:
The basic data types or fundamental data types of any programming language is called
Primitive Data Structures.
Example: The basic data types of the C language is int,char,float,double.
Non Primitive Data Structures:
These data structures can be implemented using NonPrimitive Data Structures.NonPrimitive
Data Structures can be classified into two types

Non Primitive Data Structures

Linear Data Structures Non Linear Data Structures


● Linear Data Structures:
In this Data Structure ,The data arranged in a sequential fashion means one after the
other.
● Examples:
1. Arrays: Group of homogeneous elements under a single name is called an
array.In this all the array elements are stored in a sequence.
Ex: array name is A.int A[3]={1,9,0};

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:

Here Enqueue is nothing Insert operation ,Dequeue is nothing but Delete


Operation.

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:

Link field is also called as Next field.


Example2:

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

Data Structure Operations or Operations on Data Structures:


The basic operations that are performed on data structures are as follows:
1. Insertion:Insertion means addition of a new data in a data structure.
2. Deletion: Deletion means removal of a data from a data structure if it is found.
3. Searching:It is a process to check whether a particular data is present in the data
structure or not.
4. Traversal:Traversal of a data structure means processing all the data present in it.
5. Sorting:Arranging data of a data structure in a particular order is called sorting.
6. Merging:Combining data of two similar data structures to form a new data structure of
the same type, is called merging.

Algorithm and it’s Efficiency:


Algorithm is nothing but step by step process to solve a problem.The efficiency of an
algorithm can be measured with respect to space and time.
● Space Complexity: The amount of space or memory required by an algorithm for
its execution is called Space complexity.
● The Space Complexity is calculated using the following formula
S(p)=C+Sp
Here S(p) denotes Space Complexity of a Problem.
C is a fixed part or constant part that denotes the memory requirement of
constants or variables.
Sp denotes a Variable part that depends on the problem size.
Example: for the arrays ,array size is a problem size.
Problem1:calculate Space Complexity for the given algorithm
Algorithm sum( a[],n)
{
s=0;
for i=0 to n-1
s=s+a[i];
}
Solution:
Assume variables,constants requires 1 unit of memory
In the above algorithm,
s,i,n are variables.so each requires 1 unit
so,C=1+1+1
C=3units
Array a is variable part that depends on n value
So, Sp=1*n
=n Units
Therefore Space Complexity=3+n Units

● 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

Function header/declaration statement/comments/{,} 0

Assignment statement 1

Condition/expression in the decision making statements like if,if else ,else if 1


ladder,switch statement

Body decision making statements like if,if else ,else if ladder,switch 1


statement

Condition in the loop statement n+1

Body of loop statements n

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

Frequency count for the given algorithm is 2n+1


Problem2: Calculate Frequency count for the given algorithm

Algorithm sum (a [], n)


{
s=0;
for i=0 to n-1
s=s+a[i];
}
Solution:
Frequency count
Algorithm sum( a[],n)------------------------- 0
{ -------------------------------------------- 0
s=0;-------------------------------------- 1
for i=0 to n-1------------------------------n+1( n times true,1time false)
s=s+a[i];------------------------------------ n
}------------------------------------------------- 0
____________
Total= 2n+2

Frequency count for the given algorithm is 2n+2


Problem3: Calculate Frequency count for the given algorithm
Algorithm matrixadd( a[][],b[][],c[][],m,n)
{
for i=0 to m-1
for j=0 to n-1
c[i][j]=a[i]+b[i][j];
}
Solution:
Frequency count
Algorithm matrixadd( a[][],b[][],c[][],m,n)------- 0
{ ------- 0
for i=0 to m-1 ------- m+1
for j=0 to n-1 ------- m(n+1)
c[i][j]=a[i]+b[i][j]; ------- mn×1
} ------- 0
____________
Total= 2mn+2m+1

Types of Time Complexities:


These are 3 types

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

O(log n) Logarithmic Time

O(n) Linear Time

O(nlogn) Linear Logarithmic Time

O(n2) Quadratic Time

O(2n) Exponential Time


2. Omega Notation:
It is denoted by Ω symbol. It is used to represent the minimum amount of time or the smallest
amount of time taken by the algorithm for its execution. It denotes the lower bound of the
algorithm's execution time.
● Mathematical Definition:
f(n)= Ω(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)= 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 .

You might also like