Graph Representation

Can we represent graph like ArrayList<ArrayList> if we don’t have weights and what is the advantage of representing using HashMap?

Hi Vaibhav

The advantage of using Hashmap is that the complexity of .containsKey() method is O(1). Whereas in case of arraylist .contains() method has O(n) complexity.

Hi Vaibhav
As you are not responding to this thread, I am marking your doubt as Resolved for now. Re-open it if required.

Please mark your doubts as resolved in your course’s “ Ask Doubt ” section, when your doubt is resolved.

Hey I got your point. In the videos weighted graphs were considered if the graphs are unweighted what would be the representation like HashMap<Integer, ArrayList> adjList or something else

Hi Vaibhav

In case of graphs without weights, yes you can use HashMap<Integer, ArrayList>.