I have a graph representation such that each nodes contains (int id, String categoryName). And it consist of approx 500,000 nodes. Right now say I would like to check if categoryName "computing" exist in the graph, would that mean I have to traverse through the whole graph like using BSF method? (correct me if I am wrong here). It is pretty slow now (takes up to about 3 minutes) to check if that categoryName exist, any advise on how can I improve the speed of searching and comparing of the String values?
I have a graph representation such that each nodes contains (int id, String categoryName)
Share
If you have no other data – then yes, searching the graph will be needed.
However, you can pre-process and use a
Map<String,Node>(WhereNodeis your node class) that maps from the strings to your actual nodes, and quickly find if a certainStringhas a relevant node.You can get the node from the string by using:
map.get(string)You can use a
TreeMap(which has O(logn) seek & insertion time and is ordered), and aHashMap(which implements a hash table and hasO(1)average case seek & insertion time)Note: this assumes no duplicates, i.e. no string has two different nodes representing it.
If this assumption is not correct, you can use something like Guava Multimap