[英]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. 我正在尝试使用邻接表在Java中实现有向加权边图。 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
. 它由一个大小等于顶点数量的数组组成,该数组的每个条目都是每个特定
Vertex
的后继对象的LinkedList
。
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. 我想为每个边缘增加权重,我想通过向
LinkedList
中的每个successor
对象添加权重标签来实现此目的,此外,我想为每个Vertex
添加其他变量以供将来使用。 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,其中key是顶点的标签,value是顶点的对象。
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。
HashMap<Vertex,Integer> adjListWithWeights = new HashMap<Vertex,Integer>();
You can add more functionality and attributes to your graph through Vertex class. 您可以通过Vertex类为图形添加更多功能和属性。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.