


Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!



Question 1. Which of the following sets of binary codes could not be a Huffman code for any message? Explain/justify your answer.
a) 11, 101, 100, 01, 000 b) 0, 10, 110, 1110, 1111 c) 10, 01, 010, 110, 111
Question 2. In this question, you are asked to construct some Huffman codes for a specific message.
a) Construct a Huffman code for the message “bananarama”. Show both the tree you construct and the binary codes used for each of the 5 symbols in the message. How many bits are required to represent the message using this code? (Just the message. Not the description of the code itself.)
b) You might have noticed that at some points in the process of constructing the first tree, you had the ability to choose several different sets of characters to merge. See what happens if you choose differently. In particular, try to make choices that produce a Huffman tree with a different structure than the tree you constructed for part a. Show both the tree you construct and the binary codes used for each of the 5 symbols in the message. How many bits are required to represent the message “bananarama” using this code?
Question 3. In this question, we would like you to construct a Huffman code given information about the probabilities of letter occurrences, rather than an actual message.
a) Give a Huffman code for messages composed of the six symbols a, b, c, d, e, and f assuming that the letters appear in messages with the following probabilities:
Letter Probability Letter Probability Letter Probability a 7/30 b 10/30 c 3/ d 4/30 e 4/30 f 2/
b) What is the expected number of bits per symbol when the code you constructed is used to encode messages with the given distribution?
Question 4. For this question, we would like you to investigate an alternative to Huffman codes known as Shannon- Fano codes. The Huffman algorithm builds a tree from the bottom up. It starts by merging single letters into pairs. Then, it repeatedly joins small collections of letters to form larger collections. The Shannon-Fano algorithm works instead from the top down. It starts by splitting the letters into two sets, then splits each of these sets into smaller sets until eventually all the letters have been split into sets of their own. Each split leads to the construction of a branch in the tree. When the algorithm splits a set of letters into two smaller subsets it first sorts the letters in decreasing order by their frequencies and second, finds the point in the sorted list where the sum of the frequencies on one side roughly equals the sum of the frequencies on the other side. We give a complete description of the Shannon-Fano algorithm along with an example execution of it on the message “bananarama” in the Appendix at the end of this homework. Sometimes the Shannon-Fano algorithm produces a code that is as efficient as the Huffman code, but not always. We want you to show an example message where the code produced by the Huffman algorithm is more efficient than the code produced by the Shannon-Fano algorithm.
Question 5. Consider the following simple Java program (with the import statements removed to save space). You should recognize this program from lecture. It displays a button on the screen. Initially the button’s label is “Click Here”. Each time the button is clicked, a JLabel displaying the text “That tickles” is added to the display and the label on the button is replaced by the text “Click Here.”
public class TouchyButtonAgain extends GUIManager { private JButton myButton;
public TouchyButtonAgain() { this.createWindow( 170, 300 );
contentPane.add( new JLabel( "Click on the button below" ) );
myButton = new JButton( "Click Here" ); contentPane.add( myButton ); }
public void buttonClicked( ) { contentPane.add( new JLabel( "That tickles!" ) ); myButton.setText( "Click again" ); } }
Below, you will find several programs that are similar but not identical to the program shown above. The differ- ences all involve the declarations and uses of variable and parameter names. Some of these programs will function exactly like the program shown above. Others will work differently or not at all. For each program, indicate whether a) it will work like the original program, b) a syntax error will be detected as soon as you compile the program, or c) the program will run, but it will not behave like the original (perhaps causing an error to occur while it runs). In cases b and c, briefly describe the error or change in the program’s behavior. While we certainly cannot stop you from actually typing in and running these programs, this assignment will be of much greater value if you answer the questions by simply carefully examining their text and applying your knowledge of how Java programs are interpreted.
a) public class TouchyButtonAgain extends GUIManager { private JButton myButton;
public TouchyButtonAgain() { this.createWindow( 170, 300 );
contentPane.add( new JLabel( "Click on the button below" ) );
myButton = new JButton( "Click Here" ); contentPane.add( new JButton( "Click Here" ) ); }
public void buttonClicked( ) { contentPane.add( new JLabel( "That tickles!" ) ); myButton.setText( "Click again" ); } }
b) public class TouchyButtonAgain extends GUIManager {
public TouchyButtonAgain() { JButton myButton;
this.createWindow( 170, 300 );
contentPane.add( new JLabel( "Click on the button below" ) );
myButton = new JButton( "Click Here" ); contentPane.add( myButton ); }
public void buttonClicked( ) { contentPane.add( new JLabel( "That tickles!" ) ); myButton.setText( "Click again" );
Similarly, the list {r, b} will result in the building of the tree:
Now, we get to apply Step 4 again to these two trees, since they are the trees built from the prefix and suffix of the list {n, m, r, b}. The result is to build the tree:
Finally, we follow Step 4 one last time to combine the trivial tree built for the prefix {a} with the tree we just obtained for {n, m, r, b} to obtain the complete tree:
Notice that the the Shannon-Fano tree for bananarama is very similar to the Huffman tree you created in Ques- tion 2. But, as we note above, this is not always the case.