简体   繁体   English

如何知道我的图中是否存在顶点?

[英]How to know whether a vertex exists in my graph?

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. 我不知道写两个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 我试图删除顶点,如果它们相等,但显然这不起作用,因为最后我的图形将为空:S

This is the manual page for the Vertex interface. 这是 Vertex界面的手册页

Any help is welcome. 欢迎任何帮助。 Thanks, and merry christmas! 谢谢,圣诞快乐!

You should first check what graph.insertVertex(V value) does. 您应该首先检查graph.insertVertex(V value)作用。 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 with value does not already exist; 如果软件包构建得很好(我怀疑文档很差),那么只有当一个有value的顶点不存在时,该方法才会创建一个新的顶点; otherwise it returns the existing vertex of value value . 否则返回值的现有顶点value

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. 但是我无法从非文档中判断出包是否真的假定给定值存在单个顶点以及insertVertex是否正确运行。

Here is some code in case insertVertex does not check for duplication: 以下是一些代码,以防insertVertex不检查重复:

(I replaced ElementoDecorado<Integer> by Integer for readability) (I取代ElementoDecorado<Integer>Integer为了可读性)

while(fr.hasNextLine()) {
   int nodeId1 = fr.nextInt();
   int nodeId2 = fr.nextInt();
   Vertex<Integer> vert1 = null;
   Vertex<Integer> vert2 = null;

   for(Vertex<Integer> v : g.vertices()) {
      int nodeId = v.element();
      if(nodeId == nodeId1) 
         vert1 = v;
      else if (nodeId == nodeId2) // assumes can't have nodeId1 == nodeId2
         vert2 = v;
    }
    if (vert1 == null)
       vert1 = g.insertVertex(nodeId1);
    if (vert2 == null)
       vert2 = g.insertVertex(nodeId2);

    g.insertEdge(vert1, vert2, null);
 }

Before you insert the vertex in the graph, ask if there is a already a String value for the given vertex key. 在图中插入顶点之前,询问是否已存在给定顶点键的String值。

vertex = new ElementoDecorado<Integer>(fr.nextInt());
if(graph.get(key) != null) { 
    //it exists, don't insert it
} else { 
    g.insertVertex(vertex)
}

Know your own data structures. 了解自己的数据结构。 Ask yourself, what is the graph made of? 问问自己,图表是由什么组成的? It's just a mapping of : 它只是一个映射:

ElementoDecorado<Integer> => String

Also do not name your variables things like: g . 也不要将变量命名为: g It doesn't convey any meaning. 它没有任何意义。

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: 首先,不要直接添加两个顶点,只需读取它们:

v1 = new ElementoDecorado<Integer>( fr.nextInt() );
v2 = new ElementoDecorado<Integer>( fr.nextInt() );

Then, in the for , you check whether the vertex exists, just like you were trying. 然后,在for ,检查顶点是否存在,就像你在尝试一样。 Your if s could look like this: 你的if可能是这样的:

if( !v1_exists && v.equals( v1 ) ) {
    v1 = v;
    v1_exists = true;
}

And similarly for v2 . 同样适用于v2 At the end, if and only if v1_exists is false, you add the vertex: 最后,当且仅当v1_exists为false时,添加顶点:

// right anfter the for-each
if ( !v1_exists ) {
    g.addVertex( v1 );
}
if ( !v1_exists ) {
    g.addVertex( v2 );
}

g.insertEdge( v1, v2, "edge" );
// etc

There are a few optimizations you could also do, such as stopping the for when both vertices are found, but that should do it. 您还可以进行一些优化,例如在找到两个顶点时停止for ,但应该这样做。

Note that it might be a better way of doing it, but I don't know those classes. 请注意,它可能是一种更好的方法,但我不知道这些类。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM