Assignment 2
Assignment 2
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.
(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:
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:
Each node stores an additional field size, which represents the number
Operations
1. Insert(x):
2. Delete(x):
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).
Summary of Complexities
The standard approach using a plain AVL tree allows Find(k) in O(log
n). By augmenting the tree with additional information and maintaining
Question 3:
and 𝐵 ∈ 𝑅❑𝑞× 𝑟 is 𝑝 ⋅𝑞 ⋅𝑟
Scalar Multiplications: The cost of multiplying two matrices 𝐴∈ 𝑅𝑝× 𝑞
scalar multiplications.
𝐶
𝑛−1= (
1 2 ( n -1 )
𝑛 n -1 )
for n−1 pairs of matrices to be multiplied.
You can use a dynamic programming approach to compute the cost of all
possible parenthesizations and then average them out.
Step 5: Algorithm