Greedy algorithm application-Huffman coding

Source: Internet
Author: User

Huffman coding is applied to the encoding of data files and image compression. Its compression rate is usually between 20%~90%, in the case of long-distance communication, it is often necessary to convert the text to be transferred into a string consisting of binary characters, and the total length of the message to be transmitted as short as possible. It is obvious that the total length of the message to be transmitted can be reduced by using the shortest possible encoding of the number of occurrences of a point article.

The core idea of Huffman coding:

(1) Each character with a 0,1 string as its code, and requires any one character code is not the prefix of other character code.

(2) using the frequency of characters in the file to establish a 0,1 string representation of the optimal representation of each character, even if the occurrence of high-frequency characters to obtain a shorter encoding, the occurrence of low-frequency characters to obtain a longer encoding.

(3) The frequency of the characters appearing in the file as the weights of the leaf nodes of a binary tree, and the optimal prefix code is obtained by constructing a Huffman tree.

The construction of Huffman tree

Huffman Tree is also called the optimal binary tree. It is composed of n weighted leaf nodes in all two-fork tree, with a weighted path length of the smallest two-tree (that is, all leaf nodes in the tree with the length of the path of the sum of the weights). Obviously, the process of finding Huffman coding is the process of constructing Huffman tree.

Steps to construct Huffman tree

1) a forest f={t1,t2 of n binary trees with n nodes corresponding to the given n weights {W1,W2......W3}. Tn}, where each binary tree ti has only one weight value of the root node of WI, and its left and right subtree is empty.

2) in the forest F select the two root node weights of the smallest two fork tree, as a new binary tree of the left and right sub-tree, mark the new binary tree root node weights for its left and right sub-tree root node weights of the sum.

3) Remove the two binary trees selected from F, and add the newly formed two tree to the forest F.

4) Repeat the second to third operation, until the forest contains only a binary tree, at this time to get this binary tree is Huffman tree.

Huffman Tree has no degree of 1 nodes, and has n characters of Huffman tree node total of 2n-1, so you can use sequential storage structure to implement Huffman algorithm. Stores the node information in a one-dimensional array of 2n-1. When the Huffman tree is constructed, the encoding process of each character is to walk a path from the leaf node to the root node. The decoding process takes a path from the root node to the leaf node. Thus, the node structure of Huffman tree contains information about its children's nodes and parents.

Greedy algorithm application-Huffman coding

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.