
Homework 9 solutions 1
CMPSC 465, Spring 2009, Homework 9
Due: Saturday,April 18, 1 pm. slide under the door of IST 346H, between 7 pm Friday
and 1 pm Saturday theywill be box in front of IST 337.
Problem 1.
a) Explain howPictures for Prim’sAlgorithm illustrate the algorithm. What are the
edge costs? What is the meaning of the colors of edges? What is the meaning of
red and blue numbers?
Answer. Red numbers are priorities of nodes in the priority queue Q.The nodes which
were already removedfrom the priority queue by DeleteMin(Q)hav e blue numbers (with
priority values theyhad when theywere removed). Because the priority of a node is the
least edge cost that connects it to the tree, we can see that dashed edges have cost 1, thin
solid edges have cost 2, solid edges with thin cross-bars have cost 3, with thick cross-bars
have cost 4 and thick edges have cost 5. Edges with blue background are in the tree, and
edges with pink background are those least cost edges connecting non-tree nodes with the
tree.
b) Use printouts of the third page of that file to illustrate howKruskal’salgorithm runs.
Answer. See page 2.
b) Use printouts of the third page of that file to illustrate howDisjksta’salgorithm runs.
Answer. See page 2.
Problem 2. Your task is to compress a text in which each symbol is random, 0 or 1, but
1’soccur with probability that is twice larger.Makean‘‘alphabet’’ofstrings of length 3
and provide Huffman code. Howmuch shorter will the code be compared with the text,
on the average?
Answer. The ‘‘alphabet’’has 8 ‘‘symbols’’: 000 with frequency1/27, 001, 010, 100 with
frequency2/27, 011, 101, 110 with frequency4/27, and 111 with frequency8/27. We
multiply frequencies by 27, and we have frequencysequence 1,2,2,2,4,4,4,8. We create
node A for 000, 001 with frequency3,and B for 010, 100 with 4; nowwehav e frequency
sequence 3,4,4,4,4,8. We create C for A,B and D for 011, 101 and we get frequency
sequence 4,7,8,8. Then we get 11,16 and finally,27.
Decoding: 27 has grandchildren 4,7,8,8, 00 is the code of 111, 01 of 011 and we still
decode 10(7) and 11(8).
We giv e codes 110 an 111 to 110 and 101.
10 has 4 grandchildren that get codes 1000, 1001, 1010, 1011. Their joint frequency
is 7.
Before, we had total frequencyof27, each code length 3, the total was 81.
Nowsymbols with frequency7hav e codes by 1 longer,and symbols with frequency
12 have codes by 1 shorter,sothe net savings is 5, which means 100% ×5/81 = 6.17%
savings.