1
votes

So, basically this is what I have so far.

public List<String> cycleSearch(Graph<String,String> g) throws Exception{
    List<String> list = null;
    Graph<String,String> auxG = g;
    for(String aux : g.getVertices()){
        String aux2 = aux;
        if(g.degree(aux)>1){
            if(auxG.removeVertex(aux2)){
            for(String d : g.getSuccessors(aux2)){
                for(String a : g.getSuccessors(aux)){
                    if(a!=d){
                        list = findPath(auxG,d,a);
                        if(list!=null){
                            list.add(0,aux);
                            list.add(aux);
                            return list;
                            }
                        }
                    }
                }
            }
        }
        auxG = g;
    }
    return null;
}

What the method does is basically to search for a cycle in a hypergraph based on JUNG.

The idea it's to recieve a graph on the parameters and then create a variable (as the same type) to later remove a vertex from it without making any changes on the original graph, just in case a cycle isn't found. That way I can use a method called findPath(,,), once the vertex is removed. The method will 'create' another path without walking through the removed vertex.

My compiler says there is problem right here:

            for(String d : g.getSuccessors(aux2))

I've been programming graphs in Java(JUNG) for only 1 month. HELP

2
most likely g.getSuccessors() returns null for aux2. - Dragondraikk
you mentioned compiler error? what is the compiler error? - Abubakkar
*** Read Graph simpleGraph.xml: #edges: 16 #nodes: 11 Finding diameter from simpleGraph.xml Exception in thread "main" java.lang.NullPointerException at GraphDFSWalker.cycleSearch(GraphDFSWalker.java:108) at GraphDFSWalker.main(GraphDFSWalker.java:153) The line #108 says that there is a error in for(String d : g.getSuccessors(aux2)) The line #153 says there is a problem in this line: List<String> path = walker.cycleSearch(g); - artavia23
What implementation of Graph are you using? None of the JUNG-provided implementations should be returning null for a getSuccessors() call (and in fact it's arguably a violation of the contract for that method to do so). - Joshua O'Madadhain
@JoshuaO'Madadhain i'm using the hypergraph implementation. - artavia23

2 Answers

0
votes

Instead of using null to represnt no successors, you could just use an empty list, and thus avoid the NullPointerException. Replace return null; with return new LinkedList<>();.

0
votes

There is a bug in JUNG that this code has turned--getSuccessors() should be returning an empty collection if the graph does not have the vertex, rather than returning null, which is what SetHypergraph's implementation does. Sorry about that. (In general, you can avoid this by wrapping the getSuccessors(x) inside a containsVertex(x) if statement.)

However, the reason why you're encountering this bug is that you're doing at least a couple of things that do not make sense:

(1) you're assigning auxG to g (so they're referencing the same object); this is misleading and not helpful. Similarly, you're assigning aux2 to aux, which is also misleading.

(2) you're removing aux[2] from auxG and then asking for aux2's successors in auxG. aux2 will not have any successors in auxG once it's removed.

(3) Since aux2 and aux are the same vertex, your innermost loop will also not do anything useful for the same reason; there will be no successors.

You need to rethink your entire algorithm, because this simply is not correct as designed.