AA_Assignement1234567

Download as pdf or txt
Download as pdf or txt
You are on page 1of 4

THE NATIONAL INSTITUTE OF ENGINEERING

(An Autonomous Institution under VTU)

Problem Solving Assignment 1

MODULE 1

1) A hash table uses open addressing with linear probing. Write algorithms
for INSERT and SEARCH operations in Linear Probing. Given a hash
function h(x) = x mod 10, insert the following keys into the table and
resolve collisions using linear probing:
Keys: 14,51,30,21,59,32,31,79 in order.

Show the final hash table after all insertions.

2) Given a direct-address table of size 1000 and a set of keys from 1 to


1000, explain the steps to insert, delete, and search for keys.
3) Suppose you need to manage a database of student IDs using a direct-
address table. If the student ID range is from 1 to 5000, explain how you
would design your table.
4) Given a hash table with size 7 and the hash function h(x) = x % 7, insert
the following keys using linear probing:
Keys: 49, 63, 56, 92, 25, 37, 82

Show the final state of the hash table after all insertions.

5) Consider a hash table of size 11 and the following hash functions:


h1 (k)=k mod 11 and h2(k)=1+(k mod 9). Insert the keys
{27, 18, 29, 28, 39} using double hashing.

6) Given a set of keys {10, 22, 31, 4, 15, 28, 17, 88, 59}, show how they
would be stored in a hash table of size 10 using a division hash function
h(k) = k mod 10 and handle collisions using separate chaining.
7) Consider the hash function of size 13. Apply chaining technique to insert
the following keys 18,41,22,44,59,32,31,73,55 into the hash table having
a hash function h(x)=x mod 13. Comment on the time complexity of
dictionary operations when chaining is used
8) Consider a hash table that uses open addressing with linear probing.
Assume the hash table has a size of 10 and uses the hash function h(k)=k
mod 10. You are given the following sequence of keys to insert into the
hash table: 21, 10, 31, 25, 33, 12, and 44.
a) Show the state of the hash table after inserting each key, using linear
probing to resolve collisions. Clearly indicate how collisions are handled
for each insertion.

b) Explain the advantages and disadvantages of using open addressing


(such as linear probing) compared to separate chaining for hash table
collision resolution.

9) Consider a scenario where you are required to design a perfect hash


function for a set of distinct keys. Answer the following questions:

a) Explain the concept of Perfect Hashing. How does it differ from other
hashing techniques like open addressing and separate chaining?

b) Suppose you have a set of keys {15,27,33,42,56,60}. Design a two-


level perfect hashing scheme for this set. Provide detailed steps, including
how the hash functions are chosen and how the two levels are
constructed.

c) Discuss the advantages and limitations of using perfect hashing. Under


what circumstances would you prefer perfect hashing over other collision
resolution methods?

10) Consider a static set of distinct keys: {21, 34, 48, 59, 72, 85}.
Design a two-level perfect hashing scheme for this set using an
appropriate first-level and second-level hash function. Provide detailed
steps in your design.
11) Insert the following sequence of numbers into an empty Red-Black
Tree: 10, 20, 30, 15, 25, 40, 50, 35. Show the tree after each insertion and
colour adjustment, detailing any rotations or recoloring.
12) Consider the hash function of size 11. Apply chaining technique to
insert the following keys 10,22,31,4,15,28,17,88,59 into the hash table
having a hash function h(k)=kmod 11. Comment on the time complexity
of dictionary operations when chaining is used
MODULE 2

1) A rod of length 8 and price list [1, 5, 8, 9, 10, 17, 17, 20].

What is the maximum profit that can be obtained by cutting the rod?

2) Given a rod of length 8 and the following prices for each length of the
rod, determine the maximum obtainable profit using dynamic
programming.

Length 1 2 3 4 5 6 7 8
Price 1 5 8 9 10 17 17 20
3) Construct an OBST for the keys [5, 10, 15, 20] with search probabilities
[0.1, 0.2, 0.4, 0.3] & dummy node probabilities [0.05, 0.1, 0.1, 0.05,
0.05]. How do the dummy nodes affect the overall search cost compared
to the case without dummy nodes?
4) Consider the keys K={1,2,3,4}, with P={0.1,0.2,0.4,0.3} and
Q={0.05,0.1,0.05,0.05,0.1}. Compute the OBST and draw its structure.
What is the expected cost of the tree?

5) Suppose P={0.15,0.10,0.05,0.10,0.20} and


Q={0.05,0.10,0.05,0.05,0.05,0.10}. Compute the structure of the Optimal
Binary Search Tree for the given probabilities.

6) Construct the Huffman Tree for the given characters and their
frequencies.
Given character frequencies:
Character Frequency
A 5
B 9
C 12
D 13
E 16
F 45
(b) Based on the tree you constructed, assign Huffman codes to each
character.
(c) What is the average length of the Huffman code? Show your
calculations.
(d) Compare the total number of bits required to encode the message
using Huffman coding versus a fixed-length code.

6) Given the following set of activities with their start and finish times. Find
the maximum number of activities that can be performed by a single person,
assuming that a person can only work on one activity at a time.

Activity A1 A2 A3 A4 A5 A6
Start 1 3 0 5 5 8
Finish 2 4 6 7 9 9

Solve this using the greedy method and explain the reasoning behind your
selection of activities.

7)Travelling Salesperson Problem (TSP) using Dynamic Programming:


You are given a graph with 4 cities and the following cost matrix:

C1 C2 C3 C4
C1 0 10 15 20
C2 10 0 35 25
C3 15 35 0 30
C4 20 25 30 0

Use dynamic programming to find the minimum cost of visiting all


cities exactly once and returning to the starting city. Show the table of
subproblem solutions and derive the final optimal cost.

You might also like