-1

I read this algorithm for using 2 queues to implement Huffman codes, and it claims that with sorted input the runtime can be O(n): https://www.geeksforgeeks.org/efficient-huffman-coding-for-sorted-input-greedy-algo-4/

I do not quite understand why it is O(n): the algorithm iterate all the initial N nodes, however it wold construct merged node and enqueue to the 2nd queue during the iteration. It seems like the whole iteration process would take O(n log n) for me.

1 Answers1

2

At each step, you dequeue 2 nodes (O(1) time), build a new node (O(1) time) and enqueue 1 node (O(1) time). The number of nodes enqueued starts at n and goes down by one each step, so there are exactly n-1 steps before there's a single node left.

The reason the overall algorithm is not O(n) time if the elements don't start sorted (by frequency) is that to initialize the algorithm you have to enqueue the elements in the first queue in decreasing order of frequency, so you need to sort them. If they're sorted already, you can enqueue them in O(n) time.

Paul Hankin
  • 54,811
  • 11
  • 92
  • 118