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

Algorithm Design Theory - Lab 4

Thai Minh Dung

March 31, 2024

Problem 1
Solution. Let N be the number of vertices in this Graph G. Applying Kruskal’s
algorithm, we choose N − 1 edges such that their weights are all 1 to construct
the Minimum Spanning Tree. We get the total weight of the MST is
2 · (N − 1). ■

Problem 2
Solution. This is the widest path problem. Hence, we got the following algo-
rithm to solve the problem which is a modified version of the Dijkstra’s Algo-
rithm:
procedure F INDW IDEST(G,V, E , h, d )
C ← −∞ ▷ C is an array where C[i] is the answer for node i.
C [d ] := ∞ ▷ Assign answer for the depot = ∞.
Q ← all nodes in graph
while Q is not empty do
u ← node in Q with max C [u]
remove u from Q
for v in neighbors(u) do
newW i d t h ← mi n(C [u], h[u, v])
if newW i d t h > C [v] then
C [v] ← newW i d t h
end if
end for
end while
return C
end procedure

1
The following algorithm has total time complexity of O (E ·l og (V )) as it is similar
to Dijkstra’s algorithm. ■

Problem 3
Solution. The greedy property and optimal structure of the Huffman coding al-
gorithm:

• Greedy property: The Huffman coding algorithm builds an optimal pre-


fix code using a greedy strategy. During each iteration, the algorithm
merges the two symbols with the lowest frequencies into a single node
that has a weight equal to their sum. This process is repeated until all
symbols are merged into a single tree. This approach is greedy because
at each step the algorithm chooses the two symbols with the lowest fre-
quencies, without considering their future impact on the encoding of
other symbols.

• Optimal structure: The optimal prefix code for a set of characters can be
obtained using a recursive approach in Huffman coding. This involves
combining the two characters with the lowest frequencies into a single
node, and repeating the process with the resulting set of characters un-
til all characters are combined into a single root node. By applying this
process recursively, the optimal prefix code for the original set of charac-
ters can be constructed by combining the optimal prefix codes for smaller
sets of characters. This efficient construction of the Huffman tree and the
optimal prefix code is possible due to the optimal substructure property
of Huffman coding.

2
Problem 4

Solution. Firstly, ’d’ and ’e’ are chosen by the algorithm to create node 2 + 2 = 4
and assign the edges to ’d’ and ’e’ 0 and 1, respectively.
Secondly, ’b’ and ’c’ are chosen to create node 4 + 4 = 8 and assign the edges to
’b’ and ’c’ 0 and 1, respectively.
Next, node 4 and ’a’ are chosen to create node 4+8 = 12 and assign the edges to
4 and ’a’ 0 and 1.
Finally, node 8 and 12 are added up to node 20, with edge 0 and 1, respectively.
We got the following Huffman coding table:
⇒ Huffman("aaaaaaaabbbbccccddee") = 11111111111111110000000001010101100100101101.

Character Frequency Huffman code


a 8 11
b 4 00
c 4 01
d 2 100
e 2 101

You might also like