Download as pdf or txt
Download as pdf or txt
You are on page 1of 11

Q.1) a) Define the following terms with suitable example.

[4]
i) Data Structure [1 Marks]
A Data Structures is particular way of organizing data into a computer so that
it can be used effectively.
Example considers set of elements which can be stored in array data structure.
Any element in above array can be referred by an index.

ii) Abstract Data Type [1 Marks]


An ADT is a mathematical model of a data structure that specifies the type of data stored, the
operations supported on them, and the types of parameters of the operations.
An ADT specifies what each operation does, but not how it does it. Typically, an
ADT can be implemented using one of many different data structures.
iii) Algorithm [1 Marks]
An Algorithm is defined as steps-by-step procedure or method for solving a problem
by computer in finite number of steps
iv) Flowchart [1 Marks]
“Flowchart is graphical representation of the algorithms”

b) What is frequency count? [2 Marks]


why is frequency count important in the analysis of algorithm. [3 Marks]
The step count method is one of the method to analyze the algorithm. In this method,
we count number of times one instruction is executing from that we will try to find the
complexity of the algorithm.
Example- obtain the frequency count for the following code
do 1 1
{ 0 0
a++ n 5
if ( i == 5) n 5
break; 1 1
i++; n 5
}while(i<=n); n 5
Total 4n+2 22

c) Write an algorithm to "compute the sum of the digits of the given number. [3 Marks]
Justify that your algorithm satisfies all the characteristics of an algorithm [3 Marks]
Write an algorithm to "compute the sum of the digits of the given number
o Step 1: Get number by user
o Step 2: Get the modulus/remainder of the number
o Step 3: sum the remainder of the number
o Step 4: Divide the number by 10
o Step 5: Repeat the step 2 while number is greater than 0.
o Step 6 Stop

Justify that your algorithm satisfies all the characteristics of an algorithm


1. Unambiguous – The Algorithm is be clear and unambiguous. Each of its steps (or
phases), and their inputs/outputs is clear and must lead to only one meaning.
2. Input − An algorithm should have 1 or more well-defined inputs.
3. Output − An algorithm should have 1 or more well-defined outputs, and should match the
desired output.
4. Finiteness − Algorithms is terminate after step 6
5. Independent − An algorithm should have step-by-step directions, which is independent of
any programming code.
OR
Q.2) a) Give complete classification of data structure with one example of each.
[4 Marks]
The Data Structure Classification can be divided into two basics types
1. Primitive Data Structure
2. Non-Primitive Data Structure
1. Primitive Data Structure
Primitive Data Structure are those which arfe predefined way of storing data by the
system.
e.g. integer, character, float, pointer etc.

2. Non-Primitive Data Structure


The data types that are derived from primary data types known as non- Primitive Data
types. These data types are used to store group of values.
e.g. array, linked list , stack. Tree, graph etc.

Linear Data Structure


Linear Data Structure in which data elements is arranged in sequentially.
e.g. array, Linked list, stack, queue.

Non-Linear Data Structure


Non-Linear Data Structure in which data elements is arranged in hierarchical manner.
e.g. Tree, graph etc.

b) Explain divide & conquer Strategy [2.5 Marks]


and Greedy strategy with suitable example. [2.5 Marks]
In Divide & Conquer method a given problem is divided into smaller sub-problems.
These sub-problems are solved independently.
If necessary, the solutions of the sub-problems are combined to get a solution to the
original problem.
If the sub-problem are large enough the Divide & Conquer is reapplied.

The generated sub-problem are usually o some type as the original problem hence
sometimes recursive algorithms are used in Divide & Conquer strategy.
Example of Divide & Conquer strategy
Marge sort
The merge sort is starting algorithm that uses the Divide & Conquer strategy in this
method division is dynamically carried out.
Merge sort an on input array with n elements consists of three steps.
Step 1- Divide
Partition array into two sub-list s1 & s2 with n/2 elements in each.
Step 2- Conquer
Then sort sub-list s1 and s2
Step 3- Combine
Merge s1 and s2 into unique sorted groups
Example – 70,20,30,40,10,50,60

2. Greedy Technique
This method is popular for obtaining the optimized solutions.
In Greedy Technique, the solution is constructed through a sequence of steps , each
expanding a partially constructed solution obtained so far , until complete solution to the
problem is reached.
In. Greedy Technique following activity is performed
1. First we select some solution from input domain
2.Then we check whether the solution is feasible or not
3. From the set of feasible solution, particular solution that satisfies or nearly satisfies the
objective of the function such a solution is called as optimal solution.
4. as greedy method works in stages. At each stage only one input is considered at each time.
Based on this input it is decided whether particular input gives the optimal solution or not
Example of Greedy Technique
Dijkstras algorithm is popular algorithm for finding shortest path using Greedy
Technique.
This algorithm is called Single Source shortest path algorithm.
In this algorithm, for a given vertex is called as source the shortest path to all other
vertices is obtained.
In this algorithm the main focus is not to find only one single path but to find the
shortest pts from any vertex to all other reaming verities.
This algorithm applicable to graphs with non-negative weights only.
Consider a weighted connected graph is given below.

Now we will consider each vertex as a source & will find the shortest distance from this
vertex to every other remaining vertex .Let us start with vertex A
Source Distance with Path show in graph
vertex other
A A-B Path=4
A-C Path=8
A-D Path=∞
A-E Path=∞

B B-C
Path=4+1=5
B-D
Path=4+3=7
B-E
Path==∞
C C-D
Path=5+7=12
C-E
Path=5+3=8

D D-E Path=
7+8=15

But we have one shortest path distance obtained from A-E i.e. A-B-C-E with path
length =4+1+3=8.
Similarly, shortest path can be obtained by choosing source & destination.

e) Draw flowchart to check whether a given number is a perfect square of an


integer. [5 Marks]
What is the time complexity of your algorithm? [1 Marks]

The time complexity of your algorithm O(log log n)


Q3) a) what are advantages & [2 Marks]
disadvantages of sequential organization of data structure? [2 Marks]
Advantages of sequential organization of data structure
• It is simple to program and easy to design.
• Sequential data structure is best use if storage space.

Disadvantages sequential organization of data structure


• Sequential data structure is time consuming process.
• It has high data redundancy.
• Random searching is not possible.

b) Explain row major & [2.5 Marks]


column major representation of arrays in computer memory. [2.5 Marks]
i) Row major Representation
If the elements are stored in row wise manner then it is called as Row major
Representation.
Example – if you want to store elements 10 20 30 40 50 60 then in two dimensional array.

The elements will be stored horizontally. To access any elements in two dimensional
array we must specify both its row number & column number. That is why we need two
variables which is act as row index & column index.
In row major matrix, the element at a[i][j] will be equal to
Address of [i][j] = base address + row_index * total no.of column +column_index)* element-
size

Example- Consider integer array int arr[3][4] declared in program. If the base address
is 1050, find the address of the element arr[2][3] with row major Representation.
base address + row_index * total no.of column +column_index)* element-size
a[2][3] =1050+(2*4+3)*2 = 1050+(8+3)*2=1050+11*2=1072
ii) Column Major Representation
If elements are stored in column wise manner then it is called as Column Major
Representation.
Example – if you want to store elements 10 20 30 40 50 60 then the elements will be filled
up by column wise manner as follows
In Column major matrix, the element at a[i][j] will be equal to
Address of [i][j] = base address + Column _index * total no.of row +row_index)* element-
size
Example- Consider integer array int arr[3][4] declared in program. If the base address
is 1050, find the address of the element arr[2][3] with Column major Representation.
base address + Column _index * total no.of row +row_index)* element-size
a[2][3] =1050+(3*3+2)*2 = 1050+(9+2)*2=1050+11*2=1072

c) Write an algorithm to perform polynomial addition [5 Marks]


state the time complexity of the algorithm [1 Marks]
Algorithm:
Assume that the two polynomials say A and B and the resultant polynomial storing
the addition result is stored in one large array.
1. Set a pointer i to point to the first term in a polynomial A.
2. Set a pointer j to point to first term in a polynomial B.
3. Set a pointer k to point to the first position in array C.
4. Read the number of terms of A polynomial in variable tl and read the number of terms of B
polynomial in variable t2 & read the n.
5.While i < tl and j < t2 then
{ if exponent at ith position of A polynomial is equal to the exponent at jth position of
polynomial B then
Add the coefficients at that position from both the polynomial and store the result in C
arrays coefficient field at position k copy the exponent either pointed by i or j at position k in
C array.
Increment i, j and k to point to the next position in the array A, B and C.
}
else
{
if the exponent position i is greater than the exponent at position j in polynomial B
then
{
copy the coefficient at position i from A polynomial into coefficient field at position k
of array C copy the exponent pointed by i into the exponent field at position k in array C.
Increment i, k pointers.
}
else
{
Copy the coefficient at position j of B polynomial into coefficient field at position k in C
array.
Copy the exponent pointed by j into exponent field at position k in C array.
Increment j and k pointers to point to the next position in array B and C.
}
}
while i< t1
{
copy the coefficient at position i of into the coefficient field at position k in C.
copy the exponent pointed by i into the exponent field at position k in C array.
Increment i and k to point to the next position in arrays A and C.
}
while j < t2
{
Copy the coefficient at position j of B into the coefficient field at position k in C.
Copy the exponent pointed by j into exponent field at position k in C.
Increment j, k to point to the next position in B and C arrays.
8. Display the complete array C as the addition of two given polynomials.
9. Stop.
Time complexity of the above algorithm and program is O(m+n) where m and n are orders of
two given polynomials

OR
Q4) a) Write a short note on storage representation of an array. [4 Marks]

The array can be represented using


i) Row major Representation ii) Column Major Representation

i) Row major Representation


If the elements are stored in row wise manner then it is called as Row major
Representation.
Example – if you want to store elements 10 20 30 40 50 60 then in two dimensional array.
The elements will be stored horizontally. To access any elements in two dimensional
array we must specify both its row number & column number. That is why we need two
variables which is act as row index & column index.
In row major matrix, the element at a[i][j] will be equal to
Address of [i][j] = base address + row_index * total no.of column +column_index)* element-
size

Example- Consider integer array int arr[3][4] declared in program. If the base address
is 1050, find the address of the element arr[2][3] with row major Representation.
base address + row_index * total no.of column +column_index)* element-size
a[2][3] =1050+(2*4+3)*2 = 1050+(8+3)*2=1050+11*2=1072
ii) Column Major Representation
If elements are stored in column wise manner then it is called as Column Major
Representation.
Example – if you want to store elements 10 20 30 40 50 60 then the elements will be filled
up by column wise manner as follows

In Column major matrix, the element at a[i][j] will be equal to


Address of [i][j] = base address + Column _index * total no.of row +row_index)* element-
size
Example- Consider integer array int arr[3][4] declared in program. If the base address
is 1050, find the address of the element arr[2][3] with Column major Representation.
base address + Column _index * total no.of row +row_index)* element-size
a[2][3] =1050+(3*3+2)*2 = 1050+(9+2)*2=1050+11*2=1072

b) Write pseudo code to reverse the in numbers in one dimensional array. [5 Marks]

 Input the number of elements of an array.


 Input the array elements.
 Traverse the array from the last.
 Print all the elements

c) Write an algorithm to perform sparse matrix addition & [5 Marks]


state its time complexity. [1 Marks]
1. Obtain the triplet form both Sparse matrix.
2. Create a new triplet to store result matrix.
3. Copy number of rows & column from any sparse matrix to result matrix.
4. Let i,j,k be the indices of sparse matrix 1,2,3 respectively.
5. Initialize i,j,k to 1
6. Traverse both matrices from second row
if(row no. of matrix1== row no. of matrix2)
{ if(column no.of matrix1==column no. of matrix2 )
{ Make the addition of non-zero & store it into result matrix by
incrementing indices.
}
else
{ Which ever has less column value that to result matrix by incrementing
respective indices.
}
}
else
{Compare row of both sparse matrix & which ever has less row value copy that to
result matrix by incrementing respectively indices.
}
7. Copy stop 6 till end of any matrix triplet.
8. Copy the reaming term of sparse matrix (if any) to resultant matrix.
time complexity of O(n), where n is the number of non-zero elements in the larger matrix
amongst the two.

You might also like