0

Suppose, I want to create a vector of vectors to store/find the edges between the nodes in a graph. There are many points in the graph which doesn't have any edge and I don't want to save them. e.g. there are 2 millions nodes which 1.5 million of them don't have any edge. Moreover, each node which I save could have 1 to couple hundreds edges. After, I saved all the edges, I want to remove the edges which are not exist in both direction. So, if edge (i,j) exist but edge(j,i) doesn't exist, I want to erase the (i,j).

I used "vector of vector" to communicate what I want to create and I know it doesn't scale as it would be completely dense. So, using vector of vectors format, I start to go through the first vector(suppose it is i) and for each item in it's the second vector (suppose it is j), I need to check if there is i in the second vector of j'th first vector. Which I need to be fast and preferably constant time. Something like hash table which I think std::set might help. At this point, if the other edge (j,i) does not exist, I need to remove the current edge (i,j).

What would be a good container for my scenario?

eSadr
  • 395
  • 5
  • 21
  • 1
    You want a `std::unordered_map`, which has amortized O(1) lookup. `std::set` has a complexity guarantee of O(log n), and is (almost always) implemented as a red-black tree. – Yuushi May 03 '16 at 04:25
  • For retrieval: std::vector is O(n) std::map and std::set are O(log(n)) but std::unordered_set is O(1) – Jerry Jeremiah May 03 '16 at 04:26
  • 4
    I suggest [std::unordered_set](http://en.cppreference.com/w/cpp/container/unordered_set) – R Sahu May 03 '16 at 04:35
  • Try a sorted vector, too. It plays much more nicely with the cache. – T.C. May 03 '16 at 06:42

0 Answers0