Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HashMap Space Complexity

Here's a sample solution for "Populating Next Right Pointers in Each Node" puzzle:

Populate each next pointer to point to its next right node. If there is no next right node, the next pointer should be set to NULL.

public void connect(Node root) {
    HashMap<Integer,List<Node>> map = new HashMap<Integer,List<Node>>();
    traverse(root, map , 0);
}

private void traverse(Node root, HashMap<Integer,List<Node>> map , int level){

    if(root != null){

        if(map.containsKey(level)){
            List<Node> temp = map.get(level);
            Node set = temp.get(temp.size() -1 );
            set.next = root;
            root.next = null;
            temp.add(root);
            map.put(level,temp);
        }
        else{
            root.next = null;
            List<Node> temp = new ArrayList<Node>();
            temp.add(root);
            map.put(level,temp);
        }
        level++;
        traverse(root.left, map , level);
        traverse(root.right, map,level);
        System.out.println("test");
    }
}

The solution itself doesn't really matter, but what I'm struggling with is determining its Space complexity:

Logically the type of object we are storing in a HashMap should make a difference on its Space complexity, but how we can determine it by having the key and value of the map?

In other words, if we are storing just 5 keys (for 5 nodes) in this map, can we conclude that the space complexity of HashMap<Integer,List<Node>> map = new HashMap<Integer,List<Node>>(); is just O(n) or since the value of those keys are a List is should be more than that?

like image 218
Yar Avatar asked Apr 16 '17 04:04

Yar


People also ask

Is HashMap space efficient?

Besides that, hashmaps are less space efficient than arrays because they always have a load factor which is smaller than one, which is the same as saying that they keep more entries allocated than necessary due to the way they work.

What is the space complexity of Hashset in Java?

It's both: O(n+k).

Why is complexity of HashMap O 1?

the general amortized complexity of O(1) a bad hashCode() implementation could result to multiple collisions, which means that in the worst case every object goes to the same bucket, thus O(N) if each bucket is backed by a List .

Why the default size of HashMap is 16 why not 14 or 15?

The default load factor of HashMap is 0.75f (75% of the map size). The problem is, keeping the bucket size fixed (i.e., 16), we keep on increasing the total number of items in the map that disturbs time complexity. When we increase the total number of buckets, total items in each bucket starts increasing.


Video Answer


1 Answers

since we are storing just 5 keys in this map, can we conclude that the space complexity of HashMap> map = new HashMap>(); is just O(n) or since the value of those keys are a List is should be more than that?

No.

The general implementation of HashMap uses bucket which is basically a chain of linked lists each node containing <key, value> pair. So if you have duplicate nodes, that doesn't matter - it will still replicate each key with it's value in linked list node.

HashMap separate chaining

You can find different hashtable implementation and their collision resolution techniques here.

Edit

so in this example we have 5 keys and each key points to a set of List, but how we are determining the overall space complexity?

Space Complexity of hashmap in big-O notation is O(n) where n is the number of entries. Remember that, big-O notation depicts the order of growth with the number of input, it doesn't reflect the exact numerical space an algorithm takes. For hashmap, with the increase of the number of entries, the hashmap's space will increase linearly. So space complexity is O(n).

But, I think you're looking for the exact space a hashmap takes which solely depends on the hashing function, type of key and values. In the above picture, total space taken is [number of cell in buckets(hash) + number of entries in each bucket/linked list] and each entry takes [size of key type + size of value type] space.

HIH.

like image 129
Kaidul Avatar answered Sep 16 '22 16:09

Kaidul