Professional Documents
Culture Documents
Unit-1 PPT (DS)
Unit-1 PPT (DS)
(KCS-301)
19-Sep-22 2
What is Information?
19-Sep-22 3
• What is structure??
Can we extract information without structure
19-Sep-22 5
As Its Data structure using C
• Do you know C programming..?
Pre-requisite:
• Control statements
• Loops
• Functions and recursion
• Arrays
• Strings
• Structures
• Pointers
What is Data Structure?
• Data structure is study for knowing the right way of storing data in
digital space.
• Data structures imparts discipline among the data for efficient and
fast access.
7
Definition..
• Data structure is representation of the logical relationship
existing between individual elements of data.
• In other words, a data structure is a way of organizing all data
items that considers not only the elements stored but also their
relationship to each other.
• Data structures defines a particular way of storing and
organizing data in a computer so that it can be used
efficiently.
Importance of data structure
• DS is core foundation of writing good codes, reduces coding cost, and
enhances data accuracy being ultimate goal of any company.
• Major IT Companies/High Paying companies like google, Amazon,
Microsoft often ask questions related to data structures to check the
problem solving capability of candidate.
• Learning data structures leads to gradual improvement in problem
solving skills.
• Competent students in data structures and algorithm are also good in
data processing, reasoning and aptitude problems, thus directly
increases chances of placement.
Unit-1 Syllabus
Course outcomes( Data Structures- KCS301)
CO CO Statement BT Knowledge level
CO 1 Apply the knowledge of various data structures and its operations Apply Conceptual,
Procedural
CO 3 Analyze efficiency of different algorithms using time and space complexity Analyze Conceptual,
Procedural
CO 5 Implement the suitable data structure with respect to its performance to Create Conceptual,
model a real world problem Procedural,
Metacognitive
What is a Computer Program?
• To exactly know, what is data structure? We must know:
• What is a computer program?
Some mysterious
processing Output
Input
12
Problem??
• You Need to find your unique name from a sorted names list in word
file of 50000 pages manually. What approach you will use???
Introduction
array
Linked list
queue
tree stack
16
Real life applications areas
• 2- D array for record keeping
• Stack of plates
• Queue for buses/other transport
• Printer queue
• Google maps
19-Sep-22 17
Classification of Data Structure
• Data structure are normally divided into two broad categories:
• Primitive Data Structure
• Non-Primitive Data Structure
Classification of Data Structure
Data structure
Primitive DS Non-Primitive DS
Non-Primitive DS
Linear Non-Linear
• AI (e.g. arrays)
Performance analysis of
Algorithm
Algorithm?
• Refers to a method that used by computer for solution of a problem.
• It is finite set of instructions that, if followed accomplish a certain task.
Properties of algorithm:
• Input: Zero or more quantities (externally produced)
• Output: One or more quantities
• Definiteness: Clarity, precision of each instruction
• Finiteness: The algorithm has to stop after a finite (may be very
large) number of steps
• Effectiveness: Each instruction has to be basic enough and feasible
Complexity Analysis of Algorithms
(Time and space complexity)
• Devising Validating Analyzing
• As an algorithms is executed, It uses CPU for operations and Memory for holding data
and program.
• So Analysis of Algorithms here refer to task of determining how much computing time
and storage an algorithm requires.
• Estimates regarding space required is Space complexity and estimates regarding time is
called time complexity.
Why complexity analysis Required?
Space Complexity
• Instruction Space
• Data space
All this contribute the Extra memory and we need to minimize this
through better programming style.
Why Time Complexity
• Often more important than space complexity
• space available (for computer programs!) tends to be larger and larger
• time is still a problem for all of us
• Average-case running time assumes that all inputs of a given size are
equally likely.
Worst-case, Average-case, Best-case Time Complexity
• Best-case running time The term ‘best-case performance’ is used to
analyze an algorithm under optimal conditions.
• For example, the best case for a simple linear search on an array
occurs when the desired element is the first in the list.
7000
• Run the program with inputs of
6000
varying size and composition.
Time (ms)
5000
1000
• Plot the results.
0
• Problems?
0 50 100
Input Size
Limitations of Experimental approach
Here, the number of iterations is half the number of the loop factor.
So, here the efficiency can be given as
Algorithm Efficiency
• Logarithmic Loops
In logarithmic loops, the loop-controlling variable is either multiplied or divided
during each iteration of the loop. For example, look at the loops given below:
• Both loops will be executed only 10 times and not 1000 times
• we can conclude that the efficiency of loops in which iterations divide or multiply
the loop-controlling variables can be given as
Algorithm Efficiency
• Nested Loops
Linear logarithmic loop
Step of 1 2 3 4 .. K K+1
loop
i 2 Pow(2,p Pow(2,p Pow(2,p Pow(2,p Pow(2,p
ow(2,1) ow(2,2) ow(2,3) ow(2,k- ow(2,k)
1)
Estimate the time complexity
Solution
Asymptotic Notations
• Using asymptotic notations, we analyze the complexities of an
algorithm and its performance.
Comp 122
Examples
O(g(n)) = {f(n) : positive constants c and n0, such
that n n0, we have 0 f(n) cg(n) }
Comp 122
Omega Notation, Ω:
• Omega Notation (Ω): It represents the lower bound of the runtime of an
algorithm. It is used for calculating the best time an algorithm can take to
complete its execution, i.e., it is used for measuring the best case
time complexity of an algorithm.
Comp 122
Example
• Scenario-2
Algorithm Time (seconds) Space(Bytes)
Approach -1 10 100
Approach-2 8 200
Hard to decide, Matter of choice at cost of others
Example..:
• If data is stored uncompressed, it takes more space but less time than
if the data were stored compressed but it takes time to run the
compression algorithm
• Loop based and non loop based programs
• Recursive vs. Non Recursive programs.
Recursive vs. Non Recursive Program
(An Example of space time tradeoff)
• Data=AAAABBBBBBCCCCC • Data=AAAABBBBBBCCCCC
• Apply Compression Algo. • Use Uncompressed data
• Compressed data: A4B6C5 directly But will take more
Space.
To Access:
• Apply Decompression algo
on Data to get
AAAABBBBBBCCCCC from
A4B6C5
• Then Use.
ADTs: The Entities which carries blueprint of definitions of data attributes and associated operations
while user is not aware of Implementation
• Abstract Data type (ADT) is a type (or
class) for objects whose behavior is
defined by a set of value and a set of • It does not specify how data will be
operations. organized in memory and what
algorithms will be used for
• The definition of ADT only mentions implementing the operations.
what operations are to be performed
but not how these operations will be • It is called “abstract” because it gives
implemented an implementation-independent
view.
• The user of data type does not need
to know how that data type is
implemented
• E.g. we have been using Primitive
values like int, float, char
ADT as a black box which hides the inner structure and design of the data type from
User/Application
SK Classes …. (Sanjeev
Kr. Kapoor, AP, KIET Group of Institutions)
Array ADT
• Instances: ordered collection of zero or more elements.
• Operations. :
• 1. Is emplty
• 2. size
• 3. Index
• 4. Insert
• 5. delete
• 6. Traverse
Stack as ADT: Conceptual View(LIFO)
Stack as an ADT:
• e.g. Stack if we consider as ADT. We need to
understand what operations it needs to
perform:
1. Push(): To insert the element in stack list
2. Pop(): To delete the element from stack list
3. Create(): To create a stack
4. Isempty: To check whether stack empty or
not
5. Size(): to Know size of stack
SK Classes …. (Sanjeev
Kr. Kapoor, AP, KIET Group of Institutions)
Others examples of ADTs
• List
• Queue
• Graph
• Tree etc.
Summary Quiz
• What is data structure
• Example of Non Linear data structure
• Why we need data structure?
• Explain static and dynamic Implementation?
19-Sep-22 91
Try the following
• Write a program/Algorithm for Largest of three numbers.
Void main()
{int i;
for(i=0;i<10;i++)
printf(“%d”, i)
break;
}
19-Sep-22 92
Arrays
An array is a list of a finite number of homogenous
(similar data) elements.
This means that an array can store either all integers, all
floating point numbers, all characters (string) or any other
complex data type but all of same type.
Array elements are stored in consecutive memory location
called index or subscript.
Basics of Array
• An array is a type of data structure that stores a fixed-size of a homogeneous
collection of data. In short, we can say that array is a collection of variables of the
same type.
• For example, if you want to store 100 integers, you can create a 1D array for it.
int data[100];
• Declaration of a 1-dimentional array
dataType arrayName[arraySize];
Example: float mark[5];
• Here, we declared an array, mark, of floating-point type. And its size is 5. Meaning,
it can hold 5 floating-point values.
• It's important to note that the size and type of an array cannot be changed once it is
declared.
In C array is declared as
int a[10], here index starts from 0 (LB).
Size of array is fixed. Being static allocation.
• Length / Size of array = UB – LB + 1
• Where UB is the largest index, called the upper
bound, and LB is the smallest index, called the lower
bound of the array and
Length = UB if LB =1.
022 11 2 30 3 9 4 3
• The size of array can be determined as :
• Size = UB – LB +1
• For array num size will be :
• UB = 4
• LB = 0
• Size = 4 – 0 +1 = 5
• Size of array in bytes (i.e. amount of memory array occupies)
• Size in bytes = size of array x size of base type
• E.g. if we have array
int num [5];
• Then size in bytes = 5 x 2 = 10 bytes
• Similarly, if we have array
float num[5];
• Then size in bytes = 5 x 4 = 20 bytes
Addressing of element in 1-D Array in Memory map.
• Let A be a linear array stored in successive memory cells.
LOC(A[K]) = address of the element A[K] of the array A
1000
1001
1002
1003
• 406
Consider array a[-25,-24……+75+, base
address=1000,size of each element,w=10, find the
address of element at index 60
Consider array a[-25,-24……+75+, base
address=1000,size of each element,w=10, find the
address of element at index 60
• Solution
• Address(A[60])=1000+(60-(-25))*10
• 1000+85*10
• 1850
Two-Dimensional Array(General Terminology)
• A two dimensional array A is a collection of m * n elements
and each element is specified by pair of integers (such as J, K)
called subscripts
LB<=J<= UB AND
LB<=K<=UB
• Element of A with first subscript j and second subscript k will
denoted by
AJ,K or A[J,K]
• Two-dimensional arrays are called matrices in mathematics and
tables in business applications.
• The element A[J,K] appears in row J and column K
• A row is a horizontal list of elements and a column is a vertical
list of elements.
• Suppose A is a two-dimensional m x n array. The first
dimension of A contains the index set 1,2…..m with
lower bound 1 and upper bound m
• The second dimension of A contains the index set
1,2…..n with lower bound 1 and upper bound n.
• The length of a given dimension
Length = upper bound – lower bound + 1
• Size of array = m x n
EXAMPLE
LOC (A[j,k])=Base(A)+w[(N(j-LBr)+(k-LBc)]
(row major order)
Where Base(A) = address of 1st element of A, w = Bytes per
Element and N = total no. of columns in array=UBc-LBc+1/ UB2-
LB2+1
LOC (A[j,k])=Base(A)+w[(M(k-LBc)+(j-LBr)]
(column major order)
Where Base(A) = address of 1st element of A, w = Bytes per element
and M = total no. of rows in array UBr-LBr+1 /UB1-LB1+1
Find address of A[2,2] IN GIVEN ARRAY
Array of size 3x3 row index={0,1,2} Column index={0,1,2}
2006 2008 2010
2012 2014 2016
2018 2020 2022
19-Sep-22 157
Problems of Static Implementation
1. There is no bound checking in C for array boundaries.
2. If you store less number of elements than the number of elements
for which you have declared memory, Then the rest of the memory
will be wasted
19-Sep-22 158
Dynamic Implementation of Data Structures
• Implementation of Data Structures at Run time is
known as Dynamic Implementation
• Problems of Arrays can be removed here
• We need not to know in advance, the size of
memory required
• C provides following functions:
1. malloc( )
2. calloc( )
3. free( )
4. realloc( )
19-Sep-22 159
Dynamic Implementation of Data Structures
(contd..)
1. malloc( ): It allocates a block of memory in bytes. It acts as a
request to RAM for allocation of memory. If request is granted then
it returns a pointer to the first block of that memory. The syntax is:
malloc(number of elements * size of each element);
e.g.
int *ptr;
ptr = malloc(10 * sizeof (int) );
Gives address of void type for first byte of allocated memory and NULL if could not
allocate.Type casting may required.
Availabe in alloc.h header file.
19-Sep-22 160
Similarly allocation of structure can be done
• struct student
• {
• int roll;
• char name[30];
• float percentage;
• };
• struct student *str_ptr
• st_ptr=(struct student *) malloc(sizeof(struct student));
if st_ptr==NULL ?????
Dynamic Implementation of Data Structures
(contd..)
2. calloc( ): This function works exactly similar to malloc( ) function
except for the fact that it needs two arguments as against one
argument by malloc( )
e.g.
int *ptr;
ptr = (int *) calloc (10,2);
• (int *) is the casting operator that will change the void to int type
pointer.
• Memory allocated by malloc( ) contains Garbage value, Whereas,
Memory allocated by calloc( ) contains all zeros.
19-Sep-22 162
Dynamic Implementation of Data Structures
(contd..)
3. Free( ): It is used to de-allocate the previously allocated memory
using malloc( ) or calloc( ) functions. The syntax is:
free ( ptr_var);
19-Sep-22 163
Dynamic Implementation of Data Structures
(contd..)
4. realloc( ): It is used to resize the size of memory block which is
already allocated. It found use of in two situations:
1. If the allocated memory block is insufficient for current application
2. If the allocated memory is much more than what is required by the current
application
The Syntax of this function:
ptr_var = realloc(ptr_var, new_size);
19-Sep-22 164
Sparse
Matrices:
Introduction
Memory Representations of sparse matrix
Triplet Representation
Linked List Representation
Classification of Sparse Matrix
Triangular Matrices
Band Matrix
What are SPARSE MATRICES?
A matrix is a 2-dimensional data object composed of m rows and n columns, therefore
• having total m x n values.
In computer programming, a matrix can be defined with a 2-dimensional array.
Any array with 'm' rows and 'n' columns represent a m X n matrix.
If most of the elements of the matrix have 0 value, then such a matrix is termed as
• “Sparse matrix”
┌ an example of a sparse
• Consider the following as ┐ matrix A:
| 10 0 3 0 0 0 |
| 21 2 0 4 0 0 |
| 0 3 6 0 5 0 |
| 0 0 23 24 0 26 |
| 81 0 0 94 65 0 |
| 71 92 0 0 55 36 |
└ ┘
Why to use Sparse Matrix?
• Storage: There are lesser non-zero elements than zeros and thus lesser memory can
be used to store only those elements.
– space calculation: Matrix having m rows and n columns the space required to store
the numbers will be m*n*s where s is the number of bytes required to store the
value.
E.g. Suppose there are 10 rows and 10 columns and we have to store the integer
values then the space complexity will be bytes.
10*10*2=200 bytes.
• Computing time: Computing time can be saved by logically designing a data
structure traversing only non-zero elements.
Sparse Matrix Representations
Representing a sparse matrix with a 2D array results in wastage of memory and
processing time.
E.g. consider a matrix of size 100 X 100 containing only 10 non-zero elements.
– In this matrix, only 10 spaces are filled with non-zero values and remaining spaces of the
matrix are filled with zero.
– Space allocated: 100 X 100 X 2 = 20,000 bytes to store this integer matrix.
– Access time of 10 non-zero elements: 10,000 scans.
... ...
info(p) p next(p)
Accessing nodes in a linked list
first
head
A B C
A B C D
Creation of single
node.
Head NULL
30
data next NULL
Head NULL
30
data next NULL
tmp X Item to be
inserted
A B C
curr
'' 194
Inserting after a given Node/LOC
Deleting the Element(From Beginning)
Head
2000
1000
temp
45 2000 15 4000 25 NULL
1000
p(initially)
45 2000 15 4000 25 NULL
1000
Increment p to q will
q->next is 4000 reach at location hold
before the node to address
delete of node
Deleting the Specific Node,Need 2 Pointers?
Head
• q=p->next
1000 NULL
4000
p(initially)
45 2000 15 4000 25 NULL
1000
A B C
START
Inserting the at beginning of Circular linked
list
A B C
START
E NULL
New Node
Inserting an element in the beginning of a Circular
List
1. If AVAIL==NULL then Write Overflow and Return
2. Set NEW=AVAIL and AVAIL=LINK[AVAIL]
3. INFO[NEW]=ITEM
4. Set PTR=START
5. Repeat Step 6 while LINK[PTR]!=START
6. PTR=LINK[PTR]
7. Set LINK[NEW]=START
8. Set LINK[PTR]=NEW
9. Set START=NEW
10. Exit
Inserting an element at the end of a Circular List
9. Exit
Deleting the First node from Circular List
NULL 30 NULL
Create_Node() for DLL Node
n=3000(New node address)
NULL 30 NULL
Inserting a new node at the end
Null NULL 30 NULL 4000 NULL 30 NULL