Assignment-3 DSA
Assignment-3 DSA
06320802821
ECE-B
Q1) Explain the linear and quadratic probe collision resolution methods using an example.
Collision Resolution:
When two keys hash to the same index, collision resolution strategies help place them in the hash
table.
1. Linear Probing:
o Start from the collision index and probe sequentially until an empty slot is found.
o Example:
Hash table size = 7; hash function: h(k)=kmod 7h(k) = k \mod 7h(k)=kmod7
Insert keys: 10,20,1510, 20, 1510,20,15
2. Quadratic Probing:
o Example:
Hash table size = 7; hash function: h(k)=kmod 7h(k) = k \mod 7h(k)=kmod7
Insert keys: 10,20,1510, 20, 1510,20,15
Place at index 2.
Q2) Write a pseudocode for multiplying two polynomials A and B represented as arrays of
coefficients.
Pseudocode:
Algorithm MultiplyPolynomials(A, B)
3. For i = 0 to m-1:
For j = 0 to n-1:
4. Return C
Example:
Q3) Define the merge sort algorithm and its divide-and-conquer strategy.
Definition:
Merge sort is a divide-and-conquer sorting algorithm. It divides the array into two halves, sorts each
half recursively, and then merges the sorted halves.
Steps:
3. Combine: Merge the two sorted halves into one sorted array.
Pseudocode:
Merge Function:
Q4) Describe different sparse matrix representations such as COO and CSR.
Sparse matrices are matrices with most elements being zero. Storing them efficiently reduces
memory usage.
o Example: 0 0 3
4 0 0
0 5 0
o COO: [(0,2,3),(1,0,4),(2,1,5)]
o Example:
For the matrix above:
Values: [3, 4, 5]
Recursive Function:
Function CountNodes(Tree)
1. If Tree is NULL:
Return 0
Explanation:
Otherwise, count the root node (1), recursively count nodes in the left subtree, and
recursively count nodes in the right subtree.
Example:
1
/\
2 3
/\
4 5
CountNodes(Tree)=1+CountNodes(Left)+CountNodes(Right)
=1+(1+2)+(1)=5