익명 사용자
로그인하지 않음
토론
기여
계정 만들기
로그인
IT 위키
검색
Huffman Code
편집하기
IT 위키
이름공간
문서
토론
더 보기
더 보기
문서 행위
읽기
편집
원본 편집
역사
경고:
로그인하지 않았습니다. 편집을 하면 IP 주소가 공개되게 됩니다.
로그인
하거나
계정을 생성하면
편집자가 사용자 이름으로 기록되고, 다른 장점도 있습니다.
스팸 방지 검사입니다. 이것을 입력하지
마세요
!
'''Huffman Code''' is a lossless data compression algorithm that assigns variable-length binary codes to input characters based on their frequency. It is widely used in data compression applications such as file compression and encoding. ==Concept== Huffman coding works by assigning shorter codes to more frequent characters and longer codes to less frequent characters, minimizing the total number of bits used to represent the data. ==Algorithm== The Huffman coding algorithm follows these steps: #'''Calculate Frequency:''' Count the occurrence of each character in the input data. #'''Build a Priority Queue:''' Create a min-heap where each node represents a character and its frequency. #'''Construct a Huffman Tree:''' #*Extract the two nodes with the lowest frequency. #*Merge them into a new node with their combined frequency. #*Repeat until only one node remains, which becomes the root of the tree. #'''Assign Binary Codes:''' Traverse the tree and assign binary values (0 for left, 1 for right) to generate unique Huffman codes for each character. ==Example== Given the input string '''hello world''', the frequency table and corresponding Huffman codes might be: {| class="wikitable" !Character!!Frequency!!Huffman Code |- |h||1||1100 |- |e||1||1101 |- |l||3||10 |- |o||2||00 |- |w||1||1110 |- |r||1||1111 |- |d||1||01 |}The compressed representation replaces each character with its Huffman code. ==Implementation== A simple implementation of Huffman coding in Python:<syntaxhighlight lang="python"> import heapq from collections import Counter, namedtuple class Node(namedtuple("Node", ["char", "freq", "left", "right"])): def __lt__(self, other): return self.freq < other.freq def build_huffman_tree(text): freq = Counter(text) heap = [Node(char, freq, None, None) for char, freq in freq.items()] heapq.heapify(heap) while len(heap) > 1: left = heapq.heappop(heap) right = heapq.heappop(heap) merged = Node(None, left.freq + right.freq, left, right) heapq.heappush(heap, merged) return heap[0] def generate_codes(node, prefix="", codebook={}): if node: if node.char: codebook[node.char] = prefix generate_codes(node.left, prefix + "0", codebook) generate_codes(node.right, prefix + "1", codebook) return codebook text = "hello world" tree = build_huffman_tree(text) codes = generate_codes(tree) print(codes) </syntaxhighlight> ==Advantages== *'''Lossless Compression''' **Ensures no data loss in reconstruction. *'''Efficient Encoding''' **Optimized for frequent characters, reducing overall storage size. *'''Used in Many Applications''' **Commonly applied in ZIP, GZIP, JPEG, and other compression formats. ==Limitations== *'''Requires Frequency Table''' **The table must be transmitted alongside the encoded data. *'''Not Optimal for Small Texts''' **Overhead can be significant for short messages. *'''Sensitive to Input Data''' **Performance depends on character frequency distribution. ==Applications== *'''File Compression''' **Used in ZIP and GZIP formats. *'''Multimedia Encoding''' **Applied in JPEG and MP3 compression. *'''Data Transmission''' **Efficient encoding in communication protocols. ==See Also== *[[Data Compression]] *[[Prefix Code]] *[[Run-Length Encoding]] *[[Shannon-Fano Coding]] *[[Entropy Encoding]]
요약:
IT 위키에서의 모든 기여는 크리에이티브 커먼즈 저작자표시-비영리-동일조건변경허락 라이선스로 배포된다는 점을 유의해 주세요(자세한 내용에 대해서는
IT 위키:저작권
문서를 읽어주세요). 만약 여기에 동의하지 않는다면 문서를 저장하지 말아 주세요.
또한, 직접 작성했거나 퍼블릭 도메인과 같은 자유 문서에서 가져왔다는 것을 보증해야 합니다.
저작권이 있는 내용을 허가 없이 저장하지 마세요!
취소
편집 도움말
(새 창에서 열림)
둘러보기
둘러보기
대문
최근 바뀜
광고
위키 도구
위키 도구
특수 문서 목록
문서 도구
문서 도구
사용자 문서 도구
더 보기
여기를 가리키는 문서
가리키는 글의 최근 바뀜
문서 정보
문서 기록