Semester
Subject
Year
Tribhuwan University
2082
Bachelor Level / Third Year / Fifth Semester / Science
(Design and Analysis of Algorithms)
Full Marks: 60
Pass Marks: 24
Time: 3 Hours
Candidates are required to give their answers in their own words as for as practicable.
The figures in the margin indicate full marks.
Long Answers Questions
An optimal solution is the best possible solution to a problem that maximizes or minimizes the objective function while satisfying all given constraints.
No, a greedy algorithm does not always guarantee an optimal solution.
Cases where greedy gives optimal solution:
Cases where greedy FAILS to give optimal solution:
Step i: Calculate Frequency of each character
String: S U P E R D U P E R C S I T
| Character | Frequency |
|---|---|
| S | 2 |
| U | 2 |
| P | 2 |
| E | 2 |
| R | 2 |
| (space) | 2 |
| D | 1 |
| C | 1 |
| I | 1 |
| T | 1 |
Total characters = 16
Step ii: Build the Huffman Tree using Greedy approach
The greedy strategy is: always pick two nodes with the smallest frequencies and merge them.
a. Pick D(1) and C(1) → merge → new node DC(2)
b. Pick I(1) and T(1) → merge → new node IT(2)
c. Now all nodes have frequency 2: S(2), U(2), P(2), E(2), R(2), Space(2), DC(2), IT(2)
d. Pick S(2) and U(2) → merge → SU(4)
e. Pick P(2) and E(2) → merge → PE(4)
f. Pick R(2) and Space(2) → merge → R_(4)
g. Pick DC(2) and IT(2) → merge → DCIT(4)
h. Pick SU(4) and PE(4) → merge → SUPE(8)
i. Pick R_(4) and DCIT(4) → merge → R_DCIT(8)
j. Pick SUPE(8) and R_DCIT(8) → merge → Root(16)
Step iii: Final Huffman Tree Structure

Step iv: Assign Huffman Codes (Left = 0, Right = 1)
| Character | Code | Bits |
|---|---|---|
| S | 000 | 3 |
| U | 001 | 3 |
| P | 010 | 3 |
| E | 011 | 3 |
| R | 100 | 3 |
| Space | 101 | 3 |
| D | 1100 | 4 |
| C | 1101 | 4 |
| I | 1110 | 4 |
| T | 1111 | 4 |
Step v: Calculate Total Bits
The Huffman coding algorithm is a perfect example where the greedy approach guarantees an optimal solution — it produces the minimum weighted path length (prefix-free code) for data compression. However, greedy algorithms do not guarantee optimality for all problems.
Short Answers Questions
