如何知道图中是否存在顶点?
我正在制作一种从文件加载图表的方法。这很简单,但是如果有重复的顶点,该方法也会插入到图中,所以我试图避免这种情况。
这是我当前的代码:
public static Graph<ElementoDecorado<Integer>, String> loadGraphFromFile(File f) {
boolean v1_exists = false, v2_exists = false;
Graph<ElementoDecorado<Integer>, String> g = new AdjacencyListGraph<ElementoDecorado<Integer>, String>();
Vertex<ElementoDecorado<Integer>> v1, v2, aux = null;
Scanner fr;
try {
fr = new Scanner(f);
while(fr.hasNextLine()) {
v1 = g.insertVertex(new ElementoDecorado<Integer>(fr.nextInt()));
v2 = g.insertVertex(new ElementoDecorado<Integer>(fr.nextInt()));
for(Vertex<ElementoDecorado<Integer>> v : g.vertices()) {
if(v.equals(v1)) {
/*aux = v;
v1_exists = true;*/
}
if(v.equals(v2)) {
/*aux = v;
v2_exists = true;*/
}
}
g.insertEdge(v1, v2, "edge");
v1_exists = v2_exists = false;
}
} catch(FileNotFoundException e) {
e.printStackTrace();
}
return g;
}
我不知道在两个 if 中写入什么。我尝试删除顶点(如果它们相等),但显然这不起作用,因为最后我的图将是空的:S
欢迎任何帮助。 谢谢,圣诞快乐!
I'm making a method that load a graph from a file. It's very simple, but if there are repeated vertex, the method'll insert too into the graph, so I'm trying to avoid this.
This is my current code:
public static Graph<ElementoDecorado<Integer>, String> loadGraphFromFile(File f) {
boolean v1_exists = false, v2_exists = false;
Graph<ElementoDecorado<Integer>, String> g = new AdjacencyListGraph<ElementoDecorado<Integer>, String>();
Vertex<ElementoDecorado<Integer>> v1, v2, aux = null;
Scanner fr;
try {
fr = new Scanner(f);
while(fr.hasNextLine()) {
v1 = g.insertVertex(new ElementoDecorado<Integer>(fr.nextInt()));
v2 = g.insertVertex(new ElementoDecorado<Integer>(fr.nextInt()));
for(Vertex<ElementoDecorado<Integer>> v : g.vertices()) {
if(v.equals(v1)) {
/*aux = v;
v1_exists = true;*/
}
if(v.equals(v2)) {
/*aux = v;
v2_exists = true;*/
}
}
g.insertEdge(v1, v2, "edge");
v1_exists = v2_exists = false;
}
} catch(FileNotFoundException e) {
e.printStackTrace();
}
return g;
}
I don't know what to write into the two ifs. I have tried to delete the vertex if they are equal but obviously this doesn't work cause at the end my graph'll be empty :S
This is the manual page for the Vertex interface.
Any help is welcome.
Thanks, and merry christmas!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您应该首先检查
graph.insertVertex(V value)
的作用。如果包构建得体(我从糟糕的文档中对此表示怀疑),那么只有在具有value
的顶点尚不存在时,该方法才会创建一个新顶点;否则它返回值value
的现有顶点。但是,我无法从非文档中判断该包是否确实假设给定值只有一个顶点以及
insertVertex
的行为是否正确。以下是一些代码,以防
insertVertex
不检查重复:(为了便于阅读,我将
ElementoDecorado
替换为Integer
)You should first check what
graph.insertVertex(V value)
does. If the package was build decently (which I doubt from the poor documentation), then that method will only create a new vertex if a vertex withvalue
does not already exist; otherwise it returns the existing vertex of valuevalue
.However I can't tell from the non-documentation whether the package really assumes that there is a single vertex for a given value and whether
insertVertex
behaves correctly.Here is some code in case
insertVertex
does not check for duplication:(I replaced
ElementoDecorado<Integer>
byInteger
for readability)问题是您在检查是否需要添加顶点之前先将顶点添加到图表中。
首先,不要直接添加两个顶点,而是读取它们:
然后,在
for
中,检查顶点是否存在,就像您尝试的那样。您的if
可能如下所示:v2
也类似。最后,当且仅当v1_exists
为 false 时,您才添加顶点:您还可以进行一些优化,例如当两个顶点都为 false 时停止
for
找到了,但这应该可以。请注意,这可能是更好的方法,但我不知道这些课程。
The problem is that you're adding the vertices to the graph BEFORE you check if you need to add them.
First, instead of directly adding both vertices, just read them:
Then, in the
for
, you check whether the vertex exists, just like you were trying. Yourif
s could look like this:And similarly for
v2
. At the end, if and only ifv1_exists
is false, you add the vertex:There are a few optimizations you could also do, such as stopping the
for
when both vertices are found, but that should do it.Note that it might be a better way of doing it, but I don't know those classes.
在图中插入顶点之前,询问给定顶点键是否已有一个
String
值。了解您自己的数据结构。问问自己,图表是由什么组成的?它只是 : 的映射
,也不要将变量命名为:
g
。它没有传达任何意义。Before you insert the vertex in the graph, ask if there is a already a
String
value for the given vertex key.Know your own data structures. Ask yourself, what is the graph made of? It's just a mapping of :
Also do not name your variables things like:
g
. It doesn't convey any meaning.