Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

removed data redundency by huffman algorithm and made test cases along with analysis of algorithm and a readme.md #515

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
97 changes: 97 additions & 0 deletions pydatastructs/huffman/Analysis.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
import random


def rand_codeword(n): # function to generate a random n length codeword

word = ""
for i in range(n):
# give 0 or 1 as the ith bit in the codeword
temp = str(random.randint(0, 1))
word += temp
return word # n length codeword


def BSC(x, p): # simulate the output of BSC channel
y = ""
for i in range(len(x)):
# generate a float b/w 0 and 1; if it is <= P(bitflip) then bitflip occurs
check = random.random()
if check <= p:
if x[i] == "1": # bitflip
y += "0"
else:
y += "1"
else:
y += x[i] # no bitflip
return y


# Driver code starts here

f = open("Results.txt", 'w')

for i in range(6):
# parameters
N = 2000

n = int(input("n = "))
f.write("n = ")
f.write(str(n))
f.write("\n")
k = int(input("k = "))
f.write("k = ")
f.write(str(k))
f.write("\n")
p = float(input("p = "))
f.write("p = ")
f.write(str(p))
f.write("\n")

codeword_list = set() # set to store 2^k random codewords
# set ensures that there is no duplication
while len(codeword_list) < pow(2, k):
word = rand_codeword(n)
# generate 2^k random n length codewords and store them in the set
codeword_list.add(word)

Len = len(codeword_list) # cardinality of code C = 2^k
pOfEmin = 1
for c in range (5):
E = 0
for i in range(N):
# pick a random codeword from the set as input to BSC
ip = random.choice(tuple(codeword_list))
y = BSC(ip, p) # y is output of BSC

j = 0
min = n # variable to track minimun Hamming distance
word = "" # codeword with minimum Hamming distance
for j in codeword_list:
dist = 0 # initialising distance to be 0
for k in range(n):
if y[k] != j[k]:
dist += 1 # incrementing distance when flipped bit is found
if dist < min: # updating min Hamming distance and estimate
min = dist
# word is the estimate; if it is not equal to the input, then E is incremented
word = j
if word != ip:
E += 1

# No. of decoding errors
f.write("E = ")
f.write(str(E))
f.write("\n")

# P(error) for a random code C and BSC with parameters (n, k, p)
pOfE = E/N
if pOfE < pOfEmin:
pOfEmin = pOfE

f.write("P(Error) = ")
f.write(str(pOfE))
f.write("\n")

f.write("Minimum P(Error) = ")
f.write(str(pOfEmin))
f.write("\n\n")
7 changes: 7 additions & 0 deletions pydatastructs/huffman/Decoded1.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane
Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane
Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane
Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane
Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane
Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane
Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane Ananya Sane
1 change: 1 addition & 0 deletions pydatastructs/huffman/Decoded2.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
do not go gentle into that good night old age should burn and rave at close of day rage rage against the dying of the light though wise men at do not go gentle into that good night old age should burn and rave at close of day rage rage against the dying of the light though wise men at do not go gentle into that good night old age should burn and rave at close of day rage rage against the dying of the light though wise men at do not go gentle into that good night old age should burn and rave at close of day rage rage against the dying of the light though wise men at do not go gentle into that good night old age should burn and rave at close of day rage rage against the dying of the light though wise men at do not go gentle into that good night old age should burn and rave at close of day rage rage against the dying of the light though wise men at do not go gentle into that good night old age should burn and rave at close of day rage rage against the dying of the light though wise men at do not go gentle into that good night old age should burn and rave at close of day rage rage against the dying of the light though wise men at do not go gentle into that good night old age should burn and rave at close of day rage rage against the dying of the light though wise men at do not go gentle into that good night old age should burn and rave at close of day rage rage against the dying of the light though wise men at do not go gentle into that good night old age should burn and rave at close of day rage rage against the dying of the light though wise men at do not go gentle into that good night old age should burn and rave at close of day rage rage against the dying of the light though wise men at do not go gentle into that good night old age should burn and rave at close of day rage rage against the dying of the light though wise men at do not go gentle into that good night old age should burn and rave at close of day rage rage against the dying of the light though wise men at do not go gentle into that good night old age should burn and rave at close of day rage rage against the dying of the light though wise men at do not go gentle into that good night old age should burn and rave at close of day rage rage against the dying of the light though wise men at do not go gentle into that good night old age should burn and rave at close of day rage rage against the dying of the light though wise men at do not go gentle into that good night old age should burn and rave at close of day rage rage against the dying of the light though wise men at do not go gentle into that good night old age should burn and rave at close of day rage rage against the dying of the light though wise men at do not go gentle into that good night old age should burn and rave at close of day rage rage against the dying of the light though wise men at do not go gentle into that good night old age should burn and rave at close of day rage rage against the dying of the light though wise men at do not go gentle into that good night old age should burn and rave at close of day rage rage against the dying of the light though wise men at do not go gentle into that good night old age should burn and rave at close of day rage rage against the dying of the light though wise men at do not go gentle into that good night old age should burn and rave at close of day rage rage against the dying of the light though wise men at do not go gentle into that good night old age should burn and rave at close of day rage rage against the dying of the light though wise men at do not go gentle into that good night old age should burn and rave at close of day rage rage against the dying of the light though wise men at do not go gentle into that good night old age should burn and rave at close of day rage rage against the dying of the light though wise men at do not go gentle into that good night old age should burn and rave at close of day rage rage against the dying of the light though wise men at do not go gentle into that good night old age should burn and rave at close of day rage rage against the dying of the light though wise men at do not go gentle into that good night old age should burn and rave at close of day rage rage against the dying of the light though wise men at do not go gentle into that good night old age should burn and rave at close of day rage rage against the dying of the light though wise men at do not go gentle into that good night old age should burn and rave at close of day rage rage against the dying of the light though wise men at do not go gentle into that good night old age should burn and rave at close of day rage rage against the dying of the light though wise men at do not go gentle into that good night old age should burn and rave at close of day rage rage against the dying of the light though wise men at do not go gentle into that good night old age should burn and rave at close of day rage rage against the dying of the light though wise men at do not go gentle into that good night old age should burn and rave at close of day rage rage against the dying of the light though wise men at do not go gentle into that good night old age should burn and rave at close of day rage rage against the dying of the light though wise men at do not go gentle into that good night old age should burn and rave at close of day rage rage against the dying of the light though wise men at do not go gentle into that good night old age should burn and rave at close of day rage rage against the dying of the light though wise men at do not go gentle into that good night old age should burn and rave at close of day rage rage against the dying of the light though wise men at do not go gentle into that good night old age should burn and rave at close of day rage rage against the dying of the light though wise men at do not go gentle into that good night old age should burn and rave at close of day rage rage against the dying of the light though wise men at do not go gentle into that good night old age should burn and rave at close of day rage rage against the dying of the light though wise men at do not go gentle into that good night old age should burn and rave at close of day rage rage against the dying of the light though wise men at do not go gentle into that good night old age should burn and rave at close of day rage rage against the dying of the light though wise men at do not go gentle into that good night old age should burn and rave at close of day rage rage against the dying of the light though wise men at do not go gentle into that good night old age should burn and rave at close of day rage rage against the dying of the light though wise men at do not go gentle into that good night old age should burn and rave at close of day rage rage against the dying of the light though wise men at do not go gentle into that good night old age should burn and rave at close of day rage rage against the dying of the light though wise men at do not go gentle into that good night old age should burn and rave at close of day rage rage against the dying of the light though wise men at
Loading