Can we use Map of Map for storing weighted graph?

Revision en1, by trunghieu11, 2015-10-06 13:07:46

As I know one of ways for storing a weighted graph is using Adjacency List but cons of this solution is when we want to get weight of a pair vertex this will cost O(n) by looping the adjacency vertex.

So I have a solution to improve that by using Map<NodeID, Map<NodeID, Weight>> then when we want to get weight of a pair vertex, for example (1, 2) we just call Node[1].get(Node[2]), this only cost O(1).

Is this correct? I'm using Java so can it have a risk?

Tags weighted graph

History

 
 
 
 
Revisions
 
 
  Rev. Lang. By When Δ Comment
en1 English trunghieu11 2015-10-06 13:07:46 515 Initial revision (published)