如何在核心java中实现图形? [英] How to implement graph in core java?

查看:88
本文介绍了如何在核心java中实现图形?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个定向的未加权图。给出节点数量和节点之间的所有链路。我试图用矢量数组完成任务,但是java不支持它。 ArrayList和Vectors支持随机访问迭代器,但无法在java中完成,因为我是新手。我不想为它使用二维矩阵。我想将它实现为N个给定节点的数组,其中每个节点都有一个连接到它的节点的列表。请有人提供一个伪代码或任何可以帮助我的东西。例如,一张图表显示为

  5 
3 4
4 2
1 5
4 3
1 3
2 5

5给出了编号为1到5的节点。以下是从第一个节点到第二个节点的有向边。我想将它表示为图的邻接列表。任何人都可以实施它吗?

解决方案

adjacency list ,例如 Map< Node,List< Node>> List< List< Node>> 可能是合适的。



附录:使用Java Collections ,注意 Map List interfaces 提供特有的方法,而您可能希望选择特定的实现 a>,根据您要使用数据结构实现的算法的要求。



附录:有一个相关的例子
这里


I've a directed unweighted graph. Number of nodes and all links between nodes are given. I tried to do the task with array of vectors but java doesn't support it. ArrayList and Vectors support random access iterators but unable to do it in java as I'm new to it. I don't want to use 2-Dimensional matrix for it. I want to implement it as an array of N given nodes, where each node has a list of those nodes which are connected to it. Please somebody provide a pseudocode or anything which could help me. For Example, a graph is given as

5
3 4
4 2
1 5
4 3
1 3
2 5

here 5 nodes numbered 1 to 5 are given. Following are the directed edges from first node to second node. I want to represent it as adjacency list of graph. Could anybody give the implementation of it?

解决方案

An adjacency list such as Map<Node, List<Node>> or List<List<Node>> may be suitable.

Addendum: In using Java Collections, it may be helpful to note that Map and List are interfaces that provide characteristic methods, while you may want to choose specific implementations based on the requirements of the algorithms you want to implement using your data structure.

Addendum: There's a related example here.

这篇关于如何在核心java中实现图形?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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