Data Structure and Algorithms II - Final - Summer 2023
Data Structure and Algorithms II - Final - Summer 2023
Any examinee found adopting unfair means will be expelled from the trimester / program as per UIU
disciplinary rules.
There are six questions. Answer all of them. Show full simulation/tabulations wherever necessary. Figures in
the right-hand margin indicate full marks.
1. (a) Find the Minimum Spanning Tree (MST) for the given graph in Figure 1 using Prim’s [4]
algorithm. Show the details of your calculation. Explain why your MST satisfies the
properties of minimum spanning tree.
2. (a) What is a rolling hash and how does it help in efficiently updating the hash value in the [1]
context of the Rabin-Karp algorithm?
(b) Why is it necessary to use the modulo operation in the hash function of the Rabin-Karp [1]
algorithm?
(c) You are given a DNA sequence and a pattern. The DNA sequence is represented by a ]3]
series of nucleotides (A, C, G, and T) which means the sequence contains only the letters
A, C, G, and T. You need to figure out the number of occurrences of the given pattern in
the DNA sequence using Rabin-Karp algorithm. You must show the Hash values and
calculations for all the substrings.
1
Sequence = AGCTAGCGAGCTAG
Pattern = AGCTAG
3. (a) Can you calculate the shortest path from vertex A to vertex E in the following graph? [2]
If yes, mention the name of the algorithm you will use. If no, write down why.
(b) Find out the shortest path from vertex A to vertex G in the following graph using [5]
Dijkstra’s shortest path algorithm. Show the calculations in detail and write down the
vertices in the shortest path along with the path length.
(c) Find out a topological ordering of the following directed acyclic graph. You may use
[3]
Breadth First Search (BFS) or Depth First Search (DFS) to find the order.
2
Figure 4: A directed acyclic graph G(V,E) for Question 3(c)
4. (a) The following table shows the parent array of a Disjoint set (Rooted tree
implementation). Perform the following operations sequentially using path compression
and union-by-rank heuristic:
[2]
i. Draw the disjoint set forest
[1]
ii. Redraw the forest after Union(3,7)
[1]
iii. Redraw the forest after Union(5,10)
[1]
iv. What will be returned by Find-Set(9)?
Index 0 1 2 3 4 5 6 7 8 9 10 11
Parent 0 0 1 2 2 4 4 0 8 8 9 8
CONNECTED_COMPONENTS(G)
for each vertex v in V[G] do
MAKE_SET (v)
for each edge (u, v) in E[G] do
if FIND_SET(u) != FIND_SET(v) then
UNION(u, v)
5. (a) Consider an open-addressing hash table as shown below. The table already contains four
data items, and other empty slots contain NIL. Assume that collisions are handled using the
hash function
h(k, i) = (h'(k) + i h2(k)) mod 13.
where h'(k) = (3k + 5) mod 13,
and h2(k) = (5k -12) mod 13.
0 1 2 3 4 5 6 7 8 9 10 11 12
20 39 60 45
3
(b) What is primary clustering? What can we do to avoid primary clustering in hash table? [2]
(c) Suppose given a hash table of size 10, you are asked to insert 13 keys in the table. How [1]
can you solve this? Explain.
6. (a) What is the difference between Deterministic and Non-deterministic Algorithms? [1]
Explain briefly with proper examples.
(b) When does a problem belong to the complexity class NP? How does it differ from P- [2]
class problems?
(c) What does the following diagram represent? Explain briefly. [2]