Skip to content

Instantly share code, notes, and snippets.

@velicast
Last active December 18, 2015 21:29
Show Gist options
  • Save velicast/5847412 to your computer and use it in GitHub Desktop.
Save velicast/5847412 to your computer and use it in GitHub Desktop.
Algoritmo de Huffman. Laboratorio 4 Sistemas de comunicación.
n = input("N simbols (N >= 2): ");
cw = cell(n, n-1);
prob = zeros(n-1);
add = zeros(n-1);
for i = 1 : n
printf("p%d = ", i);
prob(i) = input("");
end
prob(:) = sort(prob(:), 'descend');
for i = 2 : n-1
a = prob(n-i+1);
b = prob(n-i+2);
prob(n-i+1) = a+b;
prob(:) = sort(prob(:), 'descend');
for j = 1 : n-i+1
if prob(j) == a+b
add(i) = j;
end
end
end
cw(1, n-1) = '0';
cw(2, n-1) = '1';
i = n-1;
while i > 1
p = add(i);
for j = 1 : p-1
cw(j, i-1) = cw(j, i);
end
cw(n-i+1, i-1) = strcat(cw(p,i), '0');
cw(n-i+2, i-1) = strcat(cw(p,i), '1');
for j = p+1 : n-i+1
cw(j-1, i-1) = cw(j, i);
end
i = i-1;
end
for j = 1 : n
printf("x%d = %s\n", j, cw{j, 1})
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment