in Algorithms
2,970 views
1 vote
1 vote
what is Space complexity of Huffman coding?
in Algorithms
3.0k views

2 Answers

2 votes
2 votes
Best answer

If we have n symble then  we need to store each Symble in Array so 

Space complexity = O(n)

selected by

14 Comments

To store the frequencies of the characters. Right?
3
3
Yes........
0
0
I think it should be O(1). Huffman Coding uses Min-heap and heapsort. Heapsort has O(1) space complexity. We don't include the space required to store the input in Space Complexity.
0
0

Space complexity does includes input space.

https://www.geeksforgeeks.org/g-fact-86/

Also,input will be array of characters along with frequencies.

Can we convert it to minheap inplace?

0
0
Space complexity = input + extra.

Hear input is n symble ----> o(n)

Extra is size of stack is --------> o( logn)

Space = n + long = o (logn)
0
0

Space = n + long = o (logn) .It is 0(n).May be typeo

Please tell how will we create min heap without extra space?

0
0

I didn't know the difference between auxiliary space complexity and space complexity. Thanks for sharing. And heapsort is inplace because of this:

https://stackoverflow.com/questions/22233532/why-does-heap-sort-have-a-space-complexity-of-o1

https://www.quora.com/What-is-the-space-complexity-of-heap-sort

0
0
0
0
edited by
Heap sort's space complexity should be O(logn). Right? As the algo uses max-heapify internally. And so stack records needed is O(logn)
2
2

@Akash , Good question ! 

Max-Heapify uses tail recursion because function call is at the end. Due to this tail recursion , it minimizes the space complexity of heap-sort to O(1).

So, Space Complexity of Heap-Sort is O(1).

To understand , how space complexity becomes O(1) using tail-recursion .

watch this  video and then apply max-heapify algorithm for the below example and observe the space complexity using stack method.

Max-Heapify Algorithm

Example

other link :- https://stackoverflow.com/questions/72209/recursion-or-iteration

2nd thing is , we can also convert this recursive max-heapify procedure to iterative procedure which will take O(1) space.

2
2
@ankit

So if asked for Space Complexity in an exam, should we consider input size or not?!
0
0
Thank you for such a detailed explanation.  I understood the concept but what about Huffman coding's space complexity? Is it O(n) or O(1). According to my understanding, we need n extra space to keep track of frequencies of the characters. What is your opinion on that.
0
0

@Akhilesh ,

I have given the link of University of Texas where it is mentioned that :-

We often speak of "extra" memory needed, not counting the memory needed to store the input itself.

So, we should not have to consider the input size to find the space complexity of an algorithm.

@Akash ,it should be O(n) which you have already mentioned because we have to store frequencies of characters from the input file seperately in the array.

4
4
Does the input will not contain the elements array and its frequency as its input?
0
0
0 votes
0 votes
To obtain Huffman coding we use data structure called min heap. At the initial phase all the nodes has to be present in heap(satisfying min-heap property).

If there are n elements in tree then space complexity will be O(n) which is extra space required.