问题描述
我必须使用图形(数据结构)的应用程序,但是我不知道如何表示它们,并且询问是否可以给我一些提示。
我应该创建一个类顶点和边缘吗?如果是,他们的属性应该是什么?
我建议使用邻接列表进行图表。
最简单的方法可能是制作一个顶点
类,其中包含一个 ArrayList< Vertex>
到相邻顶点的链接列表。这足以代表任何图表,您不需要单独的 Edge
类。
您可以添加任何您喜欢顶点类的其他数据属性,但链接列表是您严格需要的。
请注意,您可以使用定向边(单向链接)或无向边(相邻顶点相互指向)。
I have to make an application that uses Graphs (Data Structure) but I don't know how to represent them, and was asking if you can give me some hints.
Should I create a class Vertex and Edge? If yes, what should be their attributes?
I suggest using adjacency lists for graphs.
The simplest way is probably to make a Vertex
class, which contains an ArrayList<Vertex>
list of links to adjacent vertexes. This is sufficient to represent any graph, you don't need a separate Edge
class.
You can add whatever other data attributes you like to the vertex class, but the list of links is all you strictly need.
Note that you can have either directed edges (one-way links) or undirected edges (adjacent vertices point back to each other).
这篇关于Java中的图表示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!