如何在核心java中实现图?

发布于 2024-12-11 11:40:47 字数 334 浏览 0 评论 0 原文

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

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

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

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

自此以后,行同陌路 2024-12-18 11:40:47

邻接列表,例如Map>List>可能是合适的。

附录:在使用 Java 集合 时,注意以下内容可能会有所帮助MapList提供特征方法的接口,而您可能希望选择特定的实现基于您想要使用数据结构实现的算法的要求。

附录:有一个相关示例这里

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.

捎一片雪花 2024-12-18 11:40:47

您可以根据您的目的使用许多集合数据结构,特别是哈希表或集合。 Java 为您提供了许多集合通用容器(HashMap、ArrayList 等)。我不是 Java 专家,但是搜索 Java Collections 会得到很多结果,例如 本教程

You could use many collection data structures, in particular hash tables or sets, for your purpose. Java gives you a lot of collection generic containers (HashMap-s, ArrayList-s etc etc.). I'm not a Java expert, but searching for Java Collections give a big lot of results, e.g. this tutorial

抠脚大汉 2024-12-18 11:40:47

太糟糕了,您要求实现有向未加权图而不是直接使用。否则,我会建议您使用一个现成的框架来处理几乎所有与网络/图相关的东西,称为 JUNG2。您可以在 GUI 或非 GUI 模式下使用它。这将为您节省大量修修补补的时间。以下是其教程链接:

http://www.grotto-networking.com/JUNG /JUNG2-Tutorial.pdf

Too bad that you are asking for an implementation of a directed unweighted graph rather than for a straight-use. Otherwise, I will suggest you to use a readily-made framework for almost anything that relates to network/graph called JUNG2. You can use it either in GUI or in non-GUI mode. It will save you a lot of time tinkering. Following is its tutorial link:

http://www.grotto-networking.com/JUNG/JUNG2-Tutorial.pdf

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文