简体   繁体   中英

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. 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!

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 with value does not already exist; otherwise it returns the existing vertex of 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.

Here is some code in case insertVertex does not check for duplication:

(I replaced ElementoDecorado<Integer> by Integer for readability)

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.

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 . 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. Your if s could look like this:

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

And similarly for v2 . At the end, if and only if v1_exists is false, you add the vertex:

// 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.

Note that it might be a better way of doing it, but I don't know those classes.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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