简体   繁体   中英

Java Adjacency list implementation of graph with directed weighted edges

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM