ajout code huffman et arithmétique
This commit is contained in:
parent
618f6bcc11
commit
930cfb8a90
4 changed files with 166 additions and 0 deletions
29
455-Codage_Sources/algo_code/code_arithmetique.py
Executable file
29
455-Codage_Sources/algo_code/code_arithmetique.py
Executable file
|
@ -0,0 +1,29 @@
|
|||
#!/usr/bin/env python3
|
||||
|
||||
import numpy as np
|
||||
|
||||
|
||||
p_s = [0.1,0.9]
|
||||
P = np.random.rand(N)
|
||||
X = [0 for p in P if p > p_s[0] else 1]
|
||||
|
||||
|
||||
def binary(n,b=2,m):
|
||||
"""
|
||||
Convertie un nombre décimal en sa version binaire tronqué à m bits.
|
||||
"""
|
||||
return np.ceil(n*b**m)
|
||||
|
||||
def arithm(X):
|
||||
l=[0]
|
||||
h= [1]
|
||||
for x in X:
|
||||
if x == 0:
|
||||
h.append(l[-1]+p_s[0]*(h[-1]-l[-1]))
|
||||
l.append(l[-1])
|
||||
else:
|
||||
l.append(l[-1]+p_s[0]*(h[-1]-l[-1]))
|
||||
h.append(h[-1])
|
||||
|
||||
lmb = (l[-1]+h[-1])/2
|
||||
mu = int(-np.log2(h[-1]-l[-1]))+1
|
24
455-Codage_Sources/algo_code/gen_markov1.py
Executable file
24
455-Codage_Sources/algo_code/gen_markov1.py
Executable file
|
@ -0,0 +1,24 @@
|
|||
#!/usr/bin/env python3
|
||||
# -*- coding: utf-8 -*-
|
||||
"""
|
||||
Created on Mon Jan 28 12:31:21 2019
|
||||
|
||||
@author: pac
|
||||
"""
|
||||
|
||||
import numpy as np
|
||||
P = [[0.8,0.1,0.1],[0.1,0.7,0.2],[0.1,0.2,0.7]]
|
||||
def get_state(p,cump):
|
||||
print(p, cump)
|
||||
for i in range(len(cump)):
|
||||
if p < cump[i]:
|
||||
return i
|
||||
|
||||
def gen_markov1(N,P,init_state=0):
|
||||
cumP = np.cumsum(P,1)
|
||||
states = [init_state]
|
||||
p = np.random.rand(N)
|
||||
for k in range(N):
|
||||
states.append(get_state(p[k],cumP[states[-1]]))
|
||||
return states
|
||||
print(gen_markov1(100,P,init_state=1))
|
37
455-Codage_Sources/algo_code/huffman.py
Executable file
37
455-Codage_Sources/algo_code/huffman.py
Executable file
|
@ -0,0 +1,37 @@
|
|||
#!/usr/bin/python3
|
||||
|
||||
import numpy as np
|
||||
|
||||
def get_2min(l):
|
||||
min1 = 0
|
||||
min2 = 1
|
||||
for k in range(1,len(l)):
|
||||
if l[k]<=l[min1]:
|
||||
min2 = min1
|
||||
min1 = k
|
||||
elif l[k]<=l[min2]:
|
||||
min2 = k
|
||||
return min1,min2
|
||||
|
||||
def huffman_rec(p):
|
||||
if len(p) == 2:
|
||||
C = ['1','0']
|
||||
print(p,C)
|
||||
return C
|
||||
else:
|
||||
min1,min2 = get_2min(p)
|
||||
min1,min2 = min(min1,min2),max(min1,min2)
|
||||
print(p,min1,min2)
|
||||
p_save=p.pop(min2)
|
||||
p[min1] = p[min1]+p_save
|
||||
C = huffman_rec(p)
|
||||
C.insert(min2,C[min1]+'1')
|
||||
C[min1] +='0'
|
||||
p.insert(min2,p_save)
|
||||
p[min1] -= p_save
|
||||
print(p,C)
|
||||
return C
|
||||
|
||||
p = [25,20,15,12,10,8,5,5]
|
||||
|
||||
print(huffman_rec(p))
|
76
455-Codage_Sources/algo_code/huffman2.py
Executable file
76
455-Codage_Sources/algo_code/huffman2.py
Executable file
|
@ -0,0 +1,76 @@
|
|||
#!/usr/bin/env python3
|
||||
|
||||
import subprocess
|
||||
|
||||
class Noeud(object):
|
||||
def __init__(self,p=0,left=None,right=None,code='',name=''):
|
||||
self.left = left
|
||||
self.right= right
|
||||
if left != None and right != None :
|
||||
self.p = left.p + right.p
|
||||
self.name =left.name+right.name
|
||||
else:
|
||||
self.p = p
|
||||
self.name = name
|
||||
self.code = code
|
||||
def __lt__(self,other):
|
||||
return self.p<other.p
|
||||
def __repr__(self):
|
||||
return self.name
|
||||
|
||||
table = [
|
||||
('A', 25),
|
||||
('B', 20),
|
||||
('C', 15),
|
||||
('D', 12),
|
||||
('E', 10),
|
||||
('F', 8),
|
||||
('G', 5),
|
||||
('H', 5)]
|
||||
|
||||
table_noeud = [Noeud(name=x[0],p=x[1]) for x in table]
|
||||
#print(table_noeud)
|
||||
def create_tree(table_noeud):
|
||||
queue = table_noeud.copy()
|
||||
while len(queue) > 2:
|
||||
queue.sort()
|
||||
print(queue)
|
||||
l=queue.pop(0)
|
||||
r=queue.pop(0)
|
||||
queue.append(Noeud(left=l,right=r))
|
||||
root= Noeud(left=queue[0],right=queue[1])
|
||||
return root
|
||||
root_node= create_tree(table_noeud)
|
||||
print(root_node)
|
||||
|
||||
def gen_code(node,prefix=''):
|
||||
if node.left != None:
|
||||
node.code = prefix
|
||||
gen_code(node.left,prefix+'0')
|
||||
gen_code(node.right,prefix+'1')
|
||||
else:
|
||||
node.code = prefix
|
||||
print(node.name,node.code)
|
||||
|
||||
gen_code(root_node)
|
||||
|
||||
def draw_tree(node):
|
||||
if len(node.name) == 1: # feuille
|
||||
desc ='N{} [label="{}:{}", fontcolor=blue, fontsize=16, width=2, shape=box];\n'.format(node.code, node.name, node.code)
|
||||
else:
|
||||
desc = 'N{} [label="{}"];\n'.format(node.code,node.code)
|
||||
desc += draw_tree(node.left)
|
||||
desc += draw_tree(node.right)
|
||||
desc += 'N{}:n -> N{}:e;\n'.format(node.code,node.left.code)
|
||||
desc += 'N{}:s -> N{}:e;\n'.format(node.code,node.right.code)
|
||||
return desc
|
||||
|
||||
with open('graph.dot','w') as f:
|
||||
f.write('digraph G {\n ')
|
||||
f.write(' splines=ortho \n')
|
||||
f.write('rankdir=RL;\n')
|
||||
f.write(draw_tree(root_node))
|
||||
f.write('{rank =same; N' + '; N'.join([n.code for n in table_noeud]) +';}\n')
|
||||
f.write('}')
|
||||
subprocess.call('dot -Tpng graph.dot -o graph.png', shell=True)
|
||||
print('done')
|
Loading…
Reference in a new issue