How to resolve the algorithm Huffman coding step by step in the Ol programming language

Published on 12 May 2024 09:40 PM
#Ol

How to resolve the algorithm Huffman coding step by step in the Ol programming language

Table of Contents

Problem Statement

Huffman encoding is a way to assign binary codes to symbols that reduces the overall number of bits used to encode a typical string of those symbols. For example, if you use letters as symbols and have details of the frequency of occurrence of those letters in typical strings, then you could just encode each letter with a fixed number of bits, such as in ASCII codes. You can do better than this by encoding more frequently occurring letters such as e and a, with smaller bit strings; and less frequently occurring letters such as q and x with longer bit strings. Any string of letters will be encoded as a string of bits that are no-longer of the same length per letter. To successfully decode such as string, the smaller codes assigned to letters such as 'e' cannot occur as a prefix in the larger codes such as that for 'x'. The Huffman coding scheme takes each symbol and its weight (or frequency of occurrence), and generates proper encodings for each symbol taking account of the weights of each symbol, so that higher weighted symbols have fewer bits in their encoding. (See the WP article for more information). A Huffman encoding can be computed by first creating a tree of nodes:

Traverse the constructed binary tree from root to leaves assigning and accumulating a '0' for one branch and a '1' for the other at each node. The accumulated zeros and ones at each leaf constitute a Huffman encoding for those symbols and weights:

Using the characters and their frequency from the string: create a program to generate a Huffman encoding for each character as a table.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Huffman coding step by step in the Ol programming language

Source code in the ol programming language

(define phrase "this is an example for huffman encoding")

; prepare initial probabilities table
(define table (ff->list
   (fold (lambda (ff x)
            (put ff x (+ (ff x 0) 1)))
      {}
      (string->runes phrase))))

; just sorter...
(define (resort l)
   (sort (lambda (x y) (< (cdr x) (cdr y))) l))
; ...to sort table
(define table (resort table))

; build huffman tree
(define tree
   (let loop ((table table))
      (if (null? (cdr table))
         (car table)
         (loop (resort (cons
            (cons
               { 1 (car table) 0 (cadr table)}
               (+ (cdar table) (cdadr table)))
            (cddr table)))))))

; huffman codes
(define codes
   (map (lambda (i)
         (call/cc (lambda (return)
            (let loop ((prefix #null) (tree tree))
               (if (ff? (car tree))
                  (begin
                     (loop (cons 0 prefix) ((car tree) 0))
                     (loop (cons 1 prefix) ((car tree) 1)))
                  (if (eq? (car tree) i)
                     (return (reverse prefix))))))))
      (map car table)))


(print "weights: ---------------------------")
(for-each (lambda (ch)
      (print (string (car ch)) ": " (cdr ch)))
   (reverse table))

(print "codes: -----------------------------")
(map (lambda (char code)
      (print (string char) ": " code))
   (reverse (map car table))
   (reverse codes))


  

You may also check:How to resolve the algorithm Symmetric difference step by step in the PicoLisp programming language
You may also check:How to resolve the algorithm Pangram checker step by step in the MATLAB programming language
You may also check:How to resolve the algorithm Square but not cube step by step in the Forth programming language
You may also check:How to resolve the algorithm Magic squares of odd order step by step in the AutoHotkey programming language
You may also check:How to resolve the algorithm Sorting algorithms/Shell sort step by step in the Mathematica/Wolfram Language programming language