开发者

Insert and search quickly

What is the bes开发者_如何学JAVAt data-structure for doing following operation quickly

  • Insert
  • Find.

Thanks Avinash


If the same data structure needs to do both of the operations then it should be a hash map.


according to me hash_map


A good implementation for graph adjancency lists is using dynamically allocated vectors of integers.

Let's suppose you have at most N nodes in your graph. You can store the graph in an array of N dynamically allocated vectors of integers. It will look like this:

vector[N]

To insert a edge from node x to node y you use:

vector[x].push(y)

This way if the graph is sparse ( doesn't have a lot of edges) you can find all the outgoing edges of a node quickly.

If you want to find if there's an edge between x and y you have to go through vector[x] and search for it. If you want to speed that up you can additionaly use a 2 dimensional boolean array if the number of nodes is small ( less then 1000 is reasonable).

If you have a lot of nodes and want to speed that operation up you can use a hashmap.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜