Presentation is loading. Please wait.

Presentation is loading. Please wait.

Huffman’s Algorithm 11/02/081. 2 Weighted 2-tree A weighted 2-tree T is an extended binary tree with n external nodes and each of the external nodes is.

Similar presentations


Presentation on theme: "Huffman’s Algorithm 11/02/081. 2 Weighted 2-tree A weighted 2-tree T is an extended binary tree with n external nodes and each of the external nodes is."— Presentation transcript:

1 Huffman’s Algorithm 11/02/081

2 2 Weighted 2-tree A weighted 2-tree T is an extended binary tree with n external nodes and each of the external nodes is assigned a nonnegative weight. The weighted path length P of 2-tree T is defined to be the sum of all weighted path lengths i.e. P= W 1 L 1 +W 2 L 2 +………..+W n L n. 23511 3 5 2 5 25 P 1 = 2.2+3.2+5.2+11.2 P 2 = 2.1+3.3+5.3+11.2 P 3 = 2.3+3.3+5.2+11.1 = 42 = 48 = 36 T 1 : T 2 : T 3 : Example:

3 11/02/083 Huffman’s Algorithm Huffman's algorithm is used for building an extended binary tree (2-tree) with a minimum weighted path length from a set of given weights. Suppose w 1 and w 2 are two minimum weights among the n given weights w 1, w 2,….w n. Find a tree T’ which gives a solution for the n-1 weights w 1 +w 2, w 3, w 4,…..w n. Then in the tree T’, replace the external nodes by the subtree. The new 2-tree T is the desired solution. Algorithm: Huffman’s (L, T) 1. Begin with a forest of trees. All trees are one node, with the weight of the tree equal to the weight of L. 2. Repeat this step until there is only one tree: Choose two trees T1 and T2 with the smallest weights. Create a new tree T whose root has a weight equal to the sum of the weights T 1 + T 2 and whose left subtree is T 1 and whose right subtree is T 2. 3. The final single tree T is an extended binary tree.

4 11/02/084 Example Suppose a set of 5 weights {1, 2, 3, 3, 4} are given. Construct a 2-tree having a minimum weighted path length using Huffman’s algorithm. Step 1: Step 3: Step 2: Step 4: Step 5: Final 2-tree P = 1.3+2.3+3.2+3.2+4.2 = 29

5 11/02/08Shaily Kabir,Dept. of CSE, DU5 Example: Suppose a set of 8 data items with their weights are given. Construct a 2-tree having minimum weighted path length using Huffman’s algorithm. Data Item: Weights: ABCDEFGH 2251119211255 The Resulting 2-tree with minimum weighted-path length

6 11/02/086 Application of Huffman’s Algorithm Data Item Coding Huffman's algorithm is used to construct a tree that is used for data coding. Example: 100 4456 22 31 22 A 12 25 G 19 D 11 F 11 C 7 2E2E 5B5B 5H5H 0 0 0 0 0 0 0 1 1 1 1 1 1 1 Code for Data Items A: 00 B: 11011 C: 011 D: 111 E: 11010 F: 010 G: 10 H: 1100

7 11/02/08Shaily Kabir,Dept. of CSE, DU7 Data Compression Huffman's algorithm is used to construct a tree that is used for data compression. Example: The string S “apple pie” is to be encoded. Using ASCII Code: The string “apple pie” can be encoded as follows: 110000111100001110000110110011001011000000111000011010011100101 [64 bits] CharASCIICode a971100001 p1121110000 l1091101100 e1011100101 i1061101001 space321000000

8 11/02/088 Using Huffman’s Code: Each character has an associated weight equal to the number of times the character occurs in a file. The characters in S have the following weights: CharWeights a1 p3 l1 e2 i1 Space ‘ ‘1 a1a1 l1l1 i1i1 ‘ 1 e2e2 p3p3 22 4 5 9 01 0 0 0 0 1 1 1 1 CharCode a000 p11 l001 e10 i010 Space ‘ ‘011 The string “apple pie” can be encoded as follows: 0001111001100111101010 [21 bits]

9 11/02/089 END!!!


Download ppt "Huffman’s Algorithm 11/02/081. 2 Weighted 2-tree A weighted 2-tree T is an extended binary tree with n external nodes and each of the external nodes is."

Similar presentations


Ads by Google