Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Assignment 2: (Due at 10:30 a.m on Friday of Week 10) Question 1 (Given in Tutorial 5) Question 2 (Given in Tutorial 7) If you do Question 1 only, you.

Similar presentations


Presentation on theme: "1 Assignment 2: (Due at 10:30 a.m on Friday of Week 10) Question 1 (Given in Tutorial 5) Question 2 (Given in Tutorial 7) If you do Question 1 only, you."— Presentation transcript:

1 1 Assignment 2: (Due at 10:30 a.m on Friday of Week 10) Question 1 (Given in Tutorial 5) Question 2 (Given in Tutorial 7) If you do Question 1 only, you get 60 points. If you do Question 2 only, you get 90 points. If you correctly do both Question 1 and Question 2, you get 100 points. Bonus: 5 Points will be given to those who write a Java program for the Huffman code algorithm.

2 2 Review of Lecture 1 to Lecture 6 Lecture 1: Some concept: Pseudo code, Abstract Data Type. (Page 60 of text book.) Stack. Give the ADT of stack (slide 11 of lecture1) The interface is on slide 19. (Q: Is the interface equivalent to ADT? Not really. We need the method for insertion and deletion, i.e., first in last out. ) Applications: parentheses matching

3 3 Lecture 2: Linked list Singly linked list Doubly linked list Just know how to setup a list. (Assignment 1) Lecture 3: Analysis of Algorithms (important) Primitive operations Count number of primitive operations for an algorithm big-O notation 2n  O(n), 5n 2 +10n+11++>O(n 2 ).

4 4 Lecture 4: Tree Definition of tree (slide 7) Tree terminology: root, internal node, external node (leaf), depth of a node, height of a node, height of a node. Inorder traversal of a binary tree Tree ADT, slide 11, Binary tree ADT, slide 17 In terms of programming, understand TreeInExample1.java. (If tested in exam, java codes will be given. I do not want to give long code.)

5 5 Lecture 5: More on Trees Linked Structure for Binary Tree. Just understand the node: Preorder traversal for any tree Postorder traversal for any tree Array-Based representation of binary tree (slide 9) Algorithms for Depth(), Height() slide 12-15. 

6 6 Lecture 6: Priority Queue (Heeps) Priority Queue ADT (slide 2) Heap: 1. definition of heap 2.What does “heap-order” mean? 3.Complete Binary tree (what is a complete binary?) 4.Height of a complete binary tree with n nodes is O(log n). 5.Insert a node into a heap runtimg time O(log n). 6.removeMin: remove a node with minimum key. Running time O(log n) Array-based complete binary tree representation. Show a sample exam paper.

7 7 Lecture 6: Priority Queue (Heeps) Priority Queue ADT (slide 2) Heap: 1. definition of heap 2.What does “heap-order” mean? 3.Complete Binary tree (what is a complete binary?) 4.Height of a complete binary tree with n nodes is O(log n). 5.Insert a node into a heap runtimg time O(log n). 6.removeMin: remove a node with minimum key. Running time O(log n) Array-based complete binary tree representation. Show a sample exam paper.

8 8 Exercise: Give some trees and ask students to give InOrder, PostOrder and PreOrder. Tutorial 6 of Question 2: Using PreOrder. Given a complete binary, write the array representation. Given an array, draw the complete binary tree. Given a heap, show the steps to removMin. Given a heap, show the steps to insert a node with key 3. (Do it for the tree version, do it for an array version.) Linear time construction of a heap.

9 Hash Tables9 Huffman codes (Page 565 Chapter 12.4) Binary character code: each character is represented by a unique binary string. A data file can be coded in two ways: abcdef frequency(%)4513121695 fixed-length code000001010011100101 variable-length code 010110011111011100 The first way needs 100  3=300 bits. The second way needs 45  1+13  3+12  3+16  3+9  4+5  4=232 bits.

10 Hash Tables10 Variable-length code Need some care to read the code. 001011101 (codeword: a=0, b=00, c=01, d=11.) Where to cut? 00 can be explained as either aa or b. Prefix of 0011: 0, 00, 001, and 0011. Prefix codes: no codeword is a prefix of some other codeword. (prefix free) Prefix codes are simple to encode and decode.

11 Hash Tables11 Using codeword in Table to encode and decode Encode: abc = 0.101.100 = 0101100 (just concatenate the codewords.) Decode: 001011101 = 0.0.101.1101 = aabe abcdef frequency(%)4513121695 fixed-length code000001010011100101 variable-length code 010110011111011100

12 Hash Tables12 Encode: abc = 0.101.100 = 0101100 (just concatenate the codewords.) Decode: 001011101 = 0.0.101.1101 = aabe (use the (right)binary tree below:) a:4 5 b:13c:1 2 d:16e: 9 f:5 0 1 10 0 1486 142858 00 0 0 0 11 1 1 a:4 5 b:13c:1 2 d:16 e: 9 f:5 55 2530 14 10 0 01 0 0 0 0 11 1 1 Tree for the fixed length codeword Tree for variable- length codeword

13 Hash Tables13 Binary tree Every nonleaf node has two children. The fixed-length code in our example is not optimal. The total number of bits required to encode a file is f ( c ) : the frequency (number of occurrences) of c in the file d T (c): denote the depth of c ’ s leaf in the tree

14 Hash Tables14 Constructing an optimal code Formal definition of the problem: Input: a set of characters C={c 1, c 2, …, c n }, each c  C has frequency f[c]. Output: a binary tree representing codewords so that the total number of bits required for the file is minimized. Huffman proposed a greedy algorithm to solve the problem.

15 Hash Tables15 a:4 5 d:16e: 9 f:5b:13c:1 2 a:4 5 d:16 e: 9 f:5 14 01 b:13c:1 2 (a) (b)

16 Hash Tables16 a:4 5 d:16 e: 9 f:5 14 01 b:13c:1 2 25 01 a:4 5 b:13c:1 2 d:16 e: 9 f:5 2530 14 01 0 011 (c) (d)

17 Hash Tables17 a:4 5 b:13c:1 2 d:16 e: 9 f:5 55 2530 14 10 0 01 0 0 0 0 11 1 1 a:4 5 b:13c:1 2 d:16 e: 9 f:5 55 2530 14 01 0 0 0 11 1 (e) (f)

18 Hash Tables18 HUFFMAN(C) 1n:=|C| 2Q:=C 3for i:=1 to n-1 do 4z:=ALLOCATE_NODE() 5x:=left[z]:=EXTRACT_MIN(Q) 6y:=right[z]:=EXTRACT_MIN(Q) 7f[z]:=f[x]+f[y] 8INSERT(Q,z) 9return EXTRACT_MIN(Q)

19 Hash Tables19 The Huffman Algorithm This algorithm builds the tree T corresponding to the optimal code in a bottom-up manner. C is a set of n characters, and each character c in C is a character with a defined frequency f[c]. Q is a priority queue, keyed on f, used to identify the two least-frequent characters to merge together. The result of the merger is a new object (internal node) whose frequency is the sum of the two objects.

20 Hash Tables20 Time complexity Lines 4-8 are executed n-1 times. Each heap operation in Lines 4-8 takes O(lg n) time. Total time required is O(n lg n). Note: The details of heap operation will not be tested. Time complexity O(n lg n) should be remembered.

21 Hash Tables21 Another example: e:4a:6c:6b:9d:11 c:6b:9d:11 e:4a:6 10 01

22 Hash Tables22 d:11 e:4a:6 10 01 c:6b:9 15 0 1 c:6b:9 15 0 1 d:11 e:4a:6 10 01 21 01

23 Hash Tables23 c:6b:9 15 0 1 d:11 e:4a:6 10 01 21 01 36 01 Summary Huffman Code: Given a set of characters and frequency, you should be able to construct the binary tree for Huffman codes. Proofs for why this algorithm can give optimal solution are not required.


Download ppt "1 Assignment 2: (Due at 10:30 a.m on Friday of Week 10) Question 1 (Given in Tutorial 5) Question 2 (Given in Tutorial 7) If you do Question 1 only, you."

Similar presentations


Ads by Google