0% found this document useful (0 votes)
5 views

Assignment-3 DSA

Uploaded by

divyajainxyz723
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Assignment-3 DSA

Uploaded by

divyajainxyz723
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Assignment 3 Divya Jain

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

 Key 10 → 10 mod 7=3 → placed at index 3.

 Key 20 → 20 mod 7=6 → placed at index 6.

 Key 15 → 15 mod 7=1 → placed at index 1.

2. Quadratic Probing:

o Use quadratic increments to resolve collisions

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

 Key 10 → 10mod 7=3 → placed at index 3.

 Key 20 → 20mod 7=6 → placed at index 6.

 Key 15 → 15 mod 7=1, collision!

 Probe (12)=1(1^2) = 1(12)=1: (1+1^2)mod 7=2

 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)

1. Let m = length(A) and n = length(B)

2. Initialize result array C of size (m + n - 1) with zeros

3. For i = 0 to m-1:
For j = 0 to n-1:

C[i + j] += A[i] * B[j]

4. Return C

Example:

A(x) = 3x ^ 2 + 2x + 1 coefficients: A = [3, 2, 1]

B(x) = x + 4 coefficients: B = [1, 4]

Result: C(x) = (3x ^ 2 + 2x + 1)(x + 4) = 3x ^ 3 + 14x ^ 2 + 9x + 4 Coefficients: C = [3, 14, 9, 4]

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:

1. Divide: Split the array into two halves.

2. Conquer: Recursively sort each half.

3. Combine: Merge the two sorted halves into one sorted array.

Pseudocode:

Algorithm MergeSort(A, start, end)

1. If start >= end, return

2. mid = (start + end) / 2

3. MergeSort(A, start, mid)

4. MergeSort(A, mid+1, end)

5. Merge(A, start, mid, end)

Merge Function:

Algorithm Merge(A, start, mid, end)

1. Create temporary arrays Left and Right

2. Copy elements from A[start...mid] into Left

3. Copy elements from A[mid+1...end] into Right

4. Merge Left and Right back into A[start...end]

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.

1. Coordinate List (COO):

o Store the matrix as a list of (row, column, value) tuples.

o Example: 0 0 3

4 0 0

0 5 0

o COO: [(0,2,3),(1,0,4),(2,1,5)]

2. Compressed Sparse Row (CSR):

o Store three arrays:

 Values: Non-zero elements.

 Column Indices: Column index of each non-zero element.

 Row Pointers: Start index of each row in the value array.

o Example:
For the matrix above:

 Values: [3, 4, 5]

 Column Indices: [2, 0, 1]

 Row Pointers: [0, 1, 2, 3]

Q5) Write a recursive function to count the number of nodes in a tree.

Recursive Function:

Function CountNodes(Tree)

1. If Tree is NULL:

Return 0

2. Return 1 + CountNodes(Tree.left) + CountNodes(Tree.right)

Explanation:

 If the tree is empty, the count is 0.

 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

You might also like