Assignment 9 with Answer - Data Structures and Algorithms | CMPSC 465, Assignments of Computer Science

Material Type: Assignment; Class: Data Structures and Algorithms; Subject: Computer Science; University: Penn State - Main Campus; Term: Spring 2009;

Typology: Assignments

Pre 2010

Uploaded on 09/24/2009

koofers-user-6zy-2
koofers-user-6zy-2 🇺🇸

8 documents

1 / 5

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
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.
pf3
pf4
pf5

Partial preview of the text

Download Assignment 9 with Answer - Data Structures and Algorithms | CMPSC 465 and more Assignments Computer Science in PDF only on Docsity!

Due: Saturday, April 18, 1 pm. slide under the door of IST 346H, between 7 pm Friday^ CMPSC 465, Spring 2009, Homework 9

and 1 pm Saturday they will be box in front of IST 337.

Problem 1. a) Explain how Pictures for Prim’s Algorithm illustrate the algorithm. What are the

edge costs? What is the meaning of the colors of edges? What is the meaning ofred and blue numbers?

Answer. were already removed from the priority queue by Red numbers are priorities of nodes in the priority queue DeleteMin(Q) have blue numbers (with Q. The nodes which

priority values they had when they were removed). Because the priority of a node is theleast 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-barshave 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 thetree.

b) Answer. Use printouts of the third page of that file to illustrate how Kruskal’s algorithm runs. See page 2.

b) Answer. Use printouts of the third page of that file to illustrate how Disjksta’s algorithm runs. See page 2.

Problem 2. 1’s occur with probability that is twice larger. Make an ‘‘alphabet’’ of strings of length 3 Your task is to compress a text in which each symbol is random, 0 or 1, but

and provide Huffman code. How much shorter will the code be compared with the text,on the average?

Answer. frequency 2/27, 011, 101, 110 with frequency 4/27, and 111 with frequency 8/27. The ‘‘alphabet’’ has 8 ‘‘symbols’’: 000 with frequency 1/27, 001, 010, 100 with We

multiply frequencies by 27, and we have frequency sequence 1,2,2,2,4,4,4,8. We createnode A for 000, 001 with frequency 3, and B for 010, 100 with 4; now we have frequency

sequence 3,4,4,4,4,8.sequence 4,7,8,8. Then we get 11,16 and finally, 27. We create C for A,B and D for 011, 101 and we get frequency

decode 10(7) and 11(8).Decoding: 27 has grandchildren 4,7,8,8, 00 is the code of 111, 01 of 011 and we still

We give 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 frequency of 27, each code length 3, the total was 81.

12 have codes by 1 shorter, so the net savings is 5, which means 100%Now symbols with frequency 7 have codes by 1 longer, and symbols with frequency × 5/81 = 6.17%

savings.

Exercise 6, page 333. order of the frequencies of the characters. Construct Huffman tree in linear time, given the alphabet sorted in

cisely, non-decreasing) frequencies. Thus we can implement the priority queue of nodesWe observe that the new nodes are constructed in order of increasing (more pre-

from two queues: one for the initial nodes, the characters, with the least frequent charac-ter in front, and the second for the new nodes. We select the least frequent node by com-

paring two queue fronts, we do it again, and the new node, the parent of the two nodesthus selected, goes to the rear of the queue of new nodes.

(1,2,2,2,4,4,4,8)(), 1,2 extracted, 3 formedFor Problem 2, this would work as follows:

(2,2,4,4,4,8)(3), 2,2 extracted, 4 formed(4,4,4,8)(3,4), 3,4 extracted, 7 formed

(4,4,4,8)(7), 4,4 extracted, 8 formed(4,8)(7,8), 4,7 extracted, 11 formed

(8)(8,11), 8,8 extracted, 16 formed()(11,16), 11,16 extracted, 27 formed

Problem 1b. a b c d

e f g h i j k l m n o p

a b c d e f g h i j k l m n o p

a b c d e f g h i j k l a b c d^ m^ n^ o^ p e f g h i j k l m n o p

a b c d e f g h i j k l m n o p

a b c d e f g h i j k l a b c d^ m^ n^ o^ p e f g h i j k l m n o p

a b c d e f g h i j k l m n o p

a b c d e f g h i j k l a b c d^ m^ n^ o^ p e f g h i j k l m n o p

a b c d e f g h i j k l m n o p

a b c d e f g h i j k l a b c d^ m^ n^ o^ p e f g h i j k l m n o p

a b c d e f g h i j k l m n o p

a b c d e f g h i j k l m n o p

Problem 1c.

a b c d e f g h i j k l m n o p

a e bf cg (^) 1 dh i j k l m n o p

a b c d e f g h i j k l m n o p 3 5

a b c d e f g h i j k l m n o p

a e bf (^) 2 cg dh i j k l m n o p

a b c d e f g h i j k l m n o p 5

a b c d e f g h i j k l m n o p

a e bf (^) 4 cg dh i j k l m n o p

a e bf 4 cg dh 5 i j k l

After the last selection, all tentative selections for path edges (pink) will be final, and^ m^ n^ o^ p

there are no more priority changes.