0% found this document useful (0 votes)
14 views18 pages

CSE221 - Spring23 Final Solution

The document outlines two examination papers from BRAC University for the course CSE 221: Algorithms, covering topics such as Minimum Spanning Trees, NP-completeness, and Huffman tree construction. It includes various algorithmic problems with specific questions and solutions related to graph theory, dynamic programming, and encoding methods. Each section specifies the marks allocated for each question, indicating a structured assessment format.

Uploaded by

akbarsaad49
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views18 pages

CSE221 - Spring23 Final Solution

The document outlines two examination papers from BRAC University for the course CSE 221: Algorithms, covering topics such as Minimum Spanning Trees, NP-completeness, and Huffman tree construction. It includes various algorithmic problems with specific questions and solutions related to graph theory, dynamic programming, and encoding methods. Each section specifies the marks allocated for each question, indicating a structured assessment format.

Uploaded by

akbarsaad49
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 18

BRAC UNIVERSITY

Department of Computer Science and Engineering

Examination: Semester Final Exam Semester: Spring 2023


Duration: 1 Hour 40 Minutes Full Marks: 40

CSE 221: Algorithms

Answer the following questions.


Figures in the right margin indicate marks.

Name: ID: Section:

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 the MST is 2+3+4+3+4+5=21

b. Problem Y is NP-complete. If A professor takes an instance of Y, converts it into an instance of the 02


CO3 𝑛
shortest path problem in 2 time, and claims that Y is in P now, is the claim valid?
Explain your answer.

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

Solution: worst is n-approximation, next 2-approximation, best is 1.5-approximation

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.

H - 1000 u - 000 e - 011


o - 1001 d - 001 l - 110
<space> - 1010 n - 010 t - 111
S - 1011

You are also given the following information:

● The frequency of each leaf node except e, l, and t is 1.


● The left and right child nodes of the root have frequencies 5 and 8 respectively.

Now answer the following questions.


CO2 01
a) Suppose in a Huffman tree, the distances from the root to the pair of leaves denoting the
letters k and b are 5 and 2 respectively, which letter between them is more frequent in the
original text? Just mention the letter. 03
b) Draw the Huffman tree from the given coding table above. 02
c) Continuing on Q(b), what are the frequencies of l, and t in the original text? Just mention
the frequencies.

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 𝐺.

Now answer the following questions.

CO2 a) Find a vertex in 𝐺 that is guaranteed to be contained in a source connected component. 01


Recall that a source connected component in an SCC DAG is a node that has indegree zero.
For example, in the following SCC DAG, {𝑎, 𝑏, 𝑒} is a source connected component.

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

Examination: Mid Semester Exam Semester: Spring 2023


Duration: 1 Hour 40 Minutes Full Marks: 40

CSE 221: Algorithms

Answer the following questions.


Figures in the right margin indicate marks.

Name: ID: Section:

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

b. Problem Y is NP-complete. If A professor takes an instance of Y, converts it into an instance of the 02


CO3 3
shortest path problem in 𝑛 time, and claims that Y is in P now, is the claim valid?
Explain your answer.

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

Solution: Worst is n-approximation, next is 5-approximation, best is 2-approximation


2 You are Monco, a video game character, you have just stepped into level 2. This game requires you
to acquire necessity items to sustain through a level and this is done by trading reward points from
the previous level. From level 1 you have gained 9,000 points. Below is a list of items you can get
by trading your reward points. Each item has certain properties but be wise to pick the ones which
will maximize your sustainability by maximizing mission impact.

Gold Pain Waterproof Sun Energy Night


Bar Killer Boots glasses Bar Vision

Trade 8000 2000 2000 2000 1000 3000


Price (points)

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

You are also given the following information:

● The frequency of each leaf node except e, l, and c is 1.


● The left and right child nodes of the root have frequencies 5 and 8 respectively.

Now answer the following questions.


CO2 a) Suppose in a Huffman tree, the distances from the root to the pair of leaves denoting the 01
letters m and e are 3 and 5 respectively, which letter between them is more frequent in the
original text? Just mention the letter.
b) Draw the Huffman tree from the given coding table above. 03
c) Continuing on Q(b), what are the frequencies of e, and c in the original text? Just mention 02
the frequencies.

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).

Solutions (question 3):


a) m.

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 𝐺.

Now answer the following questions.

CO2 a) Find a vertex in 𝐺 that is guaranteed to be contained in a source connected component. 01


Recall that a source connected component in an SCC DAG is a node that has indegree zero.
For example, in the following SCC DAG, {𝑎, 𝑏, 𝑒} is a source connected component.

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

1b- not valid, convert should be polynomial but 2n is 1b- valid


2 exponential. 2

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

2a- 3+4+9=16 2a- 5+3+2+4=14, item 1,2,3,5 painkiller, boots, sunglass,


5 7 night vision

2b- items: 0,1,3 diamond, jewelry, painting or jewelry, gold 2b- O(2^N)
2 crest 3

2c- Not valid


3

3a- b 3b-3 tree draw 3a- m 3b-3 tree draw


1 1

3c- 2,2 3c- 2,2


2 2

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

4e- (a,b,e) (c) (d,f,g,h,i) 4e- (a,b,e) (c) (d,f,g,h,i)


2 2

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

You might also like