Java中的图表示 [英] Graph representation in Java

查看:185
本文介绍了Java中的图表示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须使用图形(数据结构)的应用程序,但是我不知道如何表示它们,并且询问是否可以给我一些提示。



我应该创建一个类顶点和边缘吗?如果是,他们的属性应该是什么?

解决方案

我建议使用邻接列表进行图表。



最简单的方法可能是制作一个顶点类,其中包含一个 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中的图表示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆