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

Assignment 2

The document discusses a problem involving two queues where the goal is to determine if any element from one queue is present in the other, establishing a lower bound of O(n²) for brute force comparison. It proposes an efficient algorithm using a hash table that achieves a time complexity of O(n). Additionally, it outlines an augmented AVL tree data structure for managing insertions, deletions, and finding the n/4-th element, improving the find operation to O(1) while maintaining O(log n) for insert and delete operations.

Uploaded by

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

Assignment 2

The document discusses a problem involving two queues where the goal is to determine if any element from one queue is present in the other, establishing a lower bound of O(n²) for brute force comparison. It proposes an efficient algorithm using a hash table that achieves a time complexity of O(n). Additionally, it outlines an augmented AVL tree data structure for managing insertions, deletions, and finding the n/4-th element, improving the find operation to O(1) while maintaining O(log n) for insert and delete operations.

Uploaded by

Lewis Mk
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Question 1:

(a) Derive a lower bound for this problem.

Problem: We are given two queues of size n , and the goal is to determine if
any element from one queue is also present in the other queue. We are not
comparing the values directly but rather the elements (i.e., their memory
locations or some specific properties).

Lower Bound: In the worst case, any algorithm that solves this problem
must examine each element from both queues. The reason is that if there is
only one common element between the queues, we won't know which one it
is without comparing each element from the first queue with each element
from the second queue.

To check all pairs of elements, you would need to make n×n=n2


comparisons, leading to a time complexity lower bound of O(n2).

Hence, the lower bound for this problem is O(n2).

(b) Design an algorithm for this problem. Derive its time complexity.

Algorithm:

1. Use a hash table (set) to store the elements of the first queue.
2. Iterate over the elements of the second queue, checking for each
element if it is present in the hash table.

Steps:

1. Create an empty set.

O(1) time per insertion, so it takes O(n) for all elements.


2. For each element in the first queue, insert it into the set. This takes

lookup operation takes O (1), so for all elements, it takes O(n).


3. For each element in the second queue, check if it exists in the set. This

Thus, the algorithm's time complexity is O(n) for inserting elements into the
set, plus O(n) for checking the elements of the second queue, resulting in an
overall time complexity of O(n).
This is an improvement over the O(n2) brute force approach.

Question 2:

Data Structure: Augmented AVL Tree

We augment the AVL tree with the following properties:

 Each node stores an additional field size, which represents the number

Each node stores pointers to the n/4n-th element and maintains


of nodes in its subtree.

updates to this pointer during insertions and deletions.

Operations

1. Insert(x):

takes O(log⁡n) time for balancing.


a. We perform the standard AVL tree Insert(x) operation, which

b. During the insertion, we update the size field of each node as we

c. Additionally, we may need to update the pointer to the n/4n-th


traverse the tree.

in O(log⁡n) by leveraging the size fields during the insertion path


element if the new element affects its position. This can be done

Time complexity: O(log⁡n)


traversal.

2. Delete(x):

also takes O(log⁡n) time for balancing.


a. We perform the standard AVL tree Delete(x) operation, which

b. During the deletion, we update the size field of each node as we

c. Similar to insertion, if the deleted element affects the n/4n-th


traverse the tree.

element's position, we update the pointer to the n/4n-th


element. This update can again be done in O(log⁡n) using the
size fields.

Time complexity: O(log⁡n)

3. Find(n/4):
a. Since we maintain a direct pointer to the n/4n-th element,
finding the element takes constant time O(1).

pointer in O(log⁡n), but the actual retrieval remains O(1).


b. If necessary, during insertions or deletions, we update this

Time complexity: O(1)O(1)O(1)

Summary of Complexities

Insert(x): O(log⁡n) time and O(log⁡n) space


Delete(x): O(log⁡n) time and O(log⁡n) space

Find(n/4): O(1) time and O(1) space



Improvements Over the Baseline

The standard approach using a plain AVL tree allows Find(k) in O(log⁡
n). By augmenting the tree with additional information and maintaining

a pointer to the n/4n-th element, we reduce the time complexity of


Find(n/4) to O(1).

same at O(log⁡n), but the improvement lies in reducing the time


 The time and space complexities for Insert(x) and Delete(x) remain the

complexity for the specific Find(n/4) query.

finding the n/4-th element.


This data structure efficiently balances the needs of insertion, deletion, and

Question 3:

Step 1: Define the Problem and Key Terms

 Matrix Chain Multiplication: Given a sequence of matrices


𝑀 1 ​, 𝑀 2 ​, … , 𝑀𝑛 , the goal is to find the optimal way to parenthesize
the matrices to minimize the number of scalar multiplications required.

and 𝐵 ∈ 𝑅❑𝑞× 𝑟 is 𝑝 ⋅𝑞 ⋅𝑟
 Scalar Multiplications: The cost of multiplying two matrices 𝐴∈ 𝑅𝑝× 𝑞

scalar multiplications.

to multiply a sequence of n matrices when every possible way of


 Average Work: The average number of scalar multiplications required

placing parentheses is equally likely.


Step 2: Number of Parenthesizations

The number of different ways to parenthesize a sequence of nmatrices (i.e.,


how many different matrix multiplications orders exist) is given by the
Catalan number:

𝐶
𝑛−1= (
1 2 ( n -1 )
𝑛 n -1 )
for n−1 pairs of matrices to be multiplied.

Step 3: Expected Scalar Multiplications

To compute the average number of scalar multiplications:

 For each valid parenthesization, calculate the number of scalar


multiplications.
 Sum these values and divide by the total number of parenthesizations
(which is 𝐶 𝑛−1 )

You can use a dynamic programming approach to compute the cost of all
possible parenthesizations and then average them out.

Step 4: Dynamic Programming Approach to Evaluate Cost

The dynamic programming approach for matrix chain multiplication

parenthesization. Let the dimensions of the nnn matrices be given by an


optimization can be adapted here to calculate the cost for each

array p[], where the dimension of matrix 𝑀 𝑖 is 𝑝 [ 𝑖 −1 ] ×𝑝 [ 𝑖 ].

1. Subproblem: For matrices 𝑀 𝑖 to 𝑀 𝑗 , the cost of multiplying them can


be computed by splitting the sequence at 𝑘 (where 𝑖 ≤ 𝑘< 𝑗 ), so that:

𝐶𝑜𝑠𝑡 ( 𝑖 , 𝑗 )=𝐶𝑜𝑠𝑡 (𝑖 , 𝑘 ) +𝐶𝑜𝑠𝑡 ( 𝑘+1 , 𝑗 )+ 𝑝 [ 𝑖− 1 ] ⋅𝑝 [ 𝑘 ] ⋅𝑝 [ 𝑗 ]

2. Store Results: Use a table m[ 𝑖 ] [ 𝑗 ] to store the cost of multiplying


matrices 𝑀𝑖𝑡𝑜 𝑀𝑗 .
3. Average Work Calculation: Once you compute the cost for every
possible parenthesization (stored in the table), sum all the costs and
divide by the total number of parenthesizations

Step 5: Algorithm

You might also like