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

Assignment-3 (Dynamic Programming and Greedy

Algorithm) AID-525: Data Structures and Algorithms


Autumn Semester 2022-23

1. Solve the following 0-1 knapsack problem using a dynamic-programming approach.


Assume a knapsack capacity of M = 10.

2. Given two words 𝑢 and 𝑣 over some alphabet, The edit distance 𝑑(𝑢, 𝑣) is defined as
the the minimum number of edit operations needed to convert one word to another.
These edits include adding a character, deleting a character, and changing a character.
Compute the edit distance between the words:
u=sample and v=dampen
u=gingerale and v=ginger
u=claim and v=climb

3. Apply the DP algorithm to the following matrix chain multiplication problem:

4. What is the multiplication complexity of A(BC) if A has size 2 ×10, B has size
10 × 20, and C has size 20 × 7? Same question for (AB)C.

Greedy Algorithm:
5. Alice wants to throw a party and is deciding whom to invite. She has 𝑛 people to choose
from, and she has made up a list of which pairs of these people know each other. She
wants to pick as many people as possible, subject to two constraints: at the party, each
person should have at least five other people whom they know and five other people
whom they don't know. Give an efficient algorithm that takes as input the list of 𝑛
people and the list of pairs who know each other and outputs the best choice of party
invitees. Give the running time in terms of 𝑛.

6. Suppose you are given n ropes of different lengths; you need to connect these ropes
into one rope. The cost to connect two ropes is equal to sum of their lengths. Design
and analyse a greedy algorithm to connect the ropes with minimum cost. For example:
Suppose you have three ropes with lengths 2, 5, and 8. If you chose first to connect the
length 5 and 8 ropes, then connect the length 2 and 13 ropes, the total cost would be
(5 + 8) + (13+ 2) = 28. However, if you first chose to connect the length 2 and 5 ropes,
then the length 7 and 8 ropes, the total cost would be (2 + 5) + (7 + 8) = 22 (which
happens to be optimal).

1 1 1 1 1
7. Suppose that symbols 𝑎, 𝑏, 𝑐, 𝑑, 𝑒 occur with frequencies 2 , 4 , , , respectively.
8 16 16
What is a Huffman encoding of the alphabet? If this encoding is applied to a file
consisting of 1,000,000 characters with the given frequencies, what is the length of the
encoded file in bits?

8. Suppose we are allowed values 0, 1, or 2 (instead of just 0 or 1) for constructing an


optimal prefix code. Create a modified Huffman algorithm for compressing sequences
of characters from an alphabet of size 𝑛, where the characters occur with known
frequencies 𝑓1 , 𝑓2 , . . . , 𝑓𝑛 . Your algorithm should encode each character with a variable-
length codeword over the values 0, 1, 2 such that no codeword is a prfix of another
codeword and so as to obtain the maximum possible compression. Prove your algorithm
generates a prefix code.

9. You are given a set 𝑋 = {𝑥1 , 𝑥2 , . . . , 𝑥𝑛 } of points on the real line. Your task is to design
a greedy algorithm that finds a smallest set of intervals, each of length 2, that contains
all the given points.
Example: Suppose that 𝑋 = {1.5, 2.0, 2.1, 5.7, 8.8, 9.1, 10.2}. Then the three intervals
[1.5, 3.5], [4, 6], and [8.7, 10.7] are length-2 intervals such that every x ∈ X is contained
in one of the intervals. Note that 3 is the minimum possible number of intervals because
points 1.5, 5.7, and 8.8 are far enough from each other that they have to be covered by
3 distinct intervals. Also, note that my solution is not unique – for example, I can shift
the middle interval [4, 6] to the right, say to [5.7, 7.7], without disturbing the other
intervals, and we would still have an optimal solution.
(a) Suppose that elements of 𝑋 are presented in increasing order. Describe (using
pseudocode) a greedy algorithm, running in 𝑂(𝑛) time, for this problem.
(b) Using the approach that we used for the proof of correctness of the Interval
Scheduling greedy algorithm prove that your algorithm indeed produces an optimal
solution. Your proof needs to be clear and precise, in addition to being correct.

You might also like