views:

71

answers:

3

Hi, What is the best data-structure for doing following operation quickly

  • Insert
  • Find.

Thanks Avinash

+1  A: 

according to me hash_map

aeh
+2  A: 

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

Boris Pavlović
Thanks, I am trying to figure out best implementation for GRAPH Adjancy list. any thoughts
Avinash
There's this BOOST C++ Library (http://en.wikipedia.org/wiki/Boost_Graph_Library)
Boris Pavlović
A: 

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.

EyyoFyber