graph - What are the disadvantages if I replace each linked list in adjacency-list with hash table?

后端 未结 3 443
说谎
说谎 2021-01-30 18:26

In CLRS excise 22.1-8 (I am self learning, not in any universities)

Suppose that instead of a linked list, each array entry Adj[u] is a hash table conta

3条回答
  •  情话喂你
    2021-01-30 18:46

    Questions 3 and 4 are very open. Besides the thoughts from other two, one problem with hash table is that it's not an efficient data structure for scanning elements from the beginning to the end. In a real world, sometimes it's pretty common to enumerate all the neighbors for a given vertex (e.g., BFS, DFS), and that somehow compromises the use of a direct hash table.

    One possible solution for this is to chain existing buckets in hash table together so that they form a doubly-linked list. Every time a new element is added, connect it to the end of the list; Whenever an element is removed, remove it from the list and fix the link relation accordingly. When you want to do an overall scan, just go through this list.

    The drawback of this strategy, of course, is more space. There is a two-pointer overhead per element. Also, the addition/removal of an element takes more time to build/fix the link relation.

    I'm not too worried about collisions. The hash table of a vertex stores its neighbors, each of which is unique. If its key is unique, there is no chance of collision.

提交回复
热议问题