Greedy Technique · Algorithms
11 questions · asked in 7 of 27 years (2000–2026)
MCQ 7 · MSQ 1 · NAT 31-mark 6 · 2-mark 5
Questions 6–10 of 11
Answer: (D)
Exchange argument: take two neighbouring jobs then that start at time . Their cost is . If we swap them, it is . The first is smaller by , so should come first when , that is when . Other jobs are not affected by the swap. So any order with a neighbouring pair out of this rule can be improved, and the best order is non-increasing .
Check with = P1 (1,3), P2 (2,5), P3 (3,2), P4 (4,4), where :
(A) order P3, P1, P4, P2 (increasing ): completion times 2, 5, 9, 14, so .
(B) order P4, P3, P2, P1 (decreasing ): completion times 4, 6, 11, 14, so .
(C) order P4, P2, P3, P1 (decreasing ): completion times 4, 9, 11, 14, so .
(D) order P3, P4, P2, P1 (decreasing ): completion times 2, 6, 11, 14, so , the smallest.
Answer: (D)
What is the average length of the Huffman code for the letters a,b,c,d,e,f?
Answer: (D)
Given probabilities:
Huffman tree:
Codes and lengths:
Average length:
Answer: (D)
Which of the following is the Huffman code for the letter a, b, c, d, e, f?
Answer: (A)
Treat the probabilities as frequencies out of 32: a, b, c, d, e, f get 16, 8, 4, 2, 1, 1.
Build the Huffman tree by merging the two smallest nodes each time: e + f = 1 + 1 = 2, then d + (ef) = 2 + 2 = 4, then c + (def) = 4 + 4 = 8, then b + (cdef) = 8 + 8 = 16, then a + (bcdef) = 16 + 16 = 32.
Label each left branch 0 and each right branch 1. Reading the paths from the root gives a = 0, b = 10, c = 110, d = 1110, e = 11110, f = 11111, which is option (A).
Check with the average code length: (A) gives . The other options are longer on average: (B) has lengths 2, 2, 3, 3, 3, 3, giving ; (C) has lengths 2, 2, 2, 3, 4, 4, giving ; (D) uses 3 bits for every letter, giving 3. So only (A) can be a Huffman code.
Answer: (A)
a:1, b:1, c:2, d:3, e:5, f:8, g:13, h:21
A Huffman code is used to represent the characters. What is the sequence of characters corresponding to the following code?
110111100111010
Answer: (A)
Build the Huffman tree by merging the two smallest nodes each time:
, , , , , , .
Reading the paths from the root, the codewords needed are . The rest are .
No codeword is a prefix of another, so the string splits in only one way:
gives , that is .
Answer: (A)
What is the maximum profit earned?
Answer: (A)
Order by profit: T3 (30, d=5), T9 (25, d=3), T7 (23, d=2), T2 (20, d=2), T4 (18, d=3), T5 (18, d=4), T8 (16, d=7), T1 (15, d=7), T6 (10, d=5).
T3 goes to slot 5. T9 goes to slot 3. T7 goes to slot 2. T2 wants slot 2, which is taken, so it goes to slot 1.
T4 (d=3): slots 1, 2, 3 are all full, so it is skipped.
T5 goes to slot 4. T8 goes to slot 7. T1 wants slot 7, which is taken, so it goes to slot 6.
T6 (d=5): slots 1 to 5 are all full, so it is skipped.
Schedule: slot 1 T2, slot 2 T7, slot 3 T9, slot 4 T5, slot 5 T3, slot 6 T1, slot 7 T8.
Profit .
Answer: (A)
Keep practising