我有一个有向未加权图。给出了节点的数量和节点之间的所有链接。我尝试使用向量数组来完成该任务,但 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?
发布评论
评论(3)
邻接列表,例如
Map>或
List
可能是合适的。>
附录:在使用 Java 集合 时,注意以下内容可能会有所帮助
Map
和List
是 提供特征方法的接口,而您可能希望选择特定的实现基于您想要使用数据结构实现的算法的要求。附录:有一个相关示例这里。
An adjacency list such as
Map<Node, List<Node>>
orList<List<Node>>
may be suitable.Addendum: In using Java Collections, it may be helpful to note that
Map
andList
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 为您提供了许多集合通用容器(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
太糟糕了,您要求实现有向未加权图而不是直接使用。否则,我会建议您使用一个现成的框架来处理几乎所有与网络/图相关的东西,称为 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