CSE221 - Spring23 Final Solution
CSE221 - Spring23 Final Solution
1 a. You work as an engineer for the Roads and Highways department in a district. The district has 7 07
CO2 thanas that are represented by the 7 vertices in the following graph. The edges between the vertices
represent the roads that connect one thana with another and the weight of an edge represents the
length of the road.
A recent flood has totally damaged these roads and immediate repair work is needed. The cost to
repair a road is proportionate to its length. However, your department does not have the budget to
repair all the roads so you need to repair a subset of the roads. Which algorithm should you use
here to find out the roads that need to be repaired to keep all the thanas connected to one another
with the minimum possible cost?
Show the step by step simulation of the algorithm. You may pick any algorithm of your preference
that you think will solve the problem.
Solution:
Solution: The claim is not valid. This is an example of reduction and in reduction, the time required
to convert an instance into another one should be polynomial but 2n is exponential.
c. Rank the following three approximation algorithms from the worst to the best: 01
CO3 2-approximation algorithm, 1.5-approximation algorithm, n-approximation algorithm
2 Two infamous thieves, Denver and Nairobi, planned to rob the famous Louvre Museum. Before the
scene, they both agreed on the fact that none of them will break any item as all the items in the
museum are too precious, and taking a fraction of any item won’t sell on the black market. If it fits
in the bag as a whole, they will take it, otherwise, leave it as it is.
Both of them arrived at the Royal Treasury with an empty knapsack weighing a total of 7 kg each.
Even though both thieves are experts in their fields, they take slightly different approaches. Denver
believes he will use a Dynamic Programming Approach to rob the items in the most efficient
manner possible. Nairobi, on the other hand, believes that if she chooses a Greedy Approach, she
will make the most money.
The objects in the Royal Treasury Museum are listed below.
Objects Diamond Jewelry Sculpture Painting Gold Crest
Profit ($) 3 4 12 9 12
Weight (kg) 1 2 8 4 5
CO2 05
CO2 a) Simulate your dynamic programming algorithm to find the maximum profit Denver can 02
make.
CO3 b) From your memory/dp table, find out which items would he take to make this amount of 03
profit?
c) Does Nairobi’s belief remain valid after the robbery? Prove it.
Solutions
a)
b)
c)
Since Nairobi getting less profit than Denver ($16), so her belief doesn’t remain valid.
3 Following are the codes generated from a text for a Huffman tree construction.
Your lazy friend, Tom, used a very simple encoding method for compressing a text file. The text
file contains the string : Sudden Hello. He used a constant number of bits for encoding each of the
n distinct characters in this text. The constant is 𝑐𝑒𝑖𝑙(𝑙𝑜𝑔2𝑛) in this case. On the other hand, you
have compressed the text file using your own huffman tree from Q(b). 01
d) Mention how many bits each of the characters in Tom’s encoding scheme contain. 03
e) Compare between the number of bits needed to decode the above-mentioned string using
your friend’s scheme and your Huffman tree from Q(b).
Solutions (question 3)
a) b
b) Tree:
c) 2,2
d) 4
e) Huffman encoding. Because, Tom’s encoding needs 12 x 4 = 48. The Huffman
encoding scheme needs 40 bits (4+3+3+3+3+3+4+4+3+3+3+4 = 40 ).
Answer only one of 4, 4(or)
4 Your friend is trying to find the strongly connected components of a directed graph 𝐺. Recall that
this involves first running depth-first search on 𝐺, and then running depth-first search on the
𝑅
reverse graph, 𝐺 , using the decreasing order of finish time. Your friend has done this and found the
following table. Here,𝑎, 𝑏, 𝑐, 𝑑, 𝑒, 𝑓 , 𝑔, ℎ, 𝑖 are the vertices of 𝐺.
This image is just an example to show how SCC can convert a graph into DAG, and the source connected component
b) Let 𝑥 be your answer to (a). Find all the vertices in the strongly connected component that 05
contains 𝑥.
c) If the vertices you found in (b) are deleted from 𝐺, which set of vertices is guaranteed to be
01
a source connected component of the resulting graph? Write the list of those vertices.
d) How many strongly connected components does 𝐺 have? Just write the answer. 01
e) Draw the DAG of the strongly connected components of 𝐺. There is more than one correct 02
answer, anyone of those will be accepted.
Solutions
Question 4 (or) is on the next page.
4 or Consider the network of roads connecting a set of cities given by the graph below.
CO2 a) Simulate Dijkstra’s algorithm to find the weight of the shortest path from 𝐺 to 𝐵. 08
b) There is a proposal to add one new road to this network, and there is a list of five pairs of
cities between which the new road can be built. Each such potential road has an associated 02
length. As a designer for the public works department, you are asked to determine the road
whose addition to the existing network would result in the maximum decrease in the
distance from 𝐺 to 𝐵. One way to do this would be to add each of the five edges separately,
run Dijkstra’s algorithm five different times, and then compare the results. But can you do
this by invoking Dijkstra’s algorithm only two times (and possibly doing a linear amount of
extra work)?
Present your idea with a pseudo-code/flowchart/step by step instruction/algorithm
explanation.
Solutions
(b) (sketch) - Run Dijkstra’s algorithm from G on the original graph. Let d_G(v) be the
distance values.
- Run Dijkstra’s algorithm from B on the reverse graph. Let d_B(v) the distance values.
- Find the edge (u, v) that minimizes the quantity is d_G(u)+w(u, v) + d_B(v).
BRAC UNIVERSITY
Department of Computer Science and Engineering
1 a. You work as an engineer for the Roads and Highways department in a district. The district has 7 07
CO2 thanas that are represented by the 7 vertices in the following graph. The edges between the vertices
represent the roads that connect one thana with another and the weight of an edge represents the
length of the road.
A recent flood has totally damaged these roads and immediate repair work is needed. The cost to
repair a road is proportionate to its length. However, your department does not have the budget to
repair all the roads so you need to repair a subset of the roads. Which algorithm should you use
here to find out the roads that need to be repaired to keep all the thanas connected to one another
with the minimum possible cost?
Show the step by step simulation of the algorithm. You may pick any algorithm of your preference
that you think will solve the problem.
Solution:
The length of the MST is 1+2+2+3+3+4=15
Solution: The claim is valid. This is an example of reduction and in reduction, the time required to
convert an instance into another one should be polynomial. The time complexity n3 is polynomial.
c. Rank the following three approximation algorithms from the worst to the best: 01
CO3 n-approximation algorithm, 2-approximation algorithm, 5-approximation algorithm
Mission 1 5 3 2 2 4
Impact
CO2 07
CO3 03
a) Simulate your chosen algorithm on the below items to find your picked items.
b) If you were to apply brute force, what would be the time complexity and why? Explain.
Assume that points you have gained are denoted as P, and the number of items is N.
Solutions
b) Need to find all the combination of the items and select that combination whose total price
is less or equal to 9000 and impact is maximum. O(2^N)
3 Following are the codes generated from a text for a Huffman tree construction.
J - 1000 n - 000 l - 011
o - 1001 h - 001 e - 110
<space> - 1010 m - 010 c - 111
W - 1011
Your lazy friend, Tom, used a very simple encoding method for compressing a text file. The text
file contains the string : Welcome John. He used a constant number of bits for encoding each of the
n distinct characters in this text. The constant is 𝑐𝑒𝑖𝑙(𝑙𝑜𝑔2𝑛) in this case. On the other hand, you
have compressed the text file using your own huffman tree from Q(b).
d) Mention how many bits each of the characters in Tom’s encoding scheme contain. 01
03
e) Compare between the number of bits needed to decode the above-mentioned string using
your friend’s scheme and your Huffman tree from Q(b).
b) Tree:
c) 2,2
d) 4
e) Huffman encoding. Because, Tom’s encoding needs 12 x 4 = 48. The Huffman
encoding scheme needs 41 bits (4+3+3+3+4+3+3+4+4+4+3+3 = 41).
Answer only one of 4, 4(or)
4 Your friend is trying to find the strongly connected components of a directed graph 𝐺. Recall that
this involves first running depth-first search on 𝐺, and then running depth-first search on the
𝑅
reverse graph, 𝐺 , using the decreasing order of finish time. Your friend has done this and found the
following table. Here,𝑎, 𝑏, 𝑐, 𝑑, 𝑒, 𝑓 , 𝑔, ℎ, 𝑖 are the vertices of 𝐺.
This image is just an example to show how SCC can convert a graph into DAG, and the source connected component
b) Let 𝑥 be your answer to (a). Find all the vertices in the strongly connected component that 05
contains 𝑥.
c) If the vertices you found in (b) are deleted from 𝐺, which set of vertices is guaranteed to be
01
a source connected component of the resulting graph? Write the list of those vertices.
d) How many strongly connected components does 𝐺 have? Just write the answer. 01
e) Draw the DAG of the strongly connected components of 𝐺. There is more than one correct 02
answer, anyone of those will be accepted.
Solutions
Question 4 (or) is on the next page.
4 or Consider the network of roads connecting a set of cities given by the graph below.
CO2 08
a) Simulate Dijkstra’s algorithm to find the weight of the shortest path from 𝐺 to 𝐵.
b) There is a proposal to add one new road to this network, and there is a list of five pairs of 02
cities between which the new road can be built. Each such potential road has an associated
length. As a designer for the public works department, you are asked to determine the road
whose addition to the existing network would result in the maximum decrease in the
distance from 𝐺 to 𝐵. One way to do this would be to add each of the five edges separately,
run Dijkstra’s algorithm five different times, and then compare the results. But can you do
this by invoking Dijkstra’s algorithm only two times (and possibly doing a linear amount of
extra work)?
Present your idea with a pseudo-code/flowchart/step by step instruction/algorithm
explanation.
Solutions
(b) check set A
1a- 1a-
7 7
2+3+4+3+4+5=21 1+2+2+3+3+4=15
1c- worst is n-approximation, next 2-approximation, best 1c- Worst is n-approximation, next is 5-approximation,
1 is 1.5-approximation 1 best is 2-approximation
2b- items: 0,1,3 diamond, jewelry, painting or jewelry, gold 2b- O(2^N)
2 crest 3
3d- 4 3d- 4
1 1
3e- Tom’s encoding needs 12 x 4 = 48. The Huffman 3e- Tom’s encoding needs 12 x 4 = 48. The Huffman
3 needs 40 bits 3 needs 41 bits
4a- D or any from D,F,G,H,I 4b-5 D,F,G,H,I 4c-1 C 4d-1 4a- F or any from D,F,G,H,I 4b-5 D,F,G,H,I 4c-1 C 4d-1
1 3 1 3
4oa 4oa
-8 g n p u w d c h b -8 g n p u w d c h b
0 9 14 26 23 32 30 35 45 0 8 13 24 21 29 35 32 39
4ob One from G, one from B on reverse graph, find min edge 4ob One from G, one from B on reverse graph, find min edge
-2 (u,v) for G-u-v-B -2 (u,v) for G-u-v-B