创建有向图
我想创建一个有向图,我有三个变量:字符串x,y,z 第一个顶点包含 3 个变量,下一个顶点包含接下来的 3 个变量,直到循环结束
我有这个:
BufferedReader reader = new BufferedReader(
new StringReader(contentTextArea.getText()));
try {
str =reader.readLine();
while(str != null) {
String splitted[] = str.split("\\|");
String x = splitted[0].trim();
String y = splitted[1].trim();
String z = splitted[2].trim();
}
}
所以这段代码每次给我 3 个字符串,我想每次创建一个顶点和一条边,这将最后创建一个图表。 代码可能是这样的,但我不知道里面写什么 createVertex(String x,y,z)
、addEdge()
方法。
public class graph {
createVertex (String x,y,z);
addEdge ();
}
i want create a directed graph , i have three variables : String x,y,z
and the first vertex contain the 3 variables and the next contain the next 3 until the end of the loop
i have this:
BufferedReader reader = new BufferedReader(
new StringReader(contentTextArea.getText()));
try {
str =reader.readLine();
while(str != null) {
String splitted[] = str.split("\\|");
String x = splitted[0].trim();
String y = splitted[1].trim();
String z = splitted[2].trim();
}
}
so this code give me each time 3 strings and i want for each time to create a vertex and an edge and this will create a graph at the end.
the code could be like this, but i don't know what to write insidecreateVertex (String x,y,z)
,addEdge ()
methods.
public class graph {
createVertex (String x,y,z);
addEdge ();
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您想创建一条线带,即每条线代表一个顶点,一条边由两个连续的顶点形成,您可以创建/使用一个代表顶点和边的
Vertex
类> 具有对形成其端点的 2 个顶点的引用的类。在您的图形类中,您可以只有一个顶点列表和一个边列表。
然后,
createVertex()
可以将一个新顶点添加到列表中,而addEdge()
将使用列表中的最后两个顶点创建一条边(如果至少有两个顶点) !)并将边放入边列表中。如果稍后有由特定顶点(即不是最后两个顶点)形成的边,您可以使用列表中每个顶点的索引来引用它们并通过这些索引定义边(即从顶点 0 到 5 的边可以是定义为
0,5
)。If you want to create a linestrip, i.e. each line represents a vertex and an edge is formed by two consecutive vertices you could create/use a
Vertex
class representing a vertex and anEdge
class that has a reference to the 2 vertices that form its end points.In your graph class you could then just have a list of vertices and a list of edges.
createVertex()
could then just add a new vertex to the list whereasaddEdge()
would create an edge using the last two vertices on the list (if there are at least two!) and put the edge on the edge list.If you later have edges that are formed by specific vertices (i.e. not the two last ones) you might use the index of each vertex in the list to reference them and define the edge via those indices (i.e. edge from vertex 0 to 5 could be defined as
0,5
).这实际上取决于您如何表示图表。我建议使用第三方库,例如 JGraph 或 JGraphT。如果您无法使用第三方库(例如用于家庭作业或您只是想学习),那么您需要定义自己的 Graph 类。
两种常见的表示形式是连通矩阵和邻接表。任何一种表现形式都适合您。
创建新顶点很简单,只需在 JGraph 图上调用 addVertex 即可。这将返回一个顶点对象。您需要为其提供两个参数:名称和数据对象。对于名称,可以使用递增的 ID 号或原始行字符串。然后,您需要从三个字符串中创建一个数据对象,提供自定义数据对象对我来说最有意义。
我将跟踪插入的最后一个节点(从空开始),然后每当前一个节点不为空时在顶点之间创建边。确保每次迭代更新前一个顶点。
That really depends on how you are representing your graph. I would recommend using a third party library such as JGraph or JGraphT. If you can't use a third party library (e.g. for homework or you just want to learn) then you need to define your own Graph class.
Two common representations are connectivity matrices and adjacency lists. Either representations could work for you.
Creating a new vertex is easy, just call addVertex on the JGraph graph. That will return a vertex object. You will need to provide it with two parameters, a name and a data object. For the name either use an incrementing id number or the original line string. You'll then need to create a data object out of the three strings, providing a custom data object makes the most sense to me.
I would keep track of the last node inserted (starts null) and then create edges between vertices whenever the previous i s not null. Making sure to update the previous vertex every iteration.