如何将字符串转换为图形中的顶点?

时间:2016-01-31 09:55:00

标签: graph jung

我试图运行jung库的命中算法。这个算法的输入是一个图,所以我创建了一个,我用它作为这个算法的输入。但是当我运行它时,Eclipse会生成此错误:

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)

以下是我创建图表的方式:

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);

           }

这就是我在主类中调用命中算法的方法:

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

IDE指出错误发生在命令类中的这个块中:

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));
        }
    }

我确定这是一个播放错误,但我无法解决它。 请有人帮帮我

1 个答案:

答案 0 :(得分:1)

您的类路径中似乎有两个不兼容的JUNG版本:一个使用泛型(例如Graph)使其成为版本2.x,另一个使用旧式Vertex接口(版本1)。 X

您需要从类路径中删除版本1.x版本。