I suggest using adjacency lists for graphs.
The simplest way is probably the Vertex class, which contains a list of ArrayList<Vertex> links for adjacent vertices. This is enough to represent any graph; you do not need a separate Edge class.
You can add any other data attributes that you like in the vertex class, but the list of links is all you need.
Note that you can have either directed edges (one-way links) or undirected edges (adjacent vertices point to each other).
source share