Amira
Amira

Reputation: 33

how to convert a string to a vertex in a graph?

I'm trying to run hits algororithm of jung library. The input of this algorithm is a graph, so I created ones and I use it as an input of this algorithm. But when I run it, Eclipse generate this error:

Exception in thread "main" java.lang.ClassCastException: java.lang.String
cannot be cast to edu.uci.ics.jung.graph.Vertex
at com.tweets.algorithms.HITS.initialize(HITS.java:52)
at com.tweets.algorithms.HITS.<init>(HITS.java:41)
at com.tweets.test.Main.main(Main.java:121)

Here is how I created my graph:

private static String getId(int nodeId){
     return "Node " + nodeId;
}



private static String getId(int nodeId, int neighborId){
        return "Edge " + nodeId + " -> " + neighborId;
}



public static Graph<String, Integer> createGraph1(String graphId,
                                              boolean[][] adjacencyMatrix){
    Graph<String,Integer> g = new DirectedSparseGraph <String,Integer>();

    for (int nodeId = 0; nodeId < adjacencyMatrix.length; nodeId++)
         g.addVertex(getId(nodeId));


    for (int nodeId = 0; nodeId < adjacencyMatrix.length; nodeId++)
        for (int neighborId = 0; neighborId < adjacencyMatrix[nodeId].length; neighborId++)
            if (adjacencyMatrix[nodeId][neighborId])
                 //g.addEdge(getId(nodeId, neighborId),nodeId , neighborId);
                   g.addEdge(neighborId,getId(nodeId),getId(neighborId));

                   return(g);

           }

And this is how I call the hits algorithm in the main class:

HITS ranker = new HITS(g);
                 ranker.evaluate();
                ranker.printRankings(true,false);

And the IDE indicated that the error is in this bloc in the hits class:

protected void initialize(Graph g) {

        super.initialize(g, true, false);

        mPreviousAuthorityScores = new HashMap();
        mPreviousHubScores = new HashMap();

        for (Iterator vIt = g.getVertices().iterator(); vIt.hasNext();) {
            Vertex currentVertex = (Vertex) vIt.next();
            setRankScore(currentVertex, 1.0, AUTHORITY_KEY);
            setRankScore(currentVertex, 1.0, HUB_KEY);

            mPreviousAuthorityScores.put(currentVertex, new MutableDouble(0));
            mPreviousHubScores.put(currentVertex, new MutableDouble(0));
        }
    }

I'm sure that's a cast error, but I can't resolve it. Can someone help me please

Upvotes: 1

Views: 1026

Answers (1)

Joshua O&#39;Madadhain
Joshua O&#39;Madadhain

Reputation: 2704

It appears that you have two incompatible versions of JUNG in your classpath: one that's using generics (e.g. Graph) which makes it version 2.x, and one that is using the old-style Vertex interfaces, which is version 1.x.

You need to remove the version 1.x version from your classpath.

Upvotes: 1

Related Questions