1
votes

I am trying to implement a directed weighted edge graph in Java using adjacency lists. It consists of an array with the size equal to the number of vertices, each entry of the array is a LinkedList of the successors of that each particular Vertex.

I want to add weight to each edge, I was thinking of doing this by adding a weight label to each successor Object in the LinkedList, furthermore I want to add other variables per Vertex for future use. If I want to do this, I would have to create a new Data structure for the vertices and a separate one as an adjacency lists. What would be an efficient design to combine both as a single Data structure?

1

1 Answers

2
votes

You should represent your graph as a HashMap where key is label of vertices and value is vertex objects.

HashMap<String,Vertex> graph = new HashMap<String,Vertex>();

Vertex is a class encapsulating vertex attributes.There will be an attribute HashMap for adjacent vertices with weights.

HashMap<Vertex,Integer> adjListWithWeights = new HashMap<Vertex,Integer>();

You can add more functionality and attributes to your graph through Vertex class.