Skip to content

Instantly share code, notes, and snippets.

@nboubakr
Created January 1, 2015 11:35
Show Gist options
  • Save nboubakr/0eec4ea650eeb6dc21f9 to your computer and use it in GitHub Desktop.
Save nboubakr/0eec4ea650eeb6dc21f9 to your computer and use it in GitHub Desktop.
Huffman coding implementation in Python
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Le codage de Huffman
# Théorie de l'information et du codage
# Etudiant: Boubakr NOUR <n.boubakr@gmail.com>
# Universite Djilali Liabes (UDL) de Sidi Bel Abbes
import heapq
from collections import defaultdict
def encode(frequency):
heap = [[weight, [symbol, '']] for symbol, weight in frequency.items()]
heapq.heapify(heap)
while len(heap) > 1:
lo = heapq.heappop(heap)
hi = heapq.heappop(heap)
for pair in lo[1:]:
pair[1] = '0' + pair[1]
for pair in hi[1:]:
pair[1] = '1' + pair[1]
heapq.heappush(heap, [lo[0] + hi[0]] + lo[1:] + hi[1:])
return sorted(heapq.heappop(heap)[1:], key=lambda p: (len(p[-1]), p))
data = "The frog at the bottom of the well drifts off into the great ocean"
frequency = defaultdict(int)
for symbol in data:
frequency[symbol] += 1
huff = encode(frequency)
print "Symbol".ljust(10) + "Weight".ljust(10) + "Huffman Code"
for p in huff:
print p[0].ljust(10) + str(frequency[p[0]]).ljust(10) + p[1]
@tejas121999
Copy link

Write Python program for implementing Huffman Coding Algorithm. Discuss the complexity of algorithm.
clickk on this following link
https://thecomputhub.blogspot.com/2020/05/Write%20Python%20program%20for%20implementing%20Huffman%20Coding%20Algorithm.%20Discuss%20the%20complexity%20of%20algorithm..html

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment