0% found this document useful (0 votes)
7 views22 pages

Chapter 1 Ds Notes

The document provides an overview of data structures, including their need, classification into primitive and non-primitive types, and operations that can be performed on them. It also explains algorithms, their properties, and complexity analysis using Big O, Big Theta, and Big Omega notations. Additionally, it discusses memory representation of one-dimensional and two-dimensional arrays along with example programs for insertion, deletion, and searching elements.

Uploaded by

neharyadav2910
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)
7 views22 pages

Chapter 1 Ds Notes

The document provides an overview of data structures, including their need, classification into primitive and non-primitive types, and operations that can be performed on them. It also explains algorithms, their properties, and complexity analysis using Big O, Big Theta, and Big Omega notations. Additionally, it discusses memory representation of one-dimensional and two-dimensional arrays along with example programs for insertion, deletion, and searching elements.

Uploaded by

neharyadav2910
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/ 22

Q:What is data structure? Explain different categories in which data structure can be divided.

Give example for each


one NOV-17,APR-18

Q:What is data structure? Explain primitive and non-primitive data structure APR-19
❖ Need of Data Structure:

● It gives different level of organization data.

● It tells how data can be stored and accessed in its elementary level.

● Provide operation on group of data ,such as adding an item,looking up highest


priority item.
● Provide a means to manage huge amount of data efficiency.

● Provide fast searching and sorting of data.

● Classification of Data Structure


Q.List and explain different operations that can be performed on a data structure NOV-17
U

● Abstract Data Type:


● Mathematical model of the data objects that make up a data type as well as the functions that
operate on these objects.
● ADTs are entities that are definitions of data and operations but do not have implementation details.
It has two parts
i)Describe the way components are related to each other.
ii)The operations that can be performed on the data type.
e.g.Lists,graph,array along with their objects
Q: What is an algorithm? Explain properties of an algorithm.APR-19,APR-18
● Algorithm:
An algorithm can be defined as finite collection of well-defined steps designed to solve a particular
problem.
Characteristics of an algorithm:
1. Input: An algorithm must take some inputs that are required for the solution of a problem
2. Process: an algorithm must perform certain operations on the input data which are necessary for the
solution of the problem.
3. Output: An algorithm should produce certain output after processing the inputs.
4. Finiteness: An algorithm must terminate after executing certain finite number of steps.
5. Effectiveness: Every step of an algorithm should play a role in the solution to the problem. Also, each
step must be unambiguous, feasible and definite.

Q:What is time and space complexity ?Explain Big O and Big Theta notation.APR-19

● Complexity of an Algorithm
1.Space complexity
● The Space complexity of a program is defined as the amount of memory it needs to run
to completion.
● The space complexity can be measured using experimental method, which is done by
running the program and then measuring the actual space occupied by the program
during execution.
● Space complexity is the sum of the following components:
(i) Instruction space:
● The program which is written by the user is the source program. When this program is
compiled, a compiled version of the program is generated.
● For executing the program an executable version of the program is generated.

● The space occupied by these three when the program is under execution, will account
for the instruction space.
(ii) Data space:
● The space needed by the constants, simple variables, arrays, structures and other data
structures will account for the data space.
● The Data space depends on the following factors:

● Structure size – It is the sum of the size of component variables of the structure.
● Array size – Total size of the array is the product of the size of the data type and the
number of array locations.
(iii) Environment stack space:
● The Environment stack space is used for saving information needed to resume
execution of partially completed functions. That is whenever the control of the
program is transferred from one function to another during a function call, then the
values of the local variable of that function and return address are stored in the
environment stack. This information is retrieved when the control comes back to the
same function.
● The environment stack space depends on the following factors:

● Return address

● Values of all local variables and formal parameters.

● The Total space occupied by the program during the execution of the program is the
sum of the fixed space and the variable space.
● (i) Fixed space - The space occupied by the instruction space, simple variables and
constants.
● (ii) Variable space – The dynamically allocated space to the various data structures and
the environment stack space varies according to the input from the user.
● Space complexity S(P) = c + S p

● c -> Fixed space or constant space

● Sp -> Variable space

2. Time complexity

● Time complexity of the program is defined as the amount of computer time it needs to
run to completion.
● The time complexity of the program depends on the following factors:

● Compiler used – some compilers produce optimized code which consumes less time to
get executed.
● Compiler options – The optimization options can be set in the options of the compiler.

● Target computer – The speed of the computer or the number of instructions executed
per second differs from one computer to another
● The total time taken for the execution of the program is the sum of the compilation
time and the execution time.
(i) Compile time – The time taken for the compilation of the program to produce the
intermediate object code or the compiler version of the program. The compilation
time is taken only once as it is enough if the program is compiled once. If optimized
code is to be generated, then the compilation time will be higher.
(ii) Run time or Execution time - The time taken for the execution of the program. The
optimized code will take less time to get executed.
● Time complexity T(P) = c + T p
c ->Compile time
Tp -> Run time or execution time
Q.Define different asymptotic notation used to measure complexity of an algorithm NOV-17

● ASYMPTOTIC NOTATIONS

● Asymptotic notations – Asymptotic notations are the notations used to describe the
behavior of the time or space complexity.
● Let us represent the time complexity and the space complexity using the common
function f(n).
● The various asymptotic notations are:
(i) ( Big Oh notation )O
(ii) ( Omega notation ) Ω
(iii) ( Theta notation ) θ
● O – Big Oh notation

● Big - Oh notation is used to define the upper bound of an algorithm in terms of Time
Complexity.
Big - Oh notation always indicates the maximum time required by an algorithm for all
input values.
● Big - Oh notation describes the worst case of an algorithm time complexity.

● Consider function f(n) as time complexity of an algorithm and g(n) is the most
significant term.
● If f(n) <= C g(n) for all n >= n0, C > 0 and n0 >= 1. Then we can represent f(n) as O(g(n))

● Consider the following graph drawn for the values of f(n) and C g(n) for input (n) value
on X-Axis and time required is on Y-Axis
Big - Oh Notation can be defined as follows...
f(n) = O(g(n))

Example
Consider the following f(n) and g(n)...
f(n) = 3n + 2
g(n) = n
If we want to represent f(n) as O(g(n)) then it must satisfy f(n) <= C g(n) for all values of C >
0 and n0>= 1
f(n) <= C g(n)
⇒3n + 2 <= C n
Above condition is always TRUE for all values of C = 4 and n >= 2.
By using Big - Oh notation we can represent the time complexity as follows...
3n + 2 = O(n)
.

● Big - Omega Notation (Ω)

● Big - Omega notation is used to define the lower bound of an algorithm in terms of Time
Complexity.
Big-Omega notation always indicates the minimum time required by an algorithm for all input
values.

● Big-Omega notation describes the best case of an algorithm time complexity

● Consider function f(n) as time complexity of an algorithm and g(n) is the most significant
term. If f(n) >= C g(n) for all n >= n0, C > 0 and n0 >= 1. Then we can represent f(n) as Ω(g(n)).
● Consider the following graph drawn for the values of f(n) and C g(n) for input (n) value on X-
Axis and time required is on Y-Axis

Big - Omega Notation can be defined as follows......

f(n) = Ω(g(n))

Example
Consider the following f(n) and g(n)...
f(n) = 3n + 2
g(n) = n
If we want to represent f(n) as Ω(g(n)) then it must satisfy f(n) >= C g(n) for all values of C >
0 and n0>= 1
f(n) >= C g(n)
⇒3n + 2 >= C n
Above condition is always TRUE for all values of C = 1 and n >= 1.
By using Big - Omega notation we can represent the time complexity as follows...
3n + 2 = Ω(n)

● Big - Theta Notation (Θ)

● Big - Theta notation is used to define the average bound of an algorithm in terms of Time
Complexity.
Big - Theta notation always indicates the average time required by an algorithm for all input
values.
● Big - Theta notation describes the average case of an algorithm time complexity

Consider function f(n) as time complexity of an algorithm and g(n) is the most significant term. If
C1 g(n) <= f(n) <= C2 g(n) for all n >= n0, C1 > 0, C2 > 0 and n0 >= 1. Then we can represent f(n) as
Θ(g(n)).
Consider the following graph drawn for the values of f(n) and C g(n) for input (n) value on X-Axis and
time required is on Y-Axis
Big - Theta Notation can be defined as follows...

f(n) = Θ(g(n))

Example
Consider the following f(n) and g(n)...
f(n) = 3n + 2
g(n) = n
If we want to represent f(n) as Θ(g(n)) then it must satisfy C1 g(n) <= f(n) <= C2 g(n) for all values
of C1 > 0, C2 > 0 and n0>= 1
C1 g(n) <= f(n) <= C2 g(n)
⇒C1 n <= 3n + 2 <= C2 n
Above condition is always TRUE for all values of C1 = 1, C2 = 4 and
n >= 2.
By using Big - Theta notation we can represent the time compexity as follows...
3n + 2 = Θ(n)
Q Discus memory representation of one dimensional array.NOV-17

❖ Array

● Array is a container which can hold fix number of items and these items should be of
same type.
● Most of the datastructure make use of array to implement their algorithms.

● Following are important terms to understand the concepts of Array.


Element − Each item stored in an array is called an element.
Index − Each location of an element in an array has a numerical index which is used to
identify the element.
● Array Representation Arrays can be declared in various ways in different languages. For
illustration, let's take C array declaration.
❖ One-dimensional array:
Algorithm to insert, delete and search an element in an array

Insertion

● Input the array elements, the position of the new element to be inserted and the
new element.
● Insert the new element at that position and shift the rest of the elements to right by
one position.
Deletion

● Input the array elements, the position of the new element to be inserted and the
new element.
● Delete the element and shift the rest of the elements to left by one position.

Search

● Input the array elements, the element to be searched.


● Traverse the array and check if the element is present in the array.
● Display "Yes" if it is present in the array, else display "No".

Program to insert an element in an array

#include<stdio.h>
#include<conio.h>
void main()
{
int a[5],i;
printf(“Enter array elements\n”);
for(i=0;i<5;i++)
{
scanf(“%d”,&a[i]);
}
printf(“\nArray Elements are:\n”);
for(i=0;i<5;i++)
{
printf(“%d \t”,a[i]);
}
getch();

Program to delete an element in an array


/* C program to delete an element in an array */

#include <stdio.h>
int main()

int array[100], position, c, n;

printf(“Enter the number of elements of the array : “);

scanf(“%d”, &n);

printf(“\nInput the array elements : “);

for (c = 0; c < n; c++)

scanf(“%d”, &array[c]);

printf(“\nEnter the position : “);

scanf(“%d”, &position);

if (position >= n+1)

printf(“\nDeletion not possible.\n”);

else

for (c = position – 1; c < n – 1; c++)

array[c] = array[c+1];

printf(“\nArray after deletion : “);

for (c = 0; c < n – 1; c++)

printf(“%d\n”, array[c]);

return 0;

Program to search an element in an array


// C program to search an element in an array
#include <stdio.h>

int main()

int array[100], ele, c, n;

printf(“Enter the number of elements of the array : “);

scanf(“%d”, &n);

printf(“\nInput the array elements : “);

for (c = 0; c < n; c++)

scanf(“%d”, &array[c]);

printf(“\nEnter element : “);

scanf(“%d”, &ele);

for(c = 0; c < n ; c++)

if(array[c] == ele)

printf(“\nElement found\n”);

return 0;

❖ Two dimensional array

● A two dimensional array is a collection of data elements placed in rows and columns.

● The syntax is used to declare two dimensional array includes two subscripts of which
one specifies the number of rows and the other specifies the number of columns.
● These two subscripts are used to reference an element in an array.
● Syntax to declare the two dimensional array is as follows

<data type> <array name>[row size][column size];

● Syntax to initialize the two dimensional array is as follows

<data type><array name>[row size][column size]={values}’

● Example

● Memory representation of 2D array:

● Memory representation of 2D array is different from the linear array

● In 2D array possible two types of memory arrangements.

● They are:

● I]ROW major arrangement

| 0th row | 1st row | 2nd row |


4 3 5 6 8 9
502 504 506 508 510 512
//C program for matrix addition:

#include <stdio.h>

int main()
{
int m, n, c, d, first[10][10], second[10][10], sum[10][10];

printf("Enter the number of rows and columns of matrix\n");


scanf("%d%d", &m, &n);
printf("Enter the elements of first matrix\n");

for (c = 0; c < m; c++)

for (d = 0; d < n; d++)

scanf("%d", &first[c][d]);

printf("Enter the elements of second matrix\n");

for (c = 0; c < m; c++)


for (d = 0 ; d < n; d++)
scanf("%d", &second[c][d]);

printf("Sum of entered matrices:-\n");

for (c = 0; c < m; c++) {


for (d = 0 ; d < n; d++) {
sum[c][d] = first[c][d] + second[c][d];
printf("%d\t", sum[c][d]);
}
printf("\n");
}

return 0;
}
//C program to find transpose of a matrix
#include <stdio.h>

int main()
{
int m, n, c, d, matrix[10][10], transpose[10][10];

printf("Enter the number of rows and columns of a matrix\n");


scanf("%d%d", &m, &n);
printf("Enter elements of the matrix\n");

for (c = 0; c < m; c++)


for (d = 0; d < n; d++)
scanf("%d", &matrix[c][d]);

for (c = 0; c < m; c++)


for (d = 0; d < n; d++)
transpose[d][c] = matrix[c][d];

printf("Transpose of the matrix:\n");

for (c = 0; c < n; c++) {


for (d = 0; d < m; d++)
printf("%d\t", transpose[c][d]);
printf("\n");
}

return 0;
}

//Matrix multiplication in C language

#include <stdio.h>

int main()
{
int m, n, p, q, c, d, k, sum = 0;
int first[10][10], second[10][10], multiply[10][10];

printf("Enter number of rows and columns of first matrix\


n");
scanf("%d%d", &m, &n);
printf("Enter elements of first matrix\n");

for (c = 0; c < m; c++)


for (d = 0; d < n; d++)
scanf("%d", &first[c][d]);

printf("Enter number of rows and columns of second matrix\


n");
scanf("%d%d", &p, &q);

if (n != p)
printf("The multiplication isn't possible.\n");
else
{
printf("Enter elements of second matrix\n");

for (c = 0; c < p; c++)


for (d = 0; d < q; d++)
scanf("%d", &second[c][d]);

for (c = 0; c < m; c++) {


for (d = 0; d < q; d++) {
for (k = 0; k < p; k++) {
sum = sum + first[c][k]*second[k][d];
}

multiply[c][d] = sum;
sum = 0;
}
}

printf("Product of the matrices:\n");

for (c = 0; c < m; c++) {


for (d = 0; d < q; d++)
printf("%d\t", multiply[c][d]);

printf("\n");
}
}

return 0;
}

Q.What are the advantages and limitations of an array? APR-18

Advantages

• It is use to storing the data of same data type with same size

• It allows us to store known number of elements in it.

• It allocates memory in contiguous memory locations for its elements. It does not allocate any
extra space/ memory for its elements. Hence there is no memory overflow or shortage of
memory in arrays.

• Iterating the arrays using their index are faster compared to any other methods like linked list
etc.

• It can be used to implement other data structures like linked lists, stacks, queues, trees,
graphs etc.

Limitations

• Array is the static kind of data structure. Memory used by array cannot be increased or
decreased whether it is allocated at run time or compile time.

• Insertion and deletion of elements are very time consuming n array. When a new element s to
be inserted at the middle of the array then, elements are required to move to create the space
for new element. On the other hand, if an element is to be deleted from the array then, all its
preceding elements needs to be moved to fill the vacated position of the array.

• Only homogeneous elements can be stored n the array. Therefore, n case we want to store
the data of mixed type, the array cannot be used

❖ Sparse Matrix:
What is Sparse Matrix?
● In computer programming, a matrix can be defined with a 2-dimensional array.

● Any array with 'm' columns and 'n' rows represent a m X n matrix. There may be a
situation in which a matrix contains more number of ZERO values than NON-ZERO
values. Such matrix is known as sparse matrix.
● Sparse matrix is a matrix which contains very few non-zero elements.

When a sparse matrix is represented with a 2-dimensional array, we waste a lot of space to
represent that matrix.
For example, 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.
That means, totally we allocate 100 X 100 X 2 = 20000 bytes of space to store this integer
matrix. And to access these 10 non-zero elements we have to make scanning for 10000 times.
To make it simple we use the following sparse matrix representation.
Sparse Matrix Representations
Why to use Sparse Matrix instead of simple matrix ?

● Storage: There are lesser non-zero elements than zeros and thus lesser memory can be used to
store only those non-zero elements.
● Computing time:Computing time can be saved by logically designing a data structure traversing
only non-zero elements..

A sparse matrix can be represented by using TWO representations, those are as follows...

1)Triplet Representation (Array Representation)


In this representation, we consider only non-zero values along with their row and column
index values. In this representation, the 0th row stores the total number of rows, total
number of columns and the total number of non-zero values in the sparse matrix.

For example, consider a matrix of size 5 X 6 containing 6 number of non-zero values. This
matrix can be represented as shown in the image...

In above example matrix, there are only 6 non-zero elements ( those are 9, 8, 4, 2, 5 & 2) and
matrix size is 5 X 6. We represent this matrix as shown in the above image.
Here the first row in the right side table is filled with values 5, 6 & 6 which indicates that it is a
sparse matrix with 5 rows, 6 columns & 6 non-zero values.
The second row is filled with 0, 4, & 9 which indicates the non-zero value 9 is at the 0th-row
4th column in the Sparse matrix. In the same way, the remaining non-zero values also follow a
similar pattern.
2)Linked Representation
In linked list, each node has four fields. These four fields are defined as:

Row: Index of row, where non-zero element is located


Column: Index of column, where non-zero element is located
Value: Value of the non zero element located at index – (row,column)
Next node: Address of the next node

You might also like